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).

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

Constructor Summary
EDIParser()
           
 
Method Summary
protected static void assertMappingConfigValid(Reader mappingConfigStream)
          Assert that the supplied mapping configuration is valid.
 ContentHandler getContentHandler()
           
 DTDHandler getDTDHandler()
           
 EntityResolver getEntityResolver()
           
 ErrorHandler getErrorHandler()
           
 boolean getFeature(String name)
           
 Object getProperty(String name)
           
 void parse(InputSource ediInputSource)
          Parse an EDI InputSource.
 void parse(String systemId)
          The following methods are currently unimplemnted...
static org.milyn.schema.ediMessageMapping10.EdimapDocument.Edimap parseMappingModel(InputStream mappingConfigStream)
          Parse the supplied mapping model config stream and return the generated EdiMap.
static org.milyn.schema.ediMessageMapping10.EdimapDocument.Edimap parseMappingModel(Reader mappingConfigStream)
          Parse the supplied mapping model config stream and return the generated EdiMap.
 void setContentHandler(ContentHandler contentHandler)
           
 void setDTDHandler(DTDHandler arg0)
           
 void setEntityResolver(EntityResolver arg0)
           
 void setErrorHandler(ErrorHandler arg0)
           
 void setFeature(String name, boolean value)
           
 void setMappingModel(org.milyn.schema.ediMessageMapping10.EdimapDocument.Edimap mappingModel)
          Set the EDI mapping model to be used in all subsequent parse operations.
 void setProperty(String name, Object value)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EDIParser

public EDIParser()
Method Detail

parseMappingModel

public static org.milyn.schema.ediMessageMapping10.EdimapDocument.Edimap parseMappingModel(InputStream mappingConfigStream)
                                                                                    throws IOException,
                                                                                           SAXException
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(Edimap).

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.

parseMappingModel

public static org.milyn.schema.ediMessageMapping10.EdimapDocument.Edimap parseMappingModel(Reader mappingConfigStream)
                                                                                    throws IOException,
                                                                                           SAXException
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(Edimap).

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.

assertMappingConfigValid

protected static void assertMappingConfigValid(Reader mappingConfigStream)
                                        throws IOException,
                                               SAXException
Assert that the supplied mapping configuration is valid.

Parameters:
mappingConfigStream -
Throws:
IOException - Failed to read the schema.
SAXException - Invalid configuration.

setMappingModel

public void setMappingModel(org.milyn.schema.ediMessageMapping10.EdimapDocument.Edimap mappingModel)
Set the EDI mapping model to be used in all subsequent parse operations.

The model can be generated through a call to parseMappingModel(InputStream).

Parameters:
mappingModel - The mapping model.

parse

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

Specified by:
parse in interface XMLReader
Throws:
IOException
SAXException

setContentHandler

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

getContentHandler

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

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)
                   throws SAXNotRecognizedException,
                          SAXNotSupportedException
Specified by:
getFeature in interface XMLReader
Throws:
SAXNotRecognizedException
SAXNotSupportedException

setFeature

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

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 © 2008. All Rights Reserved.