class RGen::Serializer::XMLSerializer
Constants
- INDENT_SPACE
Public Class Methods
new(file)
click to toggle source
# File lib/rgen/serializer/xml_serializer.rb, line 9 def initialize(file) @indent = 0 @lastStartTag = nil @textContent = false @file = file end
Public Instance Methods
endTag(tag)
click to toggle source
# File lib/rgen/serializer/xml_serializer.rb, line 32 def endTag(tag) @indent -= 1 unless handleLastStartTag(true, true) output " "*@indent*INDENT_SPACE unless @textContent output "</#{tag}>\n" end @textContent = false end
serialize(rootElement)
click to toggle source
# File lib/rgen/serializer/xml_serializer.rb, line 16 def serialize(rootElement) raise "Abstract class, overwrite method in subclass!" end
startTag(tag, attributes={})
click to toggle source
# File lib/rgen/serializer/xml_serializer.rb, line 20 def startTag(tag, attributes={}) @textContent = false handleLastStartTag(false, true) if attributes.is_a?(Hash) attrString = attributes.keys.collect{|k| "#{k}=\"#{attributes[k]}\""}.join(" ") else attrString = attributes.collect{|pair| "#{pair[0]}=\"#{pair[1]}\""}.join(" ") end @lastStartTag = " "*@indent*INDENT_SPACE + "<#{tag} "+attrString @indent += 1 end
writeText(text)
click to toggle source
# File lib/rgen/serializer/xml_serializer.rb, line 41 def writeText(text) handleLastStartTag(false, false) output "#{text}" @textContent = true end
Protected Instance Methods
containmentReferences(element)
click to toggle source
# File lib/rgen/serializer/xml_serializer.rb, line 74 def containmentReferences(element) @containmentReferences ||= {} @containmentReferences[element.class] ||= eAllReferences(element).select{|r| r.containment} end
eAllAttributes(element)
click to toggle source
# File lib/rgen/serializer/xml_serializer.rb, line 54 def eAllAttributes(element) @eAllAttributes ||= {} @eAllAttributes[element.class] ||= element.class.ecore.eAllAttributes end
eAllReferences(element)
click to toggle source
# File lib/rgen/serializer/xml_serializer.rb, line 49 def eAllReferences(element) @eAllReferences ||= {} @eAllReferences[element.class] ||= element.class.ecore.eAllReferences end
eAllStructuralFeatures(element)
click to toggle source
# File lib/rgen/serializer/xml_serializer.rb, line 59 def eAllStructuralFeatures(element) @eAllStructuralFeatures ||= {} @eAllStructuralFeatures[element.class] ||= element.class.ecore.eAllStructuralFeatures end
eachReferencedElement(element, refs) { |r,te| ... }
click to toggle source
# File lib/rgen/serializer/xml_serializer.rb, line 64 def eachReferencedElement(element, refs, &block) refs.each do |r| targetElements = element.getGeneric(r.name) targetElements = [targetElements] unless targetElements.is_a?(Array) targetElements.each do |te| yield(r,te) end end end
Private Instance Methods
handleLastStartTag(close, newline)
click to toggle source
# File lib/rgen/serializer/xml_serializer.rb, line 81 def handleLastStartTag(close, newline) return false unless @lastStartTag output @lastStartTag output close ? "/>" : ">" output "\n" if newline @lastStartTag = nil true end
output(text)
click to toggle source
# File lib/rgen/serializer/xml_serializer.rb, line 90 def output(text) @file.write(text) end