Class: EdifactConverter::EDI2XML11::XmlHandler

Inherits:
EdifactConverter::EmptyHandler show all
Defined in:
lib/edifact_converter/edi2xml11/xml_handler.rb

Defined Under Namespace

Classes: XmlElement

Instance Attribute Summary collapse

Attributes inherited from EdifactConverter::EmptyHandler

#locator, #next_handler

Instance Method Summary collapse

Methods inherited from EdifactConverter::EmptyHandler

#initialize, #method_missing

Constructor Details

This class inherits a constructor from EdifactConverter::EmptyHandler

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class EdifactConverter::EmptyHandler

Instance Attribute Details

#currentObject

Returns the value of attribute current.



8
9
10
# File 'lib/edifact_converter/edi2xml11/xml_handler.rb', line 8

def current
  @current
end

#documentObject

Returns the value of attribute document.



8
9
10
# File 'lib/edifact_converter/edi2xml11/xml_handler.rb', line 8

def document
  @document
end

Instance Method Details

#endDocumentObject



51
52
53
54
55
56
# File 'lib/edifact_converter/edi2xml11/xml_handler.rb', line 51

def endDocument
  unless current == document
    raise EdifactConverter::EdifactError.new "Internal Error, please report to [email protected]", Position.new(0,0)
  end
  super
end

#endElementObject



83
84
85
86
# File 'lib/edifact_converter/edi2xml11/xml_handler.rb', line 83

def endElement
  self.current = current.parent
  super
end

#endSegment(name) ⇒ Object



73
74
75
76
# File 'lib/edifact_converter/edi2xml11/xml_handler.rb', line 73

def endSegment(name)
  self.current = self.current.parent
  super
end

#endSegmentGroup(name) ⇒ Object



63
64
65
66
# File 'lib/edifact_converter/edi2xml11/xml_handler.rb', line 63

def endSegmentGroup(name)
  self.current = current.parent
  super
end

#startDocumentObject



46
47
48
49
# File 'lib/edifact_converter/edi2xml11/xml_handler.rb', line 46

def startDocument
  self.document = self.current = XmlElement.new name: 'Edifact', position: Position.new(0, 0)
  super
end

#startElementObject



78
79
80
81
# File 'lib/edifact_converter/edi2xml11/xml_handler.rb', line 78

def startElement
  self.current = XmlElement.new name: 'Elm', position: locator.position, parent: current
  super
end

#startSegment(name) ⇒ Object



68
69
70
71
# File 'lib/edifact_converter/edi2xml11/xml_handler.rb', line 68

def startSegment(name)
  self.current = XmlElement.new name: name.encode(Encoding::UTF_8), position: locator.position, parent: current
  super
end

#startSegmentGroup(name, hidden = false) ⇒ Object



58
59
60
61
# File 'lib/edifact_converter/edi2xml11/xml_handler.rb', line 58

def startSegmentGroup(name, hidden = false)
  self.current = XmlElement.new name: name.encode(Encoding::UTF_8), position: locator.position, parent: current, hidden: hidden
  super
end

#value(value) ⇒ Object



88
89
90
91
92
# File 'lib/edifact_converter/edi2xml11/xml_handler.rb', line 88

def value(value)
  XmlElement.new name: 'SubElm', position: locator.position, parent: current, text: value.encode(Encoding::UTF_8)
  #val.text = value.encode(Encoding::UTF_8)
  super
end

#xmlObject



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/edifact_converter/edi2xml11/xml_handler.rb', line 94

def xml
  builder = Nokogiri::XML::Builder.new(:encoding => 'ISO-8859-1') do |xml|
    xml.comment "Created from Edifact at #{Time.now.utc}"
    document.render xml
  end
  xml = builder.doc
  xml.xpath("//Elm").each do |elm|
    elm.children.last.remove if elm.children.last.content == ""
  end
  xml
end