class Terrestrial::Cli::Editor::AndroidXML

Public Class Methods

add_import(file) click to toggle source
# File lib/terrestrial/cli/editor/android_xml.rb, line 10
def self.add_import(file)
  # Not needed
end
find_and_edit_line(string_entry) click to toggle source
# 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
new(entry) click to toggle source
# 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

Public Instance Methods

add_attributes() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_document() click to toggle source
# 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