Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
Milyn
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<p>This tutorial is an extension of the <a class="confluence-link" href="/display/MILYN/Smooks+Example+-+edi-to-xml" data-linked-resource-id="24248659" data-linked-resource-type="page" data-linked-resource-default-alias="Smooks Example - edi-to-xml" data-base-url="http://docs.codehaus.org">edi-to-xml</a> and <a class="confluence-link" href="/display/MILYN/Smooks+Example+-+xml-to-java" data-linked-resource-id="78868" data-linked-resource-type="page" data-linked-resource-default-alias="Smooks Example - xml-to-java" data-base-url="http://docs.codehaus.org">javabean-populator</a> tutorials. It illustrates how to go from (transform) an EDI message to a populated Java Object Model.</p> <img class="editor-inline-macro" src="/plugins/servlet/confluence/placeholder/macro?definition=e3RvYzppbmRlbnQ9MjBweHxzdHlsZT1kaXNjfQ&locale=en_GB&version=2" data-macro-name="toc" data-macro-parameters="indent=20px|style=disc"> <p> <br /> <strong><a href="http://svn.codehaus.org/milyn/trunk/smooks-examples/edi-to-java"><strong>SVN</strong></a></strong> - <strong><a class="confluence-link unresolved" data-content-title="downloads" data-linked-resource-default-alias="downloads" href="#"><strong>Download</strong></a></strong> - <strong><a class="confluence-link" href="/display/MILYN/Tutorials" data-linked-resource-id="49525" data-linked-resource-type="page" data-linked-resource-default-alias="Tutorials" data-base-url="http://docs.codehaus.org"><strong>Other Tutorials</strong></a></strong></p> <p>Other Relevant Info:</p> <ul> <li><a class="confluence-link" href="/display/MILYN/Smooks+Example+-+edi-to-xml" data-linked-resource-id="24248659" data-linked-resource-type="page" data-linked-resource-default-alias="Smooks Example - edi-to-xml" data-base-url="http://docs.codehaus.org">edi-to-xml</a> tutorial.</li> <li><a class="confluence-link" href="/display/MILYN/Smooks+Example+-+xml-to-java" data-linked-resource-id="78868" data-linked-resource-type="page" data-linked-resource-default-alias="Smooks Example - xml-to-java" data-base-url="http://docs.codehaus.org">javabean-populator</a> tutorial.</li> </ul> <p> <br /> <span style="text-decoration: underline;">To Build</span>: "mvn clean install"<br /> <span style="text-decoration: underline;">To Run</span>: "mvn exec:java"</p> <p><img class="editor-inline-macro" src="/plugins/servlet/confluence/placeholder/macro?definition=e2FuY2hvcjp0cmFuc2Zvcm1lcn0&locale=en_GB&version=2" data-macro-name="anchor" data-macro-default-parameter="transformer"></p> <h1>Transforming from an EDI to Javabean Object Graph</h1> <p>Something to note when looking at this example is that Smooks doesn't perform this EDI-to-Java transform in a 2 step "pipeline" like process. It doesn't generate and intermediate XML, which it then has to parse and process etc. Smooks takes the EDI message and, using the EdiSax parser, goes straight to a DOM representation of the EDI message. This is then used by Smooks, through the Javabean Cartridge, to populate the Java Object Graph.</p> <p>If you haven't already looked at the <a class="confluence-link" href="/display/MILYN/Smooks+Example+-+xml-to-java" data-linked-resource-id="78868" data-linked-resource-type="page" data-linked-resource-default-alias="Smooks Example - xml-to-java" data-base-url="http://docs.codehaus.org">javabean-populator</a> tutorial, you may be interested in looking at that before looking at this example.</p> <p>So here's the source edi file that is to be transformed:</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> HDR*1*0*59.97*64.92*4.95*Wed Nov 15 13:45:28 EST 2006 CUS*user1*Harry^Fletcher*SD ORD*1*1*364*The 40-Year-Old Virgin*29.98 ORD*2*1*299*Pulp Fiction*29.99 </pre></td></tr></table> <p>And this is the expected result of our transformation (i.e. a simple System.out of the populated <a href="http://svn.codehaus.org/milyn/trunk/smooks-examples/edi-to-java/src/main/java/example/model/">Object Model</a> - see the <a href="http://svn.codehaus.org/milyn/trunk/smooks-examples/edi-to-java/src/main/java/example/model/Order.java">toString method in the Order class</a>):</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> ==============EDI as Java Object Graph============= Order Header: Customer: Fletcher, Harry Date: Wed Nov 15 19:45:28 CET 2006 Details: ID=1, Status=0, Total=64.92 Order Items: (0): ProductID=364, Quantity=1, Title='The 40-Year-Old Virgin', Price=29.98 (1): ProductID=299, Quantity=1, Title='Pulp Fiction', Price=29.99 ====================================== </pre></td></tr></table> <p>This transformation could be taken a step further, whereby the EDI populated Object Model is used as part of a Model Driven Transformation. See the <a class="confluence-link" href="/display/MILYN/Smooks+Example+-+model-driven-basic" data-linked-resource-id="9240869" data-linked-resource-type="page" data-linked-resource-default-alias="Smooks Example - model-driven-basic" data-base-url="http://docs.codehaus.org">model-driven-basic</a> tutorial.</p> <p><img class="editor-inline-macro" src="/plugins/servlet/confluence/placeholder/macro?definition=e2FuY2hvcjpjb25maWd1cmF0aW9ufQ&locale=en_GB&version=2" data-macro-name="anchor" data-macro-default-parameter="configuration"></p> <h1>The Smooks Configuration</h1> <p>The Smooks configuration for this transformation contains:</p> <ol> <li>A configuration for generating an XML model (SAX event stream) from the input EDI, as with the <a class="confluence-link" href="/display/MILYN/Smooks+Example+-+edi-to-xml" data-linked-resource-id="24248659" data-linked-resource-type="page" data-linked-resource-default-alias="Smooks Example - edi-to-xml" data-base-url="http://docs.codehaus.org">edi-to-xml</a> tutorial.</li> <li>A set of Javabean population configuration for populating the <a href="http://svn.codehaus.org/milyn/trunk/smooks-examples/edi-to-java/src/main/java/example/model/">Object Model</a>, as with the <a class="confluence-link" href="/display/MILYN/Smooks+Example+-+xml-to-java" data-linked-resource-id="78868" data-linked-resource-type="page" data-linked-resource-default-alias="Smooks Example - xml-to-java" data-base-url="http://docs.codehaus.org">javabean-populator</a> tutorial.</li> </ol> <p><br class="atl-forced-newline" /> Here's the configuration ("smooks-config.xml"):</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> <?xml version="1.0"?> <smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd" xmlns:jb="http://www.milyn.org/xsd/smooks/javabean-1.1.xsd"> <!-- Configure the EDI Reader to parse the message stream into a stream of SAX events. --> <reader class="org.milyn.smooks.edi.SmooksEDIReader"> <params> <!-- Generate the event stream based on this "mapping model". --> <param name="mapping-model">/example/edi-to-xml-order-mapping.xml</param> </params> </reader> <!-- Create an "example.beans.Order" bean instance when we visit the start of the <order> element. Assign the new bean instance to the beanId of "order". The "header" and "orderItemList" beans are wired in. --> <jb:bindings beanId="order" class="example.model.Order" createOnElement="order"> <jb:wiring property="header" beanIdRef="header" /> <jb:wiring property="orderItems" beanIdRef="orderItemList" /> </jb:bindings> <!-- Create an "example.beans.Header" bean instance when we visit the start of the <order> element. Note, we're creating this bean instance before we encounter the actual <header> element. This bean is wired into the "order" bean. --> <jb:bindings beanId="header" class="example.model.Header" createOnElement="order"> <jb:wiring property="customer" beanIdRef="customer" /> <jb:value property="orderId" data="header/order-id" /> <jb:value property="orderStatus" decoder="Long" data="header/status-code" /> <jb:value property="netAmount" decoder="BigDecimal" data="header/net-amount" /> <jb:value property="totalAmount" decoder="BigDecimal" data="header/total-amount" /> <jb:value property="tax" decoder="BigDecimal" data="header/tax" /> <jb:value property="date" decoder="Date" data="header/date"> <jb:decodeParam name="format">EEE MMM dd HH:mm:ss z yyyy</jb:decodeParam> </jb:value> </jb:bindings> <!-- Create an "example.beans.Customer" bean instance when we visit the start of the <customer-details> element. This bean is wired into the "header" bean. --> <jb:bindings beanId="customer" class="example.model.Customer" createOnElement="customer-details"> <!-- Customer bindings... --> <jb:value property="userName" data="customer-details/username" /> <jb:value property="firstName" data="customer-details/name/firstname" /> <jb:value property="lastName" data="customer-details/name/lastname" /> <jb:value property="state" data="customer-details/state" /> </jb:bindings> <!-- Create an ArrayList when we visit the start of the <order> element. The "orderItem" beans are wired into this list and this list is wired into the "order" bean. --> <jb:bindings beanId="orderItemList" class="java.util.ArrayList" createOnElement="order"> <jb:wiring beanIdRef="orderItem" /> </jb:bindings> <!-- Create an "example.beans.OrderItem" bean instance when we visit the start of the <order-item> element. This bean is wired into the "orderItemList" ArrayList. --> <jb:bindings beanId="orderItem" class="example.model.OrderItem" createOnElement="order-item"> <!-- OrderItem bindings... --> <jb:value property="quantity" decoder="Integer" data="order-item/quantity" /> <jb:value property="productId" decoder="String" data="order-item/product-id" /> <jb:value property="price" decoder="BigDecimal" data="order-item/price" /> <jb:value property="title" data="order-item/title" /> </jb:bindings> </smooks-resource-list> </pre></td></tr></table> <p>Here's the edi mapping ("/src/main/java/example/edi-to-xml-order-mapping.xml"). See the <a class="confluence-link" href="/display/MILYN/Smooks+Example+-+edi-to-xml" data-linked-resource-id="24248659" data-linked-resource-type="page" data-linked-resource-default-alias="Smooks Example - edi-to-xml" data-base-url="http://docs.codehaus.org">edi-to-xml</a> tutorial for more details on this mapping: <br class="atl-forced-newline" /></p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> <?xml version="1.0" encoding="UTF-8"?> <medi:edimap xmlns:medi="http://www.milyn.org/schema/edi-message-mapping-1.0.xsd"> <medi:description name="DVD Order" version="1.0" /> <medi:delimiters segment="&#10;" field="*" component="^" sub-component="~" /> <medi:segments xmltag="Order"> <medi:segment segcode="HDR" xmltag="header"> <medi:field xmltag="order-id" /> <medi:field xmltag="status-code" /> <medi:field xmltag="net-amount" /> <medi:field xmltag="total-amount" /> <medi:field xmltag="tax" /> <medi:field xmltag="date" /> </medi:segment> <medi:segment segcode="CUS" xmltag="customer-details"> <medi:field xmltag="username" /> <medi:field xmltag="name"> <medi:component xmltag="firstname" /> <medi:component xmltag="lastname" /> </medi:field> <medi:field xmltag="state" /> </medi:segment> <medi:segment segcode="ORD" xmltag="order-item" maxOccurs="-1"> <medi:field xmltag="position" /> <medi:field xmltag="quantity" /> <medi:field xmltag="product-id" /> <medi:field xmltag="title" /> <medi:field xmltag="price" /> </medi:segment> </medi:segments> </medi:edimap> </pre></td></tr></table> <p><img class="editor-inline-macro" src="/plugins/servlet/confluence/placeholder/macro?definition=e2FuY2hvcjpleGVjdXRpb259&locale=en_GB&version=2" data-macro-name="anchor" data-macro-default-parameter="execution"></p> <h1>Executing The Transformation</h1> <p>Again, it's exactly the same as with the <a class="confluence-link" href="/display/MILYN/Smooks+Example+-+java-basic" data-linked-resource-id="78566" data-linked-resource-type="page" data-linked-resource-default-alias="Smooks Example - java-basic" data-base-url="http://docs.codehaus.org">java-basic</a> tutorial:</p> <table class="wysiwyg-macro" data-macro-name="noformat" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e25vZm9ybWF0fQ&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre> // Instantiate Smooks with the config... Smooks smooks = new Smooks("smooks-config.xml"); JavaResult result = new JavaResult(); // Filter the input message to the JavaResult... smooks.filter(new StreamSource(messageIn), result); return (Order) result.getBean("order"); </pre></td></tr></table> <p>Of course, you'd typically cache the <a class="confluence-link" href="http://milyn.codehaus.org/javadoc/smooks/org/milyn/Smooks.html" shortcut-key="milyn_public_html" shortcut-parameter="javadoc/smooks/org/milyn/Smooks.html" data-linked-resource-default-alias="unnamed link" data-base-url="http://docs.codehaus.org">Smooks</a> instance.</p> <p>See the <a href="http://svn.codehaus.org/milyn/trunk/smooks-examples/edi-to-java/src/main/java/example/Main.java">example/Main.java</a> in the example source.</p>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced