Class: PropertyList::XmlGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/property-list/xml_generator.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gnu_dtd: false, base64_width: 68, base64_indent: true, indent_unit: "\t", initial_indent: '') ⇒ XmlGenerator

Returns a new instance of XmlGenerator.



39
40
41
42
43
44
45
46
47
48
# File 'lib/property-list/xml_generator.rb', line 39

def initialize gnu_dtd: false, base64_width: 68, base64_indent: true, indent_unit: "\t", initial_indent: ''
  @gnu_dtd = gnu_dtd
  @indent_unit = indent_unit
  @indent_level = 0
  @initial_indent = initial_indent
  @indent = @initial_indent + @indent_unit * @indent_level
  @base64_bytes_per_line = (base64_width * 6) / 8
  @base64_indent = base64_indent
  @output = []
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



49
50
51
# File 'lib/property-list/xml_generator.rb', line 49

def output
  @output
end

Instance Method Details

#generate(obj) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/property-list/xml_generator.rb', line 51

def generate obj
  if obj.respond_to? :to_plist_xml
    xml = obj.to_plist_xml
    if !xml.start_with?(@indent)
      xml = @indent + xml
    end
    if !xml.end_with?("\n")
      xml += "\n"
    end
    @output << xml
    return
  end

  case obj
  when Array
    if obj.empty?
      empty_tag 'array'
    else
      tag 'array' do
        obj.each {|e| generate e }
      end
    end
  when Hash
    if obj.empty?
      empty_tag 'dict'
    else
      tag 'dict' do
        obj.keys.sort_by(&:to_s).each do |k|
          v = obj[k]
          tag 'key', escape_string(k.to_s)
          generate v
        end
      end
    end
  when true, false
    empty_tag obj
  when Time
    tag 'date', obj.utc.strftime('%Y-%m-%dT%H:%M:%SZ')
  when Date # also catches DateTime
    tag 'date', obj.strftime('%Y-%m-%dT%H:%M:%SZ')
  when String
    tag 'string', escape_string(obj)
  when Symbol
    tag 'string', escape_string(obj.to_s)
  when Float
    if obj.to_i == obj
      tag 'real', obj.to_i
    else
      tag 'real', obj
    end
  when Integer
    tag 'integer', obj
  when IO, StringIO
    obj.rewind
    contents = obj.read
    data_tag contents
  else
    raise UnsupportedTypeError, obj.class.to_s
  end
end