Class: Terrestrial::Cli::Parser::AndroidXML

Inherits:
BaseParser
  • Object
show all
Defined in:
lib/terrestrial/cli/parser/android_xml.rb

Constant Summary collapse

LANGUAGE =
:android_xml

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseParser

find_nslocalizedstrings, find_string, scan_lines

Constructor Details

#initialize(file) ⇒ AndroidXML

Returns a new instance of AndroidXML.



15
16
17
18
19
# File 'lib/terrestrial/cli/parser/android_xml.rb', line 15

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

Class Method Details

.find_api_calls(file) ⇒ Object



11
12
13
# File 'lib/terrestrial/cli/parser/android_xml.rb', line 11

def self.find_api_calls(file)
  self.new(file).find_api_calls
end

.find_strings(file) ⇒ Object



7
8
9
# File 'lib/terrestrial/cli/parser/android_xml.rb', line 7

def self.find_strings(file)
  self.new(file).find_strings
end

Instance Method Details

#build_new_string_entry(node) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/terrestrial/cli/parser/android_xml.rb', line 37

def build_new_string_entry(node)
  Hash.new.tap do |entry|
    entry["language"] = LANGUAGE
    entry["file"] = @path
    entry["string"] = get_string_from_node(node)
    entry["type"] = "android-strings-xml"
    entry["line_number"] = nil
    # entry.variables = get_variables_from_string(entry.string)
    entry["identifier"] = node.attributes["name"]
  end
end

#build_registry_entry_hash(node) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/terrestrial/cli/parser/android_xml.rb', line 49

def build_registry_entry_hash(node)
  Hash.new.tap do |entry|
    entry["string"] = get_string_from_node(node)
    entry["context"] = node.attributes["context"] || ""
    entry["file"] = @path
    entry["line_number"] = nil
    entry["type"] = "android-strings-xml"
    entry["id"] = node.attributes["name"]
  end
end

#find_api_callsObject



29
30
31
32
33
34
35
# File 'lib/terrestrial/cli/parser/android_xml.rb', line 29

def find_api_calls
  result = []
  REXML::XPath.each(@document, "//resources/string[@terrestrial=\"true\"]") do |node|
    result << build_registry_entry_hash(node)
  end
  result
end

#find_stringsObject



21
22
23
24
25
26
27
# File 'lib/terrestrial/cli/parser/android_xml.rb', line 21

def find_strings
  result = []
  REXML::XPath.each(@document, "//resources/string") do |node|
    result << build_new_string_entry(node)
  end
  result
end

#get_string_from_node(node) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/terrestrial/cli/parser/android_xml.rb', line 60

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

#get_variables_from_string(string) ⇒ Object



76
77
78
# File 'lib/terrestrial/cli/parser/android_xml.rb', line 76

def get_variables_from_string(string)
  string.scan(/(\%\d\$[dsf])/).map {|match| match[0] }
end