fixed typos and formatting

This commit is contained in:
Mark Fisher
2010-07-21 16:41:25 +00:00
parent 4875b14062
commit af6bd18145
6 changed files with 191 additions and 206 deletions

View File

@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.xml.transformer;
import java.util.HashMap;
@@ -26,32 +27,27 @@ import org.springframework.integration.core.MessageHeaders;
/**
* {@link TransformerConfigurer} instance which looks for headers and uses them
* to configure the provided {@link Transformer} instance. For example a header
* named 'xslt_paramter_X' will cause the transformer to be configured with a
* named 'xslt_parameter_X' will cause the transformer to be configured with a
* property named 'X' with the value of the header. A property named
* 'xslt_output_property_X' will cause an output property on the transformer to be
* set with this header's value.
*
*
* @author Jonas Partner
*
*/
public class DefaultTransformerConfigurer implements TransformerConfigurer {
public void configureTransfomer(Message<?> message, Transformer transformer) {
public void configureTransformer(Message<?> message, Transformer transformer) {
Map<String,Object> parameters = extractParameterHeaders(message.getHeaders());
for(String paramName: parameters.keySet()){
for (String paramName: parameters.keySet()) {
transformer.setParameter(paramName, parameters.get(paramName));
}
Map<String, String> outputProperties = extractOutputPropertyHeaders(message.getHeaders());
for(String outputPropertyName : outputProperties.keySet()){
for (String outputPropertyName : outputProperties.keySet()) {
transformer.setOutputProperty(outputPropertyName, outputProperties.get(outputPropertyName));
}
}
protected Map<String, String> extractOutputPropertyHeaders(
MessageHeaders headers) {
protected Map<String, String> extractOutputPropertyHeaders(MessageHeaders headers) {
Map<String, String> parameters = new HashMap<String, String>();
int prefixStringLength = XsltHeaders.OUTPUT_PROPERTY.length();
for (String key : headers.keySet()) {
@@ -59,11 +55,11 @@ public class DefaultTransformerConfigurer implements TransformerConfigurer {
Object headerValue = headers.get(key);
if (!(headerValue instanceof String)) {
throw new IllegalArgumentException(
"Xslt Transfomer only supports String output properties received header of type"
"XSLT Transformer only supports String output properties received header of type "
+ headerValue.getClass().getName()
+ " for header named " + key);
}
parameters.put(key.substring(prefixStringLength), (String)headerValue);
parameters.put(key.substring(prefixStringLength), (String) headerValue);
}
}
return parameters;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2010 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.
@@ -21,19 +21,15 @@ import javax.xml.transform.Transformer;
import org.springframework.integration.core.Message;
/**
* Allows customistation of the transformer based on the recevied message prior
* to transformation
* Allows customization of the transformer based on the received message prior to transformation.
*
* @author Jonas Partner
*
*/
public interface TransformerConfigurer {
/**
* Callback method called by Xslt transfomer implementations after transformer is creates
* @param message
* @param transformer
* Callback method called by XSLT transformer implementations after transformer is created.
*/
public void configureTransfomer(Message<?> message, Transformer transformer);
public void configureTransformer(Message<?> message, Transformer transformer);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2010 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.
@@ -20,19 +20,16 @@ import javax.xml.transform.Transformer;
/**
* Message headers that can be used to configure the {@link Transformer}
* instance used for Xsl transformation
* instance used for XSL transformation.
*
* @author Jonas Partner
*
*/
public abstract class XsltHeaders {
public static final String PREFIX = "xslt_";
public static final String OUTPUT_PROPERTY = PREFIX + "output_property_";
public static final String PARAMETER = PREFIX + "parameter_";
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2010 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.
@@ -31,7 +31,7 @@ import org.w3c.dom.Document;
import org.springframework.core.io.Resource;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessagingException;
import org.springframework.integration.core.MessagingException;
import org.springframework.integration.transformer.AbstractTransformer;
import org.springframework.integration.xml.result.DomResultFactory;
import org.springframework.integration.xml.result.ResultFactory;
@@ -44,170 +44,163 @@ import org.springframework.xml.transform.StringSource;
import java.io.IOException;
/**
* Thread safe XSLT transformer implementation which returns a transformed {@link Source},
* {@link Document}, or {@link String}. If alwaysUseSourceResultFactories is
* false (default) the following logic occurs
* Thread safe XSLT transformer implementation which returns a transformed
* {@link Source}, {@link Document}, or {@link String}. If
* alwaysUseSourceResultFactories is false (default) the following logic occurs
* <p/>
* {@link String} payload in results in {@link String} payload out
* <p/>
* {@link Document} payload in {@link Document} payload out
* {@link Document} payload in results in {@link Document} payload out
* <p/>
* {@link Source} payload in {@link Result} payload out, type will be determined
* by the {@link ResultFactory}, {@link DomResultFactory} by default. If an
* instance of {@link ResultTransformer} is registered this will be used to
* convert the result.
* {@link Source} payload in results in {@link Result} payload out, type will be
* determined by the {@link ResultFactory}, {@link DomResultFactory} by default.
* If an instance of {@link ResultTransformer} is registered this will be used
* to convert the result.
* <p/>
* If alwaysUseSourceResultFactories is true then the ResultFactory and
* {@link SourceFactory} will be used to create the {@link Source} from the
* payload and the {@link Result} to pass into the transformer. An instance of
* {@link ResultTransformer} can also be provided to convert the Result prior to
* returning
*
* returning.
*
* @author Jonas Partner
* @author Mark Fisher
*/
public class XsltPayloadTransformer extends AbstractTransformer {
private final Templates templates;
private final Templates templates;
private final ResultTransformer resultTransformer;
private final ResultTransformer resultTransformer;
private volatile SourceFactory sourceFactory = new DomSourceFactory();
private volatile SourceFactory sourceFactory = new DomSourceFactory();
private volatile ResultFactory resultFactory = new DomResultFactory();
private volatile ResultFactory resultFactory = new DomResultFactory();
private volatile boolean alwaysUseSourceResultFactories = false;
private volatile TransformerConfigurer transformerConfigurer = new DefaultTransformerConfigurer();
private volatile boolean alwaysUseSourceResultFactories = false;
private volatile TransformerConfigurer transformerConfigurer = new DefaultTransformerConfigurer();
public XsltPayloadTransformer(Templates templates) throws ParserConfigurationException {
this(templates, null);
}
public XsltPayloadTransformer(Templates templates) throws ParserConfigurationException {
this(templates, null);
}
public XsltPayloadTransformer(Templates templates,
ResultTransformer resultTransformer)
throws ParserConfigurationException {
this.templates = templates;
this.resultTransformer = resultTransformer;
}
public XsltPayloadTransformer(Templates templates, ResultTransformer resultTransformer) throws ParserConfigurationException {
this.templates = templates;
this.resultTransformer = resultTransformer;
}
public XsltPayloadTransformer(Resource xslResource) throws Exception {
this(TransformerFactory.newInstance().newTemplates(
createStreamSourceOnResource(xslResource)), null);
}
public XsltPayloadTransformer(Resource xslResource) throws Exception {
this(TransformerFactory.newInstance().newTemplates(createStreamSourceOnResource(xslResource)), null);
}
public XsltPayloadTransformer(Resource xslResource, ResultTransformer resultTransformer) throws Exception {
this(TransformerFactory.newInstance().newTemplates(
createStreamSourceOnResource(xslResource)), resultTransformer);
}
/**
* Compensate for the fact that a Resource <i>may</i> not be a File or even addressable through a URI.
* If it is, we want the created StreamSource to read other resources relative to the provided one, if it
* isn't, it loads from the default path.
*/
private static StreamSource createStreamSourceOnResource(Resource xslResource) throws IOException {
try {
String systemId = xslResource.getURI().toString();
return new StreamSource(xslResource.getInputStream(), systemId);
} catch (IOException e) {
return new StreamSource(xslResource.getInputStream());
}
}
public XsltPayloadTransformer(Resource xslResource, ResultTransformer resultTransformer) throws Exception {
this(TransformerFactory.newInstance().newTemplates(createStreamSourceOnResource(xslResource)), resultTransformer);
}
/**
* @param sourceFactory
*/
public void setSourceFactory(SourceFactory sourceFactory) {
Assert.notNull(sourceFactory, "SourceFactory can not be null");
this.sourceFactory = sourceFactory;
}
/**
* Compensate for the fact that a Resource <i>may</i> not be a File or even
* addressable through a URI. If it is, we want the created StreamSource to
* read other resources relative to the provided one. If it isn't, it loads
* from the default path.
*/
private static StreamSource createStreamSourceOnResource(Resource xslResource) throws IOException {
try {
String systemId = xslResource.getURI().toString();
return new StreamSource(xslResource.getInputStream(), systemId);
}
catch (IOException e) {
return new StreamSource(xslResource.getInputStream());
}
}
/**
* @param resultFactory
*/
public void setResultFactory(ResultFactory resultFactory) {
Assert.notNull(sourceFactory, "ResultFactory can not be null");
this.resultFactory = resultFactory;
}
/**
* Sets the SourceFactory.
*/
public void setSourceFactory(SourceFactory sourceFactory) {
Assert.notNull(sourceFactory, "SourceFactory can not be null");
this.sourceFactory = sourceFactory;
}
/**
* Forces use of {@link ResultFactory} and {@link SourceFactory} even for
* directly supported payloads such as {@link String} and {@link Document}
*
* @param alwaysUserSourceResultFactories
*
*/
public void setAlwaysUseSourceResultFactories(
boolean alwaysUserSourceResultFactories) {
this.alwaysUseSourceResultFactories = alwaysUserSourceResultFactories;
}
/**
* Sets the ResultFactory
*/
public void setResultFactory(ResultFactory resultFactory) {
Assert.notNull(sourceFactory, "ResultFactory can not be null");
this.resultFactory = resultFactory;
}
/**
* Specifies whether {@link ResultFactory} and {@link SourceFactory} should always
* be used, even for directly supported payloads such as {@link String} and {@link Document}.
*/
public void setAlwaysUseSourceResultFactories(boolean alwaysUserSourceResultFactories) {
this.alwaysUseSourceResultFactories = alwaysUserSourceResultFactories;
}
@Override
protected Object doTransform(Message<?> message) throws Exception {
Transformer transformer = buildTransformer(message);
Object payload = message.getPayload();
Object transformedPayload = null;
if (this.alwaysUseSourceResultFactories) {
transformedPayload = transformUsingFactories(payload, transformer);
} else if (payload instanceof String) {
transformedPayload = transformString((String) payload, transformer);
} else if (payload instanceof Document) {
transformedPayload = transformDocument((Document) payload, transformer);
} else if (payload instanceof Source) {
transformedPayload = transformSource((Source) payload, payload, transformer);
} else {
// fall back to trying factories
transformedPayload = transformUsingFactories(payload, transformer);
}
return transformedPayload;
}
Object transformedPayload = null;
if (this.alwaysUseSourceResultFactories) {
transformedPayload = transformUsingFactories(payload, transformer);
}
else if (payload instanceof String) {
transformedPayload = transformString((String) payload, transformer);
}
else if (payload instanceof Document) {
transformedPayload = transformDocument((Document) payload, transformer);
}
else if (payload instanceof Source) {
transformedPayload = transformSource((Source) payload, payload, transformer);
}
else {
// fall back to trying factories
transformedPayload = transformUsingFactories(payload, transformer);
}
return transformedPayload;
}
protected Object transformUsingFactories(Object payload, Transformer transformer) throws TransformerException {
Source source = sourceFactory.createSource(payload);
return transformSource(source, payload, transformer);
}
protected Object transformUsingFactories(Object payload, Transformer transformer) throws TransformerException {
Source source = sourceFactory.createSource(payload);
return transformSource(source, payload, transformer);
}
protected Object transformSource(Source source, Object payload, Transformer transformer) throws TransformerException {
Result result = resultFactory.createResult(payload);
transformer.transform(source, result);
protected Object transformSource(Source source, Object payload, Transformer transformer) throws TransformerException {
Result result = resultFactory.createResult(payload);
transformer.transform(source, result);
if (resultTransformer != null) {
return resultTransformer.transformResult(result);
}
return result;
}
if (resultTransformer != null) {
return resultTransformer.transformResult(result);
}
return result;
}
protected String transformString(String stringPayload, Transformer transformer) throws TransformerException {
StringResult result = new StringResult();
transformer.transform(
new StringSource(stringPayload), result);
return result.toString();
}
protected Document transformDocument(Document documentPayload, Transformer transformer) throws TransformerException {
DOMSource source = new DOMSource(documentPayload);
Result result = resultFactory.createResult(documentPayload);
if (!DOMResult.class.isAssignableFrom(result.getClass())) {
throw new MessagingException(
"Document to Document conversion requires a DOMResult-producing ResultFactory implementation");
}
DOMResult domResult = (DOMResult) result;
transformer.transform(source, domResult);
return (Document) domResult.getNode();
}
protected Transformer buildTransformer(Message<?> message) throws TransformerException{
Transformer transformer = this.templates.newTransformer();
if(this.transformerConfigurer != null){
this.transformerConfigurer.configureTransfomer(message, transformer);
}
return transformer;
}
protected String transformString(String stringPayload, Transformer transformer) throws TransformerException {
StringResult result = new StringResult();
transformer.transform(new StringSource(stringPayload), result);
return result.toString();
}
protected Document transformDocument(Document documentPayload, Transformer transformer) throws TransformerException {
DOMSource source = new DOMSource(documentPayload);
Result result = resultFactory.createResult(documentPayload);
if (!DOMResult.class.isAssignableFrom(result.getClass())) {
throw new MessagingException(
"Document to Document conversion requires a DOMResult-producing ResultFactory implementation.");
}
DOMResult domResult = (DOMResult) result;
transformer.transform(source, domResult);
return (Document) domResult.getNode();
}
protected Transformer buildTransformer(Message<?> message) throws TransformerException {
Transformer transformer = this.templates.newTransformer();
if (this.transformerConfigurer != null) {
this.transformerConfigurer.configureTransformer(message, transformer);
}
return transformer;
}
}

View File

@@ -1,3 +1,19 @@
/*
* Copyright 2002-2010 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.
* You may obtain a copy of the License at
*
* 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,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.xml.transformer;
import static org.junit.Assert.*;
@@ -18,7 +34,10 @@ import org.junit.Test;
import org.springframework.integration.core.Message;
import org.springframework.integration.message.MessageBuilder;
public class DefaultTransformerConfiguerTests {
/**
* @author Jonas Partner
*/
public class DefaultTransformerConfigurerTests {
StubTransformer transformer;
@@ -30,18 +49,16 @@ public class DefaultTransformerConfiguerTests {
this.transformerConfigurer = new DefaultTransformerConfigurer();
}
@Test
public void testSettingParametersAndOutputProperties(){
Message<String> testMessage = MessageBuilder.withPayload("test")
.setHeader("xslt_parameter_headerOne",1)
.setHeader("xslt_parameter_headerTwo", "string")
.setHeader("xslt_output_property_outOne","1")
.setHeader("xslt_output_property_outTwo","2")
.build();
transformerConfigurer.configureTransfomer(testMessage, transformer);
.setHeader("xslt_parameter_headerOne",1)
.setHeader("xslt_parameter_headerTwo", "string")
.setHeader("xslt_output_property_outOne","1")
.setHeader("xslt_output_property_outTwo","2")
.build();
transformerConfigurer.configureTransformer(testMessage, transformer);
Object paramOne = transformer.getParameter("headerOne");
assertEquals("Wrong value for headerOne parameter",1, paramOne);
@@ -53,97 +70,78 @@ public class DefaultTransformerConfiguerTests {
String outPropertyTwo = transformer.getOutputProperty("outTwo");
assertEquals("Wrong value for headerTwo parameter","2", outPropertyTwo);
}
@Test(expected = IllegalArgumentException.class)
public void testNonStringOutputPropertyHeader(){
public void testNonStringOutputPropertyHeader() {
Message<String> testMessage = MessageBuilder.withPayload("test")
.setHeader("xslt_output_property_outOne",12)
.build();
transformerConfigurer.configureTransfomer(testMessage, transformer);
.setHeader("xslt_output_property_outOne",12)
.build();
transformerConfigurer.configureTransformer(testMessage, transformer);
}
private static class StubTransformer extends Transformer{
Map<String,Object> paramterMap = new HashMap<String, Object>();
Map<String,Object> parameterMap = new HashMap<String, Object>();
Map<String, String> outputProperties = new HashMap<String, String>();
@Override
public void clearParameters() {
paramterMap.clear();
parameterMap.clear();
}
@Override
public ErrorListener getErrorListener() {
// TODO Auto-generated method stub
return null;
}
@Override
public Properties getOutputProperties() {
// TODO Auto-generated method stub
return null;
}
@Override
public String getOutputProperty(String name)
throws IllegalArgumentException {
public String getOutputProperty(String name) throws IllegalArgumentException {
return outputProperties.get(name);
}
@Override
public Object getParameter(String name) {
return paramterMap.get(name);
return parameterMap.get(name);
}
@Override
public URIResolver getURIResolver() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setErrorListener(ErrorListener listener)
throws IllegalArgumentException {
// TODO Auto-generated method stub
public void setErrorListener(ErrorListener listener) throws IllegalArgumentException {
}
@Override
public void setOutputProperties(Properties oformat) {
// TODO Auto-generated method stub
}
@Override
public void setOutputProperty(String name, String value)
throws IllegalArgumentException {
public void setOutputProperty(String name, String value) throws IllegalArgumentException {
outputProperties.put(name, value);
}
@Override
public void setParameter(String name, Object value) {
paramterMap.put(name, value);
parameterMap.put(name, value);
}
@Override
public void setURIResolver(URIResolver resolver) {
// TODO Auto-generated method stub
}
@Override
public void transform(Source xmlSource, Result outputTarget)
throws TransformerException {
// TODO Auto-generated method stub
public void transform(Source xmlSource, Result outputTarget) throws TransformerException {
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 the original author or authors.
* Copyright 2002-2010 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.
@@ -13,8 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.xml.transformer;
package org.springframework.integration.xml.transformer;
import static junit.framework.Assert.assertTrue;
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
@@ -29,16 +29,21 @@ import org.springframework.integration.core.MessagingException;
import org.springframework.integration.xml.util.XmlTestUtil;
import org.springframework.xml.transform.StringResult;
public class ResultToStringTransfomerTests {
/**
* @author Jonas Partner
*/
public class ResultToStringTransformerTests {
ResultToStringTransformer transformer;
private ResultToStringTransformer transformer;
private String doc = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>";
@Before
public void setUp(){
public void setUp() {
transformer = new ResultToStringTransformer();
}
@Test
public void testWithDomResult() throws Exception {
DOMResult result = XmlTestUtil.getDomResultForString(doc);