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>The primary goals of this example are to introduce you to the following:</p> <ol> <li><a class="confluence-link" href="#transformer" data-anchor="transformer" data-linked-resource-default-alias="transformer" data-base-url="http://docs.codehaus.org">A very basic <strong>Fragment</strong> Transformer written in Java</a>.</li> <li><a class="confluence-link" href="#configuration" data-anchor="configuration" data-linked-resource-default-alias="configuration" data-base-url="http://docs.codehaus.org">The Smooks configuration file</a>.</li> <li><a class="confluence-link" href="#execution" data-anchor="execution" data-linked-resource-default-alias="execution" data-base-url="http://docs.codehaus.org">Executing the Smooks Transformation</a>.</li> </ol> <p> <br /> <strong><a href="http://svn.codehaus.org/milyn/trunk/smooks-examples/java-basic">SVN</a></strong> - <strong><a class="confluence-link unresolved" data-content-title="downloads" data-linked-resource-default-alias="downloads" href="#">Download</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">Other Tutorials</a></strong></p> <p><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>The Fragment Transformer</h1> <p>Smooks is a <strong>Fragment</strong> based Transformation Framework. That means you can write (or reuse) Transformers that apply transformations on message fragments, as opposed to the message as a whole.</p> <p>In this example we build a very simple (silly) fragment transformer in raw Java. Smooks supports applying transformations through a number of technologies (not just Java). However, underpinning all of these is an <a class="confluence-link" href="/display/MILYN/Visitor" data-linked-resource-id="78568" data-linked-resource-type="page" data-linked-resource-default-alias="Visitor" data-base-url="http://docs.codehaus.org">Element Visitor</a> implementation of one sort or another. All transformation technologies supported by Smooks are hooked into the transformation process through an implementation of an <a class="confluence-link" href="/display/MILYN/Visitor" data-linked-resource-id="78568" data-linked-resource-type="page" data-linked-resource-default-alias="Visitor" data-base-url="http://docs.codehaus.org">Element Visitor</a>.</p> <p>In this example, we implement a very simple <a class="confluence-link" href="http://milyn.codehaus.org/javadoc/smooks/org/milyn/delivery/dom/DOMElementVisitor.html" shortcut-key="milyn_public_html" shortcut-parameter="javadoc/smooks/org/milyn/delivery/dom/DOMElementVisitor.html" data-linked-resource-default-alias="unnamed link" data-base-url="http://docs.codehaus.org">DOMElementVisitor</a> that simply renames the DOM Element of the fragment at which it is "targeted".</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> //example/BasicJavaTransformer.java public class BasicJavaTransformer implements DOMElementVisitor { private String newElementName; public void setConfiguration(SmooksResourceConfiguration resourceConfig) throws SmooksConfigurationException { newElementName = resourceConfig.getStringParameter("newName"); } public void visitBefore(Element element, ExecutionContext executionContext) { // Not doing anything on this visit - wait until after visiting // the elements child content... } public void visitAfter(Element element, ExecutionContext executionContext) { // Just rename the target element - keeping child elements - not keeping attributes. DomUtils.renameElement(element, newElementName, true, false); } } </pre></td></tr></table> <p>As you can see from the code, the implementation gets the new name for the targeted fragment via the configuration (in the <strong>setConfiguration</strong> method). It doesn't apply any transformation on the <strong>visitBefore</strong> event. On the <strong>visitAfter</strong> event, it applies the rename transformation on the targeted Element (message fragment) (See <a class="confluence-link" href="http://milyn.codehaus.org/javadoc/smooks/org/milyn/xml/DomUtils.html#renameElement(org.w3c.dom.Element,%20java.lang.String,%20boolean,%20boolean)" shortcut-key="milyn_public_html" shortcut-parameter="javadoc/smooks/org/milyn/xml/DomUtils.html#renameElement(org.w3c.dom.Element,%20java.lang.String,%20boolean,%20boolean)" data-linked-resource-default-alias="unnamed link" data-base-url="http://docs.codehaus.org">DomUtils.html.renameElement</a>).</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>In order to apply this transformer to a message fragment, a <a class="confluence-link" href="http://milyn.codehaus.org/javadoc/smooks/org/milyn/cdr/SmooksResourceConfiguration.html" shortcut-key="milyn_public_html" shortcut-parameter="javadoc/smooks/org/milyn/cdr/SmooksResourceConfiguration.html" data-linked-resource-default-alias="unnamed link" data-base-url="http://docs.codehaus.org">Smooks Configuration</a> needs to be created. This configuration will target the transformer at a particular message fragment.</p> <p>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.0.xsd"> <resource-config selector="c b"> <resource>example.BasicJavaTransformer</resource> <param name="newName">xxx</param> </resource-config> </smooks-resource-list> </pre></td></tr></table> <p>The <strong>resource-config</strong> is what's most important here. It tells Smooks to apply the "example.BasicJavaTransformer" <strong>resource</strong> on all <b> elements that are enclosed by a parent <c> element. This is what's called a "Contextual Selector". Contextual Selectors follow the same basic rules as their <a href="http://www.w3.org/TR/CSS1#contextual-selectors">namesake from the CSS Spec</a>.</p> <p>So, taking the sample message supplied with this example:</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> <a> <b> <c> <b></b> </c> </b> </a> </pre></td></tr></table> <p>The instance of the "example.BasicJavaTransformer" <strong>resource</strong> created for the above configuration will only be applied to the inner most <b> element, producing the following output:</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> <a> <b> <c> <xxx></xxx> </c> </b> </a> </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>Executing the transform is a piece of cake. To do this we use 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> class as follows:</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); // Filter the input message to the outputWriter... smooks.filter(new StreamSource(messageInStream), new StreamResult(messageOutStream)); </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/java-basic/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