Module: EDI::E

Defined in:
lib/edi4r/edifact.rb,
lib/edi4r/edifact-rexml.rb

Overview

UN/EDIFACT add-ons to EDI module,

Methods for XML support for the UN/EDIFACT module

:include: ../../AuthorCopyright

$Id: edifact-rexml.rb,v 1.1 2006/08/01 11:14:18 werntges Exp $ – $Log: edifact-rexml.rb,v $ Revision 1.1 2006/08/01 11:14:18 werntges Initial revision

Derived from “edifact.rb” (precursor) by HWW

To-do list: SV4 - Support & testing ++

This is the XML add-on for UN/EDIFACT module of edi4r (hence '::E')

It leaves all real work to the base classes. Only the UNA information is treated in a special way (as a “Parameter” element of the header) and dealt with here.

Defined Under Namespace

Classes: CDE, DE, EDISyntaxError, Interchange, Message, MsgGroup, Segment, UNA

Constant Summary

Illegal_Charset_Patterns =

Use pattern for allowed chars of UNOC charset if none given explicitly

Hash.new(/[^-A-Za-z0-9 .,()\/=!%"&*;<>'+:?\u{00}-\u{10ffff}}]+/)

Class Method Summary collapse

Class Method Details

.count_escapes(str, e) ⇒ Object

:nodoc:



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/edi4r/edifact.rb', line 129

def count_escapes( str, e ) # :nodoc:
  n = 0
  (str.length-1).downto(0) do |i|
    if str[i]==e
      n += 1
    else
      return n
    end
  end
  n
end

.edi_split(str, s, e, max = 0) ⇒ Object

Utility: Separator method for UN/EDIFACT segments/CDEs

The given string typically comprises an EDIFACT segment or a CDE. We want to split it into its elements and return those in an array. The tricky part is the proper handling of character escaping!

Examples:

CDE = "1234:ABC:567" 	 --> ['1234','ABC','567']
CDE = "1234::567"		 --> ['1234','','567']
CDE = ":::SOMETEXT"	 --> ['','','','SOMETEXT']
Seg = "TAG+1++2:3:4+A?+B=C" --> ['TAG','1','','2:3:4','A+B=C']

NOTE: This function might be a good candidate for implementation in “C”

Also see: ../../test/test_edi_split.rb

str

String to split

s

Separator char (an Integer)

e

Escape / release char (an Integer)

max

Max. desired number of result items, default = all

Returns:

Array of split results (strings without their terminating separator)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/edi4r/edifact.rb', line 98

def edi_split( str, s, e, max=0 )
  results, item, start = [], '', 0
  while start < str.length do
    # match_at = index of next separator, or -1 if none found
    match_at = ((start...str.length).find{|i| str[i] == s}) || str.length
    item += str[start...match_at]
    # Count escapes in front of separator. No real separator if odd!
    escapes = count_escapes( item, e )
    if escapes & 1 == 1 # odd
      raise EDISyntaxError, "Pending escape char in #{str}" if match_at == str.length
      (escapes/2+1).times {item.chop!} # chop off duplicate escapes
      item << s # add separator as regular character
    else # even
      (escapes/2).times {item.chop!}  # chop off duplicate escapes
      results << item
      item = ''
    end
    start = match_at + 1
  end
  #
  # Do not return trailing empty items
  #
  results << item unless item.empty?
  return results if results.empty?
  while results.any? && results.last.empty?; results.pop; end
  results
end