Class: Translatomatic::ResourceFile::Plist::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/translatomatic/resource_file/plist.rb

Overview

Adapted from nokogiri-plist parser

Instance Method Summary collapse

Instance Method Details

#next_valid_sibling(node) ⇒ Object



143
144
145
146
147
148
# File 'lib/translatomatic/resource_file/plist.rb', line 143

def next_valid_sibling(node)
  until node.nil? or valid_type? node.name
    node = node.next_sibling
  end
  node
end

#parse(xml, options = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/translatomatic/resource_file/plist.rb', line 79

def parse(xml, options = {})
  @converters = {
    'integer' => Proc.new { |node| node.content.to_i },
    'real'    => Proc.new { |node| node.content.to_f },
    'string'  => Proc.new { |node| node.content.to_s },
    # DateTime.parse(node.content)
    'date'    => Proc.new { |node| node.content.to_s },
    'true'    => Proc.new { |node| true },
    'false'   => Proc.new { |node| false },
    'dict'    => Proc.new { |node| parse_dict(node) },
    'array'   => Proc.new { |node| parse_array(node) },
    'data'    => Proc.new { |node| node.content.to_s }
  }.merge(options[:converters] || {})

  @dict_class = options[:dict_class] || Hash
  plist = xml.root
  plist = plist.children.first if plist.name == "plist"
  result = parse_value_node(next_valid_sibling plist)
  plist_node_value(result)
end

#parse_array(node) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/translatomatic/resource_file/plist.rb', line 133

def parse_array(node)
  node.children.inject([]) do |result, child|
    if valid_node?(child)
      plist_node = parse_value_node(child)
      result << plist_node_value(plist_node)
    end
    result
  end
end

#parse_dict(node) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/translatomatic/resource_file/plist.rb', line 113

def parse_dict(node)
  node.xpath('./key').inject(@dict_class.new) do |result, key_node|
    plist_node = parse_value_node(next_valid_sibling key_node)
    value = plist_node_value(plist_node)
    result[key_node.content] = value
    result
  end
end

#parse_value_node(value_node) ⇒ Object



100
101
102
103
# File 'lib/translatomatic/resource_file/plist.rb', line 100

def parse_value_node(value_node)
  value = @converters[value_node.name].call(value_node)
  PlistNode.new(value_node, value)
end

#plist_node_value(plist_node) ⇒ Object

if the PlistNode value is an array or hash, use that directly instead of the PlistNode.



124
125
126
127
128
129
130
131
# File 'lib/translatomatic/resource_file/plist.rb', line 124

def plist_node_value(plist_node)
  content = plist_node.content
  if content.kind_of?(Array) || content.kind_of?(Hash)
    content
  else
    plist_node
  end
end

#valid_node?(node) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/translatomatic/resource_file/plist.rb', line 109

def valid_node?(node)
  valid_type?(node.name) or node.name == "key"
end

#valid_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/translatomatic/resource_file/plist.rb', line 105

def valid_type?(type)
  @converters.has_key? type
end