org.milyn.edisax
Class EDIParser

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

public class EDIParser
extends Object
implements 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 String FEATURE_IGNORE_EMPTY_NODES
           
static String FEATURE_IGNORE_NEWLINES
           
static String FEATURE_VALIDATE
           
 
Constructor Summary
EDIParser()
           
 
Method Summary
 void endElement(MappingNode node, boolean indent)
           
 void endElement(String elementName, String namespace, boolean indent)
           
 ContentHandler getContentHandler()
           
 DTDHandler getDTDHandler()
           
 EntityResolver getEntityResolver()
           
 ErrorHandler getErrorHandler()
           
 boolean getFeature(String name)
           
 Map<String,Boolean> getFeatures()
           
 org.milyn.lang.MutableInt getIndentDepth()
          Get the indent depth counter
 Object getProperty(String name)
           
 void mapFields(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(InputSource ediInputSource)
          Parse an EDI InputSource.
 void parse(String systemId)
          The following methods are currently unimplemnted...
static EdifactModel parseMappingModel(InputStream mappingConfigStream)
          Parse the supplied mapping model config stream and return the generated EdiMap.
static EdifactModel parseMappingModel(InputStream mappingConfigStream, Description mappingDescription, URI resourceURI, URI importBaseURI)
          Parse the supplied mapping model config stream and return the generated EdiMap.
static EdifactModel parseMappingModel(Reader mappingConfigStream)
          Parse the supplied mapping model config stream and return the generated EdiMap.
static EdifactModel parseMappingModel(Reader mappingConfigStream, Description mappingDescription, URI resourceURI, URI importBaseURI)
          Parse the supplied mapping model config stream and return the generated EdiMap.
static EdifactModel parseMappingModel(String mappingConfig, URI baseURI)
          Parse the supplied mapping model config path and return the generated EdiMap.
 EDIParser setBufferedSegmentReader(BufferedSegmentReader segmentReader)
           
 void setContentHandler(ContentHandler contentHandler)
           
 void setDTDHandler(DTDHandler arg0)
           
 void setEntityResolver(EntityResolver arg0)
           
 void setErrorHandler(ErrorHandler arg0)
           
 void setFeature(String name, boolean value)
           
 void setIndentDepth(org.milyn.lang.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 setNamespaceDeclarationStack(org.milyn.namespace.NamespaceDeclarationStack nsStack)
          Set the NamespaceDeclarationStack to be used by the reader instance.
 void setProperty(String name, Object value)
           
 void startElement(MappingNode node, boolean indent)
           
 void startElement(String elementName, String namespace, boolean indent)
           
 void startElement(String elementName, String namespace, boolean indent, Attributes attributes)
           
 
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 String FEATURE_VALIDATE
See Also:
Constant Field Values

FEATURE_IGNORE_NEWLINES

public static final String FEATURE_IGNORE_NEWLINES
See Also:
Constant Field Values

FEATURE_IGNORE_EMPTY_NODES

public static final String FEATURE_IGNORE_EMPTY_NODES
See Also:
Constant Field Values
Constructor Detail

EDIParser

public EDIParser()
Method Detail

setNamespaceDeclarationStack

public void setNamespaceDeclarationStack(org.milyn.namespace.NamespaceDeclarationStack nsStack)
Set the NamespaceDeclarationStack to be used by the reader instance.

Parameters:
nsStack - The NamespaceDeclarationStack to be used by the reader instance.

parseMappingModel

public static EdifactModel parseMappingModel(String mappingConfig,
                                             URI baseURI)
                                      throws IOException,
                                             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:
IOException - Error reading the model stream.
SAXException - Invalid model.
EDIConfigurationException - when edi-mapping-configuration is incorrect.

parseMappingModel

public static EdifactModel parseMappingModel(InputStream mappingConfigStream)
                                      throws IOException,
                                             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:
IOException - Error reading the model stream.
SAXException - Invalid model.
EDIConfigurationException - when edi-mapping-configuration is incorrect.

parseMappingModel

public static EdifactModel parseMappingModel(InputStream mappingConfigStream,
                                             Description mappingDescription,
                                             URI resourceURI,
                                             URI importBaseURI)
                                      throws IOException,
                                             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:
IOException - Error reading the model stream.
SAXException - Invalid model.
EDIConfigurationException - when edi-mapping-configuration is incorrect.

parseMappingModel

public static EdifactModel parseMappingModel(Reader mappingConfigStream)
                                      throws IOException,
                                             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:
IOException - Error reading the model stream.
SAXException - Invalid model.
EDIConfigurationException - when edi-mapping-configuration is incorrect.

parseMappingModel

public static EdifactModel parseMappingModel(Reader mappingConfigStream,
                                             Description mappingDescription,
                                             URI resourceURI,
                                             URI importBaseURI)
                                      throws IOException,
                                             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:
IOException - Error reading the model stream.
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 org.milyn.lang.MutableInt getIndentDepth()
Get the indent depth counter

Returns:
Indent depth counter.

setIndentDepth

public void setIndentDepth(org.milyn.lang.MutableInt indentDepth)
Set the indent depth counter

Parameters:
indentDepth - Indent depth counter.

parse

public void parse(InputSource ediInputSource)
           throws IOException,
                  SAXException
Parse an EDI InputSource.

Specified by:
parse in interface XMLReader
Throws:
IOException
SAXException

parse

public void parse()
           throws IOException,
                  SAXException
Parse an EDI message, using a supplied segment reader.

Throws:
IOException
SAXException

setBufferedSegmentReader

public EDIParser setBufferedSegmentReader(BufferedSegmentReader segmentReader)

mapFields

public void mapFields(String[] currentSegmentFields,
                      Segment segment)
               throws 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:
SAXException - EDI processing exception.

startElement

public void startElement(MappingNode node,
                         boolean indent)
                  throws SAXException
Throws:
SAXException

startElement

public void startElement(String elementName,
                         String namespace,
                         boolean indent)
                  throws SAXException
Throws:
SAXException

startElement

public void startElement(String elementName,
                         String namespace,
                         boolean indent,
                         Attributes attributes)
                  throws SAXException
Throws:
SAXException

endElement

public void endElement(MappingNode node,
                       boolean indent)
                throws SAXException
Throws:
SAXException

endElement

public void endElement(String elementName,
                       String namespace,
                       boolean indent)
                throws SAXException
Throws:
SAXException

setContentHandler

public void setContentHandler(ContentHandler contentHandler)
Specified by:
setContentHandler in interface XMLReader

getContentHandler

public ContentHandler getContentHandler()
Specified by:
getContentHandler in interface XMLReader

getFeatures

public Map<String,Boolean> getFeatures()

parse

public void parse(String systemId)
           throws IOException,
                  SAXException
The following methods are currently unimplemnted...

Specified by:
parse in interface XMLReader
Throws:
IOException
SAXException

getFeature

public boolean getFeature(String name)
Specified by:
getFeature in interface XMLReader

setFeature

public void setFeature(String name,
                       boolean value)
Specified by:
setFeature in interface XMLReader

getDTDHandler

public DTDHandler getDTDHandler()
Specified by:
getDTDHandler in interface XMLReader

setDTDHandler

public void setDTDHandler(DTDHandler arg0)
Specified by:
setDTDHandler in interface XMLReader

getEntityResolver

public EntityResolver getEntityResolver()
Specified by:
getEntityResolver in interface XMLReader

setEntityResolver

public void setEntityResolver(EntityResolver arg0)
Specified by:
setEntityResolver in interface XMLReader

getErrorHandler

public ErrorHandler getErrorHandler()
Specified by:
getErrorHandler in interface XMLReader

setErrorHandler

public void setErrorHandler(ErrorHandler arg0)
Specified by:
setErrorHandler in interface XMLReader

getProperty

public Object getProperty(String name)
                   throws SAXNotRecognizedException,
                          SAXNotSupportedException
Specified by:
getProperty in interface XMLReader
Throws:
SAXNotRecognizedException
SAXNotSupportedException

setProperty

public void setProperty(String name,
                        Object value)
                 throws SAXNotRecognizedException,
                        SAXNotSupportedException
Specified by:
setProperty in interface XMLReader
Throws:
SAXNotRecognizedException
SAXNotSupportedException


Copyright © 2018. All rights reserved.