Class: Kwaff::Rexml::KwaffTranslator

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

Overview

Translate REXML::Document to Kwaff string

ex.

xml_str = File.open('file.xml') { |f| f.read }
rexml_document = REXML::Document.new(xml_str)
translator = Kwaff::Rexml::KwaffTranslator.new()
kwaff_str = translator.translate(rexml_document)

Instance Method Summary collapse

Methods inherited from Translator

#translate

Constructor Details

#initialize(toppings = {}) ⇒ KwaffTranslator

Returns a new instance of KwaffTranslator.



214
215
216
217
218
219
# File 'lib/kwaff/rexml.rb', line 214

def initialize(toppings={})
   @toppings = toppings
   @newline  = toppings[:newline] || "\n"
   @space    = ' ' * (toppings[:output_indent_width] || 2)
   @output   = ''
end

Instance Method Details

#_rexml_doctype_to_kwaff_doctype(rexml_doctype) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/kwaff/rexml.rb', line 259

def _rexml_doctype_to_kwaff_doctype(rexml_doctype)
   tag       = rexml_doctype.name
   public_id = rexml_doctype.instance_eval("@long_name")
   public_id = public_id.gsub(/\A"/, '').gsub(/"\z/, '') if public_id
   system_id = rexml_doctype.instance_eval("@uri")
   system_id = system_id.gsub(/\A"/, '').gsub(/"\z/, '') if system_id
   if system_id || public_id
      doctype = Kwaff::DocType.new(tag, public_id, system_id)
   else
      doctype = Kwaff::DocType.new()
      doctype.doctype = rexml_doctype.to_s
   end
   return doctype
end

#translate_comment(rexml_comment, level = 0) ⇒ Object



293
294
295
296
297
298
299
# File 'lib/kwaff/rexml.rb', line 293

def translate_comment(rexml_comment, level=0)
   prefix = (@space * level) + "# "				## *pattern*
   rexml_comment.string.split(/\r?\n/).each do |line|
      @output << prefix << line << @newline
   end
   return @output
end

#translate_document(rexml_document, level = 0) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/kwaff/rexml.rb', line 274

def translate_document(rexml_document, level=0)
   Kwaff::assert() unless rexml_document.class == REXML::Document
   @output << "?xmlversion = #{rexml_document.version.sub(/\s+encoding=/,'')}#{@newline}"
   @output << "?encoding   = #{rexml_document.encoding.to_s}#{@newline}"
   rexml_doctype = rexml_document.doctype
   if rexml_doctype
      doctype = _rexml_doctype_to_kwaff_doctype(rexml_doctype)
      nickname = nil
      if doctype.system_id
         dtype = DocType.find { |dt| doctype.system_id == dt.system_id }
         nickname = DocType.nickname(dtype) if dtype
      end
      doctype_str = nickname || rexml_doctype.to_s
      @output << "?doctype    = #{doctype_str}#{@newline}" if doctype_str
   end
   translate_element(rexml_document.root, level)
   return @output
end

#translate_element(rexml_elem, level = 0) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/kwaff/rexml.rb', line 221

def translate_element(rexml_elem, level=0)
   Kwaff::assert() unless rexml_elem.is_a?(REXML::Element)

   children = []
   rexml_elem.children.each do |child|
      children << child if !child.is_a?(REXML::Text) || child.to_s =~ /\S/
   end if rexml_elem.children

   tag = rexml_elem.expanded_name		## namespace + tagname
   @output << (@space * level) if level > 0
   @output << "* #{tag}"					## *pattern*

   text = nil
   if children.length == 1 && children[0].is_a?(REXML::Text)
      text = children[0].to_s.strip
      if text.index(?\n) == nil
         @output << " = #{text}"
      else
         text = nil
      end
   end
   @output << @newline

   rexml_elem.attributes.each do |name, value|
      @output << (@space * (level + 1))
      @output << "- #{name} = #{value}#{@newline}"		## *pattern*
   end

   if text == nil
      children.each do |child|
         #translate_element(child, level+1)
         translate(child, level+1)
      end if rexml_elem.children
   end

   return @output
end

#translate_text(rexml_text, level = 0) ⇒ Object



301
302
303
304
305
306
307
308
309
# File 'lib/kwaff/rexml.rb', line 301

def translate_text(rexml_text, level=0)
   str = rexml_text.value
   return @output if str =~ /\A\s*\z/
   prefix = (@space * level) + ". "				## *pattern*
   str.split(/\r?\n/).each do |line|
      @output << prefix << line << @newline
   end
   return @output
end