From 3daa690426cd8bd9c2da21de2897fcde8849ec11 Mon Sep 17 00:00:00 2001 From: mbazos Date: Mon, 1 Oct 2012 20:17:42 -0400 Subject: [PATCH 1/3] INT-2768 Add XSLT Transformer Factory Class Name Add the ability to inject the class name of the xslt transformer implementation. https://jira.springsource.org/browse/INT-2768 -add @author to java docs -updat copywrite year to current year add new test and check for class name that it exists. -fix xslt payload transformer and test for unknown class name, will now default to the jre default transformer. --- .../config/XsltPayloadTransformerParser.java | 7 +++- .../transformer/XsltPayloadTransformer.java | 37 +++++++++++++++---- .../xml/config/spring-integration-xml-2.2.xsd | 13 +++++++ .../XsltPayloadTransformerTests.java | 31 ++++++++++++++-- .../XsltTransformerTests-context.xml | 3 +- 5 files changed, 78 insertions(+), 13 deletions(-) diff --git a/spring-integration-xml/src/main/java/org/springframework/integration/xml/config/XsltPayloadTransformerParser.java b/spring-integration-xml/src/main/java/org/springframework/integration/xml/config/XsltPayloadTransformerParser.java index 34ec33df53..f6307097af 100644 --- a/spring-integration-xml/src/main/java/org/springframework/integration/xml/config/XsltPayloadTransformerParser.java +++ b/spring-integration-xml/src/main/java/org/springframework/integration/xml/config/XsltPayloadTransformerParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2013 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,6 +36,7 @@ import org.springframework.util.xml.DomUtils; * @author Jonas Partner * @author Mark Fisher * @author Oleg Zhurakousky + * @author Mike Bazos */ public class XsltPayloadTransformerParser extends AbstractTransformerParser { @@ -51,6 +52,7 @@ public class XsltPayloadTransformerParser extends AbstractTransformerParser { String resultTransformer = element.getAttribute("result-transformer"); String resultFactory = element.getAttribute("result-factory"); String resultType = element.getAttribute("result-type"); + String transformerFactoryClass = element.getAttribute("transformer-factory-class"); IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "xslt-param-headers"); Assert.isTrue(StringUtils.hasText(xslResource) ^ StringUtils.hasText(xslTemplates), "Exactly one of 'xsl-resource' or 'xsl-templates' is required."); @@ -60,6 +62,9 @@ public class XsltPayloadTransformerParser extends AbstractTransformerParser { else if (StringUtils.hasText(xslTemplates)) { builder.addConstructorArgReference(xslTemplates); } + if (StringUtils.hasText(transformerFactoryClass)) { + builder.addConstructorArgValue(transformerFactoryClass); + } XmlNamespaceUtils.configureResultFactory(builder, resultType, resultFactory); boolean resultFactorySpecified = StringUtils.hasText(resultFactory) || StringUtils.hasText(resultType); if(resultFactorySpecified){ diff --git a/spring-integration-xml/src/main/java/org/springframework/integration/xml/transformer/XsltPayloadTransformer.java b/spring-integration-xml/src/main/java/org/springframework/integration/xml/transformer/XsltPayloadTransformer.java index d586c858a6..149911bbed 100644 --- a/spring-integration-xml/src/main/java/org/springframework/integration/xml/transformer/XsltPayloadTransformer.java +++ b/spring-integration-xml/src/main/java/org/springframework/integration/xml/transformer/XsltPayloadTransformer.java @@ -47,6 +47,7 @@ import org.springframework.integration.xml.result.ResultFactory; import org.springframework.integration.xml.source.DomSourceFactory; import org.springframework.integration.xml.source.SourceFactory; import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.PatternMatchUtils; import org.springframework.xml.transform.StringResult; @@ -76,10 +77,11 @@ import org.springframework.xml.transform.StringSource; * @author Mark Fisher * @author Oleg Zhurakousky * @author Artem Bilan + * @author Mike Bazos */ public class XsltPayloadTransformer extends AbstractTransformer { - private final Log logger = LogFactory.getLog(this.getClass()); + private static final Log logger = LogFactory.getLog(XsltPayloadTransformer.class); private final Templates templates; @@ -107,21 +109,26 @@ public class XsltPayloadTransformer extends AbstractTransformer { } public XsltPayloadTransformer(Resource xslResource) throws Exception { - this(TransformerFactory.newInstance().newTemplates( - createStreamSourceOnResource(xslResource)), null); + this(xslResource, null, null); } public XsltPayloadTransformer(Resource xslResource, ResultTransformer resultTransformer) throws Exception { - this(TransformerFactory.newInstance().newTemplates( - createStreamSourceOnResource(xslResource)), resultTransformer); + this(xslResource, resultTransformer, null); } + + public XsltPayloadTransformer(Resource xslResource, ResultTransformer resultTransformer, String transformerFactoryClass) throws Exception { + this(getTransformerFactory(transformerFactoryClass).newTemplates(createStreamSourceOnResource(xslResource)), resultTransformer); + } + + public XsltPayloadTransformer(Resource xslResource, String transformerFactoryClass) throws Exception { + this(getTransformerFactory(transformerFactoryClass).newTemplates(createStreamSourceOnResource(xslResource)), null); + } public XsltPayloadTransformer(Templates templates, ResultTransformer resultTransformer) throws ParserConfigurationException { this.templates = templates; this.resultTransformer = resultTransformer; } - /** * Sets the SourceFactory. */ @@ -317,5 +324,21 @@ public class XsltPayloadTransformer extends AbstractTransformer { return new StreamSource(xslResource.getInputStream()); } } - + + private static TransformerFactory getTransformerFactory(String transformerFactoryImplClass) { + TransformerFactory transformerFactory = null; + + if (transformerFactoryImplClass == null || transformerFactoryImplClass.length() <= 0) { + transformerFactory = TransformerFactory.newInstance(); + } else if(ClassUtils.isPresent(transformerFactoryImplClass, ClassLoader.getSystemClassLoader())) { + transformerFactory = TransformerFactory.newInstance(transformerFactoryImplClass, ClassLoader.getSystemClassLoader()); + } else { + if (logger.isWarnEnabled()) { + logger.warn(String.format("Class [%s] was not found will default to [%s], please ensure [%s] is located on your class path", transformerFactoryImplClass, TransformerFactory.class, transformerFactoryImplClass)); + } + transformerFactory = TransformerFactory.newInstance(); + } + + return transformerFactory; + } } diff --git a/spring-integration-xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-2.2.xsd b/spring-integration-xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-2.2.xsd index 58c6ebff3a..61f37d1a3f 100644 --- a/spring-integration-xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-2.2.xsd +++ b/spring-integration-xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-2.2.xsd @@ -237,6 +237,19 @@ + + + + + + + + + + diff --git a/spring-integration-xml/src/test/java/org/springframework/integration/xml/transformer/XsltPayloadTransformerTests.java b/spring-integration-xml/src/test/java/org/springframework/integration/xml/transformer/XsltPayloadTransformerTests.java index 937b1564fe..20c93aedd9 100644 --- a/spring-integration-xml/src/test/java/org/springframework/integration/xml/transformer/XsltPayloadTransformerTests.java +++ b/spring-integration-xml/src/test/java/org/springframework/integration/xml/transformer/XsltPayloadTransformerTests.java @@ -16,6 +16,10 @@ package org.springframework.integration.xml.transformer; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertTrue; +import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; + import javax.xml.transform.Result; import javax.xml.transform.TransformerException; import javax.xml.transform.dom.DOMResult; @@ -34,15 +38,12 @@ import org.springframework.xml.transform.StringResult; import org.springframework.xml.transform.StringSource; import org.w3c.dom.Document; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; - /** * @author Jonas Partner * @author Oleg Zhurakousky * @author Gunnar Hillert + * @author Mike Bazos */ public class XsltPayloadTransformerTests { @@ -112,6 +113,28 @@ public class XsltPayloadTransformerTests { assertEquals("Wrong value from result conversion", returnValue, transformed); } + + @Test + public void testXsltPayloadWithTransformerFactoryClassname() throws Exception { + Integer returnValue = new Integer(13); + transformer = new XsltPayloadTransformer(getXslResource(), new StubResultTransformer(returnValue), + "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl"); + Object transformed = transformer + .doTransform(buildMessage(new StringSource(docAsString))); + assertEquals("Wrong value from result conversion", returnValue, + transformed); + } + + @Test + public void testXsltPayloadWithBadTransformerFactoryClassname() throws Exception { + Integer returnValue = new Integer(13); + transformer = new XsltPayloadTransformer(getXslResource(), new StubResultTransformer(returnValue), + "foo.bar.Baz"); + Object transformed = transformer + .doTransform(buildMessage(new StringSource(docAsString))); + assertEquals("Wrong value from result conversion", returnValue, + transformed); + } @Test(expected = TransformerException.class) public void testNonXmlString() throws Exception { diff --git a/spring-integration-xml/src/test/java/org/springframework/integration/xml/transformer/XsltTransformerTests-context.xml b/spring-integration-xml/src/test/java/org/springframework/integration/xml/transformer/XsltTransformerTests-context.xml index e9b189370d..c76f7e5aea 100644 --- a/spring-integration-xml/src/test/java/org/springframework/integration/xml/transformer/XsltTransformerTests-context.xml +++ b/spring-integration-xml/src/test/java/org/springframework/integration/xml/transformer/XsltTransformerTests-context.xml @@ -17,7 +17,8 @@ input-channel="paramHeadersWithStartWildCharacterChannel" output-channel="output" xsl-resource="classpath:org/springframework/integration/xml/transformer/transformer.xslt" - xslt-param-headers="*Param, foo"> + xslt-param-headers="*Param, foo" + transformer-factory-class="com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl"> Date: Wed, 25 Sep 2013 13:09:58 +0300 Subject: [PATCH 2/3] INT-2768: Add BeanClassLoaderAware --- .../config/XsltPayloadTransformerParser.java | 6 +- .../transformer/XsltPayloadTransformer.java | 86 +++++++++++-------- .../xml/config/spring-integration-xml-2.2.xsd | 13 --- .../xml/config/spring-integration-xml-3.0.xsd | 15 ++++ .../XsltPayloadTransformerTests.java | 33 ++++--- 5 files changed, 85 insertions(+), 68 deletions(-) diff --git a/spring-integration-xml/src/main/java/org/springframework/integration/xml/config/XsltPayloadTransformerParser.java b/spring-integration-xml/src/main/java/org/springframework/integration/xml/config/XsltPayloadTransformerParser.java index f6307097af..b5c3a87bfa 100644 --- a/spring-integration-xml/src/main/java/org/springframework/integration/xml/config/XsltPayloadTransformerParser.java +++ b/spring-integration-xml/src/main/java/org/springframework/integration/xml/config/XsltPayloadTransformerParser.java @@ -62,9 +62,6 @@ public class XsltPayloadTransformerParser extends AbstractTransformerParser { else if (StringUtils.hasText(xslTemplates)) { builder.addConstructorArgReference(xslTemplates); } - if (StringUtils.hasText(transformerFactoryClass)) { - builder.addConstructorArgValue(transformerFactoryClass); - } XmlNamespaceUtils.configureResultFactory(builder, resultType, resultFactory); boolean resultFactorySpecified = StringUtils.hasText(resultFactory) || StringUtils.hasText(resultType); if(resultFactorySpecified){ @@ -73,6 +70,9 @@ public class XsltPayloadTransformerParser extends AbstractTransformerParser { if (StringUtils.hasText(resultTransformer)) { builder.addConstructorArgReference(resultTransformer); } + if (StringUtils.hasText(transformerFactoryClass)) { + builder.addConstructorArgValue(transformerFactoryClass); + } List xslParameterElements = DomUtils.getChildElementsByTagName(element, "xslt-param"); if (!CollectionUtils.isEmpty(xslParameterElements)) { Map xslParameterMappings = new ManagedMap(); diff --git a/spring-integration-xml/src/main/java/org/springframework/integration/xml/transformer/XsltPayloadTransformer.java b/spring-integration-xml/src/main/java/org/springframework/integration/xml/transformer/XsltPayloadTransformer.java index 149911bbed..e79a01c2e2 100644 --- a/spring-integration-xml/src/main/java/org/springframework/integration/xml/transformer/XsltPayloadTransformer.java +++ b/spring-integration-xml/src/main/java/org/springframework/integration/xml/transformer/XsltPayloadTransformer.java @@ -19,7 +19,6 @@ package org.springframework.integration.xml.transformer; import java.io.IOException; import java.util.Map; -import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Templates; @@ -30,10 +29,9 @@ import javax.xml.transform.dom.DOMResult; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamSource; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.w3c.dom.Document; +import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.core.io.Resource; import org.springframework.expression.Expression; import org.springframework.expression.spel.support.StandardEvaluationContext; @@ -47,7 +45,6 @@ import org.springframework.integration.xml.result.ResultFactory; import org.springframework.integration.xml.source.DomSourceFactory; import org.springframework.integration.xml.source.SourceFactory; import org.springframework.util.Assert; -import org.springframework.util.ClassUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.PatternMatchUtils; import org.springframework.xml.transform.StringResult; @@ -78,19 +75,22 @@ import org.springframework.xml.transform.StringSource; * @author Oleg Zhurakousky * @author Artem Bilan * @author Mike Bazos + * @author Gary Russell */ -public class XsltPayloadTransformer extends AbstractTransformer { +public class XsltPayloadTransformer extends AbstractTransformer implements BeanClassLoaderAware { - private static final Log logger = LogFactory.getLog(XsltPayloadTransformer.class); + private final ResultTransformer resultTransformer; - private final Templates templates; + private volatile Resource xslResource; + + private volatile Templates templates; + + private String transformerFactoryClassName; private volatile StandardEvaluationContext evaluationContext; private Map xslParameterMappings; - private final ResultTransformer resultTransformer; - private volatile SourceFactory sourceFactory = new DomSourceFactory(); private volatile ResultFactory resultFactory = new DomResultFactory(); @@ -103,28 +103,38 @@ public class XsltPayloadTransformer extends AbstractTransformer { private volatile String[] xsltParamHeaders; + private ClassLoader classLoader; - public XsltPayloadTransformer(Templates templates) throws ParserConfigurationException { + + public XsltPayloadTransformer(Templates templates) { this(templates, null); } - public XsltPayloadTransformer(Resource xslResource) throws Exception { + public XsltPayloadTransformer(Resource xslResource) { this(xslResource, null, null); } - public XsltPayloadTransformer(Resource xslResource, ResultTransformer resultTransformer) throws Exception { + public XsltPayloadTransformer(Resource xslResource, ResultTransformer resultTransformer) { this(xslResource, resultTransformer, null); } - - public XsltPayloadTransformer(Resource xslResource, ResultTransformer resultTransformer, String transformerFactoryClass) throws Exception { - this(getTransformerFactory(transformerFactoryClass).newTemplates(createStreamSourceOnResource(xslResource)), resultTransformer); + + public XsltPayloadTransformer(Resource xslResource, String transformerFactoryClassName) { + Assert.notNull(xslResource, "'xslResource' must not be null."); + Assert.hasText(transformerFactoryClassName, "'transformerFactoryClassName' must not be empty String."); + this.xslResource = xslResource; + this.transformerFactoryClassName = transformerFactoryClassName; + this.resultTransformer = null; } - public XsltPayloadTransformer(Resource xslResource, String transformerFactoryClass) throws Exception { - this(getTransformerFactory(transformerFactoryClass).newTemplates(createStreamSourceOnResource(xslResource)), null); - } + public XsltPayloadTransformer(Resource xslResource, ResultTransformer resultTransformer, String transformerFactoryClassName) { + Assert.notNull(xslResource, "'xslResource' must not be null."); + this.xslResource = xslResource; + this.resultTransformer = resultTransformer; + this.transformerFactoryClassName = transformerFactoryClassName; + } - public XsltPayloadTransformer(Templates templates, ResultTransformer resultTransformer) throws ParserConfigurationException { + public XsltPayloadTransformer(Templates templates, ResultTransformer resultTransformer) { + Assert.notNull(templates, "'templates' must not be null."); this.templates = templates; this.resultTransformer = resultTransformer; } @@ -168,16 +178,32 @@ public class XsltPayloadTransformer extends AbstractTransformer { this.xsltParamHeaders = xsltParamHeaders; } + @Override + public void setBeanClassLoader(ClassLoader classLoader) { + Assert.notNull(classLoader, "'beanClassLoader' must not be null."); + this.classLoader = classLoader; + } + + @Override public String getComponentType() { return "xml:xslt-transformer"; } - @Override protected void onInit() throws Exception { super.onInit(); this.evaluationContext = ExpressionUtils.createStandardEvaluationContext(this.getBeanFactory()); + if (this.templates == null) { + TransformerFactory transformerFactory; + if (this.transformerFactoryClassName != null) { + transformerFactory = TransformerFactory.newInstance(this.transformerFactoryClassName, this.classLoader); + } + else { + transformerFactory = TransformerFactory.newInstance(); + } + this.templates = transformerFactory.newTemplates(createStreamSourceOnResource(this.xslResource)); + } } @Override @@ -276,6 +302,7 @@ public class XsltPayloadTransformer extends AbstractTransformer { return (Document) domResult.getNode(); } + private Transformer buildTransformer(Message message) throws TransformerException { // process individual mappings Transformer transformer = this.templates.newTransformer(); @@ -308,7 +335,6 @@ public class XsltPayloadTransformer extends AbstractTransformer { return transformer; } - /** * Compensate for the fact that a Resource may not be a File or even * addressable through a URI. If it is, we want the created StreamSource to @@ -324,21 +350,5 @@ public class XsltPayloadTransformer extends AbstractTransformer { return new StreamSource(xslResource.getInputStream()); } } - - private static TransformerFactory getTransformerFactory(String transformerFactoryImplClass) { - TransformerFactory transformerFactory = null; - - if (transformerFactoryImplClass == null || transformerFactoryImplClass.length() <= 0) { - transformerFactory = TransformerFactory.newInstance(); - } else if(ClassUtils.isPresent(transformerFactoryImplClass, ClassLoader.getSystemClassLoader())) { - transformerFactory = TransformerFactory.newInstance(transformerFactoryImplClass, ClassLoader.getSystemClassLoader()); - } else { - if (logger.isWarnEnabled()) { - logger.warn(String.format("Class [%s] was not found will default to [%s], please ensure [%s] is located on your class path", transformerFactoryImplClass, TransformerFactory.class, transformerFactoryImplClass)); - } - transformerFactory = TransformerFactory.newInstance(); - } - - return transformerFactory; - } + } diff --git a/spring-integration-xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-2.2.xsd b/spring-integration-xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-2.2.xsd index 61f37d1a3f..58c6ebff3a 100644 --- a/spring-integration-xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-2.2.xsd +++ b/spring-integration-xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-2.2.xsd @@ -237,19 +237,6 @@ - - - - - - - - - - diff --git a/spring-integration-xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-3.0.xsd b/spring-integration-xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-3.0.xsd index ae605e70ca..3ccf9a8344 100644 --- a/spring-integration-xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-3.0.xsd +++ b/spring-integration-xml/src/main/resources/org/springframework/integration/xml/config/spring-integration-xml-3.0.xsd @@ -237,6 +237,21 @@ + + + + + + + + + + + + diff --git a/spring-integration-xml/src/test/java/org/springframework/integration/xml/transformer/XsltPayloadTransformerTests.java b/spring-integration-xml/src/test/java/org/springframework/integration/xml/transformer/XsltPayloadTransformerTests.java index 20c93aedd9..460db25ea7 100644 --- a/spring-integration-xml/src/test/java/org/springframework/integration/xml/transformer/XsltPayloadTransformerTests.java +++ b/spring-integration-xml/src/test/java/org/springframework/integration/xml/transformer/XsltPayloadTransformerTests.java @@ -16,16 +16,19 @@ package org.springframework.integration.xml.transformer; -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertTrue; import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import javax.xml.transform.Result; import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactoryConfigurationError; import javax.xml.transform.dom.DOMResult; import org.junit.Before; import org.junit.Test; +import org.w3c.dom.Document; + import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; @@ -36,7 +39,6 @@ import org.springframework.integration.xml.result.StringResultFactory; import org.springframework.integration.xml.util.XmlTestUtil; import org.springframework.xml.transform.StringResult; import org.springframework.xml.transform.StringSource; -import org.w3c.dom.Document; /** @@ -56,6 +58,7 @@ public class XsltPayloadTransformerTests { @Before public void setUp() throws Exception { transformer = new XsltPayloadTransformer(getXslResource()); + transformer.afterPropertiesSet(); } @Test @@ -108,32 +111,30 @@ public class XsltPayloadTransformerTests { Integer returnValue = new Integer(13); transformer = new XsltPayloadTransformer(getXslResource(), new StubResultTransformer(returnValue)); + transformer.afterPropertiesSet(); Object transformed = transformer .doTransform(buildMessage(new StringSource(docAsString))); assertEquals("Wrong value from result conversion", returnValue, transformed); } - + @Test public void testXsltPayloadWithTransformerFactoryClassname() throws Exception { Integer returnValue = new Integer(13); transformer = new XsltPayloadTransformer(getXslResource(), new StubResultTransformer(returnValue), "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl"); + transformer.afterPropertiesSet(); Object transformed = transformer .doTransform(buildMessage(new StringSource(docAsString))); assertEquals("Wrong value from result conversion", returnValue, transformed); } - - @Test + + @Test(expected = TransformerFactoryConfigurationError.class) public void testXsltPayloadWithBadTransformerFactoryClassname() throws Exception { - Integer returnValue = new Integer(13); - transformer = new XsltPayloadTransformer(getXslResource(), new StubResultTransformer(returnValue), - "foo.bar.Baz"); - Object transformed = transformer - .doTransform(buildMessage(new StringSource(docAsString))); - assertEquals("Wrong value from result conversion", returnValue, - transformed); + transformer = new XsltPayloadTransformer(getXslResource(), "foo.bar.Baz"); + transformer.afterPropertiesSet(); + transformer.doTransform(buildMessage(new StringSource(docAsString))); } @Test(expected = TransformerException.class) @@ -151,8 +152,9 @@ public class XsltPayloadTransformerTests { Resource resource = new ClassPathResource("transform-with-import.xsl", this.getClass()); transformer = new XsltPayloadTransformer(resource); + transformer.afterPropertiesSet(); assertEquals(transformer.doTransform(buildMessage(docAsString)), - outputAsString); + outputAsString); } @@ -163,6 +165,7 @@ public class XsltPayloadTransformerTests { transformer = new XsltPayloadTransformer(resource); transformer.setResultFactory(new StringResultFactory()); transformer.setAlwaysUseResultFactory(true); + transformer.afterPropertiesSet(); Object returned = transformer.doTransform(buildMessage(XmlTestUtil.getDocumentForString(docAsString))); assertEquals("Wrong type of return ", StringResult.class, returned.getClass()); } @@ -175,6 +178,7 @@ public class XsltPayloadTransformerTests { transformer = new XsltPayloadTransformer(resource); transformer.setResultFactory(new StringResultFactory()); transformer.setAlwaysUseResultFactory(true); + transformer.afterPropertiesSet(); Object returned = transformer.doTransform(buildMessage(XmlTestUtil.getDocumentForString(docAsString))); assertEquals("Wrong type of return ", StringResult.class, returned.getClass()); } @@ -184,6 +188,7 @@ public class XsltPayloadTransformerTests { transformer = new XsltPayloadTransformer(getXslResourceThatOutputsText()); transformer.setResultFactory(new StringResultFactory()); transformer.setAlwaysUseResultFactory(true); + transformer.afterPropertiesSet(); Object returned = transformer.doTransform(buildMessage(XmlTestUtil.getDocumentForString(docAsString))); assertEquals("Wrong type of return ", StringResult.class, returned.getClass()); assertEquals("Wrong content in string", "hello world", returned.toString()); From 5617d2e2d84c78e466b9a00d1daf979328b1b449 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Wed, 25 Sep 2013 17:43:33 -0400 Subject: [PATCH 3/3] INT-2768 Polishing - Docs, White Space Add what's new; fix white space in test case. --- .../XsltPayloadTransformerTests.java | 276 +++++++++--------- src/reference/docbook/whats-new.xml | 7 + src/reference/docbook/xml.xml | 9 + 3 files changed, 154 insertions(+), 138 deletions(-) diff --git a/spring-integration-xml/src/test/java/org/springframework/integration/xml/transformer/XsltPayloadTransformerTests.java b/spring-integration-xml/src/test/java/org/springframework/integration/xml/transformer/XsltPayloadTransformerTests.java index 460db25ea7..7bee4335b5 100644 --- a/spring-integration-xml/src/test/java/org/springframework/integration/xml/transformer/XsltPayloadTransformerTests.java +++ b/spring-integration-xml/src/test/java/org/springframework/integration/xml/transformer/XsltPayloadTransformerTests.java @@ -5,7 +5,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -49,176 +49,176 @@ import org.springframework.xml.transform.StringSource; */ public class XsltPayloadTransformerTests { - private XsltPayloadTransformer transformer; + private XsltPayloadTransformer transformer; - private String docAsString = "test"; + private final String docAsString = "test"; - private String outputAsString = "test"; + private final String outputAsString = "test"; - @Before - public void setUp() throws Exception { - transformer = new XsltPayloadTransformer(getXslResource()); + @Before + public void setUp() throws Exception { + transformer = new XsltPayloadTransformer(getXslResource()); transformer.afterPropertiesSet(); - } + } - @Test - public void testDocumentAsPayload() throws Exception { - Object transformed = transformer.doTransform(buildMessage(XmlTestUtil - .getDocumentForString(docAsString))); - assertTrue("Wrong return type for document payload", Document.class - .isAssignableFrom(transformed.getClass())); - Document transformedDocument = (Document) transformed; - assertXMLEqual(outputAsString, XmlTestUtil - .docToString(transformedDocument)); - } + @Test + public void testDocumentAsPayload() throws Exception { + Object transformed = transformer.doTransform(buildMessage(XmlTestUtil + .getDocumentForString(docAsString))); + assertTrue("Wrong return type for document payload", Document.class + .isAssignableFrom(transformed.getClass())); + Document transformedDocument = (Document) transformed; + assertXMLEqual(outputAsString, XmlTestUtil + .docToString(transformedDocument)); + } - @Test - public void testSourceAsPayload() throws Exception { - Object transformed = transformer - .doTransform(buildMessage(new StringSource(docAsString))); - assertEquals("Wrong return type for source payload", DOMResult.class, - transformed.getClass()); - DOMResult result = (DOMResult) transformed; - assertXMLEqual("Document incorrect after transformation", XmlTestUtil - .getDocumentForString(outputAsString), (Document) result - .getNode()); - } + @Test + public void testSourceAsPayload() throws Exception { + Object transformed = transformer + .doTransform(buildMessage(new StringSource(docAsString))); + assertEquals("Wrong return type for source payload", DOMResult.class, + transformed.getClass()); + DOMResult result = (DOMResult) transformed; + assertXMLEqual("Document incorrect after transformation", XmlTestUtil + .getDocumentForString(outputAsString), (Document) result + .getNode()); + } - @Test - public void testStringAsPayload() throws Exception { - Object transformed = transformer.doTransform(buildMessage(docAsString)); - assertEquals("Wrong return type for string payload", String.class, - transformed.getClass()); - String transformedString = (String) transformed; - assertXMLEqual("String incorrect after transform", outputAsString, - transformedString); - } + @Test + public void testStringAsPayload() throws Exception { + Object transformed = transformer.doTransform(buildMessage(docAsString)); + assertEquals("Wrong return type for string payload", String.class, + transformed.getClass()); + String transformedString = (String) transformed; + assertXMLEqual("String incorrect after transform", outputAsString, + transformedString); + } - @Test - public void testStringAsPayloadUseResultFactoryTrue() throws Exception { - transformer.setAlwaysUseResultFactory(true); - Object transformed = transformer.doTransform(buildMessage(docAsString)); - assertEquals("Wrong return type for useFactories true", - DOMResult.class, transformed.getClass()); - DOMResult result = (DOMResult) transformed; - assertXMLEqual("Document incorrect after transformation", XmlTestUtil - .getDocumentForString(outputAsString), (Document) result - .getNode()); - } + @Test + public void testStringAsPayloadUseResultFactoryTrue() throws Exception { + transformer.setAlwaysUseResultFactory(true); + Object transformed = transformer.doTransform(buildMessage(docAsString)); + assertEquals("Wrong return type for useFactories true", + DOMResult.class, transformed.getClass()); + DOMResult result = (DOMResult) transformed; + assertXMLEqual("Document incorrect after transformation", XmlTestUtil + .getDocumentForString(outputAsString), (Document) result + .getNode()); + } - @Test - public void testSourceWithResultTransformer() throws Exception { - Integer returnValue = new Integer(13); - transformer = new XsltPayloadTransformer(getXslResource(), - new StubResultTransformer(returnValue)); + @Test + public void testSourceWithResultTransformer() throws Exception { + Integer returnValue = new Integer(13); + transformer = new XsltPayloadTransformer(getXslResource(), + new StubResultTransformer(returnValue)); transformer.afterPropertiesSet(); - Object transformed = transformer - .doTransform(buildMessage(new StringSource(docAsString))); - assertEquals("Wrong value from result conversion", returnValue, - transformed); - } + Object transformed = transformer + .doTransform(buildMessage(new StringSource(docAsString))); + assertEquals("Wrong value from result conversion", returnValue, + transformed); + } - @Test - public void testXsltPayloadWithTransformerFactoryClassname() throws Exception { - Integer returnValue = new Integer(13); - transformer = new XsltPayloadTransformer(getXslResource(), new StubResultTransformer(returnValue), - "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl"); + @Test + public void testXsltPayloadWithTransformerFactoryClassname() throws Exception { + Integer returnValue = new Integer(13); + transformer = new XsltPayloadTransformer(getXslResource(), new StubResultTransformer(returnValue), + "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl"); transformer.afterPropertiesSet(); - Object transformed = transformer - .doTransform(buildMessage(new StringSource(docAsString))); - assertEquals("Wrong value from result conversion", returnValue, - transformed); - } + Object transformed = transformer + .doTransform(buildMessage(new StringSource(docAsString))); + assertEquals("Wrong value from result conversion", returnValue, + transformed); + } - @Test(expected = TransformerFactoryConfigurationError.class) - public void testXsltPayloadWithBadTransformerFactoryClassname() throws Exception { - transformer = new XsltPayloadTransformer(getXslResource(), "foo.bar.Baz"); + @Test(expected = TransformerFactoryConfigurationError.class) + public void testXsltPayloadWithBadTransformerFactoryClassname() throws Exception { + transformer = new XsltPayloadTransformer(getXslResource(), "foo.bar.Baz"); transformer.afterPropertiesSet(); - transformer.doTransform(buildMessage(new StringSource(docAsString))); - } + transformer.doTransform(buildMessage(new StringSource(docAsString))); + } - @Test(expected = TransformerException.class) - public void testNonXmlString() throws Exception { - transformer.doTransform(buildMessage("test")); - } + @Test(expected = TransformerException.class) + public void testNonXmlString() throws Exception { + transformer.doTransform(buildMessage("test")); + } - @Test(expected = MessagingException.class) - public void testUnsupportedPayloadType() throws Exception { - transformer.doTransform(buildMessage(new Long(12))); - } + @Test(expected = MessagingException.class) + public void testUnsupportedPayloadType() throws Exception { + transformer.doTransform(buildMessage(new Long(12))); + } - @Test - public void testXsltWithImports() throws Exception { - Resource resource = new ClassPathResource("transform-with-import.xsl", - this.getClass()); - transformer = new XsltPayloadTransformer(resource); + @Test + public void testXsltWithImports() throws Exception { + Resource resource = new ClassPathResource("transform-with-import.xsl", + this.getClass()); + transformer = new XsltPayloadTransformer(resource); transformer.afterPropertiesSet(); - assertEquals(transformer.doTransform(buildMessage(docAsString)), + assertEquals(transformer.doTransform(buildMessage(docAsString)), outputAsString); - } + } - @Test - public void documentInStringResultOut() throws Exception { - Resource resource = new ClassPathResource("transform-with-import.xsl", - this.getClass()); - transformer = new XsltPayloadTransformer(resource); - transformer.setResultFactory(new StringResultFactory()); - transformer.setAlwaysUseResultFactory(true); + @Test + public void documentInStringResultOut() throws Exception { + Resource resource = new ClassPathResource("transform-with-import.xsl", + this.getClass()); + transformer = new XsltPayloadTransformer(resource); + transformer.setResultFactory(new StringResultFactory()); + transformer.setAlwaysUseResultFactory(true); transformer.afterPropertiesSet(); - Object returned = transformer.doTransform(buildMessage(XmlTestUtil.getDocumentForString(docAsString))); - assertEquals("Wrong type of return ", StringResult.class, returned.getClass()); - } + Object returned = transformer.doTransform(buildMessage(XmlTestUtil.getDocumentForString(docAsString))); + assertEquals("Wrong type of return ", StringResult.class, returned.getClass()); + } - @Test - public void stringInDomResultOut() throws Exception { - Resource resource = new ClassPathResource("transform-with-import.xsl", - this.getClass()); - transformer = new XsltPayloadTransformer(resource); - transformer.setResultFactory(new StringResultFactory()); - transformer.setAlwaysUseResultFactory(true); + @Test + public void stringInDomResultOut() throws Exception { + Resource resource = new ClassPathResource("transform-with-import.xsl", + this.getClass()); + transformer = new XsltPayloadTransformer(resource); + transformer.setResultFactory(new StringResultFactory()); + transformer.setAlwaysUseResultFactory(true); transformer.afterPropertiesSet(); - Object returned = transformer.doTransform(buildMessage(XmlTestUtil.getDocumentForString(docAsString))); - assertEquals("Wrong type of return ", StringResult.class, returned.getClass()); - } + Object returned = transformer.doTransform(buildMessage(XmlTestUtil.getDocumentForString(docAsString))); + assertEquals("Wrong type of return ", StringResult.class, returned.getClass()); + } - @Test - public void docInStringOut() throws Exception { - transformer = new XsltPayloadTransformer(getXslResourceThatOutputsText()); - transformer.setResultFactory(new StringResultFactory()); - transformer.setAlwaysUseResultFactory(true); + @Test + public void docInStringOut() throws Exception { + transformer = new XsltPayloadTransformer(getXslResourceThatOutputsText()); + transformer.setResultFactory(new StringResultFactory()); + transformer.setAlwaysUseResultFactory(true); transformer.afterPropertiesSet(); - Object returned = transformer.doTransform(buildMessage(XmlTestUtil.getDocumentForString(docAsString))); - assertEquals("Wrong type of return ", StringResult.class, returned.getClass()); - assertEquals("Wrong content in string", "hello world", returned.toString()); - } + Object returned = transformer.doTransform(buildMessage(XmlTestUtil.getDocumentForString(docAsString))); + assertEquals("Wrong type of return ", StringResult.class, returned.getClass()); + assertEquals("Wrong content in string", "hello world", returned.toString()); + } - protected Message buildMessage(Object payload) { - return MessageBuilder.withPayload(payload).build(); - } + protected Message buildMessage(Object payload) { + return MessageBuilder.withPayload(payload).build(); + } - private Resource getXslResource() throws Exception { - String xsl = "test"; - return new ByteArrayResource(xsl.getBytes("UTF-8")); - } + private Resource getXslResource() throws Exception { + String xsl = "test"; + return new ByteArrayResource(xsl.getBytes("UTF-8")); + } - private Resource getXslResourceThatOutputsText() throws Exception { - String xsl = "hello world"; - return new ByteArrayResource(xsl.getBytes("UTF-8")); - } + private Resource getXslResourceThatOutputsText() throws Exception { + String xsl = "hello world"; + return new ByteArrayResource(xsl.getBytes("UTF-8")); + } - public static class StubResultTransformer implements ResultTransformer { + public static class StubResultTransformer implements ResultTransformer { - private Object objectToReturn; + private final Object objectToReturn; - public StubResultTransformer(Object objectToReturn) { - this.objectToReturn = objectToReturn; - } + public StubResultTransformer(Object objectToReturn) { + this.objectToReturn = objectToReturn; + } - public Object transformResult(Result result) { - return objectToReturn; - } - } + public Object transformResult(Result result) { + return objectToReturn; + } + } } diff --git a/src/reference/docbook/whats-new.xml b/src/reference/docbook/whats-new.xml index 444a45bcde..c3e1a0eee4 100644 --- a/src/reference/docbook/whats-new.xml +++ b/src/reference/docbook/whats-new.xml @@ -372,6 +372,13 @@ For more information see . +
+ XsltPayloadTransformer + + You can now specify the transformer factory class name using the + transformer-factory-class attribute. See + +
Message Headers and TCP diff --git a/src/reference/docbook/xml.xml b/src/reference/docbook/xml.xml index 813e86ac18..427d4c0723 100644 --- a/src/reference/docbook/xml.xml +++ b/src/reference/docbook/xml.xml @@ -463,6 +463,11 @@ ]]> + + Starting with Spring Integration 3.0, you can now specify the transformer factory class name using + a constructor argument. This is configured using the transformer-factory-class + attribute when using the namespace. +
ResultTransformers @@ -640,6 +645,10 @@ Xslt parameters could now be mapped to any accessible part of the Message as well as any literal value. + + Starting with Spring Integration 3.0, you can now specify the transformer factory class name using + the transformer-factory-class attribute. +
Namespace Configuration and ResultTransformers