Refactored payload and header transformers to use abstract base classes.

This commit is contained in:
Mark Fisher
2008-09-03 05:05:04 +00:00
parent b2da8c5f2a
commit 3d27d78084
20 changed files with 149 additions and 398 deletions

View File

@@ -18,26 +18,26 @@ package org.springframework.integration.xml.config;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Element;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.transformer.PayloadTransformer;
import org.springframework.integration.transformer.AbstractPayloadTransformer;
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.springframework.util.StringUtils;
import org.w3c.dom.Element;
/**
* @author Jonas Partner
* @author Mark Fisher
*/
public class XmlMarshallingTransformerParser extends
AbstractPayloadTransformerParser {
public class XmlMarshallingTransformerParser extends AbstractPayloadTransformerParser {
@Override
protected Class<? extends PayloadTransformer<?, ?>> getTransformerClass() {
protected Class<? extends AbstractPayloadTransformer<?, ?>> getTransformerClass() {
return XmlPayloadMarshallingTransformer.class;
}
@@ -47,7 +47,6 @@ public class XmlMarshallingTransformerParser extends
String resultFactory = element.getAttribute("result-factory");
String marshaller = element.getAttribute("marshaller");
String resultTransformer = element.getAttribute("result-transformer");
Assert.hasText(marshaller, "the 'marshaller' attribute is required");
Assert.hasText(resultFactory,
"the 'result-factory' attribute is required");
@@ -55,20 +54,18 @@ public class XmlMarshallingTransformerParser extends
if(StringUtils.hasText(resultTransformer)){
builder.addConstructorArgReference(resultTransformer);
}
if (resultFactory.equals("DOMResult")) {
try {
builder.addPropertyValue("resultFactory",
new DomResultFactory());
} catch (ParserConfigurationException e) {
builder.addPropertyValue("resultFactory", new DomResultFactory());
}
catch (ParserConfigurationException e) {
throw new org.springframework.integration.ConfigurationException(
"Exception creating DomResultFactory");
}
} else if (resultFactory.equals("StringResult")) {
builder
.addPropertyValue("resultFactory",
new StringResultFactory());
}
else if (resultFactory.equals("StringResult")) {
builder.addPropertyValue("resultFactory", new StringResultFactory());
}
}
}

View File

@@ -20,7 +20,7 @@ import org.w3c.dom.Element;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.transformer.PayloadTransformer;
import org.springframework.integration.transformer.AbstractPayloadTransformer;
import org.springframework.integration.transformer.config.AbstractPayloadTransformerParser;
import org.springframework.integration.xml.transformer.XmlPayloadUnmarshallingTransformer;
import org.springframework.util.Assert;
@@ -32,7 +32,7 @@ import org.springframework.util.Assert;
public class XmlUnmarshallingTransformerParser extends AbstractPayloadTransformerParser {
@Override
protected Class<? extends PayloadTransformer<?, ?>> getTransformerClass() {
protected Class<? extends AbstractPayloadTransformer<?, ?>> getTransformerClass() {
return XmlPayloadUnmarshallingTransformer.class;
}

View File

@@ -20,7 +20,7 @@ import org.w3c.dom.Element;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.transformer.PayloadTransformer;
import org.springframework.integration.transformer.AbstractPayloadTransformer;
import org.springframework.integration.transformer.config.AbstractPayloadTransformerParser;
import org.springframework.integration.xml.transformer.XsltPayloadTransformer;
import org.springframework.util.Assert;
@@ -33,7 +33,7 @@ import org.springframework.util.StringUtils;
public class XsltPayloadTransformerParser extends AbstractPayloadTransformerParser {
@Override
protected Class<? extends PayloadTransformer<?, ?>> getTransformerClass() {
protected Class<? extends AbstractPayloadTransformer<?, ?>> getTransformerClass() {
return XsltPayloadTransformer.class;
}
@@ -47,14 +47,12 @@ public class XsltPayloadTransformerParser extends AbstractPayloadTransformerPars
boolean oneHasText = StringUtils.hasText(xslResource) || StringUtils.hasText(xslTemplates);
Assert.state(!bothHaveText && oneHasText,
"Exactly one of 'xsl-resource' or 'xsl-templates' is required.");
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.addPropertyReference("sourceFactory", sourceFactory);
@@ -63,7 +61,6 @@ public class XsltPayloadTransformerParser extends AbstractPayloadTransformerPars
if (StringUtils.hasText(resultFactory)) {
builder.addPropertyReference("resultFactory", resultFactory);
}
if(StringUtils.hasText(resultTransformer)){
builder.addConstructorArgReference(resultTransformer);
}

View File

@@ -18,7 +18,7 @@ package org.springframework.integration.xml.transformer;
import javax.xml.transform.Source;
import org.springframework.integration.transformer.PayloadTransformer;
import org.springframework.integration.transformer.AbstractPayloadTransformer;
import org.springframework.integration.xml.source.DomSourceFactory;
import org.springframework.integration.xml.source.SourceFactory;
@@ -28,7 +28,7 @@ import org.springframework.integration.xml.source.SourceFactory;
*
* @author Jonas Partner
*/
public class SourceCreatingTransformer implements PayloadTransformer<Object, Source> {
public class SourceCreatingTransformer extends AbstractPayloadTransformer<Object, Source> {
private final SourceFactory sourceFactory;
@@ -41,8 +41,8 @@ public class SourceCreatingTransformer implements PayloadTransformer<Object, Sou
this.sourceFactory = sourceFactory;
}
public Source transform(Object payload) {
@Override
public Source transformPayload(Object payload) {
return this.sourceFactory.createSource(payload);
}

View File

@@ -22,7 +22,7 @@ import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.transformer.PayloadTransformer;
import org.springframework.integration.transformer.AbstractPayloadTransformer;
import org.springframework.integration.xml.result.DomResultFactory;
import org.springframework.integration.xml.result.ResultFactory;
import org.springframework.oxm.Marshaller;
@@ -35,8 +35,7 @@ import org.springframework.util.Assert;
* @author Mark Fisher
* @author Jonas Partner
*/
public class XmlPayloadMarshallingTransformer implements
PayloadTransformer<Object, Object> {
public class XmlPayloadMarshallingTransformer extends AbstractPayloadTransformer<Object, Object> {
private final Marshaller marshaller;
@@ -44,8 +43,7 @@ public class XmlPayloadMarshallingTransformer implements
private final ResultTransformer resultTransformer;
public XmlPayloadMarshallingTransformer(Marshaller marshaller,
ResultTransformer resultTransformer)
public XmlPayloadMarshallingTransformer(Marshaller marshaller, ResultTransformer resultTransformer)
throws ParserConfigurationException {
Assert.notNull(marshaller, "a marshaller is required");
this.marshaller = marshaller;
@@ -53,17 +51,18 @@ public class XmlPayloadMarshallingTransformer implements
resultFactory = new DomResultFactory();
}
public XmlPayloadMarshallingTransformer(Marshaller marshaller)
throws ParserConfigurationException {
public XmlPayloadMarshallingTransformer(Marshaller marshaller) throws ParserConfigurationException {
this(marshaller, null);
}
public void setResultFactory(ResultFactory resultFactory) {
Assert.notNull(resultFactory, "ResultFactory must not be null");
this.resultFactory = resultFactory;
}
public Object transform(Object payload) {
@Override
public Object transformPayload(Object payload) {
Object transformedPayload = null;
Result result = this.resultFactory.createResult(payload);
if (result == null) {

View File

@@ -21,67 +21,43 @@ import java.io.IOException;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import org.w3c.dom.Document;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.transformer.PayloadTransformer;
import org.springframework.integration.transformer.AbstractPayloadTransformer;
import org.springframework.integration.xml.source.DomSourceFactory;
import org.springframework.integration.xml.source.SourceFactory;
import org.springframework.oxm.Unmarshaller;
import org.springframework.util.Assert;
import org.springframework.xml.transform.StringSource;
import org.w3c.dom.Document;
/**
* An implementation of {@link PayloadTransformer} that delegates to an OXM
* {@link Unmarshaller}. Expects the payload to be of type {@link Document},
* {@link String}, {@link Source} or to have an instance of
* {@link SourceFactory} that can convert to a {@link Source}. If
* alwaysUseSourceFactory is set to true then the {@link SourceFactory} will
* be used to create the {@link Source} regardless of payload type
*
* alwaysUseSourceFactory is set to true, then the {@link SourceFactory}
* will be used to create the {@link Source} regardless of payload type.
*
* @author Jonas Partner
*/
public class XmlPayloadUnmarshallingTransformer implements
PayloadTransformer<Object, Object> {
public class XmlPayloadUnmarshallingTransformer extends AbstractPayloadTransformer<Object, Object> {
private volatile boolean alwaysUseSourceFactory = false;
private final Unmarshaller unmarshaller;
private volatile SourceFactory sourceFactory = new DomSourceFactory();
public XmlPayloadUnmarshallingTransformer(Unmarshaller unmarshaller) {
this.unmarshaller = unmarshaller;
}
public Object transform(Object payload) {
Source source = null;
if (alwaysUseSourceFactory) {
source = sourceFactory.createSource(payload);
} else if (payload instanceof String) {
source = new StringSource((String) payload);
} else if (payload instanceof Document) {
source = new DOMSource((Document) payload);
} else if (payload instanceof Source) {
source = (Source) payload;
} else {
source = this.sourceFactory.createSource(payload);
}
if (source == null) {
throw new MessagingException(
"Failed to transform message, payload not assignable from javax.xml.transform.Source and no conversion possible");
}
try {
return this.unmarshaller.unmarshal(source);
} catch (IOException e) {
throw new MessagingException("Failed to unamrshal payload", e);
}
}
/**
* If true always delegate to the {@link SourceFactory}
* If true always delegate to the {@link SourceFactory}.
*
* @param alwaysUseSourceFactory
*/
public void setAlwaysUseSourceFactory(boolean alwaysUseSourceFactory) {
@@ -89,12 +65,41 @@ public class XmlPayloadUnmarshallingTransformer implements
}
/**
*
* @param sourceFactory
*/
public void setSourceFactory(SourceFactory sourceFactory) {
Assert.notNull(sourceFactory, "SourceFactory can not be null");
Assert.notNull(sourceFactory, "sourceFactory must not be null");
this.sourceFactory = sourceFactory;
}
@Override
public Object transformPayload(Object payload) {
Source source = null;
if (this.alwaysUseSourceFactory) {
source = this.sourceFactory.createSource(payload);
}
else if (payload instanceof String) {
source = new StringSource((String) payload);
}
else if (payload instanceof Document) {
source = new DOMSource((Document) payload);
}
else if (payload instanceof Source) {
source = (Source) payload;
}
else {
source = this.sourceFactory.createSource(payload);
}
if (source == null) {
throw new MessagingException(
"failed to transform message, payload not assignable from javax.xml.transform.Source and no conversion possible");
}
try {
return this.unmarshaller.unmarshal(source);
}
catch (IOException e) {
throw new MessagingException("failed to unmarshal payload", e);
}
}
}

View File

@@ -26,9 +26,11 @@ import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
import org.springframework.core.io.Resource;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.transformer.PayloadTransformer;
import org.springframework.integration.transformer.AbstractPayloadTransformer;
import org.springframework.integration.xml.result.DomResultFactory;
import org.springframework.integration.xml.result.ResultFactory;
import org.springframework.integration.xml.source.DomSourceFactory;
@@ -36,7 +38,6 @@ import org.springframework.integration.xml.source.SourceFactory;
import org.springframework.util.Assert;
import org.springframework.xml.transform.StringResult;
import org.springframework.xml.transform.StringSource;
import org.w3c.dom.Document;
/**
* XSLT transformer implementation which returns a transformed {@link Source},
@@ -62,8 +63,7 @@ import org.w3c.dom.Document;
* @author Jonas Partner
* @author Mark Fisher
*/
public class XsltPayloadTransformer implements
PayloadTransformer<Object, Object> {
public class XsltPayloadTransformer extends AbstractPayloadTransformer<Object, Object> {
private final Templates templates;
@@ -75,8 +75,8 @@ public class XsltPayloadTransformer implements
private volatile boolean alwaysUseSourceResultFactories = false;
public XsltPayloadTransformer(Templates templates)
throws ParserConfigurationException {
public XsltPayloadTransformer(Templates templates) throws ParserConfigurationException {
this(templates, null);
}
@@ -91,34 +91,14 @@ public class XsltPayloadTransformer implements
this(TransformerFactory.newInstance().newTemplates(
new StreamSource(xslResource.getInputStream())),null);
}
public XsltPayloadTransformer(Resource xslResource, ResultTransformer resultTransformer) throws Exception {
this(TransformerFactory.newInstance().newTemplates(
new StreamSource(xslResource.getInputStream())),resultTransformer);
}
public Object transform(Object payload) throws TransformerException {
Object transformedPayload;
if (alwaysUseSourceResultFactories) {
transformedPayload = transformUsingFactories(payload);
} else if (payload instanceof String) {
transformedPayload = transformString((String) payload);
} else if (payload instanceof Document) {
transformedPayload = transformDocument((Document) payload);
} else if (payload instanceof Source) {
transformedPayload = transformSource((Source) payload, payload);
} else {
// fall back to trying factories
transformedPayload = transformUsingFactories(payload);
}
return transformedPayload;
}
/**
*
* @param sourceFactory
*/
public void setSourceFactory(SourceFactory sourceFactory) {
@@ -127,7 +107,6 @@ public class XsltPayloadTransformer implements
}
/**
*
* @param resultFactory
*/
public void setResultFactory(ResultFactory resultFactory) {
@@ -146,14 +125,34 @@ public class XsltPayloadTransformer implements
this.alwaysUseSourceResultFactories = alwaysUserSourceResultFactories;
}
protected Object transformUsingFactories(Object payload)
throws TransformerException {
@Override
public Object transformPayload(Object payload) throws TransformerException {
Object transformedPayload = null;
if (this.alwaysUseSourceResultFactories) {
transformedPayload = transformUsingFactories(payload);
}
else if (payload instanceof String) {
transformedPayload = transformString((String) payload);
}
else if (payload instanceof Document) {
transformedPayload = transformDocument((Document) payload);
}
else if (payload instanceof Source) {
transformedPayload = transformSource((Source) payload, payload);
}
else {
// fall back to trying factories
transformedPayload = transformUsingFactories(payload);
}
return transformedPayload;
}
protected Object transformUsingFactories(Object payload) throws TransformerException {
Source source = sourceFactory.createSource(payload);
return transformSource(source, payload);
}
protected Object transformSource(Source source, Object payload)
throws TransformerException {
protected Object transformSource(Source source, Object payload) throws TransformerException {
Result result = resultFactory.createResult(payload);
this.templates.newTransformer().transform(source, result);
@@ -161,24 +160,21 @@ public class XsltPayloadTransformer implements
return resultTransformer.transformResult(result);
}
return result;
}
protected String transformString(String stringPayload)
throws TransformerException {
protected String transformString(String stringPayload) throws TransformerException {
StringResult result = new StringResult();
this.templates.newTransformer().transform(
new StringSource(stringPayload), result);
return result.toString();
}
protected Document transformDocument(Document documentPayload)
throws TransformerException {
protected Document transformDocument(Document documentPayload) 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");
"Document to Document conversion requires a DOMResult-producing ResultFactory implementation");
}
DOMResult domResult = (DOMResult) result;
this.templates.newTransformer().transform(source, domResult);