[jboss-svn-commits] JBL Code SVN: r26868 - in labs/jbossesb/trunk/product: rosetta/src/org/jboss/soa/esb/actions and 5 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Jun 8 04:53:21 EDT 2009


Author: beve
Date: 2009-06-08 04:53:21 -0400 (Mon, 08 Jun 2009)
New Revision: 26868

Added:
   labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/transformation/
   labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/transformation/xslt/
   labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/transformation/xslt/ResultFactory.java
   labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/transformation/xslt/SourceFactory.java
   labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/transformation/xslt/SourceResult.java
   labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/transformation/xslt/TransformerFactoryConfig.java
   labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/transformation/xslt/XsltAction.java
   labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/transformation/
   labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/transformation/xslt/
   labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/transformation/xslt/ResultFactoryUnitTest.java
   labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/transformation/xslt/SourceFactoryUnitTest.java
   labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/transformation/xslt/XsltActionUnitTest.java
   labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/transformation/xslt/example1.xml
   labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/transformation/xslt/test1.xsl
Modified:
   labs/jbossesb/trunk/product/docs/ProgrammersGuide.odt
Log:
Work for https://jira.jboss.org/jira/browse/JBESB-2584 "Add XSL transformation action"



Modified: labs/jbossesb/trunk/product/docs/ProgrammersGuide.odt
===================================================================
(Binary files differ)

Added: labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/transformation/xslt/ResultFactory.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/transformation/xslt/ResultFactory.java	                        (rev 0)
+++ labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/transformation/xslt/ResultFactory.java	2009-06-08 08:53:21 UTC (rev 26868)
@@ -0,0 +1,92 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware
+ * LLC, and individual contributors by the @authors tag. See the copyright.txt
+ * in the distribution for a full listing of individual contributors.
+ * 
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ * 
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this software; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
+ * site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.actions.transformation.xslt;
+
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStream;
+import java.io.StringWriter;
+
+import javax.xml.transform.Result;
+import javax.xml.transform.dom.DOMResult;
+import javax.xml.transform.sax.SAXResult;
+import javax.xml.transform.stream.StreamResult;
+
+/**
+ * Factory for {@link Result}s.
+ * 
+ * @author <a href="mailto:dbevenius at jboss.com">Daniel Bevenius</a>
+ */
+public final class ResultFactory
+{
+    public enum ResultType { STRING, BYTES, DOM, SAX, SOURCERESULT }
+    
+    private static ResultFactory factory = new ResultFactory();
+    
+    private ResultFactory() {} 
+    
+    public static ResultFactory getInstance()
+    {
+        return factory;
+    }
+    
+    public Result createResult(final ResultType type)
+    {
+        Result result = null;
+        switch ( type )
+        {
+            case STRING:
+                result = new StreamResult(new StringWriter());
+                break;
+            case BYTES:
+                result = new StreamResult(new ByteArrayOutputStream());
+                break;
+            case DOM:
+                result = new DOMResult();
+                break;
+            case SAX:
+                result = new SAXResult();
+                break;
+            default:
+                break;
+        }
+        return result;
+    }
+    
+    public Object extractResult(final Result result, final ResultType type)
+    {
+        switch ( type )
+        {
+            case STRING:
+                return ((StreamResult)result).getWriter().toString();
+            case BYTES:
+                OutputStream outputStream = ((StreamResult)result).getOutputStream();
+                return ((ByteArrayOutputStream)outputStream).toByteArray();
+            case DOM:
+                return ((DOMResult)result).getNode();
+            case SAX:
+                return (SAXResult)result;
+            case SOURCERESULT:
+                return result;
+            default:
+                throw new IllegalArgumentException("Result type not supported: " + result);
+        }
+    }
+}

Added: labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/transformation/xslt/SourceFactory.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/transformation/xslt/SourceFactory.java	                        (rev 0)
+++ labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/transformation/xslt/SourceFactory.java	2009-06-08 08:53:21 UTC (rev 26868)
@@ -0,0 +1,97 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware
+ * LLC, and individual contributors by the @authors tag. See the copyright.txt
+ * in the distribution for a full listing of individual contributors.
+ * 
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ * 
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this software; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
+ * site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.actions.transformation.xslt;
+
+import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+import java.io.Reader;
+import java.io.StringReader;
+
+import javax.xml.transform.Source;
+import javax.xml.transform.stream.StreamSource;
+
+/**
+ * Code contributed from the Smooks project.
+ */
+public class SourceFactory 
+{
+    private static final SourceFactory factory = new SourceFactory();
+
+    private SourceFactory() 
+    {
+    }
+
+    public static SourceFactory getInstance() 
+    {
+        return factory;
+    }
+
+    public Source createSource(final Object from) 
+    {
+        final Source source;
+        if (from instanceof String) 
+        {
+            source = new StreamSource(new StringReader((String) from));
+        } 
+        else if (from instanceof byte[]) 
+        {
+            source = new StreamSource(new ByteArrayInputStream((byte[]) from));
+        } 
+        else if (from instanceof Reader) 
+        {
+            source = new StreamSource((Reader) from);
+        } 
+        else if (from instanceof InputStream) 
+        {
+            source = new StreamSource((InputStream) from);
+        } 
+        else if (from instanceof File)
+        {
+            source = fileSource((File) from);
+        }
+        else if (from instanceof Source) 
+        {
+            source = (Source) from;
+        } 
+        else 
+        {
+            throw new IllegalStateException("Object '" + from + "' is not of a supported type (String, byte[], Reader, InputStream, File, or Source). Try using a SourceResult instead perhaps.");
+        }
+        return source;
+    }
+    
+    private Source fileSource(final File file)
+    {
+        try
+        {
+            return new StreamSource(new BufferedInputStream(new FileInputStream(file)));
+        } 
+        catch (FileNotFoundException e)
+        {
+            throw new IllegalStateException(e.getMessage(), e);
+        }
+        
+    }
+}

Added: labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/transformation/xslt/SourceResult.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/transformation/xslt/SourceResult.java	                        (rev 0)
+++ labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/transformation/xslt/SourceResult.java	2009-06-08 08:53:21 UTC (rev 26868)
@@ -0,0 +1,63 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware
+ * LLC, and individual contributors by the @authors tag. See the copyright.txt
+ * in the distribution for a full listing of individual contributors.
+ * 
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ * 
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this software; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
+ * site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.actions.transformation.xslt;
+
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+
+/**
+ * Code contributed from the Smooks project.
+ */
+public class SourceResult
+{
+    private Source source;
+    private Result result;
+
+    public SourceResult() 
+    {
+    }
+
+    public SourceResult(final Source source, final Result result) 
+    {
+        this.source = source;
+        this.result = result;
+    }
+
+    public Source getSource() 
+    {
+        return source;
+    }
+
+    public void setSource(final Source source) 
+    {
+        this.source = source;
+    }
+
+    public Result getResult() 
+    {
+        return result;
+    }
+
+    public void setResult(final Result result) 
+    {
+        this.result = result;
+    }
+}

Added: labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/transformation/xslt/TransformerFactoryConfig.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/transformation/xslt/TransformerFactoryConfig.java	                        (rev 0)
+++ labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/transformation/xslt/TransformerFactoryConfig.java	2009-06-08 08:53:21 UTC (rev 26868)
@@ -0,0 +1,155 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware
+ * LLC, and individual contributors by the @authors tag. See the copyright.txt
+ * in the distribution for a full listing of individual contributors.
+ * 
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ * 
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this software; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
+ * site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.actions.transformation.xslt;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.xml.transform.URIResolver;
+
+import org.jboss.soa.esb.actions.transformation.xslt.ResultFactory.ResultType;
+
+/**
+ * Simple data holder for Tranformer factory configuration values.
+ * 
+ * @author <a href="mailto:dbevenius at jboss.com">Daniel Bevenius</a>
+ * @since 4.6
+ */
+public class TransformerFactoryConfig
+{
+    /**
+     * The xslt template file path.
+     */
+    private final String templateFile;
+    
+    /**
+     * The xslt template file path.
+     */
+    private final ResultType resultType;
+    
+    /**
+     * Map for transformer factory features.
+     */
+    private final Map<String, Boolean> features;
+    
+    /**
+     * Map of transformer factory attributes.
+     */
+    private final Map<String, Object> attributes;
+    
+    /**
+     * {@link URIResolver} to be used.
+     */
+    private URIResolver uriResolver;
+    
+    private TransformerFactoryConfig(final Builder builder)
+    {
+        templateFile = builder.templateFile;
+        features = builder.features;
+        attributes = builder.attributes;
+        uriResolver = builder.uriResolver;
+        resultType = builder.resultType;
+    }
+    
+    public String getTemplateFile()
+    {
+        return templateFile;
+    }
+    
+    public Map<String, Boolean> getFeatures()
+    {
+        return features;
+    }
+    
+    public Map<String, Object> getAttributes()
+    {
+        return attributes;
+    }
+    
+    public URIResolver getUriResolver()
+    {
+        return uriResolver;
+    }
+    
+    public ResultType getResultType()
+    {
+        return resultType;
+    }
+    
+    public static class Builder
+    {
+        private final String templateFile;
+        private Map<String, Boolean> features;
+        private Map<String, Object> attributes;
+        private URIResolver uriResolver;
+        private ResultType resultType;
+
+        public Builder(final String templateFile)
+        {
+            this.templateFile = templateFile;
+        }
+        
+        public Builder feature(final String name, final Boolean value)
+        {
+            if (features == null)
+                features = new HashMap<String, Boolean>();
+            
+            features.put(name, value);
+            return this;
+        }
+        
+        public Builder attribute(final String name, final Object value)
+        {
+            if (attributes == null)
+                attributes = new HashMap<String, Object>();
+            
+            attributes.put(name, value);
+            return this;
+        }
+        
+        public Builder uriResolver(final URIResolver resolver)
+        {
+            uriResolver = resolver;
+            return this;
+        }
+        
+        public Builder resultType(final ResultType type)
+        {
+            resultType = type;
+            return this;
+        }
+        
+        public TransformerFactoryConfig build()
+        {
+            if (features == null)
+                features = Collections.emptyMap();
+            
+            if (attributes == null)
+                attributes = Collections.emptyMap();
+            
+            if (resultType == null)
+                resultType = ResultType.STRING;
+            
+            return new TransformerFactoryConfig(this);
+        }
+    }
+}

Added: labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/transformation/xslt/XsltAction.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/transformation/xslt/XsltAction.java	                        (rev 0)
+++ labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/transformation/xslt/XsltAction.java	2009-06-08 08:53:21 UTC (rev 26868)
@@ -0,0 +1,437 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware
+ * LLC, and individual contributors by the @authors tag. See the copyright.txt
+ * in the distribution for a full listing of individual contributors.
+ * 
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ * 
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this software; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
+ * site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.actions.transformation.xslt;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import javax.xml.transform.ErrorListener;
+import javax.xml.transform.Result;
+import javax.xml.transform.Source;
+import javax.xml.transform.Templates;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.URIResolver;
+import javax.xml.transform.dom.DOMResult;
+import javax.xml.transform.sax.SAXResult;
+import javax.xml.transform.stream.StreamSource;
+
+import org.apache.log4j.Logger;
+import org.jboss.internal.soa.esb.assertion.AssertArgument;
+import org.jboss.internal.soa.esb.util.StreamUtils;
+import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.soa.esb.actions.AbstractActionPipelineProcessor;
+import org.jboss.soa.esb.actions.ActionLifecycleException;
+import org.jboss.soa.esb.actions.ActionProcessingException;
+import org.jboss.soa.esb.actions.transformation.xslt.ResultFactory.ResultType;
+import org.jboss.soa.esb.actions.transformation.xslt.TransformerFactoryConfig.Builder;
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.listeners.message.MessageDeliverException;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.message.MessagePayloadProxy;
+import org.jboss.soa.esb.util.ClassUtil;
+
+/**
+ * ESB Action that performs xslt tranformation.
+ * <p/>
+ * Example configuration:
+ * <pre>{@code
+ * <action name="xslt-transform" class="org.jboss.soa.esb.actions.transformation.xslt.XsltAction">
+ *    <property name="templateFile" value="/sample.xsl"/>
+ *    <property name="failOnWarning" value="true"/>
+ *    <property name="resultType" value="STRING"/>
+ * </action>
+ * }<pre>
+ * 
+ * <h3>Configuration Properties</h3>
+ * <lu>
+ *  <li><i>templateFile</i>:
+ *  The path to the xsl template to be used. Mandatory</li>
+ *  
+ *  <li><i>resultType</i>:
+ *  This property controls the output result of the transformation.
+ *  The following values are currently available:
+ *  {@link ResultType.STRING} - will produce a string.
+ *  {@link ResultType.BYTES} - will produce a byte[].
+ *  {@link ResultType.DOM} - will produce a {@link DOMResult}.
+ *  {@link ResultType.SAX} - will produce a {@link SAXResult}.
+ *  </li>
+ *  If the above does not suite your needs then you have the option of specifying both the
+ *  Source and Result by creating {@link SourceResult} object instance. This is a simple
+ *  object that holds both a {@link Source} and a {@link Result}. 
+ *  You need to create this object prior to calling this action and the type of {@link Result}
+ *  returned will be the type that was used to create the {@link SourceResult}.
+ *  
+ *  <li><i>failOnWarning</i>:
+ *  If true will cause a transformation warning to cause an exception to be thrown.
+ *  If false the failure will be logged. Default is true if not specified.</li>
+ *  
+ *  <li><i>uriResolver</i>:
+ *  Fully qualified class name of a class that implements {@link URIResolver}.
+ *  This will be set on the tranformation factory. Optional</li>
+ *  
+ *  <li><i>factory.feature.*</i>:
+ *  Factory features that will be set for the tranformation factory. Optional.
+ *  The feature name, which are fully qualified URIs will should be specified
+ *  after the 'factory.feature.' prefix. For example:
+ *  factory.feature.http://javax.xml.XMLConstants/feature/secure-processing</li>
+ *  
+ *  <li><i>factory.attribute.*</i>:
+ *  Factory attributes that will be set for the tranformation factory. Optional.
+ *  The attribute name should be specified after the 'factory.attribute.' prefix. 
+ *  For example:
+ *  factory.attribute.someVendorAttributename</li>
+ * </lu>
+ * 
+ * @author <a href="mailto:dbevenius at jboss.com">Daniel Bevenius</a>
+ * @since 4.6
+ */
+public class XsltAction extends AbstractActionPipelineProcessor
+{
+    /**
+     * Logger instance.
+     */
+    private static Logger log = Logger.getLogger(XsltAction.class);
+    
+    /**
+     * Config object that holds tranformation factory config options.
+     */
+    private TransformerFactoryConfig transformerConfig;
+    
+    /**
+     * Can be used to control whether a warning should be reported
+     * as an exception during transformation.
+     */
+    private boolean failOnWarning;
+    
+    /**
+     * The templates object.
+     */
+    private Templates xslTemplate;
+    
+    /**
+     * The {@link MessagePayloadProxy}.
+     */
+    private MessagePayloadProxy payloadProxy;
+    
+    /**
+     * Sole constructor that parses the passed-in {@link ConfigTree} for 
+     * mandatory attributes and sets the fields of this instance.
+     * 
+     * @param config The {@link ConfigTree} instance.
+     * 
+     * @throws ConfigurationException if the mandatory attribute 'xslt' has not been set.
+     */
+    public XsltAction(final ConfigTree config) throws ConfigurationException
+    {
+        transformerConfig = createConfig(config);
+        failOnWarning = config.getBooleanAttribute("failOnWarning", true);
+        payloadProxy = new MessagePayloadProxy(config);
+    }
+    
+    /**
+     * Performs the xsl tranformation of the message payload.
+     * 
+     * @param message The ESB {@link Message} object whose payload should be transformed.
+     *                The payload is extracted and set using contract specified by {@link MessagePayloadProxy}.
+     *      
+     * @return {@link Message} The same ESB Message instance passed-in but with it payload transformed.
+     * @throws ActionProcessingException if an error occurs while trying to perform the transformation.
+     */
+    public Message process(final Message message) throws ActionProcessingException
+    {
+        AssertArgument.isNotNull(message, "message");
+        try
+        {
+            final Transformer transformer = xslTemplate.newTransformer();
+            final Object payload = getPayload(message);
+            final Source source;
+            final Result result;
+            // If the payload is of SourceResult that use its source and result.
+            if (payload instanceof SourceResult)
+            {
+                final SourceResult sourceResult = (SourceResult) payload;
+                source = sourceResult.getSource();
+                result = sourceResult.getResult();
+            }
+            else
+            {
+                source = SourceFactory.getInstance().createSource(payload);
+                result = ResultFactory.getInstance().createResult(transformerConfig.getResultType());
+            }
+            
+            // Perform the transformation.
+            transformer.transform(source, result);
+            
+            // Get the result and set on the message object
+            final Object object = ResultFactory.getInstance().extractResult(result, transformerConfig.getResultType());
+            
+            return setPayload(message, object);
+        } 
+        catch (final TransformerConfigurationException e)
+        {
+            throw new ActionProcessingException(e.getMessage(), e);
+        } 
+        catch (TransformerException e)
+        {
+            throw new ActionProcessingException(e.getMessage(), e);
+        }
+    }
+
+    /**
+     * Creates the XSLTemplate.
+     * 
+     * @throws ActionLifecycleException if the {@link Templates} could not be created.
+     * 
+     */
+    @Override
+    public void initialise() throws ActionLifecycleException
+    {
+        try
+        {
+            final TransformerFactory factory = TransformerFactory.newInstance();
+            addFeatures(transformerConfig.getFeatures(), factory);
+            addAttributes(transformerConfig.getAttributes(), factory);
+            setResolver(transformerConfig.getUriResolver(), factory);
+            setErrorListener(new XslErrorListener(failOnWarning), factory);
+            
+            xslTemplate = createTemplate(transformerConfig.getTemplateFile(), factory);
+        } 
+        catch (final TransformerConfigurationException e)
+        {
+            throw new ActionLifecycleException(e.getMessage(), e);
+        } 
+    }
+    
+    private void addFeatures(final Map<String, Boolean> features, final TransformerFactory factory) throws TransformerConfigurationException
+    {
+        for (Entry<String, Boolean> entry : features.entrySet())
+        {
+            factory.setFeature(entry.getKey(), entry.getValue());
+        }
+    }
+
+    private void addAttributes(final Map<String, Object> attributes, final TransformerFactory factory) throws TransformerConfigurationException
+    {
+        for (Entry<String, Object> entry : attributes.entrySet())
+        {
+            factory.setAttribute(entry.getKey(), entry.getValue());
+        }
+    }
+
+    private void setResolver(final URIResolver uriResolver, final TransformerFactory factory)
+    {
+        if (uriResolver != null)
+        {
+            factory.setURIResolver(uriResolver);
+        }
+    }
+
+    private void setErrorListener(final XslErrorListener xslErrorListener, final TransformerFactory factory)
+    {
+        factory.setErrorListener(xslErrorListener);
+    }
+
+    private Templates createTemplate(final String templateFile, final TransformerFactory factory) throws ActionLifecycleException, TransformerConfigurationException
+    {
+        InputStream stream = null;
+        try
+        {
+            stream = StreamUtils.getResource(templateFile);
+            return factory.newTemplates(new StreamSource(stream));
+        } 
+        catch (final ConfigurationException e)
+        {
+            throw new ActionLifecycleException(e.getMessage(), e);
+        } 
+        finally
+        {
+            if (stream != null)
+            {
+                try { stream.close(); } catch (final IOException ignore) { log.error("Exception while closing stream", ignore); }
+            }
+        }
+    }
+
+    /**
+     * Parses the passed-in ESB {@link ConfigTree} and populates a {@link TransformerFactoryConfig}.
+     * 
+     * @param config The ESB {@link ConfigTree}.
+     * @return {@link TransformerFactoryConfig}.
+     * @throws ConfigurationException
+     */
+    private TransformerFactoryConfig createConfig(final ConfigTree config) throws ConfigurationException
+    {
+        final Builder builder = new TransformerFactoryConfig.Builder(config.getRequiredAttribute("templateFile"));
+        extractFeatures(config, builder);
+        extractAttributes(config, builder);
+        createUrlResolver(config, builder);
+        builder.resultType(ResultFactory.ResultType.valueOf(config.getRequiredAttribute("resultType")));
+        return builder.build();
+    }
+
+    /**
+     * Extracts the factory attributes and adds them to the builder.
+     * 
+     * @param config The ESB {@link ConfigTree}.
+     * @param builder The {@link TransformerFactoryConfig.Builder}.
+     * @return Builder To support method chainging.
+     */
+    void extractAttributes(final ConfigTree config, final Builder builder)
+    {
+        for (final String attrName : config.getAttributeNames())
+        {
+            int idx = attrName.indexOf("factory.attribute.");
+            if (idx != -1)
+            {
+                final Object value = config.getAttribute(attrName);
+                final String name = attrName.substring(idx + "factory.attribute.".length());
+                builder.attribute(name, value);
+            }
+        }
+    }
+
+    /**
+     * Extracts the 'uriResolver' attribute from the ESB {@link ConfigTree} and instantiates a class
+     * of that type. This class will be set on the passed-in builder.
+     * @param config The ESB {@link ConfigTree}.
+     * @param builder The {@link TransformerFactoryConfig.Builder}.
+     * @throws ConfigurationException If the class could not be created.
+     */
+    void createUrlResolver(final ConfigTree config, final Builder builder) throws ConfigurationException
+    {
+        final String className = config.getAttribute("uriResolver");
+        if (className != null)
+        {
+            try
+            {
+                final Class<?> resolver = ClassUtil.forName(className, getClass());
+                URIResolver uriResolver = (URIResolver) resolver.newInstance();
+                builder.uriResolver(uriResolver);
+            } 
+            catch (final ClassNotFoundException e)
+            {
+                throw new ConfigurationException(e.getMessage(), e);
+            } 
+            catch (InstantiationException e)
+            {
+                throw new ConfigurationException(e.getMessage(), e);
+            } 
+            catch (IllegalAccessException e)
+            {
+                throw new ConfigurationException(e.getMessage(), e);
+            }
+        }
+    }
+
+    /**
+     * Extracts the factory features and adds them to the builder.
+     * 
+     * @param config The ESB {@link ConfigTree}.
+     * @param builder The {@link TransformerFactoryConfig.Builder}.
+     */
+    void extractFeatures(final ConfigTree config, Builder builder)
+    {
+        for (final String attrName : config.getAttributeNames())
+        {
+            int idx = attrName.indexOf("factory.feature.");
+            if (idx != -1)
+            {
+                final String value = config.getAttribute(attrName);
+                final String name = attrName.substring(idx + "factory.feature.".length());
+                builder.feature(name, Boolean.valueOf(value));
+            }
+        }
+    }
+
+    private Object getPayload(final Message message) throws ActionProcessingException
+    {
+        try
+        {
+            return payloadProxy.getPayload(message);
+        } 
+        catch (MessageDeliverException e)
+        {
+            throw new ActionProcessingException(e.getMessage(), e);
+        }
+    }
+    
+    private Message setPayload(final Message message, final Object payload) throws ActionProcessingException
+    {
+        try
+        {
+            payloadProxy.setPayload(message, payload);
+        } 
+        catch (MessageDeliverException e)
+        {
+            throw new ActionProcessingException(e.getMessage(), e);
+        }
+        return message;
+    }
+    
+    public TransformerFactoryConfig getTranformerConfig()
+    {
+        return transformerConfig;
+    }
+
+    @Override
+    public String toString()
+    {
+        return String.format("%s templateFile=%s, failOnWarning=%b, features=%s, attributes=%s", getClass().getSimpleName(), transformerConfig.getTemplateFile(), failOnWarning, transformerConfig.getFeatures(), transformerConfig.getAttributes());
+    }
+    
+    private static class XslErrorListener implements ErrorListener 
+    {
+        private final boolean failOnWarning;
+
+        public XslErrorListener(boolean failOnWarning) 
+        {
+            this.failOnWarning = failOnWarning;
+        }
+
+        public void warning(final TransformerException exception) throws TransformerException 
+        {
+            if(failOnWarning) 
+            {
+                throw exception;
+            } 
+            else 
+            {
+                log.warn("XSL Warning.", exception);
+            }
+        }
+
+        public void error(final TransformerException exception) throws TransformerException 
+        {
+            throw exception;
+        }
+
+        public void fatalError(final TransformerException exception) throws TransformerException 
+        {
+            throw exception;
+        }
+    }
+
+}

Added: labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/transformation/xslt/ResultFactoryUnitTest.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/transformation/xslt/ResultFactoryUnitTest.java	                        (rev 0)
+++ labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/transformation/xslt/ResultFactoryUnitTest.java	2009-06-08 08:53:21 UTC (rev 26868)
@@ -0,0 +1,91 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware
+ * LLC, and individual contributors by the @authors tag. See the copyright.txt
+ * in the distribution for a full listing of individual contributors.
+ * 
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ * 
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this software; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
+ * site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.actions.transformation.xslt;
+
+import static org.junit.Assert.*;
+
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStream;
+import java.io.StringWriter;
+import java.io.Writer;
+
+import javax.xml.transform.Result;
+import javax.xml.transform.dom.DOMResult;
+import javax.xml.transform.sax.SAXResult;
+import javax.xml.transform.stream.StreamResult;
+
+import org.jboss.soa.esb.actions.transformation.xslt.ResultFactory.ResultType;
+import org.junit.Test;
+
+import junit.framework.JUnit4TestAdapter;
+
+/**
+ * Unit test for {@link ResultFactory}.
+ * <p/>
+ * 
+ * @author <a href="mailto:dbevenius at jboss.com">Daniel Bevenius</a>
+ */
+public class ResultFactoryUnitTest
+{
+    private ResultFactory resultFactory = ResultFactory.getInstance();
+    
+    @Test
+    public void createStringResult()
+    {
+        final Result result = resultFactory.createResult(ResultType.STRING);
+        assertTrue(result instanceof StreamResult);
+        final StreamResult streamResult = (StreamResult) result;
+        assertNotNull(streamResult.getWriter());
+        final Writer writer = streamResult.getWriter();
+        assertTrue(writer instanceof StringWriter);
+    }
+    
+    @Test
+    public void createByteArrayResult()
+    {
+        final Result result = resultFactory.createResult(ResultType.BYTES);
+        assertTrue(result instanceof StreamResult);
+        final StreamResult streamResult = (StreamResult) result;
+        assertNull(streamResult.getWriter());
+        final OutputStream outputStream = streamResult.getOutputStream();
+        assertNotNull(outputStream);
+        assertNotNull(outputStream instanceof ByteArrayOutputStream);
+    }
+    
+    @Test
+    public void createDOMResult()
+    {
+        final Result result = resultFactory.createResult(ResultType.DOM);
+        assertTrue(result instanceof DOMResult);
+    }
+    
+    @Test
+    public void createSAXResult()
+    {
+        final Result result = resultFactory.createResult(ResultType.SAX);
+        assertTrue(result instanceof SAXResult);
+    }
+    
+    public static junit.framework.Test suite()
+    {
+        return new JUnit4TestAdapter(ResultFactoryUnitTest.class);
+    }
+}

Added: labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/transformation/xslt/SourceFactoryUnitTest.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/transformation/xslt/SourceFactoryUnitTest.java	                        (rev 0)
+++ labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/transformation/xslt/SourceFactoryUnitTest.java	2009-06-08 08:53:21 UTC (rev 26868)
@@ -0,0 +1,102 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware
+ * LLC, and individual contributors by the @authors tag. See the copyright.txt
+ * in the distribution for a full listing of individual contributors.
+ * 
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ * 
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this software; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
+ * site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.actions.transformation.xslt;
+
+import static org.junit.Assert.*;
+
+import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringReader;
+
+import javax.xml.transform.Source;
+import javax.xml.transform.stream.StreamSource;
+
+import junit.framework.JUnit4TestAdapter;
+
+import org.junit.Test;
+
+/**
+ * Unit test for {@link SourceFactory}.
+ * <p/>
+ * 
+ * Code contributed from the Smooks project.
+ * 
+ * @author <a href="mailto:dbevenius at jboss.com">Daniel Bevenius</a>
+ *
+ */
+public class SourceFactoryUnitTest
+{
+    private SourceFactory factory = SourceFactory.getInstance();
+    
+    @Test
+    public void createStreamSourceStreamFromString()
+    {
+        final Source source = factory.createSource( "testing" );
+        assertNotNull( source );
+        assertTrue( source instanceof StreamSource );
+    }
+    
+    @Test
+    public void getSourceByteArray()
+    {
+        final Source source = factory.createSource( "test".getBytes() );
+        assertNotNull( source );
+        assertTrue( source instanceof StreamSource );
+    }
+    
+    @Test
+    public void getSourceReader()
+    {
+        final Source source = factory.createSource( new StringReader( "testing" ));
+        assertNotNull( source );
+        assertTrue( source instanceof StreamSource );
+    }
+    
+    @Test
+    public void getSourceInputStream()
+    {
+        final Source source = factory.createSource( new ByteArrayInputStream( "testing".getBytes() ) );
+        assertNotNull( source );
+        assertTrue( source instanceof StreamSource );
+    }
+    
+    @Test
+    public void getSourceFile() throws IOException
+    {
+        final File file = File.createTempFile("junit", ".test");
+        file.deleteOnExit();
+        final Source source = factory.createSource(file);
+        assertNotNull( source );
+        assertTrue( source instanceof StreamSource );
+        final StreamSource streamSource = (StreamSource) source;
+        final InputStream inputStream = streamSource.getInputStream();
+        assertTrue(inputStream instanceof BufferedInputStream);
+    }
+    
+    public static junit.framework.Test suite()
+    {
+        return new JUnit4TestAdapter(SourceFactoryUnitTest.class);
+    }
+    
+}

Added: labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/transformation/xslt/XsltActionUnitTest.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/transformation/xslt/XsltActionUnitTest.java	                        (rev 0)
+++ labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/transformation/xslt/XsltActionUnitTest.java	2009-06-08 08:53:21 UTC (rev 26868)
@@ -0,0 +1,288 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2009, Red Hat Middleware
+ * LLC, and individual contributors by the @authors tag. See the copyright.txt
+ * in the distribution for a full listing of individual contributors.
+ * 
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ * 
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this software; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
+ * site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.actions.transformation.xslt;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.StringReader;
+import java.io.StringWriter;
+import java.net.URL;
+import java.util.Map;
+
+import javax.xml.XMLConstants;
+import javax.xml.transform.Source;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.URIResolver;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
+import junit.framework.JUnit4TestAdapter;
+
+import org.custommonkey.xmlunit.XMLAssert;
+import org.custommonkey.xmlunit.XMLUnit;
+import org.jboss.internal.soa.esb.util.StreamUtils;
+import org.jboss.soa.esb.ConfigurationException;
+import org.jboss.soa.esb.actions.ActionLifecycleException;
+import org.jboss.soa.esb.actions.ActionProcessingException;
+import org.jboss.soa.esb.actions.transformation.xslt.ResultFactory.ResultType;
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.message.format.MessageFactory;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.xml.sax.SAXException;
+
+/**
+ * Unit test for {@link XsltAction}.
+ * 
+ * @author <a href="mailto:dbevenius at jboss.com">Daniel Bevenius</a>
+ */
+public class XsltActionUnitTest
+{
+    private static final String PACKAGE_PATH = "/org/jboss/soa/esb/actions/transformation/xslt";
+    private static final String TEST_XSL_1 = PACKAGE_PATH + "/test1.xsl";
+    private static final String TEST_XML_1 = PACKAGE_PATH + "/example1.xml";
+    
+    @BeforeClass public static void before()
+    {
+        XMLUnit.setIgnoreWhitespace(true);
+    }
+    
+    @Test (expected = ConfigurationException.class)
+    public void shouldThrowIfNoTemplateIsConfigured() throws ConfigurationException
+    {
+        new XsltAction(new ConfigTree("xslttest"));
+    }
+    
+    @Test public void processXsltString() throws ConfigurationException, ActionProcessingException, ActionLifecycleException, SAXException, IOException
+    {
+        final XsltAction action = new XsltAction(new ConfigBuilder().templateFile(TEST_XSL_1).resultType(ResultType.STRING).build());
+        action.initialise();
+        
+        final String xml = StreamUtils.getResourceAsString(TEST_XML_1, "UTF-8");
+        
+        final Message message = MessageFactory.getInstance().getMessage();
+        message.getBody().add(xml);
+        
+        Message processed = action.process(message);
+        XMLAssert.assertXMLEqual("<xxx/>", (String) processed.getBody().get());
+    }
+    
+    @Test public void extractFeatures() throws ConfigurationException, ActionProcessingException, ActionLifecycleException, SAXException, IOException
+    {
+        ConfigBuilder confBuilder = new ConfigBuilder().resultType(ResultType.STRING).templateFile(TEST_XSL_1);
+        confBuilder.feature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
+        ConfigTree config = confBuilder.build();
+        
+        final XsltAction action = new XsltAction(config);
+        action.initialise();
+        
+        final Map<String, Boolean> features = action.getTranformerConfig().getFeatures();
+        
+        assertNotNull(features);
+        assertEquals(1, features.size());
+        assertTrue(features.containsKey(XMLConstants.FEATURE_SECURE_PROCESSING));
+        assertEquals(true, features.get(XMLConstants.FEATURE_SECURE_PROCESSING));
+    }
+    
+    @Test public void extractAttributes() throws ConfigurationException, ActionProcessingException, ActionLifecycleException, SAXException, IOException
+    {
+        ConfigBuilder confBuilder = new ConfigBuilder().resultType(ResultType.STRING).templateFile(TEST_XSL_1);
+        confBuilder.attribute("att1", "value1").attribute("att2", "value2");
+        ConfigTree config = confBuilder.build();
+        
+        final XsltAction action = new XsltAction(config);
+        final Map<String, Object> attributes = action.getTranformerConfig().getAttributes();
+        
+        assertNotNull(attributes);
+        assertEquals(2, attributes.size());
+        assertTrue(attributes.containsKey("att1"));
+        assertTrue(attributes.containsKey("att2"));
+        assertEquals("value1", attributes.get("att1"));
+        assertEquals("value2", attributes.get("att2"));
+    }
+    
+    @Test public void createURIResolver() throws ConfigurationException, ActionProcessingException, ActionLifecycleException, SAXException, IOException
+    {
+        ConfigBuilder confBuilder = new ConfigBuilder().resultType(ResultType.STRING).templateFile(TEST_XSL_1);
+        confBuilder.uriResolver(MockUriResolver.class.getName());
+        ConfigTree config = confBuilder.build();
+        
+        final XsltAction action = new XsltAction(config);
+        action.initialise();
+        
+        URIResolver resolver = action.getTranformerConfig().getUriResolver();
+        
+        assertNotNull(resolver);
+        assertEquals(MockUriResolver.class.getName(), resolver.getClass().getName());
+    }
+    
+    @Test public void processFileSource() throws ConfigurationException, ActionLifecycleException, ActionProcessingException, SAXException, IOException
+    {
+        final XsltAction action = new XsltAction(new ConfigBuilder().resultType(ResultType.STRING).templateFile(TEST_XSL_1).build());
+        action.initialise();
+        
+        final Message message = MessageFactory.getInstance().getMessage();
+        final URL resource = getClass().getResource(TEST_XML_1);
+        final File xmlFile = new File(resource.getFile());
+        message.getBody().add(xmlFile);
+        
+        final Message processed = action.process(message);
+        XMLAssert.assertXMLEqual("<xxx/>", (String) processed.getBody().get());
+    }
+    
+    @Test public void processStringSource() throws ConfigurationException, ActionLifecycleException, ActionProcessingException, SAXException, IOException
+    {
+        final XsltAction action = new XsltAction(new ConfigBuilder().resultType(ResultType.STRING).templateFile(TEST_XSL_1).build());
+        action.initialise();
+        
+        final Message message = MessageFactory.getInstance().getMessage();
+        final String xml = "<a><b><c><b></b></c></b></a>";
+        message.getBody().add(xml);
+        
+        final Message processed = action.process(message);
+        XMLAssert.assertXMLEqual("<xxx/>", (String) processed.getBody().get());
+    }
+    
+    @Test public void processInputStreamSource() throws ConfigurationException, ActionLifecycleException, ActionProcessingException, SAXException, IOException
+    {
+        final XsltAction action = new XsltAction(new ConfigBuilder().resultType(ResultType.STRING).templateFile(TEST_XSL_1).build());
+        action.initialise();
+        
+        final Message message = MessageFactory.getInstance().getMessage();
+        final InputStream inputStream = getClass().getResourceAsStream(TEST_XML_1);
+        message.getBody().add(inputStream);
+        
+        final Message processed = action.process(message);
+        XMLAssert.assertXMLEqual("<xxx/>", (String) processed.getBody().get());
+    }
+    
+    @Test public void processReaderSource() throws ConfigurationException, ActionLifecycleException, ActionProcessingException, SAXException, IOException
+    {
+        processOneReaderSource();
+    }
+    
+    @Test public void processMultipleReaderSource() throws ConfigurationException, ActionLifecycleException, ActionProcessingException, SAXException, IOException
+    {
+        for (int i = 0; i < 100; i++)
+        {
+            processOneReaderSource();
+        }
+    }
+    
+    private void processOneReaderSource() throws ConfigurationException, ActionLifecycleException, ActionProcessingException, SAXException, IOException
+    {
+        final XsltAction action = new XsltAction(new ConfigBuilder().resultType(ResultType.STRING).templateFile(TEST_XSL_1).build());
+        action.initialise();
+        
+        final Message message = MessageFactory.getInstance().getMessage();
+        final String xml = "<a><b><c><b></b></c></b></a>";
+        final StreamSource streamSource = new StreamSource(new StringReader(xml));
+        message.getBody().add(streamSource);
+        
+        final Message processed = action.process(message);
+        XMLAssert.assertXMLEqual("<xxx/>", (String) processed.getBody().get());
+    }
+    
+    @Test public void processSourceResult() throws ConfigurationException, ActionLifecycleException, ActionProcessingException, SAXException, IOException
+    {
+        final XsltAction action = new XsltAction(new ConfigBuilder().resultType(ResultType.SOURCERESULT).templateFile(TEST_XSL_1).build());
+        action.initialise();
+        
+        final Message message = MessageFactory.getInstance().getMessage();
+        final String xml = "<a><b><c><b></b></c></b></a>";
+        final StreamSource source = new StreamSource(new StringReader(xml));
+        final StringWriter stringWriter = new StringWriter();
+        final StreamResult result = new StreamResult(stringWriter);
+        final SourceResult sourceResult = new SourceResult(source, result);
+        message.getBody().add(sourceResult);
+        
+        final Message processed = action.process(message);
+        Object object = processed.getBody().get();
+        assertTrue(object instanceof StreamResult);
+        
+        XMLAssert.assertXMLEqual("<xxx/>", ((StreamResult) processed.getBody().get()).getWriter().toString());
+    }
+    
+    public static class MockUriResolver implements URIResolver
+    {
+        public Source resolve(String href, String base) throws TransformerException
+        {
+            return null;
+        }
+    }
+
+    public static junit.framework.Test suite()
+    {
+        return new JUnit4TestAdapter(XsltActionUnitTest.class);
+    }
+    
+    private static class ConfigBuilder
+    {
+        private ConfigTree config;
+        
+        public ConfigBuilder()
+        {
+            config = new ConfigTree("xsltUnitTest");
+        }
+        
+        public ConfigBuilder templateFile(final String xslt)
+        {
+            config.setAttribute("templateFile", xslt);
+            return this;
+        }
+        
+        public ConfigBuilder resultType(final ResultType type)
+        {
+            config.setAttribute("resultType", type.toString());
+            return this;
+        }
+        
+        public ConfigBuilder feature(final String name, final Boolean value)
+        {
+            config.setAttribute("factory.feature." + name, value.toString());
+            return this;
+        }
+        
+        public ConfigBuilder attribute(final String name, final String value)
+        {
+            config.setAttribute("factory.attribute." + name, value);
+            return this;
+        }
+        
+        public ConfigBuilder uriResolver(final String className)
+        {
+            config.setAttribute("uriResolver", className);
+            return this;
+        }
+        
+        public ConfigTree build()
+        {
+            return config;
+        }
+    }
+
+}

Added: labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/transformation/xslt/example1.xml
===================================================================
--- labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/transformation/xslt/example1.xml	                        (rev 0)
+++ labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/transformation/xslt/example1.xml	2009-06-08 08:53:21 UTC (rev 26868)
@@ -0,0 +1 @@
+<a> <b> <c> <b></b> </c> </b> </a>

Added: labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/transformation/xslt/test1.xsl
===================================================================
--- labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/transformation/xslt/test1.xsl	                        (rev 0)
+++ labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/transformation/xslt/test1.xsl	2009-06-08 08:53:21 UTC (rev 26868)
@@ -0,0 +1,9 @@
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
+
+    <xsl:output method="xml" encoding="UTF-8" />
+    
+    <xsl:template match="b">
+        <xxx></xxx>
+    </xsl:template>
+
+</xsl:stylesheet>




More information about the jboss-svn-commits mailing list