edi-x12-as2/smooks/ingest-x12-config.xml (50 lines of code) (raw):
<?xml version="1.0"?>
<smooks-resource-list
xmlns="https://www.smooks.org/xsd/smooks-2.0.xsd"
xmlns:jb="https://www.smooks.org/xsd/smooks/javabean-1.6.xsd"
xmlns:edi="https://www.smooks.org/xsd/smooks/edi-2.0.xsd"
xmlns:camel="https://www.smooks.org/xsd/smooks/camel-1.5.xsd"
xmlns:ftl="https://www.smooks.org/xsd/smooks/freemarker-2.1.xsd"
xmlns:core="https://www.smooks.org/xsd/smooks/smooks-core-1.6.xsd">
<!-- Configures Smooks to tolerate run-time exceptions which allows the EDI to be tracked even when
errors occur -->
<core:filterSettings terminateOnException="false"/>
<!-- Emits an event stream from the EDI input. The 'segmentTerminator' and 'dataElementSeparator' attributes
configure the expected EDI delimiters ('%WSP*;' means zero or more whitespaces while '%NL;' means a newline). The default
schema driving the parsing behaviour is a generic EDI schema written in DFDL but can be overridden with the 'schemaUri'
config attribute -->
<edi:parser segmentTerminator="~%WSP*; %NL;%WSP*;" dataElementSeparator="*"/>
<!-- Runs a pipeline (essentially a nested Smooks execution) on each 'segment' event in order to rewrite the
segments events, making it easier to target the segments we are interested in. The child 'dataElement' events
for the segment being processed are kept in-memory since the 'maxNodeDepth' attribute is set to 0 (i.e., max
possible depth) -->
<core:smooks filterSourceOn="segment" maxNodeDepth="0">
<core:config>
<smooks-resource-list>
<!-- Rewrites the pipeline root event (i.e., the first event which is 'segment') with a custom FreeMarker template such
that it has an attribute called 'segmentId' holding the segment ID. For example:
<segment>...</segment>
becomes
<segment segmentId="ISA">...</segment>
Side note: the EDI parser's underlying DFDL processor doesn't support attributes but the
'core:rewrite' construct allows us to add attributes which permits us to target segments based on
the segment ID rather than on the segment position in the stream -->
<core:rewrite>
<ftl:freemarker applyOnElement="#document">
<ftl:template baseDir="../ftl">segment-id-attr.xml.ftl</ftl:template>
</ftl:freemarker>
</core:rewrite>
<!-- Materialises the FreeMarker template when it encounters the pipeline root event. The template can be
viewed at: https://github.com/cjmamo/camel-jbang-examples/blob/edi-x12-as2/edi-x12-as2/ftl/segment-id-attr.xml.ftl -->
<jb:bean beanId="isa" class="java.util.HashMap" createOnElement="segment[@segmentId = 'ISA']" retain="true">
<jb:value property="interchangeSenderIdQualifier" data="#/dataElement[5]"/>
<jb:value property="interchangeSenderId" data="#/dataElement[6]"/>
<jb:value property="interchangeReceiverIdQualifier" data="#/dataElement[7]"/>
<jb:value property="interchangeReceiverId" data="#/dataElement[8]"/>
<jb:value property="interchangeControlNumber" data="#/dataElement[13]"/>
</jb:bean>
<!-- Binds the segment event having the segmentId attribute 'GS' to a HashMap named 'gs' -->
<jb:bean beanId="gs" class="java.util.HashMap" createOnElement="segment[@segmentId = 'GS']" retain="true">
<jb:value property="functionalIdCode" data="#/dataElement[1]" />
<jb:value property="applicationSenderCode" data="#/dataElement[2]" />
<jb:value property="applicationReceiverCode" data="#/dataElement[3]" />
<jb:value property="groupControlNumber" data="#/dataElement[6]" />
</jb:bean>
<!-- Sends the bean 'gs' to the 'direct:tpm' Camel endpoint when the segment attribute is 'GS' -->
<camel:route beanId="gs" routeOnElement="segment[@segmentId = 'GS']">
<camel:to endpoint="direct:tpm"/>
</camel:route>
<!-- Binds the segment event having the segmentId attribute 'PO1' to a HashMap named
'purchaseOrder' -->
<jb:bean beanId="purchaseOrder" class="java.util.HashMap" createOnElement="segment[@segmentId = 'PO1']" retain="true">
<jb:value property="quantityOrdered" data="#/dataElement[2]" />
<jb:value property="totalAmount" data="segment[@segmentId = 'AMT']/dataElement[2]" />
<jb:expression property="status" initVal="'open'"/>
<jb:expression property="priority" initVal="'standard'"/>
</jb:bean>
<!-- Sends the bean 'purchaseOrder' to the 'direct:erp' Camel endpoint when the segment attribute is
equal to 'AMT' -->
<camel:route beanId="purchaseOrder" routeOnElement="segment[@segmentId = 'AMT']">
<camel:to endpoint="direct:erp"/>
</camel:route>
<!-- Binds the segment event having the segmentId attribute 'ST' to a HashMap named 'st' -->
<jb:bean beanId="st" class="java.util.HashMap" createOnElement="segment[@segmentId = 'ST']" retain="true">
<jb:value property="transactionSetIdentifier" data="#/dataElement[1]" />
</jb:bean>
</smooks-resource-list>
</core:config>
</core:smooks>
</smooks-resource-list>