Class: Kwaff::RexmlTranslator

Inherits:
Translator show all
Defined in:
lib/kwaff/rexml.rb

Overview

translate Kwaff::Document to REXML::Document

ex.

kwaff_str = File.open('file.kwaff') { |f| f.read }
kwaff_document = Kwaff.parse_document(kwaff_str)
translator = Kwaff::Rexml::RexmlTranslator.new()
rexml_document = translator.translate(kwaff_document)

Instance Attribute Summary

Attributes inherited from Translator

#toppings

Instance Method Summary collapse

Methods inherited from Translator

#translate

Constructor Details

#initialize(toppings = {}) ⇒ RexmlTranslator

Returns a new instance of RexmlTranslator.



37
38
39
# File 'lib/kwaff/rexml.rb', line 37

def initialize(toppings={})
   super(toppings)
end

Instance Method Details

#translate_comment(comment, level = 0) ⇒ Object



68
69
70
# File 'lib/kwaff/rexml.rb', line 68

def translate_comment(comment, level=0)
   return REXML::Comment.new(comment.str)
end

#translate_document(document, level = 0) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/kwaff/rexml.rb', line 53

def translate_document(document, level=0)
   headers = document.headers
   rexml_document = REXML::Document.new
   rexml_xmldecl = REXML::XMLDecl.new(headers[:xmlversion], headers[:encoding])
   rexml_document.add(rexml_xmldecl)
   if headers[:doctype]
      #$stderr.puts "*** debug: headers[:doctype]=#{headers[:doctype].inspect}"
      rexml_source  = REXML::Source.new(headers[:doctype].to_s)
      rexml_doctype = REXML::DocType.new(rexml_source)
      rexml_document.add(rexml_doctype)
   end
   rexml_document.add(document.root.to_rexml()) if document.root
   return rexml_document
end

#translate_element(elem, level = 0) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/kwaff/rexml.rb', line 41

def translate_element(elem, level=0)
   rexml_elem = REXML::Element.new(elem.tag)
   elem.attrs.each do |aname, avalue|
      rexml_elem.add_attribute(aname, avalue)
   end if elem.attrs
   elem.children.each do |child|
      rexml_node = translate(child, level)
      rexml_elem.add(rexml_node)
   end if elem.children
   return rexml_elem
end

#translate_text(text, level = 0) ⇒ Object



72
73
74
# File 'lib/kwaff/rexml.rb', line 72

def translate_text(text, level=0)
   return REXML::Text.new(text.str)
end