3

我目前正在评估 smooks (www.smooks.org)。它看起来就像我们需要的一样,但我在让一个简单的例子工作时遇到了问题。

我有一个 ant 脚本,它可以下载我所有的依赖项,包括 EDIFACT 消息的映射和绑定 jar。

我正在尝试使用他们的 EJC 将简单的 EDIFACT APERAK 消息转换为 Java(我正在使用 JavaSE 进行这个小测试)。转换失败,但出现了一些不期望出现的块(见下文)。所以我想知道我是否缺少一些配置(注意日志输出中的前几行)。

有人与 Smooks 的 EJC 合作过吗?我怎样才能获得有关它抱怨的线路的更多信息?

所以这是代码:

    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);
            }
        }
    }

当我运行它时,我得到了这个异常

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

实际的 EDIFACT 消息相当简单:

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'
COM+support@XXXXX.XX: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'

当我删除前导 UNA 和 UNB 片段时,它会出现以下异常:([APERAK][D:00B:UN]。必须至少有 1 个片段 [BGM] 实例)。有一个 BGM 部分,所以我不确定它为什么抱怨。

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)

更新: 当我从消息中删除回车时

UNH+440905601+APERAK:D:00B:UN:IMP10'BGM+313++9+RE'RFF+ACW:XXXXXXXXX1109020'DTM+182:201109021018:203'

它工作正常。但是我如何让 smooks 接受回车和空格以及两个主要的 UNA/UNB 段?我可能跳过了消息处理 smooks 通常做的某些部分。

更新 2: 发现支持 UNA/UNB 段(我的错误),但我仍然遇到回车问题。Renat 建议在 EDIParser 上使用“ignoreNewLines”选项。我已经尝试过了,但似乎没有什么不同。我也试过用这个来配置smooks:

<?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>

再次没有成功。

我觉得 D00AInterchangeFactory (或您使用的每个版本)以不同方式配置其 EDIParser 并且忽略了 ignoreNewLines 。

有没有办法获得 InterchangeFactory 正在使用的 EDIParser?

4

1 回答 1

2

您需要在 EDIParser 上添加启用“忽略新行”开关。您有多种方法可以做到这一点,例如您可以使用 XMLReader#setFeature()

http://download.oracle.com/javase/1.5.0/docs/api/org/xml/sax/XMLReader.html#setFeature(java.lang.String,%20boolean )

或直接通过 EDIParser 方法调用。在此处查看示例

https://gist.github.com/825845

和这里

https://gist.github.com/825843

雷纳特

于 2011-11-03T15:27:37.460 回答