Class: Kwaff::Rexml::ReverseTranslator

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

Overview

translate REXML::Document to Kwaff::Document

Instance Method Summary collapse

Methods inherited from Translator

#initialize, #translate

Constructor Details

This class inherits a constructor from Kwaff::Rexml::Translator

Instance Method Details

#translate_comment(rexml_comment, level = 0) ⇒ Object



346
347
348
349
# File 'lib/kwaff/rexml.rb', line 346

def translate_comment(rexml_comment, level=0)
   str = rexml_comment.to_s
   return Comment.new(str)
end

#translate_document(rexml_document, level = 0) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/kwaff/rexml.rb', line 319

def translate_document(rexml_document, level=0)
   headers = OrderedHash.new
   headers[:xmlversion] = rexml_document.version().to_s
   headers[:encoding]   = rexml_document.encoding().to_s
   if doctype = rexml_document.doctype
      doctype = doctype.to_s
      doctype << "\n" if doctype[-1] != ?\n
      headers[:doctype] = doctype.to_s
   end
   root = translate_element(rexml_document.root)
   return Document.new(headers, root)
end

#translate_element(rexml_element, level = 0) ⇒ Object



332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/kwaff/rexml.rb', line 332

def translate_element(rexml_element, level=0)
   tag      = rexml_element.name
   children = []
   attrs    = OrderedHash.new
   rexml_element.attributes.each do |aname, avalue|
      attrs[aname] = avalue
   end if rexml_element.attributes
   rexml_element.children.each do |child|
      node = translate(child)
      children << node if node
   end if rexml_element.children
   return Element.new(tag, children, attrs)
end

#translate_text(rexml_text, level = 0) ⇒ Object



351
352
353
354
355
356
# File 'lib/kwaff/rexml.rb', line 351

def translate_text(rexml_text, level=0)
   str = rexml_text.value
   return nil if str =~ /\A\s*\z/
   flag_escape = !rexml_text.is_a?(REXML::CData)
   return Text.new(str, flag_escape)
end