XML to EDIFACT - programmaticaly configure edifact unparser

199 views
Skip to first unread message

Faress Shihada

unread,
Jun 22, 2023, 12:17:23 PM6/22/23
to Smooks Users
Hi, 

I'm trying to configure Smooks to parse XML to EDIFACT. I used the below xml configuration and it worked:
<?xml version="1.0"?>
<smooks-resource-list xmlns="https://www.smooks.org/xsd/smooks-2.0.xsd"
                      xmlns:edifact="https://www.smooks.org/xsd/smooks/edifact-2.0.xsd"
                      xmlns:core="https://www.smooks.org/xsd/smooks/smooks-core-1.6.xsd">

    <core:smooks filterSourceOn="/Interchange">
        <core:action>
            <core:inline>
                <core:replace/>
            </core:inline>
        </core:action>
        <core:config>
            <smooks-resource-list>
                <edifact:unparser schemaURI="/d03b/EDIFACT-Messages.dfdl.xsd" unparseOnNode="*">
                    <edifact:messageTypes>
                        <edifact:messageType>INVOIC</edifact:messageType>
                    </edifact:messageTypes>
                </edifact:unparser>
            </smooks-resource-list>
        </core:config>
    </core:smooks>

</smooks-resource-list>

However, I want do the same configuration programmatically. How is that possible? 
I tried the below code, not sure if I'm in the correct direction:

Smooks smooks = new Smooks();
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><D03B:Interchange xmlns:D03B=\"http://www.ibm.com/dfdl/edi/un/edifact/D03B\" xmlns:srv=\"http://www.ibm.com/dfdl/edi/un/service/4.1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><UNB><S001><E0001>UNOA</E0001><E0002>4</E0002></S001><S002><E0004>LOCK</E0004><E0007>02</E0007></S002><S003><E0010>CBP-ACE-TEST</E0010><E0007>02</E0007></S003><S004><E0017>41013</E0017><E0019>1901</E0019></S004><E0020>16</E0020><S005><E0022></E0022></S005><E0026>ACE</E0026></UNB><FunctionGroup><UNG><E0038>PAXLST</E0038><S006><E0040>LOCK</E0040><E0007>02</E0007></S006><S007><E0044>CBP-ACE-TEST</E0044><E0007>02</E0007></S007><S004><E0017>41013</E0017><E0019>1901</E0019></S004><E0048>16</E0048><E0051>UN</E0051><S008><E0052>D</E0052><E0054>03B</E0054></S008></UNG><D03B:Message><UNH><E0062>PAXLST16</E0062><S009><E0065>PAXLST</E0065><E0052>D</E0052><E0054>03B</E0054><E0051>UN</E0051></S009></UNH><D03B:PAXLST><BGM><C002><E1001>10</E1001></C002><C106><E1004>LOCKKH04103101</E1004></C106><E1225>4</E1225></BGM><RFF><C506><E1153>RFA</E1153><E1154>03</E1154></C506></RFF><SegGrp-2><TDT><E8051>11</E8051><C040><E3127>LOCK</E3127><E1131>172</E1131></C040></TDT><DTM><C507><E2005>132</E2005><E2380>20041230</E2380><E2379>102</E2379></C507></DTM></SegGrp-2><SegGrp-4><NAD><E3035>VW</E3035><C082><E3039>0000041153</E3039><E1131>109</E1131></C082><C080><E3036>AOMAD</E3036><E3036>CHRIS</E3036><E3045>1</E3045></C080><C059><E3042>11107 SUNSET HILLS ROAD</E3042></C059><E3164>RESTON</E3164><C819><E3229>VA</E3229><E1131>163</E1131><E3055>5</E3055></C819><E3251>20190</E3251></NAD><ATT><E9017>2</E9017><C956><E9019>M</E9019></C956><C956><E9019>M</E9019></C956><C956><E9019>M</E9019></C956><C956><E9019>M</E9019></C956><C956><E9019>M</E9019></C956></ATT><DTM><C507><E2005>329</E2005><E2380>19350919</E2380><E2379>102</E2379></C507></DTM><EMP><E9003>4</E9003><C950><E9007>1</E9007></C950><E9035>8</E9035></EMP><NAT><E3493>2</E3493><C042><E3293>US</E3293><E3055>5</E3055></C042></NAT><SegGrp-5><DOC><C002><E1001>5K</E1001></C002><C503><E1004>P100971204141</E1004></C503></DOC><LOC><E3227>91</E3227><C517><E3225>VA</E3225><E1131>163</E1131></C517></LOC><LOC><E3227>91</E3227><C517><E3225>US</E3225><E1131>162</E1131></C517></LOC></SegGrp-5><SegGrp-5><DOC><C002><E1001>39</E1001></C002><C503><E1004>15504141</E1004></C503></DOC><LOC><E3227>91</E3227><C517><E3225>US</E3225><E1131>162</E1131></C517></LOC></SegGrp-5></SegGrp-4></D03B:PAXLST><UNT><E0074>16</E0074><E0062>PAXLST16</E0062></UNT></D03B:Message><UNE><E0060>1</E0060><E0048>16</E0048></UNE></FunctionGroup><UNZ><E0036>1</E0036><E0020>16</E0020></UNZ></D03B:Interchange>";
        DfdlSchema dfdlSchema = new DfdlSchema(new URI("/d03b/EDIFACT-Messages.dfdl.xsd"), new HashMap<String, String>(){{
            this.put("{http://www.ibm.com/dfdl/EDI/Format}SegmentTerm", "'");
            this.put("{http://www.ibm.com/dfdl/EDI/Format}FieldSep", "+");
            this.put("{http://www.ibm.com/dfdl/EDI/Format}CompositeSep", ":");
            this.put("{http://www.ibm.com/dfdl/EDI/Format}EscapeChar", "?");
            this.put("{http://www.ibm.com/dfdl/EDI/Format}RepeatSep", "*");
            this.put("{http://www.ibm.com/dfdl/EDI/Format}DecimalSep", ".");
            this.put("{http://www.ibm.com/dfdl/EDI/Format}GroupingSep", ",");
        }}, ValidationMode.Full, false, false, null);
        try {
            DfdlUnparser dfdlUnparser = new DfdlUnparser(dfdlSchema.compile());
//             this is required because of a bug in DfdlUnparser
            ResourceConfig resourceConfig = new DefaultResourceConfig();
            resourceConfig.setParameter("schemaURI", "");
            smooks.addConfiguration(resourceConfig);
            smooks.getApplicationContext().getRegistry().lookup(new LifecycleManagerLookup())
                    .applyPhase(dfdlUnparser, new PostConstructLifecyclePhase(
                            new Scope(smooks.getApplicationContext().getRegistry(), resourceConfig,
                                    dfdlUnparser)));
/////////////////////////////////////////////////
            smooks.addVisitor(dfdlUnparser, "Interchange");
            smooks.setFilterSettings(FilterSettings.newSaxNgSettings().setDefaultSerializationOn(false));
        } catch (Throwable e) {
            Log.a(this.getClass()).info(e.toString());
        }
        StringResult stringResult = new StringResult();
//        smooks.filterSource(new StreamSource(new ByteArrayInputStream(input.get("stream").toString().getBytes())), stringResult);
        smooks.filterSource(new StreamSource(new ByteArrayInputStream(xml.getBytes())), stringResult);
        output.put("result", stringResult.getResult());


I'm getting the below error:
Failed to filter source class org.apache.daffodil.japi.DaffodilUnparseErrorSAXException Cause: Unparse Error: Expected element start event for D03B:{http://www.ibm.com/dfdl/edi/un/edifact/D03B}RESMSG, but received element start event for (invalid) {http://www.ibm.com/dfdl/edi/un/edifact/D03B}Interchange.
Data location was preceding byte 0

Claude Mamo

unread,
Jun 25, 2023, 10:08:50 AM6/25/23
to smook...@googlegroups.com
You're not quite there Faress. The first thing that stands out to me is the pipeline (i.e., replace) being missing from your Smooks configuration. Smooks offers two ways for configuring it from Java: either through (a) specialised visitor classes (e.g., DfdlUnparser) or (b) generic resource configs (i.e., ResourceConfig). The rule of thumb is to use resource configs when you can because they hide away unnecessary details about visitor construction. Here's how you can achieve what you want with resource configs:

ResourceConfig edifactUnparser = new DefaultResourceConfig("*", new Properties());
edifactUnparser.setResource("");
edifactUnparser.setParameter("schemaURI", "/d03b/EDIFACT-Messages.dfdl.xsd");
edifactUnparser.setResourceType("dfdl");
edifactUnparser.setParameter("dataProcessorFactory", "org.smooks.cartridges.edifact.EdifactDataProcessorFactory");
edifactUnparser.setParameter("messageType", "INVOIC");
edifactUnparser.setParameter("segmentTerminator", "'");
edifactUnparser.setParameter("dataElementSeparator", "+");
edifactUnparser.setParameter("compositeDataElementSeparator", ":");
edifactUnparser.setParameter("escapeCharacter", "?");
edifactUnparser.setParameter("repetitionSeparator", "*");
edifactUnparser.setParameter("decimalSign", ".");
edifactUnparser.setParameter("triadSeparator", ",");

ResourceConfig pipeline = new DefaultResourceConfig("/Interchange", new Properties());
pipeline.setResource("org.smooks.engine.resource.visitor.smooks.NestedSmooksVisitor");
pipeline.setParameter("action", "REPLACE");
pipeline.setParameter("smooksResourceList", "<smooks-resource-list xmlns=\"https://www.smooks.org/xsd/smooks-2.0.xsd\">" + edifactUnparser.toXml() + "</smooks-resource-list>");

Smooks smooks = new Smooks();
smooks.addConfiguration(pipeline);
StringResult stringResult = new StringResult();
smooks.filterSource(new StreamSource(new ByteArrayInputStream(xml.getBytes())), stringResult);

System.out.println(stringResult.getResult());

return stringResult.getResult();


Claude

--
You received this message because you are subscribed to the Google Groups "Smooks Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to smooks-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/smooks-user/f2aeee56-60a9-4b16-9832-e52c5db4d318n%40googlegroups.com.

Faress Shihada

unread,
Jun 26, 2023, 5:02:37 AM6/26/23
to smook...@googlegroups.com
Thanks Claude, now I'm able to produce the edifact data.
However I'm getting the below error in runtime:
Runtime Schema Definition Warning: Cannot set variable ibmEdiFmt:EscapeChar after reading the default value. State was: VariableSet. Existing value: DataValue(?)
Schema context: EscapeCharacter Location line 109 column 14 in jar:file:/C:/Projects/edifactToXML2/lib/edi-schemas-2.0.0-RC1.jar!/EDIFACT-Common/EDIFACT-Service-Segments-4.1.dfdl.xsd
Data location was preceding byte 7


You received this message because you are subscribed to a topic in the Google Groups "Smooks Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/smooks-user/EOMMEp8kCiI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to smooks-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/smooks-user/CADvAAuhjrsuX5UxLvQLraiAFL-1Ldk69GXCwmbapFDPUvsmzQA%40mail.gmail.com.

Claude Mamo

unread,
Jun 26, 2023, 5:23:07 AM6/26/23
to smook...@googlegroups.com
This is only a warning right (i.e., not fatal)? Probably you can ignore it. Could be a minor issue in the way default values are set in the DFDL schema. Please create a bug report if you'd like me to have a look at it.

Claude

Reply all
Reply to author
Forward
0 new messages