Class: Terrestrial::Cli::Editor::AndroidXML

Inherits:
BaseEditor
  • Object
show all
Defined in:
lib/terrestrial/cli/editor/android_xml.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseEditor

edit_file

Constructor Details

#initialize(entry) ⇒ AndroidXML

Returns a new instance of AndroidXML.



14
15
16
17
18
19
20
21
22
# File 'lib/terrestrial/cli/editor/android_xml.rb', line 14

def initialize(entry)
  @path = entry.file
  @type = entry.type
  @string = entry.string
  @identifier = entry.identifier

  @document = REXML::Document.new(File.new(@path))
  @document.context[:attribute_quote] = :quote 
end

Class Method Details

.add_import(file) ⇒ Object



10
11
12
# File 'lib/terrestrial/cli/editor/android_xml.rb', line 10

def self.add_import(file)
  # Not needed
end

.find_and_edit_line(string_entry) ⇒ Object



6
7
8
# File 'lib/terrestrial/cli/editor/android_xml.rb', line 6

def self.find_and_edit_line(string_entry)
  self.new(string_entry).add_attributes
end

Instance Method Details

#add_attributesObject



24
25
26
27
28
29
# File 'lib/terrestrial/cli/editor/android_xml.rb', line 24

def add_attributes
  node = find_node(@identifier)
  node.add_attribute("terrestrial", true) 
  refresh_document(node)
  save_document
end

#find_node(name) ⇒ Object



31
32
33
# File 'lib/terrestrial/cli/editor/android_xml.rb', line 31

def find_node(name)
  REXML::XPath.first(@document, "//resources/string[@name=\"#{name}\"]")
end

#refresh_document(node) ⇒ Object



35
36
37
38
39
40
# File 'lib/terrestrial/cli/editor/android_xml.rb', line 35

def refresh_document(node)
  # This is a bit ridiculous, but necessary because
  # &*£(*$)£@*$!£ REXML
  
  @document = REXML::Document.new(node.document.to_s)
end

#save_documentObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/terrestrial/cli/editor/android_xml.rb', line 42

def save_document
  # AAAAAAAARARARARARARRARAAAGH REXML STAAAAHP
  # You can't make REXML print attributes inside double
  # quotes without monkey patching >.<
  #
  # ...seriously?

  REXML::Attribute.class_eval( %q^
                                    def to_string
                                      %Q[#@expanded_name="#{to_s().gsub(/"/, '&quot;')}"]
                                    end
                                ^)
  File.open(@path, "w") do |f|
    printer = CustomPrinter.new(2)
    printer.compact = true
    printer.width = 1000000
    printer.write(@document, f)
  end 
end