Wednesday, August 12, 2015

Parsing X12 Documents






X12 is a EDI standard for business transactions which is popular in North America.
(EDI is  sending  formatted data, by agreed message standards, from one system to another system )

Describing X12 or EDI is so long.
In this post I will just tell about how to write a simple X12 object for parsing data.
X12 lines  are just separated values so an object below will represent a common X12 segment.

For ones dealing with same things below will make sense.
*** This is just for giving idea.


public class X12Segment 
{

   public static final String NAME_ISA = "ISA" ;

    public static final String NAME_GS = "GS" ;
//................................  ALL TAGS YOU NEED

    // protected String segmentSeparator = "\n" ;

    // protected String fieldSeparator = "*" ;

    protected YX12Config config ;

    // X12SectionName|field1|field2|field3...|fieldn~
    protected String line ;

    protected String segmentName ;

    protected String[ ] fields ;

    public X12Segment( String line, YX12Config config )
    {
        this.config = config ;

        this.init( line ) ;
    }

    public void init( String line )
    {
        String strDelim = ( isBlankTrim( this.config.getSegmentDelimiter( ) ) ? this.config.getNewLineStr( ) : this.config.getSegmentDelimiter( ) ) ;

      

        this.line = line.trim( ).split( "\\" + strDelim, -2 )[ 0 ] ;

        final String[ ] tmp = this.line.split( "\\" + this.config.getFieldDelimiter( ), -2 ) ;

        this.segmentName = tmp[ 0 ] ;

        this.fields = tmp ;
    }

    public boolean checkType( String type )
    {
        return type.equals( this.getSegmentName( ) ) ;
    }

    public int getFieldCount( )
    {
        return this.fields.length - 1 ;
    }

    public String getField( int index )
    {
        return index >= this.fields.length ? null : this.fields[ index ] ;
    }

    public String[ ] getFields( )
    {
        return this.fields ;
    }

    public void setFields( String[ ] fields )
    {
        this.fields = fields ;
    }

    public String getSegmentName( )
    {
        return this.segmentName ;
    }

    public void setSegmentName( String segmentName )
    {
        this.segmentName = segmentName ;
    }

    public YX12Config getConfig( )
    {
        return this.config ;
    }

    public void setConfig( YX12Config config )
    {
        this.config = config ;
    }

    public String getLine( )
    {
        return this.line ;
    }

    public void setLine( String line )
    {
        this.line = line ;
    }

    public String toString( YX12Config config )
    {
        StringBuilder sb = new StringBuilder( this.segmentName ) ;

        for( int i = 1; i < this.fields.length; ++i )
        {
            sb.append( config.getFieldDelimiter( ) ) ;
            sb.append( this.fields[ i ] ) ;
        }

        return sb.toString( ) ;
    }

    @Override
    public String toString( )
    {
        return toString( this.config ) ;
    }

No comments:

Post a Comment