Removed MessageTransformer due to the redundancy with MessageHandler (now that Messages are immutable). Refactored existing transformers to implement PayloadTransformer instead. All message handling is now the responsibility of the delegating PayloadTransformingMessageHandler. Also added the AbstractPaylaodTransformerParser from which all existing PayloadTransformer parsers now extend.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* Copyright 2002-2008 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.
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.integration.xml.config;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
@@ -27,12 +29,9 @@ import org.springframework.integration.xml.router.XPathSingleChannelNameResolver
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.xml.xpath.XPathExpression;
|
||||
import org.springframework.xml.xpath.XPathExpressionFactory;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
*/
|
||||
public class XPathRouterParser extends AbstractSingleBeanDefinitionParser {
|
||||
|
||||
@@ -48,14 +47,13 @@ public class XPathRouterParser extends AbstractSingleBeanDefinitionParser {
|
||||
|
||||
@Override
|
||||
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
|
||||
|
||||
boolean multiChannel = Boolean.parseBoolean(element.getAttribute("multi-channel"));
|
||||
boolean resolutionRequired = Boolean.parseBoolean(element.getAttribute("resolution-required"));
|
||||
String xPathExpression = element.getAttribute("xpath-expression");
|
||||
String xPathExpressionRef = element.getAttribute("xpath-expression-ref");
|
||||
if ((StringUtils.hasText(xPathExpression) && StringUtils.hasText(xPathExpressionRef))
|
||||
|| (!StringUtils.hasText(xPathExpression) && !StringUtils.hasText(xPathExpressionRef))) {
|
||||
throw new ConfigurationException("Exactl one of xpath-expression or xpath-expression-ref is required");
|
||||
throw new ConfigurationException("Exactly one of 'xpath-expression' or 'xpath-expression-ref' is required.");
|
||||
}
|
||||
|
||||
BeanDefinitionBuilder resolverDefinitionBuilder = null;
|
||||
@@ -77,10 +75,8 @@ public class XPathRouterParser extends AbstractSingleBeanDefinitionParser {
|
||||
else {
|
||||
resolverDefinitionBuilder.addConstructorArgReference(xPathExpressionRef);
|
||||
}
|
||||
|
||||
builder.getBeanDefinition().getPropertyValues().addPropertyValue("resolutionRequired", resolutionRequired);
|
||||
builder.getBeanDefinition().getPropertyValues().addPropertyValue("channelNameResolver",
|
||||
resolverDefinitionBuilder.getBeanDefinition());
|
||||
builder.addPropertyValue("resolutionRequired", resolutionRequired);
|
||||
builder.addPropertyValue("channelNameResolver", resolverDefinitionBuilder.getBeanDefinition());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* Copyright 2002-2008 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.
|
||||
@@ -16,53 +16,40 @@
|
||||
|
||||
package org.springframework.integration.xml.config;
|
||||
|
||||
import org.springframework.beans.factory.config.RuntimeBeanReference;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.transformer.PayloadTransformer;
|
||||
import org.springframework.integration.transformer.config.AbstractPayloadTransformerParser;
|
||||
import org.springframework.integration.xml.result.DomResultFactory;
|
||||
import org.springframework.integration.xml.result.StringResultFactory;
|
||||
import org.springframework.integration.xml.transformer.XmlPayloadMarshallingTransformer;
|
||||
import org.springframework.util.Assert;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class XmlMarshallingTransformerParser extends AbstractSingleBeanDefinitionParser {
|
||||
public class XmlMarshallingTransformerParser extends AbstractPayloadTransformerParser {
|
||||
|
||||
@Override
|
||||
protected boolean shouldGenerateId() {
|
||||
return false;
|
||||
protected Class<? extends PayloadTransformer<?, ?>> getTransformerClass() {
|
||||
return XmlPayloadMarshallingTransformer.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldGenerateIdAsFallback() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
|
||||
String resourceFactory = element.getAttribute("result-factory");
|
||||
protected void parsePayloadTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
|
||||
String resultFactory = element.getAttribute("result-factory");
|
||||
String marshaller = element.getAttribute("marshaller");
|
||||
|
||||
Assert.hasText(marshaller, "A unmarshaller attribute is required");
|
||||
|
||||
Assert.hasText(resourceFactory, "A result-factory attribute is required");
|
||||
|
||||
builder.getBeanDefinition().setBeanClass(XmlPayloadMarshallingTransformer.class);
|
||||
|
||||
builder.getBeanDefinition().getConstructorArgumentValues().addGenericArgumentValue(
|
||||
new RuntimeBeanReference(marshaller));
|
||||
|
||||
if (resourceFactory.equals("DOMResult")) {
|
||||
builder.getBeanDefinition().getPropertyValues().addPropertyValue("resultFactory", new DomResultFactory());
|
||||
Assert.hasText(marshaller, "the 'marshaller' attribute is required");
|
||||
Assert.hasText(resultFactory, "the 'result-factory' attribute is required");
|
||||
builder.addConstructorArgReference(marshaller);
|
||||
if (resultFactory.equals("DOMResult")) {
|
||||
builder.addPropertyValue("resultFactory", new DomResultFactory());
|
||||
}
|
||||
else if (resourceFactory.equals("StringResult")) {
|
||||
builder.getBeanDefinition().getPropertyValues()
|
||||
.addPropertyValue("resultFactory", new StringResultFactory());
|
||||
else if (resultFactory.equals("StringResult")) {
|
||||
builder.addPropertyValue("resultFactory", new StringResultFactory());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,35 +18,29 @@ package org.springframework.integration.xml.config;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import org.springframework.beans.factory.config.RuntimeBeanReference;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.transformer.PayloadTransformer;
|
||||
import org.springframework.integration.transformer.config.AbstractPayloadTransformerParser;
|
||||
import org.springframework.integration.xml.transformer.XmlPayloadUnmarshallingTransformer;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* @author Jonas Partner
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class XmlUnmarshallingTransformerParser extends AbstractSingleBeanDefinitionParser {
|
||||
public class XmlUnmarshallingTransformerParser extends AbstractPayloadTransformerParser {
|
||||
|
||||
@Override
|
||||
protected boolean shouldGenerateId() {
|
||||
return false;
|
||||
protected Class<? extends PayloadTransformer<?, ?>> getTransformerClass() {
|
||||
return XmlPayloadUnmarshallingTransformer.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldGenerateIdAsFallback() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
|
||||
protected void parsePayloadTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
|
||||
String unmarshaller = element.getAttribute("unmarshaller");
|
||||
Assert.hasText(unmarshaller, "A unmarshaller attribute is required");
|
||||
builder.getBeanDefinition().setBeanClass(XmlPayloadUnmarshallingTransformer.class);
|
||||
builder.getBeanDefinition().getConstructorArgumentValues().addGenericArgumentValue(
|
||||
new RuntimeBeanReference(unmarshaller));
|
||||
Assert.hasText(unmarshaller, "the 'unmarshaller' attribute is required");
|
||||
builder.addConstructorArgReference(unmarshaller);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* Copyright 2002-2008 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.
|
||||
@@ -16,63 +16,50 @@
|
||||
|
||||
package org.springframework.integration.xml.config;
|
||||
|
||||
import org.springframework.beans.factory.config.RuntimeBeanReference;
|
||||
import org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.integration.transformer.PayloadTransformer;
|
||||
import org.springframework.integration.transformer.config.AbstractPayloadTransformerParser;
|
||||
import org.springframework.integration.xml.transformer.XsltPayloadTransformer;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class XsltPayloadTransformerParser extends AbstractSingleBeanDefinitionParser {
|
||||
public class XsltPayloadTransformerParser extends AbstractPayloadTransformerParser {
|
||||
|
||||
@Override
|
||||
protected boolean shouldGenerateId() {
|
||||
return false;
|
||||
protected Class<? extends PayloadTransformer<?, ?>> getTransformerClass() {
|
||||
return XsltPayloadTransformer.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean shouldGenerateIdAsFallback() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
|
||||
protected void parsePayloadTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
|
||||
String xslResource = element.getAttribute("xsl-resource");
|
||||
String xslTemplates = element.getAttribute("xsl-templates");
|
||||
|
||||
boolean bothHaveText = StringUtils.hasText(xslResource) && StringUtils.hasText(xslTemplates);
|
||||
boolean oneHasText = StringUtils.hasText(xslResource) || StringUtils.hasText(xslTemplates);
|
||||
|
||||
Assert.state(!bothHaveText && oneHasText,
|
||||
"Exaclty one of xsl-resource or xsl-templates should be specified");
|
||||
"Exactly one of 'xsl-resource' or 'xsl-templates' is required.");
|
||||
|
||||
builder.getBeanDefinition().setBeanClass(XsltPayloadTransformer.class);
|
||||
|
||||
|
||||
if(StringUtils.hasText(xslResource)){
|
||||
builder.getBeanDefinition().getConstructorArgumentValues()
|
||||
.addGenericArgumentValue(new ValueHolder(xslResource));
|
||||
} else if (StringUtils.hasText(xslTemplates)){
|
||||
builder.getBeanDefinition().getConstructorArgumentValues()
|
||||
.addGenericArgumentValue(new RuntimeBeanReference(xslTemplates));
|
||||
if (StringUtils.hasText(xslResource)) {
|
||||
builder.addConstructorArgValue(xslResource);
|
||||
}
|
||||
|
||||
else if (StringUtils.hasText(xslTemplates)) {
|
||||
builder.addConstructorArgReference(xslTemplates);
|
||||
}
|
||||
|
||||
String sourceFactory = element.getAttribute("source-factory");
|
||||
if(StringUtils.hasText(sourceFactory)){
|
||||
builder.getBeanDefinition().getPropertyValues().addPropertyValue("sourceFactory", new RuntimeBeanReference(sourceFactory));
|
||||
if (StringUtils.hasText(sourceFactory)) {
|
||||
builder.addPropertyReference("sourceFactory", sourceFactory);
|
||||
}
|
||||
|
||||
String resultFactory = element.getAttribute("result-factory");
|
||||
if(StringUtils.hasText(resultFactory)){
|
||||
builder.getBeanDefinition().getPropertyValues().addPropertyValue("resultFactory", new RuntimeBeanReference(resultFactory));
|
||||
if (StringUtils.hasText(resultFactory)) {
|
||||
builder.addPropertyReference("resultFactory", resultFactory);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,16 +19,12 @@ package org.springframework.integration.xml.result;
|
||||
import javax.xml.transform.Result;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
*/
|
||||
public class DomResultFactory implements ResultFactory {
|
||||
|
||||
public Result getNewResult(Message<?> message) {
|
||||
public Result createResult(Object payload) {
|
||||
return new DOMResult();
|
||||
}
|
||||
|
||||
|
||||
@@ -18,16 +18,14 @@ package org.springframework.integration.xml.result;
|
||||
|
||||
import javax.xml.transform.Result;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
|
||||
/**
|
||||
* Factory to create {@link ResultFactory} possibly taking into account the
|
||||
* passed {@link Message} instance
|
||||
* @author Jonas Partner
|
||||
* Factory to create a {@link Result} possibly taking into account the
|
||||
* provided message payload instance.
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*/
|
||||
public interface ResultFactory {
|
||||
|
||||
Result getNewResult(Message<?> message);
|
||||
Result createResult(Object payload);
|
||||
|
||||
}
|
||||
|
||||
@@ -18,17 +18,14 @@ package org.springframework.integration.xml.result;
|
||||
|
||||
import javax.xml.transform.Result;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.xml.transform.StringResult;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
*/
|
||||
public class StringResultFactory implements ResultFactory {
|
||||
|
||||
public Result getNewResult(Message<?> message) {
|
||||
public Result createResult(Object payload) {
|
||||
return new StringResult();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* Copyright 2002-2008 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.
|
||||
@@ -18,27 +18,27 @@ package org.springframework.integration.xml.source;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
import javax.sound.sampled.SourceDataLine;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
|
||||
/**
|
||||
* {@link SourceDataLine} implementation which supports creation of a
|
||||
* {@link DOMSource} from a {@link Document} or {@link String} payload
|
||||
* {@link SourceFactory} implementation which supports creation of a
|
||||
* {@link DOMSource} from a {@link Document} or {@link String} payload.
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class DomSourceFactory implements SourceFactory {
|
||||
|
||||
private final DocumentBuilderFactory docBuilderFactory;
|
||||
|
||||
|
||||
public DomSourceFactory() {
|
||||
this.docBuilderFactory = DocumentBuilderFactory.newInstance();
|
||||
}
|
||||
@@ -47,31 +47,31 @@ public class DomSourceFactory implements SourceFactory {
|
||||
this.docBuilderFactory = docBuilderFactory;
|
||||
}
|
||||
|
||||
public Source getSourceForMessage(Message<?> message) {
|
||||
if (Document.class.isAssignableFrom(message.getPayload().getClass())) {
|
||||
return createDomSourceForDocument(message);
|
||||
|
||||
public Source createSource(Object payload) {
|
||||
if (Document.class.isAssignableFrom(payload.getClass())) {
|
||||
return createDomSourceForDocument((Document) payload);
|
||||
}
|
||||
else if (message.getPayload() instanceof String) {
|
||||
return createDomSourceForString(message);
|
||||
else if (payload instanceof String) {
|
||||
return createDomSourceForString((String) payload);
|
||||
}
|
||||
throw new MessagingException(message, "Could not create Source for payload type "
|
||||
+ message.getPayload().getClass().getName());
|
||||
throw new MessagingException("Failed to create Source for payload type ["
|
||||
+ payload.getClass().getName() + "]");
|
||||
}
|
||||
|
||||
protected DOMSource createDomSourceForDocument(Message<?> message) {
|
||||
DOMSource source = new DOMSource(((Document) message.getPayload()).getDocumentElement());
|
||||
protected DOMSource createDomSourceForDocument(Document document) {
|
||||
DOMSource source = new DOMSource(document.getDocumentElement());
|
||||
return source;
|
||||
}
|
||||
|
||||
protected DOMSource createDomSourceForString(Message<?> message) {
|
||||
protected DOMSource createDomSourceForString(String s) {
|
||||
try {
|
||||
String str = (String) message.getPayload();
|
||||
Document doc = docBuilderFactory.newDocumentBuilder().parse(new InputSource(new StringReader(str)));
|
||||
Document doc = docBuilderFactory.newDocumentBuilder().parse(new InputSource(new StringReader(s)));
|
||||
DOMSource source = new DOMSource(doc.getDocumentElement());
|
||||
return source;
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new MessagingException(message, "Exception creating DOMSource", e);
|
||||
throw new MessagingException("Exception creating DOMSource", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,17 +18,14 @@ package org.springframework.integration.xml.source;
|
||||
|
||||
import javax.xml.transform.Source;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
|
||||
/**
|
||||
* Factory to create instances of {@link Source} possibly taking into account
|
||||
* the contents of the passed {@link Message}
|
||||
* Factory to create a {@link Source} possibly taking into account
|
||||
* the provided message payload instance.
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
*/
|
||||
public interface SourceFactory {
|
||||
|
||||
Source getSourceForMessage(Message<?> message);
|
||||
Source createSource(Object payload);
|
||||
|
||||
}
|
||||
|
||||
@@ -27,10 +27,8 @@ import javax.xml.transform.dom.DOMResult;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
import org.springframework.integration.transformer.MessageTransformer;
|
||||
import org.springframework.integration.transformer.PayloadTransformer;
|
||||
import org.springframework.xml.transform.StringResult;
|
||||
|
||||
/**
|
||||
@@ -38,7 +36,7 @@ import org.springframework.xml.transform.StringResult;
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*/
|
||||
public class ResultToDocumentTransformer implements MessageTransformer {
|
||||
public class ResultToDocumentTransformer implements PayloadTransformer<Result, Document> {
|
||||
|
||||
// Not guaranteed to be thread safe
|
||||
private final DocumentBuilderFactory documentBuilderFactory;
|
||||
@@ -54,39 +52,37 @@ public class ResultToDocumentTransformer implements MessageTransformer {
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Message<?> transform(Message message) {
|
||||
public Document transform(Result payload) {
|
||||
Document doc = null;
|
||||
if (DOMResult.class.isAssignableFrom(message.getPayload().getClass())) {
|
||||
doc = createDocumentFromDomResult(message, (DOMResult) message.getPayload());
|
||||
if (DOMResult.class.isAssignableFrom(payload.getClass())) {
|
||||
doc = createDocumentFromDomResult((DOMResult) payload);
|
||||
}
|
||||
else if (StringResult.class.isAssignableFrom(message.getPayload().getClass())) {
|
||||
doc = createDocumentFromStringResult(message, (StringResult) message.getPayload());
|
||||
else if (StringResult.class.isAssignableFrom(payload.getClass())) {
|
||||
doc = createDocumentFromStringResult((StringResult) payload);
|
||||
}
|
||||
else {
|
||||
throw new MessagingException(message, "Failed to create document from payload type ["
|
||||
+ message.getPayload().getClass().getName() + "]");
|
||||
throw new MessagingException("Failed to create document from payload type ["
|
||||
+ payload.getClass().getName() + "]");
|
||||
}
|
||||
return new GenericMessage<Document>(doc, message.getHeaders());
|
||||
return doc;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Document createDocumentFromDomResult(Message message, DOMResult domResult) {
|
||||
protected Document createDocumentFromDomResult(DOMResult domResult) {
|
||||
return (Document) domResult.getNode();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Document createDocumentFromStringResult(Message message, StringResult stringResult) {
|
||||
protected Document createDocumentFromStringResult(StringResult stringResult) {
|
||||
try {
|
||||
return getDocumentBuilder().parse(new InputSource(new StringReader(stringResult.toString())));
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new MessagingException(message, "Exception creating Document form StringResult payload", e);
|
||||
throw new MessagingException("Failed to create Document from StringResult payload", e);
|
||||
}
|
||||
}
|
||||
|
||||
protected synchronized DocumentBuilder getDocumentBuilder() {
|
||||
try {
|
||||
return documentBuilderFactory.newDocumentBuilder();
|
||||
return this.documentBuilderFactory.newDocumentBuilder();
|
||||
}
|
||||
catch (ParserConfigurationException e) {
|
||||
throw new MessagingException("Failed to create a new DocumentBuilder", e);
|
||||
|
||||
@@ -18,19 +18,17 @@ package org.springframework.integration.xml.transformer;
|
||||
|
||||
import javax.xml.transform.Source;
|
||||
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.transformer.MessageTransformer;
|
||||
import org.springframework.integration.transformer.PayloadTransformer;
|
||||
import org.springframework.integration.xml.source.DomSourceFactory;
|
||||
import org.springframework.integration.xml.source.SourceFactory;
|
||||
|
||||
/**
|
||||
* Transforms the payload to a {@link Source} using a {@link SourceFactory}.
|
||||
* Defaults to using a {@link DomSourceFactory} if alternative is not provided.
|
||||
* Defaults to using a {@link DomSourceFactory} if an alternative is not provided.
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*/
|
||||
public class SourceCreatingTransformer implements MessageTransformer {
|
||||
public class SourceCreatingTransformer implements PayloadTransformer<Object, Source> {
|
||||
|
||||
private final SourceFactory sourceFactory;
|
||||
|
||||
@@ -43,9 +41,9 @@ public class SourceCreatingTransformer implements MessageTransformer {
|
||||
this.sourceFactory = sourceFactory;
|
||||
}
|
||||
|
||||
public Message<?> transform(Message<?> message) {
|
||||
Source source = this.sourceFactory.getSourceForMessage(message);
|
||||
return new GenericMessage<Source>(source, message.getHeaders());
|
||||
|
||||
public Source transform(Object payload) {
|
||||
return this.sourceFactory.createSource(payload);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,24 +20,21 @@ import java.io.IOException;
|
||||
|
||||
import javax.xml.transform.Result;
|
||||
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
import org.springframework.integration.transformer.PayloadTransformer;
|
||||
import org.springframework.integration.xml.result.DomResultFactory;
|
||||
import org.springframework.integration.xml.result.ResultFactory;
|
||||
import org.springframework.oxm.Marshaller;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageHandlingException;
|
||||
import org.springframework.integration.transformer.MessageTransformer;
|
||||
import org.springframework.integration.xml.result.DomResultFactory;
|
||||
import org.springframework.integration.xml.result.ResultFactory;
|
||||
|
||||
/**
|
||||
* An implementation of {@link MessageTransformer} that delegates to an OXM
|
||||
* An implementation of {@link PayloadTransformer} that delegates to an OXM
|
||||
* {@link Marshaller}.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @author Jonas Partner
|
||||
*/
|
||||
public class XmlPayloadMarshallingTransformer implements MessageTransformer {
|
||||
public class XmlPayloadMarshallingTransformer implements PayloadTransformer<Object, Object> {
|
||||
|
||||
private final Marshaller marshaller;
|
||||
|
||||
@@ -55,26 +52,23 @@ public class XmlPayloadMarshallingTransformer implements MessageTransformer {
|
||||
this.resultFactory = resultFactory;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Message<?> transform(Message<?> message) {
|
||||
public Object transform(Object payload) {
|
||||
Object transformedPayload = null;
|
||||
Object originalPayload = message.getPayload();
|
||||
Result result = this.resultFactory.getNewResult(message);
|
||||
Result result = this.resultFactory.createResult(payload);
|
||||
if (result == null) {
|
||||
throw new MessageHandlingException(message, "Unable to marshall payload, ResultFactory returned null");
|
||||
throw new MessagingException("Unable to marshal payload, ResultFactory returned null.");
|
||||
}
|
||||
try {
|
||||
this.marshaller.marshal(originalPayload, result);
|
||||
this.marshaller.marshal(payload, result);
|
||||
transformedPayload = result;
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new MessageHandlingException(message, "failed to marshall payload", e);
|
||||
throw new MessagingException("Failed to marshal payload", e);
|
||||
}
|
||||
|
||||
if (transformedPayload == null) {
|
||||
throw new MessageHandlingException(message, "failed to transform payload");
|
||||
throw new MessagingException("Failed to transform payload");
|
||||
}
|
||||
return new GenericMessage(transformedPayload, message.getHeaders());
|
||||
return transformedPayload;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,23 +20,21 @@ import java.io.IOException;
|
||||
|
||||
import javax.xml.transform.Source;
|
||||
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
import org.springframework.integration.transformer.MessageTransformer;
|
||||
import org.springframework.integration.transformer.PayloadTransformer;
|
||||
import org.springframework.integration.xml.source.DomSourceFactory;
|
||||
import org.springframework.integration.xml.source.SourceFactory;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
|
||||
/**
|
||||
* An implementation of {@link MessageTransformer} that delegates to an OXM
|
||||
* An implementation of {@link PayloadTransformer} that delegates to an OXM
|
||||
* {@link Unmarshaller}. Expects the payload to be of type {@link Source} or to
|
||||
* have an instance of {@link SourceFactory} that can convert to a
|
||||
* {@link Source}.
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*/
|
||||
public class XmlPayloadUnmarshallingTransformer implements MessageTransformer {
|
||||
public class XmlPayloadUnmarshallingTransformer implements PayloadTransformer<Object, Object> {
|
||||
|
||||
private final Unmarshaller unmarshaller;
|
||||
|
||||
@@ -52,27 +50,25 @@ public class XmlPayloadUnmarshallingTransformer implements MessageTransformer {
|
||||
this.sourceFactory = sourceFactory;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Message<?> transform(Message<?> message) {
|
||||
public Object transform(Object payload) {
|
||||
Source source = null;
|
||||
if (Source.class.isAssignableFrom(message.getPayload().getClass())) {
|
||||
source = (Source) message.getPayload();
|
||||
if (Source.class.isAssignableFrom(payload.getClass())) {
|
||||
source = (Source) payload;
|
||||
}
|
||||
else if (this.sourceFactory != null) {
|
||||
source = this.sourceFactory.getSourceForMessage(message);
|
||||
source = this.sourceFactory.createSource(payload);
|
||||
}
|
||||
|
||||
if (source == null) {
|
||||
throw new MessagingException(message,
|
||||
"Could not transform message, payload not assignable from javax.xml.transform.Source and no conversion possible");
|
||||
throw new MessagingException(
|
||||
"Failed to transform message, payload not assignable from javax.xml.transform.Source and no conversion possible");
|
||||
}
|
||||
|
||||
try {
|
||||
Object unmarshalled = this.unmarshaller.unmarshal(source);
|
||||
return new GenericMessage(unmarshalled, message.getHeaders());
|
||||
return this.unmarshaller.unmarshal(source);
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new MessagingException(message, "Failed to unamrshal payload", e);
|
||||
throw new MessagingException("Failed to unamrshal payload", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,10 +26,7 @@ import javax.xml.transform.stream.StreamSource;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
import org.springframework.integration.transformer.MessageTransformer;
|
||||
import org.springframework.integration.transformer.PayloadTransformer;
|
||||
import org.springframework.integration.xml.result.DomResultFactory;
|
||||
import org.springframework.integration.xml.result.ResultFactory;
|
||||
import org.springframework.integration.xml.source.DomSourceFactory;
|
||||
@@ -40,8 +37,9 @@ import org.springframework.integration.xml.source.SourceFactory;
|
||||
* {@link Source}, {@link Document}, or {@link String}.
|
||||
*
|
||||
* @author Jonas Partner
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class XsltPayloadTransformer implements MessageTransformer {
|
||||
public class XsltPayloadTransformer implements PayloadTransformer<Object, Result> {
|
||||
|
||||
private final Templates templates;
|
||||
|
||||
@@ -59,27 +57,6 @@ public class XsltPayloadTransformer implements MessageTransformer {
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Message<Result> transform(Message message) {
|
||||
try {
|
||||
if (Source.class.isAssignableFrom(message.getPayload().getClass())) {
|
||||
return this.transformSource(message, (Source) message.getPayload());
|
||||
}
|
||||
Source source = this.sourceFactory.getSourceForMessage(message);
|
||||
return this.transformSource(message, source);
|
||||
}
|
||||
catch (TransformerException e) {
|
||||
throw new MessagingException(message, "XSLT transformation failed", e);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Message<Result> transformSource(Message message, Source source) throws TransformerException {
|
||||
Result result = resultFactory.getNewResult(message);
|
||||
this.templates.newTransformer().transform(source, result);
|
||||
return new GenericMessage<Result>(result, message.getHeaders());
|
||||
}
|
||||
|
||||
public void setSourceFactory(SourceFactory sourceFactory) {
|
||||
this.sourceFactory = sourceFactory;
|
||||
}
|
||||
@@ -88,4 +65,18 @@ public class XsltPayloadTransformer implements MessageTransformer {
|
||||
this.resultFactory = resultFactory;
|
||||
}
|
||||
|
||||
public Result transform(Object payload) throws TransformerException {
|
||||
if (Source.class.isAssignableFrom(payload.getClass())) {
|
||||
return this.transformSource((Source) payload);
|
||||
}
|
||||
Source source = this.sourceFactory.createSource(payload);
|
||||
return this.transformSource(source);
|
||||
}
|
||||
|
||||
protected Result transformSource(Source source) throws TransformerException {
|
||||
Result result = resultFactory.createResult(source);
|
||||
this.templates.newTransformer().transform(source, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* Copyright 2002-2008 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.
|
||||
@@ -24,18 +24,17 @@ import javax.xml.transform.dom.DOMResult;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.transformer.MessageTransformer;
|
||||
import org.springframework.xml.transform.StringResult;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.handler.MessageHandler;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.xml.transform.StringResult;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
*/
|
||||
public class XmlMarshallingTransformerParserTests {
|
||||
|
||||
@@ -43,16 +42,16 @@ public class XmlMarshallingTransformerParserTests {
|
||||
|
||||
|
||||
@Before
|
||||
public void setUp(){
|
||||
public void setUp() {
|
||||
this.appContext = new ClassPathXmlApplicationContext("XmlMarshallingTransformerParserTests-context.xml", getClass());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDefault() throws Exception {
|
||||
MessageTransformer transformer = (MessageTransformer) appContext.getBean("marshallingTransfomerNoResultFactory");
|
||||
MessageHandler transformer = (MessageHandler) appContext.getBean("marshallingTransfomerNoResultFactory");
|
||||
GenericMessage<Object> message = new GenericMessage<Object>("hello");
|
||||
Message<?> result = transformer.transform(message);
|
||||
Message<?> result = transformer.handle(message);
|
||||
assertTrue("Wrong payload type", result.getPayload() instanceof DOMResult);
|
||||
Document doc = (Document) ((DOMResult) result.getPayload()).getNode();
|
||||
assertEquals("Wrong palyoad", "hello", doc.getDocumentElement().getTextContent());
|
||||
@@ -60,9 +59,9 @@ public class XmlMarshallingTransformerParserTests {
|
||||
|
||||
@Test
|
||||
public void testDOMResult() throws Exception {
|
||||
MessageTransformer transformer = (MessageTransformer) appContext.getBean("marshallingTransfomerDOMResultFactory");
|
||||
MessageHandler transformer = (MessageHandler) appContext.getBean("marshallingTransfomerDOMResultFactory");
|
||||
GenericMessage<Object> message = new GenericMessage<Object>("hello");
|
||||
Message<?> result = transformer.transform(message);
|
||||
Message<?> result = transformer.handle(message);
|
||||
assertTrue("Wrong payload type ", result.getPayload() instanceof DOMResult);
|
||||
Document doc = (Document) ((DOMResult) result.getPayload()).getNode();
|
||||
assertEquals("Wrong palyoad", "hello", doc.getDocumentElement().getTextContent());
|
||||
@@ -70,9 +69,9 @@ public class XmlMarshallingTransformerParserTests {
|
||||
|
||||
@Test
|
||||
public void testStringResult() throws Exception {
|
||||
MessageTransformer transformer = (MessageTransformer) appContext.getBean("marshallingTransfomerStringResultFactory");
|
||||
MessageHandler transformer = (MessageHandler) appContext.getBean("marshallingTransfomerStringResultFactory");
|
||||
GenericMessage<Object> message = new GenericMessage<Object>("hello");
|
||||
Message<?> result = transformer.transform(message);
|
||||
Message<?> result = transformer.handle(message);
|
||||
assertTrue("Wrong payload type", result.getPayload() instanceof StringResult);
|
||||
}
|
||||
|
||||
|
||||
@@ -26,10 +26,10 @@ import org.junit.Test;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.handler.MessageHandler;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
import org.springframework.integration.transformer.MessageTransformer;
|
||||
import org.springframework.integration.xml.util.XmlTestUtil;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
|
||||
@@ -38,9 +38,9 @@ import org.springframework.xml.transform.StringSource;
|
||||
*/
|
||||
public class XmlUnmarshallingTransformerParserTests {
|
||||
|
||||
ApplicationContext appContext;
|
||||
private ApplicationContext appContext;
|
||||
|
||||
StubUnmarshaller unmarshaller;
|
||||
private StubUnmarshaller unmarshaller;
|
||||
|
||||
|
||||
@Before
|
||||
@@ -53,41 +53,41 @@ public class XmlUnmarshallingTransformerParserTests {
|
||||
|
||||
@Test
|
||||
public void testDefaultUnmarshall() throws Exception {
|
||||
MessageTransformer transformer = (MessageTransformer) appContext.getBean("defaultUnmarshaller");
|
||||
MessageHandler transformer = (MessageHandler) appContext.getBean("defaultUnmarshaller");
|
||||
GenericMessage<Object> message = new GenericMessage<Object>(new StringSource(
|
||||
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>"));
|
||||
Message<?> result = transformer.transform(message);
|
||||
Message<?> result = transformer.handle(message);
|
||||
assertEquals("Wrong payload after unmarshalling", "unmarshalled", result.getPayload());
|
||||
assertTrue("Wrong source passed to unmarshaller", unmarshaller.sourcesPassed.poll() instanceof StringSource);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnmarshallString() throws Exception {
|
||||
MessageTransformer transformer = (MessageTransformer) appContext.getBean("defaultUnmarshaller");
|
||||
MessageHandler transformer = (MessageHandler) appContext.getBean("defaultUnmarshaller");
|
||||
GenericMessage<Object> message = new GenericMessage<Object>(
|
||||
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>");
|
||||
Message<?> result = transformer.transform(message);
|
||||
Message<?> result = transformer.handle(message);
|
||||
assertEquals("Wrong payload after unmarshalling", "unmarshalled", result.getPayload());
|
||||
assertTrue("Wrong source passed to unmarshaller", unmarshaller.sourcesPassed.poll() instanceof DOMSource);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUnmarshallDocument() throws Exception {
|
||||
MessageTransformer transformer = (MessageTransformer) appContext.getBean("defaultUnmarshaller");
|
||||
MessageHandler transformer = (MessageHandler) appContext.getBean("defaultUnmarshaller");
|
||||
GenericMessage<Object> message = new GenericMessage<Object>(
|
||||
XmlTestUtil
|
||||
.getDocumentForString("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>"));
|
||||
Message<?> result = transformer.transform(message);
|
||||
Message<?> result = transformer.handle(message);
|
||||
assertEquals("Wrong payload after unmarshalling", "unmarshalled", result.getPayload());
|
||||
assertTrue("Wrong source passed to unmarshaller", unmarshaller.sourcesPassed.poll() instanceof DOMSource);
|
||||
}
|
||||
|
||||
@Test(expected = MessagingException.class)
|
||||
public void testUnmarshallUnsupported() throws Exception {
|
||||
MessageTransformer transformer = (MessageTransformer) appContext.getBean("defaultUnmarshaller");
|
||||
MessageHandler transformer = (MessageHandler) appContext.getBean("defaultUnmarshaller");
|
||||
GenericMessage<Object> message = new GenericMessage<Object>(new StringBuffer(
|
||||
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>"));
|
||||
transformer.transform(message);
|
||||
transformer.handle(message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/integration-xml http://www.springframework.org/schema/integration/spring-integration-xml-1.0.xsd">
|
||||
|
||||
<si-xml:xslt-transformer id="xsltTransfomerWithResource"
|
||||
<si-xml:xslt-transformer id="xsltTransformerWithResource"
|
||||
xsl-resource="org/springframework/integration/xml/config/test.xsl" />
|
||||
|
||||
|
||||
|
||||
@@ -28,9 +28,9 @@ import org.w3c.dom.Document;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.handler.MessageHandler;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.transformer.MessageTransformer;
|
||||
import org.springframework.integration.xml.util.XmlTestUtil;
|
||||
|
||||
/**
|
||||
@@ -38,9 +38,9 @@ import org.springframework.integration.xml.util.XmlTestUtil;
|
||||
*/
|
||||
public class XsltPayloadTransformerParserTests {
|
||||
|
||||
String doc = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>";
|
||||
private String doc = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>";
|
||||
|
||||
ApplicationContext applicationContext;
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
|
||||
@Before
|
||||
@@ -51,10 +51,10 @@ public class XsltPayloadTransformerParserTests {
|
||||
|
||||
@Test
|
||||
public void testWithResourceProvided() throws Exception {
|
||||
MessageTransformer messageTransformer = (MessageTransformer) applicationContext
|
||||
.getBean("xsltTransfomerWithResource");
|
||||
MessageHandler messageTransformer = (MessageHandler) applicationContext
|
||||
.getBean("xsltTransformerWithResource");
|
||||
GenericMessage<Object> message = new GenericMessage<Object>(XmlTestUtil.getDomSourceForString(doc));
|
||||
Message<?> result = messageTransformer.transform(message);
|
||||
Message<?> result = messageTransformer.handle(message);
|
||||
assertTrue("Payload was not a DOMResult", result.getPayload() instanceof DOMResult);
|
||||
Document doc = (Document) ((DOMResult) result.getPayload()).getNode();
|
||||
assertEquals("Wrong payload", "test", doc.getDocumentElement().getTextContent());
|
||||
@@ -62,10 +62,10 @@ public class XsltPayloadTransformerParserTests {
|
||||
|
||||
@Test
|
||||
public void testWithTemplatesProvided() throws Exception {
|
||||
MessageTransformer messageTransformer = (MessageTransformer) applicationContext
|
||||
MessageHandler messageTransformer = (MessageHandler) applicationContext
|
||||
.getBean("xsltTransformerWithTemplates");
|
||||
GenericMessage<Object> message = new GenericMessage<Object>(XmlTestUtil.getDomSourceForString(doc));
|
||||
Message<?> result = messageTransformer.transform(message);
|
||||
Message<?> result = messageTransformer.handle(message);
|
||||
assertTrue("Payload was not a DOMResult", result.getPayload() instanceof DOMResult);
|
||||
Document doc = (Document) ((DOMResult) result.getPayload()).getNode();
|
||||
assertEquals("Wrong payload", "test", doc.getDocumentElement().getTextContent());
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* Copyright 2002-2008 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.
|
||||
@@ -28,16 +28,14 @@ import javax.xml.transform.dom.DOMSource;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
import org.springframework.xml.transform.StringResult;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
import org.springframework.xml.transform.StringResult;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*
|
||||
*/
|
||||
public class DomSourceFactoryTests {
|
||||
|
||||
@@ -56,8 +54,7 @@ public class DomSourceFactoryTests {
|
||||
|
||||
@Test
|
||||
public void testWithDocumentPayload() throws Exception {
|
||||
GenericMessage<Object> message = new GenericMessage<Object>(doc);
|
||||
Source source = sourceFactory.getSourceForMessage(message);
|
||||
Source source = sourceFactory.createSource(doc);
|
||||
assertNotNull("Returned source was null", source);
|
||||
assertEquals("Expected DOMSource", DOMSource.class, source.getClass());
|
||||
assertEquals("Wrong content in source ", docContent, getAsString(source));
|
||||
@@ -65,8 +62,7 @@ public class DomSourceFactoryTests {
|
||||
|
||||
@Test
|
||||
public void testWithStringPayload() throws Exception {
|
||||
GenericMessage<Object> message = new GenericMessage<Object>(docContent);
|
||||
Source source = sourceFactory.getSourceForMessage(message);
|
||||
Source source = sourceFactory.createSource(docContent);
|
||||
assertNotNull("Returned source was null", source);
|
||||
assertEquals("Expected DOMSource", DOMSource.class, source.getClass());
|
||||
assertEquals("Wrong content in source ", docContent, getAsString(source));
|
||||
@@ -74,11 +70,11 @@ public class DomSourceFactoryTests {
|
||||
|
||||
@Test(expected = MessagingException.class)
|
||||
public void testWithUnsupportedPayload() throws Exception {
|
||||
GenericMessage<Object> message = new GenericMessage<Object>(12);
|
||||
sourceFactory.getSourceForMessage(message);
|
||||
sourceFactory.createSource(new Integer(12));
|
||||
}
|
||||
|
||||
String getAsString(Source source) throws Exception {
|
||||
|
||||
private String getAsString(Source source) throws Exception {
|
||||
Transformer transformer = TransformerFactory.newInstance().newTransformer();
|
||||
StringResult res = new StringResult();
|
||||
transformer.transform(source, res);
|
||||
|
||||
@@ -24,11 +24,8 @@ import javax.xml.transform.sax.SAXResult;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
import org.springframework.integration.xml.util.XmlTestUtil;
|
||||
import org.springframework.xml.transform.StringResult;
|
||||
@@ -38,9 +35,9 @@ import org.springframework.xml.transform.StringResult;
|
||||
*/
|
||||
public class ResultToDocumentTransformerTests {
|
||||
|
||||
String doc = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>";
|
||||
private String doc = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>";
|
||||
|
||||
ResultToDocumentTransformer resToDocTransformer;
|
||||
private ResultToDocumentTransformer resToDocTransformer;
|
||||
|
||||
|
||||
@Before
|
||||
@@ -52,27 +49,25 @@ public class ResultToDocumentTransformerTests {
|
||||
@Test
|
||||
public void testWithDomResult() throws Exception {
|
||||
DOMResult result = XmlTestUtil.getDomResultForString(doc);
|
||||
GenericMessage<Object> message = new GenericMessage<Object>(result);
|
||||
Message<?> transformed = resToDocTransformer.transform(message);
|
||||
assertTrue("Wrong payload type expected Document", transformed.getPayload() instanceof Document);
|
||||
Document doc = (Document) transformed.getPayload();
|
||||
Object transformed = resToDocTransformer.transform(result);
|
||||
assertTrue("Wrong transformed type expected Document", transformed instanceof Document);
|
||||
Document doc = (Document) transformed;
|
||||
assertEquals("Wrong root element name", "order", doc.getDocumentElement().getNodeName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWithStringResult() throws Exception {
|
||||
StringResult result = XmlTestUtil.getStringResultForString(doc);
|
||||
GenericMessage<Object> message = new GenericMessage<Object>(result);
|
||||
Message<?> transformed = resToDocTransformer.transform(message);
|
||||
assertTrue("Wrong payload type expected Document", transformed.getPayload() instanceof Document);
|
||||
Document doc = (Document) transformed.getPayload();
|
||||
Object transformed = resToDocTransformer.transform(result);
|
||||
assertTrue("Wrong transformed type expected Document", transformed instanceof Document);
|
||||
Document doc = (Document) transformed;
|
||||
assertEquals("Wrong root element name", "order", doc.getDocumentElement().getNodeName());
|
||||
}
|
||||
|
||||
@Test(expected = MessagingException.class)
|
||||
public void testWithUnsupportedSaxResult() throws Exception {
|
||||
SAXResult result = new SAXResult();
|
||||
GenericMessage<Object> message = new GenericMessage<Object>(result);
|
||||
resToDocTransformer.transform(message);
|
||||
resToDocTransformer.transform(result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,8 +26,6 @@ import javax.xml.transform.Result;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.integration.xml.result.StringResultFactory;
|
||||
import org.springframework.oxm.Marshaller;
|
||||
import org.springframework.oxm.XmlMappingException;
|
||||
@@ -43,10 +41,9 @@ public class XmlPayloadMarshallingTransformerTests {
|
||||
TestMarshaller marshaller = new TestMarshaller();
|
||||
XmlPayloadMarshallingTransformer transformer = new XmlPayloadMarshallingTransformer(marshaller);
|
||||
transformer.setResultFactory(new StringResultFactory());
|
||||
Message<?> message = new StringMessage("world");
|
||||
Message<?> result = transformer.transform(message);
|
||||
assertEquals(StringResult.class, result.getPayload().getClass());
|
||||
assertEquals("hello world", result.getPayload().toString());
|
||||
Object result = transformer.transform("world");
|
||||
assertEquals(StringResult.class, result.getClass());
|
||||
assertEquals("hello world", result.toString());
|
||||
assertEquals("world", marshaller.payloads.get(0));
|
||||
}
|
||||
|
||||
@@ -54,16 +51,15 @@ public class XmlPayloadMarshallingTransformerTests {
|
||||
public void testDefaultResultFactory() {
|
||||
TestMarshaller marshaller = new TestMarshaller();
|
||||
XmlPayloadMarshallingTransformer transformer = new XmlPayloadMarshallingTransformer(marshaller);
|
||||
Message<?> message = new StringMessage("world");
|
||||
Message<?> result = transformer.transform(message);
|
||||
assertEquals(DOMResult.class, result.getPayload().getClass());
|
||||
Object result = transformer.transform("world");
|
||||
assertEquals(DOMResult.class, result.getClass());
|
||||
assertEquals("world", marshaller.payloads.get(0));
|
||||
}
|
||||
|
||||
|
||||
private static class TestMarshaller implements Marshaller {
|
||||
|
||||
List<Object> payloads = new ArrayList<Object>();
|
||||
private List<Object> payloads = new ArrayList<Object>();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public boolean supports(Class clazz) {
|
||||
|
||||
@@ -24,8 +24,6 @@ import javax.xml.transform.Source;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
import org.springframework.oxm.XmlMappingException;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
@@ -39,10 +37,9 @@ public class XmlPayloadUnmarshallingTransformerTests {
|
||||
public void testStringSourceToString() {
|
||||
Unmarshaller unmarshaller = new TestUnmarshaller();
|
||||
XmlPayloadUnmarshallingTransformer transformer = new XmlPayloadUnmarshallingTransformer(unmarshaller);
|
||||
Message<?> message = new GenericMessage<StringSource>(new StringSource("world"));
|
||||
Message<?> transformed = transformer.transform(message);
|
||||
assertEquals(String.class, transformed.getPayload().getClass());
|
||||
assertEquals("hello world", transformed.getPayload().toString());
|
||||
Object transformed = transformer.transform(new StringSource("world"));
|
||||
assertEquals(String.class, transformed.getClass());
|
||||
assertEquals("hello world", transformed.toString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.integration.xml.transformer;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
|
||||
import org.junit.Before;
|
||||
@@ -28,10 +27,7 @@ import org.w3c.dom.Document;
|
||||
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.integration.message.GenericMessage;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessagingException;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
import org.springframework.integration.xml.util.XmlTestUtil;
|
||||
import org.springframework.xml.transform.StringSource;
|
||||
|
||||
@@ -40,48 +36,49 @@ import org.springframework.xml.transform.StringSource;
|
||||
*/
|
||||
public class XsltPayloadTransformerTest {
|
||||
|
||||
XsltPayloadTransformer transformer;
|
||||
private XsltPayloadTransformer transformer;
|
||||
|
||||
private String doc = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>";
|
||||
|
||||
String doc = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><order><orderItem>test</orderItem></order>";
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
transformer = new XsltPayloadTransformer(getXslResource());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testDocumentAsPayload() throws Exception {
|
||||
GenericMessage<Document> documentMessage = new GenericMessage<Document>(XmlTestUtil.getDocumentForString(doc));
|
||||
transformer.transform(documentMessage);
|
||||
|
||||
Object transformed = transformer.transform(XmlTestUtil.getDocumentForString(doc));
|
||||
DOMResult result = (DOMResult) transformed;
|
||||
String rootNodeName = ((Document) result.getNode()).getDocumentElement().getNodeName();
|
||||
assertEquals("Wrong name for root element after transform", "bob", rootNodeName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSourceAsPayload() throws Exception {
|
||||
GenericMessage<Source> message = new GenericMessage<Source>(new StringSource(doc));
|
||||
Message<?> transformed = transformer.transform(message);
|
||||
DOMResult result = (DOMResult) transformed.getPayload();
|
||||
Object transformed = transformer.transform(new StringSource(doc));
|
||||
DOMResult result = (DOMResult) transformed;
|
||||
String rootNodeName = ((Document) result.getNode()).getDocumentElement().getNodeName();
|
||||
assertEquals("Wrong name for root element after transform", "bob", rootNodeName);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringAsPayload() throws Exception {
|
||||
GenericMessage<Object> message = new GenericMessage<Object>(doc);
|
||||
Message<?> transformed = transformer.transform(message);
|
||||
DOMResult result = (DOMResult) transformed.getPayload();
|
||||
Object transformed = transformer.transform(doc);
|
||||
DOMResult result = (DOMResult) transformed;
|
||||
String rootNodeName = ((Document) result.getNode()).getDocumentElement().getNodeName();
|
||||
assertEquals("Wrong name for root element after transform", "bob", rootNodeName);
|
||||
}
|
||||
|
||||
@Test(expected = MessagingException.class)
|
||||
public void testNonXmlString() throws Exception {
|
||||
transformer.transform(new StringMessage("test"));
|
||||
transformer.transform("test");
|
||||
}
|
||||
|
||||
@Test(expected = MessagingException.class)
|
||||
public void testUnsupportedPayloadType() throws Exception {
|
||||
transformer.transform(new GenericMessage<Long>(new Long(12)));
|
||||
transformer.transform(new Long(12));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2007 the original author or authors.
|
||||
* Copyright 2002-2008 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.
|
||||
@@ -25,14 +25,15 @@ import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
|
||||
import org.springframework.xml.transform.StringResult;
|
||||
import org.w3c.dom.Document;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
import org.springframework.xml.transform.StringResult;
|
||||
|
||||
/**
|
||||
* Utill class for XML related testing
|
||||
* @author Jonas Partner
|
||||
* Utility class for XML related testing
|
||||
*
|
||||
* @author Jonas Partner
|
||||
*/
|
||||
public class XmlTestUtil {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user