class Translatomatic::ResourceFile::XML

XML resource file

Constants

TM_NS

Public Class Methods

extensions() click to toggle source

(see Base.extensions)

# File lib/translatomatic/resource_file/xml.rb, line 6
def self.extensions
  %w[xml]
end

Public Instance Methods

save(target = path, options = {}) click to toggle source

(see Base#save)

# File lib/translatomatic/resource_file/xml.rb, line 21
def save(target = path, options = {})
  return unless @doc
  add_created_by unless options[:no_created_by] || created_by?
  target.write(@doc.to_xml(indent: 2))
end
set(key, value) click to toggle source

(see Base#set)

Calls superclass method Translatomatic::ResourceFile::Base#set
# File lib/translatomatic/resource_file/xml.rb, line 11
def set(key, value)
  super(key, value)
  if @nodemap.include?(key)
    @nodemap[key].content = value
  else
    create_node(key, value)
  end
end

Private Instance Methods

add_created_by() click to toggle source
# File lib/translatomatic/resource_file/xml.rb, line 48
def add_created_by
  @created_by ||= @doc.root.add_previous_sibling(comment(created_by))
end
add_node(node) click to toggle source
# File lib/translatomatic/resource_file/xml.rb, line 84
def add_node(node)
  return if whitespace?(node.content)
  if node.comment?
    @metadata.parse_comment(node.content)
  elsif context_attribute?(node)
    @metadata.add_context(node.content)
  else
    key = "key#{@keynum}"
    @nodemap[key] = node
    @keynum += 1
    @metadata.assign_key(key)
  end
end
comment(text) click to toggle source
# File lib/translatomatic/resource_file/xml.rb, line 44
def comment(text)
  @doc.create_comment(text)
end
context_attribute?(node) click to toggle source
# File lib/translatomatic/resource_file/xml.rb, line 98
def context_attribute?(node)
  node.name == 'context' && node.namespace &&
    node.namespace.href == TM_NS
end
create_node(key, value) click to toggle source
# File lib/translatomatic/resource_file/xml.rb, line 70
def create_node(key, value)
  # separate nodes by whitespace
  text_node = Nokogiri::XML::Text.new("\n", @doc)
  @doc.root.add_child(text_node)

  # create the key/value node
  node = Nokogiri::XML::Node.new(key, @doc)
  node.content = value
  @doc.root.add_child(node)

  @nodemap[key] = node
  @properties[key] = node.content
end
empty_doc() click to toggle source
# File lib/translatomatic/resource_file/xml.rb, line 103
def empty_doc
  Nokogiri::XML('<root />')
end
init() click to toggle source
# File lib/translatomatic/resource_file/xml.rb, line 31
def init
  @nodemap = {}
  @doc = empty_doc
end
init_nodemap() click to toggle source

initialize nodemap and properties hash from nokogiri document

# File lib/translatomatic/resource_file/xml.rb, line 57
def init_nodemap
  # map of key1 => node, key2 => node, ...
  @keynum = 1
  text_nodes = @doc.search(text_nodes_xpath, tm: TM_NS)
  text_nodes.each { |node| add_node(node) }
end
init_properties() click to toggle source
# File lib/translatomatic/resource_file/xml.rb, line 52
def init_properties
  @properties = @nodemap.transform_values { |i| i ? i.content : nil }
end
load() click to toggle source
# File lib/translatomatic/resource_file/xml.rb, line 36
def load
  # parse xml with nokogiri
  @metadata.reset
  @doc = read_doc
  init_nodemap
  init_properties
end
read_doc() click to toggle source
# File lib/translatomatic/resource_file/xml.rb, line 64
def read_doc
  doc = Nokogiri::XML(@path.open, &:noblanks)
  parsing_error(doc.errors[0]) if doc.errors.present?
  doc
end
text_nodes_xpath() click to toggle source
# File lib/translatomatic/resource_file/xml.rb, line 107
def text_nodes_xpath
  '//text()|//comment()|//@tm:context'
end
whitespace?(text) click to toggle source
# File lib/translatomatic/resource_file/xml.rb, line 111
def whitespace?(text)
  text.nil? || text.strip.empty?
end