added Result transformer INT-286

Refactored OXM transformer creating separate marshalling and unamarshalling transformers and added namespace support INT-109
This commit is contained in:
Jonas Partner
2008-07-06 20:35:31 +00:00
parent b7f31fcbf0
commit 2128c1e21e
27 changed files with 973 additions and 48 deletions

View File

@@ -0,0 +1,33 @@
/*
* Copyright 2002-2007 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.config;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
/**
*
* @author Jonas Partner
*
*/
public class IntegrationXmlNamespaceHandler extends NamespaceHandlerSupport {
public void init() {
registerBeanDefinitionParser("marshalling-transformer", new XmlMarshallingTransformerParser());
registerBeanDefinitionParser("unmarshalling-transformer", new XmlUnmarshallingTransformerParser());
}
}

View File

@@ -0,0 +1,69 @@
/*
* Copyright 2002-2007 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.config;
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.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
*
*/
public class XmlMarshallingTransformerParser extends AbstractSingleBeanDefinitionParser {
@Override
protected boolean shouldGenerateId() {
return false;
}
@Override
protected boolean shouldGenerateIdAsFallback() {
return true;
}
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
String resourceFactory = 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());
}
else if (resourceFactory.equals("StringResult")) {
builder.getBeanDefinition().getPropertyValues()
.addPropertyValue("resultFactory", new StringResultFactory());
}
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright 2002-2007 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.config;
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.xml.transformer.XmlPayloadUnmarshallingTransfomer;
import org.springframework.util.Assert;
import org.w3c.dom.Element;
/**
*
* @author Jonas Partner
*
*/
public class XmlUnmarshallingTransformerParser extends AbstractSingleBeanDefinitionParser {
@Override
protected boolean shouldGenerateId() {
return false;
}
@Override
protected boolean shouldGenerateIdAsFallback() {
return true;
}
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
String unmarshaller = element.getAttribute("unmarshaller");
Assert.hasText(unmarshaller, "A unmarshaller attribute is required");
builder.getBeanDefinition().setBeanClass(XmlPayloadUnmarshallingTransfomer.class);
builder.getBeanDefinition().getConstructorArgumentValues().addGenericArgumentValue(
new RuntimeBeanReference(unmarshaller));
}
}

View File

@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
xmlns="http://www.springframework.org/schema/integration-xml"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:tool="http://www.springframework.org/schema/tool"
targetNamespace="http://www.springframework.org/schema/integration-xml"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans" />
<xsd:import namespace="http://www.springframework.org/schema/tool" />
<xsd:annotation>
<xsd:documentation><![CDATA[
Defines the configuration elements for Spring Integration's XML support.
]]></xsd:documentation>
</xsd:annotation>
<xsd:element name="marshalling-transformer">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Defines a XML marshalling transformer.
</xsd:documentation>
</xsd:annotation>
<xsd:attribute name="id" type="xsd:string" use="required" />
<xsd:attribute name="marshaller" type="xsd:string"
use="required" />
<xsd:attribute name="result-factory" default="DOMResult">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="DOMResult" />
<xsd:enumeration value="StringResult" />
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
<xsd:element name="unmarshalling-transformer">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Defines a XML unmarshalling transformer.
</xsd:documentation>
</xsd:annotation>
<xsd:attribute name="id" type="xsd:string" use="required" />
<xsd:attribute name="unmarshaller" type="xsd:string"
use="required" />
</xsd:complexType>
</xsd:element>
</xsd:schema>

View File

@@ -24,7 +24,7 @@ import org.springframework.integration.message.Message;
/**
*
* @author Jonas Partner
*
*
*/
public class DomResultFactory implements ResultFactory {

View File

@@ -24,7 +24,7 @@ import org.springframework.xml.transform.StringResult;
/**
*
* @author Jonas Partner
*
*
*/
public class StringResultFactory implements ResultFactory {

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2002-2007 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.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;
/**
* {@link SourceDataLine} implementation which supports creation of a
* {@link DOMSource} from a {@link Document} or {@link String} payload
*
* @author Jonas Partner
*
*/
public class DomSourceFactory implements SourceFactory {
private final DocumentBuilderFactory docBuilderFactory;
public DomSourceFactory() {
this.docBuilderFactory = DocumentBuilderFactory.newInstance();
}
public DomSourceFactory(DocumentBuilderFactory docBuilderFactory) {
this.docBuilderFactory = docBuilderFactory;
}
public Source getSourceForMessage(Message<?> message) {
if (Document.class.isAssignableFrom(message.getPayload().getClass())) {
return createDomSourceForDocument(message);
}
else if (message.getPayload() instanceof String) {
return createDomSourceForString(message);
}
throw new MessagingException(message, "Could not create Source for payload type "
+ message.getPayload().getClass().getName());
}
protected DOMSource createDomSourceForDocument(Message<?> message) {
DOMSource source = new DOMSource(((Document) message.getPayload()).getDocumentElement());
return source;
}
protected DOMSource createDomSourceForString(Message<?> message) {
try {
String str = (String) message.getPayload();
Document doc = docBuilderFactory.newDocumentBuilder().parse(new InputSource(new StringReader(str)));
DOMSource source = new DOMSource(doc.getDocumentElement());
return source;
}
catch (Exception e) {
throw new MessagingException(message, "Exception creating DOMSource", e);
}
}
}

View File

@@ -23,6 +23,7 @@ import org.springframework.integration.message.Message;
/**
* Factory to create instances of {@link Source} possibly taking into account
* the contents of the passed {@link Message}
*
* @author Jonas Partner
*
*/

View File

@@ -0,0 +1,95 @@
/*
* Copyright 2002-2007 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 java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.dom.DOMResult;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.transformer.MessageTransformer;
import org.springframework.xml.transform.StringResult;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
/**
* Creates a {@link Document} from a {@link Result} payload
* @author Jonas Partner
*
*/
public class ResultToDocumentTransformer implements MessageTransformer {
// Not guaranteed to be thread safe
private final DocumentBuilderFactory documentBuilderFactory;
public ResultToDocumentTransformer(DocumentBuilderFactory documentBuilderFactory) {
this.documentBuilderFactory = documentBuilderFactory;
}
public ResultToDocumentTransformer() {
this(DocumentBuilderFactory.newInstance());
}
@SuppressWarnings("unchecked")
public void transform(Message message) {
Document doc;
if (DOMResult.class.isAssignableFrom(message.getPayload().getClass())) {
doc = createDocumentFromDomResult(message, (DOMResult) message.getPayload());
}
else if (StringResult.class.isAssignableFrom(message.getPayload().getClass())) {
doc = createDocumentFromStringResult(message, (StringResult) message.getPayload());
}
else {
throw new MessagingException(message, "Could not create document from payload type "
+ message.getPayload().getClass().getName());
}
message.setPayload(doc);
}
@SuppressWarnings("unchecked")
protected Document createDocumentFromDomResult(Message message, DOMResult domResult) {
return (Document) domResult.getNode();
}
@SuppressWarnings("unchecked")
protected Document createDocumentFromStringResult(Message message, 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);
}
}
protected synchronized DocumentBuilder getDocumentBuilder() {
try {
return documentBuilderFactory.newDocumentBuilder();
}
catch (ParserConfigurationException parseE) {
throw new MessagingException("Failed to create a new DocumentBuilder", parseE);
}
}
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright 2002-2007 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 javax.xml.transform.Source;
import org.springframework.integration.message.Message;
import org.springframework.integration.transformer.MessageTransformer;
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}
* Default to using a {@link DomSourceFactory} if alternative is not provided
*
* @author Jonas Partner
*
*/
public class SourceCreatingTransformer implements MessageTransformer {
private final SourceFactory sourceFactory;
public SourceCreatingTransformer() {
sourceFactory = new DomSourceFactory();
}
public SourceCreatingTransformer(SourceFactory sourceFactory) {
this.sourceFactory = sourceFactory;
}
@SuppressWarnings("unchecked")
public void transform(Message message) {
message.setPayload(sourceFactory.getSourceForMessage(message));
}
}

View File

@@ -33,6 +33,7 @@ import org.springframework.util.Assert;
* {@link Marshaller}
*
* @author Mark Fisher
* @author Jonas Partner
*/
public class XmlPayloadMarshallingTransformer implements MessageTransformer {
@@ -46,6 +47,7 @@ public class XmlPayloadMarshallingTransformer implements MessageTransformer {
}
public void setResultFactory(ResultFactory resultFactory) {
Assert.notNull(resultFactory, " the ResultFactory can not be null");
this.resultFactory = resultFactory;
}

View File

@@ -23,12 +23,15 @@ import javax.xml.transform.Source;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.transformer.MessageTransformer;
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
* {@link Unmarshaller}
* {@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
*/
@@ -36,7 +39,7 @@ public class XmlPayloadUnmarshallingTransfomer implements MessageTransformer {
private final Unmarshaller unmarshaller;
private SourceFactory sourceFactory;
private SourceFactory sourceFactory = new DomSourceFactory();
public XmlPayloadUnmarshallingTransfomer(Unmarshaller unmarshaller) {
this.unmarshaller = unmarshaller;
@@ -58,7 +61,7 @@ public class XmlPayloadUnmarshallingTransfomer implements MessageTransformer {
if (source == null) {
throw new MessagingException(message,
"Could not transform message payload not assignable from javax.xml.transform.Source and no conversion possible");
"Could not transform message, payload not assignable from javax.xml.transform.Source and no conversion possible");
}
Object unmarshalled;

View File

@@ -20,18 +20,15 @@ import javax.xml.transform.Source;
import javax.xml.transform.Templates;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
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.Message;
import org.springframework.integration.message.MessagingException;
import org.springframework.integration.transformer.MessageTransformer;
import org.springframework.xml.transform.StringResult;
import org.springframework.xml.transform.StringSource;
import org.w3c.dom.Document;
/**
* Simple XSLT transformer implementation which returns a transformed
@@ -54,18 +51,13 @@ public class XsltPayloadTransformer implements MessageTransformer {
@SuppressWarnings("unchecked")
public void transform(Message message) {
try {
if (Document.class.isAssignableFrom(message.getPayload().getClass())) {
this.transformDocument(message);
}
else if (Source.class.isAssignableFrom(message.getPayload().getClass())) {
if (Source.class.isAssignableFrom(message.getPayload().getClass())) {
this.transformSource(message);
}
else if (String.class.equals(message.getPayload().getClass())) {
this.transformString(message);
}
else {
throw new MessagingException(message, "Unsupported payload type for transformation: "
+ message.getPayload().getClass().getName());
throw new MessagingException(message,
"Unsupported payload type for transformation expected javax.xml.transform.Source but got : "
+ message.getPayload().getClass().getName());
}
}
catch (TransformerException e) {
@@ -80,19 +72,4 @@ public class XsltPayloadTransformer implements MessageTransformer {
message.setPayload(new StringSource(result.toString()));
}
@SuppressWarnings("unchecked")
protected void transformString(Message message) throws TransformerException {
StringResult result = new StringResult();
this.templates.newTransformer().transform(new StringSource((String) message.getPayload()), result);
message.setPayload(result.toString());
}
@SuppressWarnings("unchecked")
protected void transformDocument(Message message) throws TransformerException {
Document doc = (Document) message.getPayload();
DOMResult result = new DOMResult();
this.templates.newTransformer().transform(new DOMSource(doc), result);
message.setPayload(result.getNode());
}
}

View File

@@ -0,0 +1 @@
http\://www.springframework.org/schema/integration-xml=org.springframework.integration.xml.config.IntegrationXmlNamespaceHandler

View File

@@ -0,0 +1 @@
http\://www.springframework.org/schema/integration/spring-integration-xml-1.0.xsd=org/springframework/integration/xml/config/spring-integration-xml-1.0.xsd