Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 22 Next »

The primary goals of this example are to introduce you to the following:

  1. A very basic Fragment Transformer written in Java.
  2. The Smooks configuration file.
  3. Executing the Smooks Transformation.

SVN - Download - Other Tutorials

The Fragment Transformer

Smooks is a Fragment 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.

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 Element Visitor implementation of one sort or another. All transformation technologies supported by Smooks are hooked into the transformation process through an implementation of an Element Visitor.

In this example, we implement a very simple DOMElementVisitor that simply renames the DOM Element of the fragment at which it is "targeted".

public class BasicJavaTransformer implements DOMElementVisitor {

    private String newElementName;

    public void setConfiguration(SmooksResourceConfiguration resourceConfig) 
                                 throws SmooksConfigurationException {
        newElementName = resourceConfig.getStringParameter("newName", "xxx");
    }
    
    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);
    }
}

As you can see from the code, the implementation gets the new name for the targeted fragment via the configuration (in the setConfiguration method). It doesn't apply any transformation on the visitBefore event. On the visitAfter event, it applies the rename transformation on the targeted Element (message fragment) (See DomUtils.html.renameElement).

The Smooks Configuration

In order to apply this transformer to a message fragment, a Smooks Configuration needs to be created. This configuration will target the transformer at a particular message fragment.

Here's the configuration:

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

The resource-config is what's most important here. It tells Smooks to apply the "example.BasicJavaTransformer" resource 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 namesake from the CSS Spec.

So, taking the sample message supplied with this example:

<a>
    <b>
        <c>
            <b></b>
        </c>
    </b>
</a>

The instance of the "example.BasicJavaTransformer" resource created for the above configuration will only be applied to the inner most <b> element, producing the following output:

<a>
    <b>
        <c>
            <xxx></xxx>
        </c>
    </b>
</a>

Executing The Transformation

Executing the transform is a piece of cake. To do this we use the Smooks class as follows:

// Instantiate Smooks with the config...
Smooks smooks = new Smooks(new FileInputStream("smooks-config.xml"));
// Create an exec context - no profiles....
StandaloneExecutionContext executionContext = smooks.createExecutionContext();

// Filter the input message to the outputWriter, using the execution context...
smooks.filter(new StreamSource(messageInStream), new StreamResult(messageOutStream), executionContext);

Of course, you'd typically cache the Smooks instance.

See the example/Main.java in the example source.

  • No labels