org.milyn.edisax
Class EDIParser

java.lang.Object
  extended by org.milyn.edisax.EDIParser
All Implemented Interfaces:
org.xml.sax.XMLReader

public class EDIParser
extends java.lang.Object
implements org.xml.sax.XMLReader

EDI Parser.

Generates a stream of SAX events from an EDI message stream based on the supplied mapping model.

Usage

        InputStream ediInputStream = ....
        InputStream edi2SaxMappingConfig = ....
        ContentHandler contentHandler = ....
                
        EDIParser parser = new EDIParser();
                
        parser.setContentHandler(contentHandler);
        parser.setMappingModel(EDIParser.parseMappingModel(edi2SaxMappingConfig));
        parser.parse(new InputSource(ediInputStream));
        etc... 
 

Mapping Model

The EDI to SAX Event mapping is performed based on an "Mapping Model" supplied to the parser. This model must be based on the edi-message-mapping-1.0.xsd schema.

From this schema you can see that segment groups are supported (nested segments), including groups within groups, repeating segments and repeating segment groups. Be sure to review the schema.

Example (Input EDI, EDI to XML Mapping and Output SAX Events)

The following illustration attempts to create a visualisation of the mapping process. The "input-message.edi" file specifies the EDI input, "edi-to-xml-order-mapping.xml" describes how to map that EDI message to SAX events and "expected.xml" illustrates the XML that would result from applying the mapping.

So the above illustration attempts to highlight the following:

  1. How the message delimiters (segment, field, component and sub-component) are specified in the mapping. In particular, how special characters like the linefeed character are specified using XML Character References.
  2. How segment groups (nested segments) are specified. In this case the first 2 segments are part of a group.
  3. How the actual field, component and sub-component values are specified and mapped to the target SAX events (to generate the XML).

Segment Cardinality

What's not shown above is how the <medi:segment> element supports the 2 optional attributes "minOccurs" and "maxOccurs" (default value of 1 in both cases). These attributes can be used to control the optional and required characteristics of a segment. A maxOccurs value of -1 indicates that the segment can repeat any number of times in that location of the EDI message (unbounded).

Segment Groups

Segment groups can be added using the <segmentGroup> element. A Segment group is matched by the first segment in the group. A Segment Group can contain nested <segmentGroup> elements, but the first element in a <segmentGroup> must be a <segment>. <segmentGroup> elements support minOccurs/maxOccurs cardinality. They also support an optional "xmlTag" attribute, when if present will result in the XML generated by a matched segment group being inserted inside an element having the name of the xmlTag attribute value.

Segment Matching

Segments are matched in one of 2 ways:
  1. By an exact match on the segment code (segcode).
  2. By a regex pattern match on the full segment, where the segcode attribute defines the regex pattern (e.g. segcode="1A\*a.*").

Required Values

<field>, <component> and <sub-component> configurations support a "required" attribute, which flags that <field>, <component> or <sub-component> as requiring a value.

By default, values are not required (fields, components and sub-components).

Truncation

<segment>, <field> and <component> configurations support a "truncatable" attribute. For a segment, this means that parser errors will not be generated when that segment does not specify trailing fields that are not "required" (see "required" attribute above). Likewise for fields/components and components/sub-components.

By default, segments, fields, and components are not truncatable.

Author:
tfennelly

Field Summary
static java.lang.String FEATURE_IGNORE_NEWLINES
           
static java.lang.String FEATURE_VALIDATE
           
 
Constructor Summary
EDIParser()
           
 
Method Summary
 void endElement(java.lang.String elementName, boolean indent)
           
 org.xml.sax.ContentHandler getContentHandler()
           
 org.xml.sax.DTDHandler getDTDHandler()
           
 org.xml.sax.EntityResolver getEntityResolver()
           
 org.xml.sax.ErrorHandler getErrorHandler()
           
 boolean getFeature(java.lang.String name)
           
 MutableInt getIndentDepth()
          Get the indent depth counter
 java.lang.Object getProperty(java.lang.String name)
           
 void mapFields(java.lang.String[] currentSegmentFields, Segment segment)
          Map the individual field values based on the supplied expected field configs.
 void parse()
          Parse an EDI message, using a supplied segment reader.
 void parse(org.xml.sax.InputSource ediInputSource)
          Parse an EDI InputSource.
 void parse(java.lang.String systemId)
          The following methods are currently unimplemnted...
static EdifactModel parseMappingModel(java.io.InputStream mappingConfigStream)
          Parse the supplied mapping model config stream and return the generated EdiMap.
static EdifactModel parseMappingModel(java.io.InputStream mappingConfigStream, Description mappingDescription, java.net.URI resourceURI, java.net.URI importBaseURI)
          Parse the supplied mapping model config stream and return the generated EdiMap.
static EdifactModel parseMappingModel(java.io.Reader mappingConfigStream)
          Parse the supplied mapping model config stream and return the generated EdiMap.
static EdifactModel parseMappingModel(java.io.Reader mappingConfigStream, Description mappingDescription, java.net.URI resourceURI, java.net.URI importBaseURI)
          Parse the supplied mapping model config stream and return the generated EdiMap.
static EdifactModel parseMappingModel(java.lang.String mappingConfig, java.net.URI baseURI)
          Parse the supplied mapping model config path and return the generated EdiMap.
 EDIParser setBufferedSegmentReader(BufferedSegmentReader segmentReader)
           
 void setContentHandler(org.xml.sax.ContentHandler contentHandler)
           
 void setDTDHandler(org.xml.sax.DTDHandler arg0)
           
 void setEntityResolver(org.xml.sax.EntityResolver arg0)
           
 void setErrorHandler(org.xml.sax.ErrorHandler arg0)
           
 void setFeature(java.lang.String name, boolean value)
           
 void setIndentDepth(MutableInt indentDepth)
          Set the indent depth counter
 void setMappingModel(EdifactModel mappingModel)
          Set the EDI mapping model to be used in all subsequent parse operations.
 void setProperty(java.lang.String name, java.lang.Object value)
           
 void startElement(java.lang.String elementName, boolean indent)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FEATURE_VALIDATE

public static final java.lang.String FEATURE_VALIDATE
See Also:
Constant Field Values

FEATURE_IGNORE_NEWLINES

public static final java.lang.String FEATURE_IGNORE_NEWLINES
See Also:
Constant Field Values
Constructor Detail

EDIParser

public EDIParser()
Method Detail

parseMappingModel

public static EdifactModel parseMappingModel(java.lang.String mappingConfig,
                                             java.net.URI baseURI)
                                      throws java.io.IOException,
                                             org.xml.sax.SAXException,
                                             EDIConfigurationException
Parse the supplied mapping model config path and return the generated EdiMap.

Can be used to set the mapping model to be used during the parsing operation. See setMappingModel(EdifactModel).

Parameters:
mappingConfig - Config path. Must conform with the edi-message-mapping-1.0.xsd schema.
baseURI - The base URI against which the config path is to be resolved. This works on down and helps in resolving imported model.
Returns:
The Edimap for the mapping model.
Throws:
java.io.IOException - Error reading the model stream.
org.xml.sax.SAXException - Invalid model.
EDIConfigurationException - when edi-mapping-configuration is incorrect.

parseMappingModel

public static EdifactModel parseMappingModel(java.io.InputStream mappingConfigStream)
                                      throws java.io.IOException,
                                             org.xml.sax.SAXException,
                                             EDIConfigurationException
Parse the supplied mapping model config stream and return the generated EdiMap.

Can be used to set the mapping model to be used during the parsing operation. See setMappingModel(EdifactModel).

Parameters:
mappingConfigStream - Config stream. Must conform with the edi-message-mapping-1.0.xsd schema.
Returns:
The Edimap for the mapping model.
Throws:
java.io.IOException - Error reading the model stream.
org.xml.sax.SAXException - Invalid model.
EDIConfigurationException - when edi-mapping-configuration is incorrect.

parseMappingModel

public static EdifactModel parseMappingModel(java.io.InputStream mappingConfigStream,
                                             Description mappingDescription,
                                             java.net.URI resourceURI,
                                             java.net.URI importBaseURI)
                                      throws java.io.IOException,
                                             org.xml.sax.SAXException,
                                             EDIConfigurationException
Parse the supplied mapping model config stream and return the generated EdiMap.

Can be used to set the mapping model to be used during the parsing operation. See setMappingModel(EdifactModel).

Parameters:
mappingConfigStream - Config stream. Must conform with the edi-message-mapping-1.0.xsd schema.
mappingDescription - Mapping Model Description.
resourceURI - The resource URI.
importBaseURI - The base URI for loading imports.
Returns:
The Edimap for the mapping model.
Throws:
java.io.IOException - Error reading the model stream.
org.xml.sax.SAXException - Invalid model.
EDIConfigurationException - when edi-mapping-configuration is incorrect.

parseMappingModel

public static EdifactModel parseMappingModel(java.io.Reader mappingConfigStream)
                                      throws java.io.IOException,
                                             org.xml.sax.SAXException,
                                             EDIConfigurationException
Parse the supplied mapping model config stream and return the generated EdiMap.

Can be used to set the mapping model to be used during the parsing operation. See setMappingModel(org.milyn.edisax.model.EdifactModel).

Parameters:
mappingConfigStream - Config stream. Must conform with the edi-message-mapping-1.0.xsd schema.
Returns:
The EdifactModel for the mapping model.
Throws:
java.io.IOException - Error reading the model stream.
org.xml.sax.SAXException - Invalid model.
EDIConfigurationException - when edi-mapping-configuration is incorrect.

parseMappingModel

public static EdifactModel parseMappingModel(java.io.Reader mappingConfigStream,
                                             Description mappingDescription,
                                             java.net.URI resourceURI,
                                             java.net.URI importBaseURI)
                                      throws java.io.IOException,
                                             org.xml.sax.SAXException,
                                             EDIConfigurationException
Parse the supplied mapping model config stream and return the generated EdiMap.

Can be used to set the mapping model to be used during the parsing operation. See setMappingModel(org.milyn.edisax.model.EdifactModel).

Parameters:
mappingConfigStream - Config stream. Must conform with the edi-message-mapping-1.0.xsd schema.
mappingDescription - Mapping Model Description.
resourceURI - The resource URI.
importBaseURI - The base URI for loading imports.
Returns:
The EdifactModel for the mapping model.
Throws:
java.io.IOException - Error reading the model stream.
org.xml.sax.SAXException - Invalid model.
EDIConfigurationException - when edi-mapping-configuration is incorrect.

setMappingModel

public void setMappingModel(EdifactModel mappingModel)
Set the EDI mapping model to be used in all subsequent parse operations.

The model can be generated through a call to the EDIParser.

Parameters:
mappingModel - The mapping model.

getIndentDepth

public MutableInt getIndentDepth()
Get the indent depth counter

Returns:
Indent depth counter.

setIndentDepth

public void setIndentDepth(MutableInt indentDepth)
Set the indent depth counter

Parameters:
indentDepth - Indent depth counter.

parse

public void parse(org.xml.sax.InputSource ediInputSource)
           throws java.io.IOException,
                  org.xml.sax.SAXException
Parse an EDI InputSource.

Specified by:
parse in interface org.xml.sax.XMLReader
Throws:
java.io.IOException
org.xml.sax.SAXException

parse

public void parse()
           throws java.io.IOException,
                  org.xml.sax.SAXException
Parse an EDI message, using a supplied segment reader.

Throws:
java.io.IOException
org.xml.sax.SAXException

setBufferedSegmentReader

public EDIParser setBufferedSegmentReader(BufferedSegmentReader segmentReader)

mapFields

public void mapFields(java.lang.String[] currentSegmentFields,
                      Segment segment)
               throws org.xml.sax.SAXException
Map the individual field values based on the supplied expected field configs.

Parameters:
currentSegmentFields - Segment fields from the input message.
segment - List of expected field mapping configurations that the currentSegmentFields are expected to map to.
Throws:
org.xml.sax.SAXException - EDI processing exception.

startElement

public void startElement(java.lang.String elementName,
                         boolean indent)
                  throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

endElement

public void endElement(java.lang.String elementName,
                       boolean indent)
                throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException

setContentHandler

public void setContentHandler(org.xml.sax.ContentHandler contentHandler)
Specified by:
setContentHandler in interface org.xml.sax.XMLReader

getContentHandler

public org.xml.sax.ContentHandler getContentHandler()
Specified by:
getContentHandler in interface org.xml.sax.XMLReader

parse

public void parse(java.lang.String systemId)
           throws java.io.IOException,
                  org.xml.sax.SAXException
The following methods are currently unimplemnted...

Specified by:
parse in interface org.xml.sax.XMLReader
Throws:
java.io.IOException
org.xml.sax.SAXException

getFeature

public boolean getFeature(java.lang.String name)
                   throws org.xml.sax.SAXNotRecognizedException,
                          org.xml.sax.SAXNotSupportedException
Specified by:
getFeature in interface org.xml.sax.XMLReader
Throws:
org.xml.sax.SAXNotRecognizedException
org.xml.sax.SAXNotSupportedException

setFeature

public void setFeature(java.lang.String name,
                       boolean value)
Specified by:
setFeature in interface org.xml.sax.XMLReader

getDTDHandler

public org.xml.sax.DTDHandler getDTDHandler()
Specified by:
getDTDHandler in interface org.xml.sax.XMLReader

setDTDHandler

public void setDTDHandler(org.xml.sax.DTDHandler arg0)
Specified by:
setDTDHandler in interface org.xml.sax.XMLReader

getEntityResolver

public org.xml.sax.EntityResolver getEntityResolver()
Specified by:
getEntityResolver in interface org.xml.sax.XMLReader

setEntityResolver

public void setEntityResolver(org.xml.sax.EntityResolver arg0)
Specified by:
setEntityResolver in interface org.xml.sax.XMLReader

getErrorHandler

public org.xml.sax.ErrorHandler getErrorHandler()
Specified by:
getErrorHandler in interface org.xml.sax.XMLReader

setErrorHandler

public void setErrorHandler(org.xml.sax.ErrorHandler arg0)
Specified by:
setErrorHandler in interface org.xml.sax.XMLReader

getProperty

public java.lang.Object getProperty(java.lang.String name)
                             throws org.xml.sax.SAXNotRecognizedException,
                                    org.xml.sax.SAXNotSupportedException
Specified by:
getProperty in interface org.xml.sax.XMLReader
Throws:
org.xml.sax.SAXNotRecognizedException
org.xml.sax.SAXNotSupportedException

setProperty

public void setProperty(java.lang.String name,
                        java.lang.Object value)
                 throws org.xml.sax.SAXNotRecognizedException,
                        org.xml.sax.SAXNotSupportedException
Specified by:
setProperty in interface org.xml.sax.XMLReader
Throws:
org.xml.sax.SAXNotRecognizedException
org.xml.sax.SAXNotSupportedException


Copyright © 2011. All Rights Reserved.