Class: Terrestrial::Cli::AndroidXmlParser

Inherits:
Object
  • Object
show all
Defined in:
lib/terrestrial/cli/android_xml_parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ AndroidXmlParser

Returns a new instance of AndroidXmlParser.



9
10
11
12
13
# File 'lib/terrestrial/cli/android_xml_parser.rb', line 9

def initialize(file)
  @path = file
  @file = File.new(file)
  @document = REXML::Document.new(@file)
end

Class Method Details

.parse_file(file) ⇒ Object



5
6
7
# File 'lib/terrestrial/cli/android_xml_parser.rb', line 5

def self.parse_file(file)
  new(file).parse
end

Instance Method Details

#build_entry(node) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/terrestrial/cli/android_xml_parser.rb', line 23

def build_entry(node)
  Hash.new.tap do |entry|
    entry["file"] = @path
    entry["string"] = get_string_from_node(node)
    entry["type"] = "strings.xml"
    entry["identifier"] = node.attributes["name"]
  end
end

#get_string_from_node(node) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/terrestrial/cli/android_xml_parser.rb', line 32

def get_string_from_node(node)
  # Why could the text be nil?
  #  - If it contains valid XML!
  #
  # We assume anything inside the string tag is actually 
  # what should be shown in the UI, so we just parse it 
  # as a string if we realise that the parser thinks it
  # is XML.

  if !node.get_text.nil?
    node.get_text.value
  else
    node.children.first.to_s
  end
end

#parseObject



15
16
17
18
19
20
21
# File 'lib/terrestrial/cli/android_xml_parser.rb', line 15

def parse
  result = []
  REXML::XPath.each(@document, "//resources/string[not(@terrestrial='false')]") do |node|
    result << build_entry(node)
  end
  result
end