Class: EDI::E::Interchange

Inherits:
Object
  • Object
show all
Defined in:
lib/edi4r/edifact-rexml.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse_xml(xdoc) ⇒ Object

Returns a REXML document that represents the interchange

xdoc

REXML document that contains the XML representation of a UN/EDIFACT interchange



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/edi4r/edifact-rexml.rb', line 34

def Interchange.parse_xml( xdoc )
  _root = xdoc.root
  _header  = _root.elements["Header"]
  _trailer = _root.elements["Trailer"]
  _una  = _header.elements["Parameter[@name='UNA']"]
  _una = _una.text if _una
  raise "Empty UNA" if _una and _una.empty? # remove later!
  # S001: Works for both batch and interactive EDI:
  _s001 =  _header.elements["Segment/CDE[@name='S001']"]
  _version = _s001.elements["DE[@name='0002']"].text.to_i
  _charset = _s001.elements["DE[@name='0001']"].text
  params = { :charset => _charset, :version => _version }
  if _una
    params[:una_string] = _una
    params[:show_una] = true
  end
  ic = Interchange.new( params )
  if _root.elements["Message"].nil? # correct ??
    _root.elements.each('MsgGroup') do |xel|
      ic.add( MsgGroup.parse_xml( ic, xel ), false )
    end
  else
    _root.elements.each('Message') do |xel|
      ic.add( Message.parse_xml( ic, xel ), false )
    end
  end

  ic.header  = Segment.parse_xml( ic, _header.elements["Segment"] )
  ic.trailer = Segment.parse_xml( ic, _trailer.elements["Segment"] )
  ic.validate
  ic
end

.peek_xml(xdoc) ⇒ Object

Read maxlen bytes from $stdin (default) or from given stream (UN/EDIFACT data expected), and peek into first segment (UNB/UIB).

Returns an empty Interchange object with a properly header filled.

Intended use:

Efficient routing by reading just UNB data: sender/recipient/ref/test


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/edi4r/edifact-rexml.rb', line 76

def Interchange.peek_xml(xdoc) # Handle to REXML document
  _root = xdoc.root
  _header  = _root.elements["Header"]
  _trailer = _root.elements["Trailer"]
  _una  = _header.elements["Parameter[@name='UNA']"]
  _una = _una.text if _una
  raise "Empty UNA" if _una and _una.empty? # remove later!
  # S001: Works for both batch and interactive EDI:
  _s001 =  _header.elements["Segment/CDE[@name='S001']"]
  _version = _s001.elements["DE[@name='0002']"].text.to_i
  _charset = _s001.elements["DE[@name='0001']"].text
  params = { :charset => _charset, :version => _version }
  if _una
    params[:una_string] = _una
    params[:show_una] = true
  end
  ic = Interchange.new( params )

  ic.header  = Segment.parse_xml( ic, _header.elements["Segment"] )
  ic.trailer = Segment.parse_xml( ic, _trailer.elements["Segment"] )

  ic
end

Instance Method Details

#to_din16557_4(xdoc = REXML::Document.new) ⇒ Object

Returns a REXML document that represents the interchange according to DIN 16557-4



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/edi4r/edifact-rexml.rb', line 122

def to_din16557_4( xdoc = REXML::Document.new )
  externalID = "SYSTEM \"edifact.dtd\""
  doc_element_name = 'EDIFACTINTERCHANGE'
  xdoc << REXML::XMLDecl.new
  xdoc << REXML::DocType.new( doc_element_name, externalID )

  doc_el = REXML::Element.new( doc_element_name )
  xel  = REXML::Element.new( 'UNA' ) 
  xel.attributes["UNA1"]  = una.ce_sep.chr
  xel.attributes["UNA2"]  = una.de_sep.chr
  xel.attributes["UNA3"]  = una.decimal_sign.chr
  xel.attributes["UNA4"]  = una.esc_char.chr
  xel.attributes["UNA5"]  = una.rep_sep.chr
  xel.attributes["UNA6"]  = una.seg_term.chr
  xdoc.elements << doc_el
  doc_el.elements << xel

  super( xdoc.root )
  xdoc
end

#to_xml(xdoc = REXML::Document.new) ⇒ Object

Returns a REXML document that represents the interchange



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/edi4r/edifact-rexml.rb', line 104

def to_xml( xdoc = REXML::Document.new )
  rc = super
  # Add parameter(s) to header in rc[1]
  unless @una.nil? #@una.empty?
    xel = REXML::Element.new('Parameter')
    rc[1] << xel
    xel.attributes["name"] = 'UNA'
    xel.text = @una.to_s
  end
#      rc
  xdoc
end