class EDI::Interchange
Base class of all interchanges
Attributes
Public Class Methods
Abstract class - don’t instantiate directly
EDI::Collection::new
# File lib/edi4r.rb, line 506 def initialize( user_par=nil ) super( nil, self, 'Interchange' ) @illegal_charset_pattern = /^$/ # Default: Never match a non-empty string @content = nil # nil if empty, else :messages, or :groups EDI::logger = user_par[:logger] if user_par[:logger].is_a? Logger end
Auto-detect file content, optionally decompress, return an Interchange
object of the sub-class that matches the (unzipped) content.
This is a convenience method. When you know the file contents, consider a direct call to Interchange::E::parse etc.
NOTES:
-
Make sure to
require 'zlib'
when applying this method to gzipped files. -
BZIP2 is indirectly supported by calling “bzcat”. Make sure that “bzcat” is available when applying this method to *.bz2 files.
-
Do not pass $stdin to this method - we could not “rewind” it!
# File lib/edi4r.rb, line 527 def Interchange.parse( hnd, auto_validate=true ) case rc=Interchange.detect( hnd ) when 'BZ' then Interchange.parse( EDI::Bzip2Reader.new( hnd ) ) # see "peek" when 'GZ' then Interchange.parse( Zlib::GzipReader.new( hnd ) ) when 'A' then EDI::A::Interchange.parse( hnd, auto_validate ) when 'E' then EDI::E::Interchange.parse( hnd, auto_validate ) when 'I' then EDI::I::Interchange.parse( hnd, auto_validate ) when 'S' then EDI::S::Interchange.parse( hnd, auto_validate ) when 'XA' then EDI::A::Interchange.parse_xml( REXML::Document.new(hnd) ) when 'XE' then EDI::E::Interchange.parse_xml( REXML::Document.new(hnd) ) when 'XI' then EDI::I::Interchange.parse_xml( REXML::Document.new(hnd) ) when 'XS' then EDI::S::Interchange.parse_xml( REXML::Document.new(hnd) ) else raise "#{rc}: Unsupported format key - don\'t know how to proceed!" end end
This is a dispatcher method for your convenience, similar to EDI::Interchange.parse
. It may be used for all supported EDI
standards.
- hnd
-
A
REXML document or something that can be turned into one, i.e. an IO object or a String object with corresponding contents
Returns an Interchange
object of the subclass specified by the “standard_key” atribute of the root element, e.g. a EDI::E::Interchange
.
# File lib/edi4r/rexml.rb, line 103 def Interchange.parse_xml( hnd ) # Handle to REXML document unless hnd.is_a? REXML::Document or hnd.is_a? IO or hnd.is_a? String raise "Unsupported object type: #{hnd.class}" end hnd = REXML::Document.new( hnd ) if hnd.is_a? IO or hnd.is_a? String key = hnd.root.attributes['standard_key'].strip raise "Unsupported standard key: #{key}" if key == 'generic' EDI::const_get(key)::const_get('Interchange').parse_xml( hnd ) # class_sym = (key+'Interchange').intern # EDI::const_get(class_sym).parse_xml( hnd ) end
Auto-detect file content, optionally decompress, return an empty Interchange
object of that sub-class with only the header filled.
This is a convenience method. When you know the file contents, consider a direct call to Interchange::E::peek etc.
NOTES: See Interchange.parse
# File lib/edi4r.rb, line 552 def Interchange.peek( hnd=$stdin, params={}) case rc=Interchange.detect( hnd ) # Does not exist yet! # when 'BZ': Interchange.peek( Zlib::Bzip2Reader.new( hnd ) ) # Temporary substitute, Unix/Linux only, low performance: when 'BZ' then Interchange.peek( EDI::Bzip2Reader.new( hnd ), params ) when 'GZ' then Interchange.peek( Zlib::GzipReader.new( hnd ), params ) when 'A' then EDI::A::Interchange.peek( hnd, params ) when 'E' then EDI::E::Interchange.peek( hnd, params ) when 'I' then EDI::I::Interchange.peek( hnd ) when 'S' then EDI::S::Interchange.peek( hnd ) when 'XA' then EDI::A::Interchange.peek_xml( REXML::Document.new(hnd) ) when 'XE' then EDI::E::Interchange.peek_xml( REXML::Document.new(hnd) ) when 'XI' then EDI::I::Interchange.peek_xml( REXML::Document.new(hnd) ) when 'XS' then EDI::S::Interchange.peek_xml( REXML::Document.new(hnd) ) else raise "#{rc}: Unsupported format key - don\'t know how to proceed!" end end
Public Instance Methods
Add either Message
objects or MsgGroup
objects to an interchange; mixing both types raises a TypeError.
EDI::Collection#add
# File lib/edi4r.rb, line 618 def add( obj, auto_validate=true ) err_msg = "Added object must also be a " if obj.is_a? Message @content = :messages unless @content raise TypeError, err_msg+"'Message'" if @content != :messages elsif obj.is_a? MsgGroup @content = :groups unless @content raise TypeError, err_msg+"'MsgGroup'" if @content != :groups else raise TypeError, "Only Message or MsgGroup allowed here" end obj.validate if auto_validate super( obj ) end
EDI::Collection_HT#to_xml
# File lib/edi4r/rexml.rb, line 117 def to_xml( xel_parent ) externalID = "PUBLIC\n" + " "*9 externalID += "'-//Hochschule RheinMain FB DCSM//DTD XML Representation of EDI data V1.2//EN'\n" externalID += " "*9 externalID += "'http://edi01.cs.hs-rm.de/edi4r/edi4r-1.2.dtd'" xel_parent << REXML::XMLDecl.new xel_parent << REXML::DocType.new( normalized_class_name, externalID ) rc = super xel = rc.first pos = self.class.to_s =~ /EDI::((.*?)::)?Interchange/ raise "This is not an Interchange object: #{rc}!" unless pos==0 xel.attributes["standard_key"] = ($2 and not $2.empty?) ? $2 : "generic" xel.attributes["version"] = @version.to_s xel.attributes.delete "name" rc end