Extend EDIParser 1.2.1
garuda Dec 15, 2009 9:19 AMHello experts,
this might be interessenting for those of you who want to allow a more tolerant processing of EDIFACT data from supplier . This extension is based on EDIParser 1.2.1. It`s free for any discussion.
Lets assume
<medi:delimiters segment="'" field="+" component=":" sub-component="~" />
Problem
(1) A field X is defined as non - mandatory
(2) Field X contains a component, that is mandatory
Point (1) means that you can leave the field completely. Still many EDIFACT supplier don`t leave the field, but set it empty
like "++". If use - case of point (2) occurs then an exception will be thrown.
Example
TEST++VAL1:VAL2'
with snipped from EDI mapping model (its not EDIFACT, but only a simple example):
...
<medi:segment segcode="TEST" xmltag="TEST" minOccurs="0" maxOccurs="1" truncatable="true">
<medi:field xmltag="FIELD" truncatable="true">
<medi:component xmltag="AAAA" required="true"/>
</medi:field>
<medi:field ...>
...
</medi:field>
</medi:segment>
...
Solution
Patch org.milyn.edisax.EDIParser.java (EDIParser 1.2.1). Only map a required field, if its field compontens contain any value
/**
* Map an individual segment field.
* @param fieldMessageVal The field message value.
* @param expectedField The mapping config to which the field value is expected to map.
* @param fieldIndex The field index within its segment (base 0).
* @param segmentCode The segment code within which the field exists.
* @throws SAXException EDI processing exception.
*/
private void mapField(String fieldMessageVal, Field expectedField, int fieldIndex, String segmentCode) throws SAXException {
List<Component> expectedComponents = expectedField.getComponent();
startElement(expectedField.getXmltag(), true);
// If there are components defined on this field...
if(expectedComponents.size() != 0) {
String[] currentFieldComponents = EDIUtils.split(fieldMessageVal, edifactModel.getDelimiters().getComponent(), edifactModel.getDelimiters().getEscape());
if(expectedField.isRequired() || (currentFieldComponents.length > 0)) {
assertComponentsOK(expectedField, fieldIndex, segmentCode, expectedComponents, currentFieldComponents);
// Iterate over the field components and map them...
for(int i = 0; i < currentFieldComponents.length; i++) {
String componentMessageVal = currentFieldComponents[i];
Component expectedComponent = expectedComponents.get(i);
mapComponent(componentMessageVal, expectedComponent, fieldIndex, i, segmentCode, expectedField.getXmltag());
}
}
endElement(expectedField.getXmltag(), true);
} else {
if(expectedField.isRequired() && fieldMessageVal.length() == 0) {
throw new EDIParseException(edifactModel.getEdimap(), "Segment [" + segmentCode + "], field " + (fieldIndex + 1) + " (" + expectedField.getXmltag() + ") expected to contain a value. Currently at segment number " + segmentReader.getCurrentSegmentNumber() + ".");
}
contentHandler.characters(fieldMessageVal.toCharArray(), 0, fieldMessageVal.length());
endElement(expectedField.getXmltag(), false);
}
}
Best regards,
Dennis