I am currently evaluating smooks (www.smooks.org). It looks just like what we need but I am having problems getting a simple example to work.
I've got an ant script which downloads me all the dependencies including the mapping and binding jars for EDIFACT messages.
I am trying to convert a simple EDIFACT APERAK message to Java using their EJC (I am using JavaSE for this little test). The conversion fails with an exception that some block wasn't expected (see below). So I am wondering whether I am missing some configuration (notice the first few lines in the log output).
Has anyone worked with Smooks' EJC? How can I get more info about what line it is complaining?
So this is the code:
D00BInterchangeFactory factory = D00BInterchangeFactory.getInstance();
File file = new File("aperak.edi");
BufferedInputStream ediSource = new BufferedInputStream(new FileInputStream(file));
StreamResult xmlStream = new StreamResult();
StringWriter xmlWriter = new StringWriter();
xmlStream.setWriter(xmlWriter);
UNEdifactInterchange interchange = factory.fromUNEdifact(ediSource);
//System.err.println("MEssage "+xmlWriter.toString());
if(interchange instanceof UNEdifactInterchange41){
UNEdifactInterchange41 interchange41 = (UNEdifactInterchange41)interchange;
for(UNEdifactMessage41 message: interchange41.getMessages()){
Object messageObj = message.getMessage();
System.err.println("Ref Num "+message.getMessageHeader().getMessageRefNum());
if(messageObj instanceof Aperak){
Aperak aperak = (Aperak)message.getMessage();
System.err.println("Aperak "+aperak);
}
}
}
When I run it I get this exception
02-Nov-2011 15:58:09 org.milyn.delivery.ContentDeliveryConfigBuilder$ContentHandlerExtractionStrategy addCDU
WARNING: ContentHandlerFactory [org.milyn.delivery.JavaContentHandlerFactory] unable to create resource processing instance for resource [Target Profile: [[*]], Selector: [cdu-creator], Selector Namespace URI: [null], Resource: [org.milyn.smooks.scripting.groovy.GroovyContentHandlerFactory], Num Params: [1]]. org/codehaus/groovy/control/CompilationFailedException
02-Nov-2011 15:58:10 org.milyn.delivery.ContentDeliveryConfigBuilder$ContentHandlerExtractionStrategy addCDU
WARNING: ContentHandlerFactory [org.milyn.delivery.JavaContentHandlerFactory] unable to create resource processing instance for resource [Target Profile: [[*]], Selector: [cdu-creator], Selector Namespace URI: [null], Resource: [org.milyn.smooks.scripting.groovy.GroovyContentHandlerFactory], Num Params: [1]]. org/codehaus/groovy/control/CompilationFailedException
Exception in thread "main" org.milyn.SmooksException: Failed to filter source.
at org.milyn.delivery.sax.SmooksSAXFilter.doFilter(SmooksSAXFilter.java:86)
at org.milyn.delivery.sax.SmooksSAXFilter.doFilter(SmooksSAXFilter.java:61)
at org.milyn.Smooks._filter(Smooks.java:516)
at org.milyn.Smooks.filterSource(Smooks.java:475)
at org.milyn.Smooks.filterSource(Smooks.java:449)
at org.milyn.edi.unedifact.d00b.D00BInterchangeFactory.fromUNEdifact(D00BInterchangeFactory.java:58)
at org.milyn.edi.unedifact.d00b.D00BInterchangeFactory.fromUNEdifact(D00BInterchangeFactory.java:40)
at EDITestReader.readFile(EDITestReader.java:37)
at EDITestReader.main(EDITestReader.java:59)
Caused by: org.xml.sax.SAXException: Unknown/Unexpected UN/EDIFACT control block segment code '
UN'.
at org.milyn.edisax.unedifact.handlers.r41.UNEdifact41ControlBlockHandlerFactory.getControlBlockHandler(UNEdifact41ControlBlockHandlerFactory.java:53)
at org.milyn.edisax.unedifact.UNEdifactInterchangeParser.parse(UNEdifactInterchangeParser.java:95)
at org.milyn.smooks.edi.unedifact.UNEdifactReader.parse(UNEdifactReader.java:77)
at org.milyn.delivery.sax.SAXParser.parse(SAXParser.java:70)
at org.milyn.delivery.sax.SmooksSAXFilter.doFilter(SmooksSAXFilter.java:75)
... 8 more
enter code here
The actual EDIFACT message is fairly simple:
UNA:+.? '
UNB+UNOC:3+IMP+XXX+20110902:1024+44090560'
UNH+440905601+APERAK:D:00B:UN:IMP10'
BGM+313++9+RE'
RFF+ACW:XXXXXXXXX1109020'
DTM+182:201109021018:203'
RFF+BM:XXXXXXXXX'
RFF+AGO:XXXXXXX1109020'
RFF+EQ:XXXXXXXX'
NAD+MS+IMP'
CTA+MS+:EDI'
COM+XXXXXXXXXXX:TE'
[email protected]:EM'
ERC+200:IMP02:DAK'
FTX+AAO+++ERR4045?: Gest.datum ist mehr als 90 Tage kleiner als das Tagesdatum+DE'
UNT+14+440905601'
UNZ+1+44090560'
When I remove the leading UNA and UNB segment it comes up with this exception: ([APERAK][D:00B:UN]. Must be a minimum of 1 instances of segment [BGM]). There is a BGM segment so I am not sure why it is complaining.
Caused by: org.milyn.edisax.EDIParseException: EDI message processing failed [APERAK][D:00B:UN]. Must be a minimum of 1 instances of segment [BGM]. Currently at segment number 2.
at org.milyn.edisax.EDIParser.mapSegments(EDIParser.java:460)
at org.milyn.edisax.EDIParser.mapSegments(EDIParser.java:411)
at org.milyn.edisax.EDIParser.parse(EDIParser.java:387)
at org.milyn.edisax.EDIParser.parse(EDIParser.java:371)
at org.milyn.edisax.unedifact.handlers.r41.UNHHandler.process(UNHHandler.java:80)
at org.milyn.edisax.unedifact.UNEdifactInterchangeParser.parse(UNEdifactInterchangeParser.java:98)
at org.milyn.smooks.edi.unedifact.UNEdifactReader.parse(UNEdifactReader.java:77)
at org.milyn.delivery.sax.SAXParser.parse(SAXParser.java:70)
UPDATE: When I remove the carriage returns from the message
UNH+440905601+APERAK:D:00B:UN:IMP10'BGM+313++9+RE'RFF+ACW:XXXXXXXXX1109020'DTM+182:201109021018:203'
it works fine. But how do I get smooks to accept carriage returns and whitespaces and the two leading UNA/UNB segments? I probably skipped some part of message processing smooks normally does.
UPDATE 2: Figured out UNA/UNB segments are supported (my mistake) but I am still having problems with the carriage returns. Renat suggested to use a 'ignoreNewLines' option on the EDIParser. I've tried that but it doesn't seem to make a difference. I've also tried to configure smooks with this:
<?xml version="1.0" encoding="UTF-8"?>
<smooks-resource-list
xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
xmlns:edi="http://www.milyn.org/xsd/smooks/edi-1.4.xsd">
<edi:reader mappingModel="/org/milyn/smooks/edi/xsd14/edi-to-xml-mapping.xml" ignoreNewLines="true" />
</smooks-resource-list>
Again with no success.
I have the feeling that the D00AInterchangeFactory (or what every version you use) configures its EDIParser differently and ignoreNewLines is ignored.
Is there a way to get the EDIParser the InterchangeFactory is using?
You need to add enable 'ignore new line' switch on the EDIParser. You have multiple ways to do that, for example you can use a XMLReader#setFeature()
http://download.oracle.com/javase/1.5.0/docs/api/org/xml/sax/XMLReader.html#setFeature(java.lang.String,%20boolean)
or directly via EDIParser method call. See samples here
https://gist.github.com/825845
and here
https://gist.github.com/825843
Renat