diff --git a/org.springframework.integration.xml/ivy.xml b/org.springframework.integration.xml/ivy.xml index 89c80b03a8..643bd48267 100644 --- a/org.springframework.integration.xml/ivy.xml +++ b/org.springframework.integration.xml/ivy.xml @@ -23,6 +23,7 @@ + diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/IntegrationXmlNamespaceHandler.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/IntegrationXmlNamespaceHandler.java new file mode 100644 index 0000000000..068ad6a101 --- /dev/null +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/IntegrationXmlNamespaceHandler.java @@ -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()); + } + +} diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XmlMarshallingTransformerParser.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XmlMarshallingTransformerParser.java new file mode 100644 index 0000000000..52f8dd2e98 --- /dev/null +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XmlMarshallingTransformerParser.java @@ -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()); + } + } + +} diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XmlUnmarshallingTransformerParser.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XmlUnmarshallingTransformerParser.java new file mode 100644 index 0000000000..a6ed7f073f --- /dev/null +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XmlUnmarshallingTransformerParser.java @@ -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)); + } + +} diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/spring-integration-xml-1.0.xsd b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/spring-integration-xml-1.0.xsd new file mode 100644 index 0000000000..35e2d215d6 --- /dev/null +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/spring-integration-xml-1.0.xsd @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + Defines a XML marshalling transformer. + + + + + + + + + + + + + + + + + + + + Defines a XML unmarshalling transformer. + + + + + + + + + \ No newline at end of file diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/result/DomResultFactory.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/result/DomResultFactory.java index e341c688a7..ce63bddea8 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/result/DomResultFactory.java +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/result/DomResultFactory.java @@ -24,7 +24,7 @@ import org.springframework.integration.message.Message; /** * * @author Jonas Partner - * + * */ public class DomResultFactory implements ResultFactory { diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/result/StringResultFactory.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/result/StringResultFactory.java index b7f8e65cf5..dc0c21ebfa 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/result/StringResultFactory.java +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/result/StringResultFactory.java @@ -24,7 +24,7 @@ import org.springframework.xml.transform.StringResult; /** * * @author Jonas Partner - * + * */ public class StringResultFactory implements ResultFactory { diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/source/DomSourceFactory.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/source/DomSourceFactory.java new file mode 100644 index 0000000000..677d40681a --- /dev/null +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/source/DomSourceFactory.java @@ -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); + } + } + +} diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/source/SourceFactory.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/source/SourceFactory.java index e2161db652..41110be179 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/source/SourceFactory.java +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/source/SourceFactory.java @@ -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 * */ diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/ResultToDocumentTransformer.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/ResultToDocumentTransformer.java new file mode 100644 index 0000000000..cc7651ad79 --- /dev/null +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/ResultToDocumentTransformer.java @@ -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); + } + } + +} diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/SourceCreatingTransformer.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/SourceCreatingTransformer.java new file mode 100644 index 0000000000..5d032e390c --- /dev/null +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/SourceCreatingTransformer.java @@ -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)); + } + +} diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/XmlPayloadMarshallingTransformer.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/XmlPayloadMarshallingTransformer.java index 1255795db1..b114781bec 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/XmlPayloadMarshallingTransformer.java +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/XmlPayloadMarshallingTransformer.java @@ -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; } diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/XmlPayloadUnmarshallingTransfomer.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/XmlPayloadUnmarshallingTransfomer.java index 073d72d745..2a6813976f 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/XmlPayloadUnmarshallingTransfomer.java +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/XmlPayloadUnmarshallingTransfomer.java @@ -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; diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/XsltPayloadTransformer.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/XsltPayloadTransformer.java index 7050c37c24..8ccbd75216 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/XsltPayloadTransformer.java +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/transformer/XsltPayloadTransformer.java @@ -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()); - } - } diff --git a/org.springframework.integration.xml/src/main/resources/META-INF/spring.handlers b/org.springframework.integration.xml/src/main/resources/META-INF/spring.handlers new file mode 100644 index 0000000000..6cbe04a450 --- /dev/null +++ b/org.springframework.integration.xml/src/main/resources/META-INF/spring.handlers @@ -0,0 +1 @@ +http\://www.springframework.org/schema/integration-xml=org.springframework.integration.xml.config.IntegrationXmlNamespaceHandler \ No newline at end of file diff --git a/org.springframework.integration.xml/src/main/resources/META-INF/spring.schemas b/org.springframework.integration.xml/src/main/resources/META-INF/spring.schemas new file mode 100644 index 0000000000..7e094c23d6 --- /dev/null +++ b/org.springframework.integration.xml/src/main/resources/META-INF/spring.schemas @@ -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 \ No newline at end of file diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/StubMarshaller.java b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/StubMarshaller.java new file mode 100644 index 0000000000..f6ca049357 --- /dev/null +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/StubMarshaller.java @@ -0,0 +1,48 @@ +/* + * 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 java.io.IOException; + +import javax.xml.transform.Result; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; + +import org.springframework.oxm.Marshaller; +import org.springframework.oxm.XmlMappingException; +import org.springframework.xml.transform.StringSource; + +public class StubMarshaller implements Marshaller { + + public void marshal(Object graph, Result result) throws XmlMappingException, IOException { + try { + Transformer transformer = TransformerFactory.newInstance().newTransformer(); + StringSource stringSource = new StringSource("" + graph.toString() + ""); + transformer.transform(stringSource, result); + } + catch (Exception e) { + throw new RuntimeException(e); + } + + } + + @SuppressWarnings("unchecked") + public boolean supports(Class clazz) { + return true; + } + +} diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/StubUnmarshaller.java b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/StubUnmarshaller.java new file mode 100644 index 0000000000..a51c728280 --- /dev/null +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/StubUnmarshaller.java @@ -0,0 +1,41 @@ +/* + * 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 java.io.IOException; +import java.util.LinkedList; + +import javax.xml.transform.Source; + +import org.springframework.oxm.Unmarshaller; +import org.springframework.oxm.XmlMappingException; + +public class StubUnmarshaller implements Unmarshaller { + + public LinkedList sourcesPassed = new LinkedList(); + + @SuppressWarnings("unchecked") + public boolean supports(Class clazz) { + return true; + } + + public Object unmarshal(Source source) throws XmlMappingException, IOException { + sourcesPassed.addFirst(source); + return "unmarshalled"; + } + +} diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XmlMarshallingTransformerParserTests-context.xml b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XmlMarshallingTransformerParserTests-context.xml new file mode 100644 index 0000000000..1dcae38591 --- /dev/null +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XmlMarshallingTransformerParserTests-context.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XmlMarshallingTransformerParserTests.java b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XmlMarshallingTransformerParserTests.java new file mode 100644 index 0000000000..5ba59b5718 --- /dev/null +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XmlMarshallingTransformerParserTests.java @@ -0,0 +1,74 @@ +/* + * 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 static org.junit.Assert.*; + +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.transformer.MessageTransformer; +import org.springframework.xml.transform.StringResult; +import org.w3c.dom.Document; + +/** + * + * @author Jonas Partner + * + */ +public class XmlMarshallingTransformerParserTests { + + ApplicationContext appContext; + + @Before + public void setUp(){ + appContext = new ClassPathXmlApplicationContext("XmlMarshallingTransformerParserTests-context.xml", getClass()); + } + + @Test + public void testDefault() throws Exception{ + MessageTransformer transformer = (MessageTransformer)appContext.getBean("marshallingTransfomerNoResultFactory"); + GenericMessage message = new GenericMessage("hello"); + transformer.transform(message); + assertTrue("Wrong payload type ", message.getPayload() instanceof DOMResult); + Document doc = (Document)((DOMResult)message.getPayload()).getNode(); + assertEquals("Wrong palyoad", "hello", doc.getDocumentElement().getTextContent()); + } + + @Test + public void testDOMResult() throws Exception{ + MessageTransformer transformer = (MessageTransformer)appContext.getBean("marshallingTransfomerDOMResultFactory"); + GenericMessage message = new GenericMessage("hello"); + transformer.transform(message); + assertTrue("Wrong payload type ", message.getPayload() instanceof DOMResult); + Document doc = (Document)((DOMResult)message.getPayload()).getNode(); + assertEquals("Wrong palyoad", "hello", doc.getDocumentElement().getTextContent()); + } + + @Test + public void testStringResult() throws Exception{ + MessageTransformer transformer = (MessageTransformer)appContext.getBean("marshallingTransfomerStringResultFactory"); + GenericMessage message = new GenericMessage("hello"); + transformer.transform(message); + assertTrue("Wrong payload type ", message.getPayload() instanceof StringResult); + } + +} diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XmlUnmarshallingTransformerParserTests-context.xml b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XmlUnmarshallingTransformerParserTests-context.xml new file mode 100644 index 0000000000..1c657954d1 --- /dev/null +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XmlUnmarshallingTransformerParserTests-context.xml @@ -0,0 +1,14 @@ + + + + + + + + + diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XmlUnmarshallingTransformerParserTests.java b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XmlUnmarshallingTransformerParserTests.java new file mode 100644 index 0000000000..24d378682c --- /dev/null +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/config/XmlUnmarshallingTransformerParserTests.java @@ -0,0 +1,90 @@ +/* + * 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 static org.junit.Assert.*; + +import javax.xml.transform.dom.DOMSource; + +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.MessagingException; +import org.springframework.integration.transformer.MessageTransformer; +import org.springframework.integration.xml.util.XmlTestUtil; +import org.springframework.xml.transform.StringSource; + +/** + * + * @author Jonas Partner + * + */ +public class XmlUnmarshallingTransformerParserTests { + + ApplicationContext appContext; + + StubUnmarshaller unmarshaller; + + @Before + public void setUp() { + appContext = new ClassPathXmlApplicationContext("XmlUnmarshallingTransformerParserTests-context.xml", + getClass()); + unmarshaller = (StubUnmarshaller) appContext.getBean("unmarshaller"); + } + + @Test + public void testDefaultUnmarshall() throws Exception { + MessageTransformer transformer = (MessageTransformer) appContext.getBean("defaultUnmarshaller"); + GenericMessage message = new GenericMessage(new StringSource( + "test")); + transformer.transform(message); + assertEquals("Wrong payload after unmarshalling ", "unmarshalled", message.getPayload()); + assertTrue("Wrong source passed to unmarshaller", unmarshaller.sourcesPassed.poll() instanceof StringSource); + } + + @Test + public void testUnmarshallString() throws Exception { + MessageTransformer transformer = (MessageTransformer) appContext.getBean("defaultUnmarshaller"); + GenericMessage message = new GenericMessage( + "test"); + transformer.transform(message); + assertEquals("Wrong payload after unmarshalling ", "unmarshalled", message.getPayload()); + assertTrue("Wrong source passed to unmarshaller", unmarshaller.sourcesPassed.poll() instanceof DOMSource); + } + + @Test + public void testUnmarshallDocument() throws Exception { + MessageTransformer transformer = (MessageTransformer) appContext.getBean("defaultUnmarshaller"); + GenericMessage message = new GenericMessage( + XmlTestUtil + .getDocumentForString("test")); + transformer.transform(message); + assertEquals("Wrong payload after unmarshalling ", "unmarshalled", message.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"); + GenericMessage message = new GenericMessage(new StringBuffer( + "test")); + transformer.transform(message); + } + +} diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/source/DomSourceFactoryTests.java b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/source/DomSourceFactoryTests.java new file mode 100644 index 0000000000..3ffa02f517 --- /dev/null +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/source/DomSourceFactoryTests.java @@ -0,0 +1,88 @@ +/* + * 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 static org.junit.Assert.*; + +import java.io.StringReader; + +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +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; + +/** + * + * @author Jonas Partner + * + */ +public class DomSourceFactoryTests { + + Document doc; + + DomSourceFactory sourceFactory; + + String docContent = "testValue"; + + @Before + public void setUp() throws Exception { + StringReader reader = new StringReader(docContent); + doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(reader)); + sourceFactory = new DomSourceFactory(); + } + + @Test + public void testWithDocumentPayload() throws Exception { + GenericMessage message = new GenericMessage(doc); + Source source = sourceFactory.getSourceForMessage(message); + assertNotNull("Returned source was null", source); + assertEquals("Expected DOMSource", DOMSource.class, source.getClass()); + assertEquals("Wrong content in source ", docContent, getAsString(source)); + } + + @Test + public void testWithStringPayload() throws Exception { + GenericMessage message = new GenericMessage(docContent); + Source source = sourceFactory.getSourceForMessage(message); + assertNotNull("Returned source was null", source); + assertEquals("Expected DOMSource", DOMSource.class, source.getClass()); + assertEquals("Wrong content in source ", docContent, getAsString(source)); + } + + @Test(expected = MessagingException.class) + public void testWithUnsupportedPayload() throws Exception { + GenericMessage message = new GenericMessage(12); + sourceFactory.getSourceForMessage(message); + } + + String getAsString(Source source) throws Exception { + Transformer transformer = TransformerFactory.newInstance().newTransformer(); + StringResult res = new StringResult(); + transformer.transform(source, res); + return res.toString(); + } + +} diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/transformer/ResultToDocumentTransformerTests.java b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/transformer/ResultToDocumentTransformerTests.java new file mode 100644 index 0000000000..dc8523c4fe --- /dev/null +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/transformer/ResultToDocumentTransformerTests.java @@ -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.transformer; + +import static org.junit.Assert.*; + +import javax.xml.transform.dom.DOMResult; +import javax.xml.transform.sax.SAXResult; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.integration.message.GenericMessage; +import org.springframework.integration.message.MessagingException; +import org.springframework.integration.xml.util.XmlTestUtil; +import org.springframework.xml.transform.StringResult; +import org.w3c.dom.Document; + +public class ResultToDocumentTransformerTests { + + String doc = "test"; + + ResultToDocumentTransformer resToDocTransformer; + + @Before + public void setUp() { + resToDocTransformer = new ResultToDocumentTransformer(); + } + + @Test + public void testWithDomResult() throws Exception { + DOMResult result = XmlTestUtil.getDomResultForString(doc); + GenericMessage message = new GenericMessage(result); + resToDocTransformer.transform(message); + assertTrue("Wrong payload type expected Document", message.getPayload() instanceof Document); + Document doc = (Document) message.getPayload(); + assertEquals("Wrong root element name", "order", doc.getDocumentElement().getNodeName()); + } + + @Test + public void testWithStringResult() throws Exception { + StringResult result = XmlTestUtil.getStringResultForString(doc); + GenericMessage message = new GenericMessage(result); + resToDocTransformer.transform(message); + assertTrue("Wrong payload type expected Document", message.getPayload() instanceof Document); + Document doc = (Document) message.getPayload(); + assertEquals("Wrong root element name", "order", doc.getDocumentElement().getNodeName()); + } + + @Test(expected = MessagingException.class) + public void testWithUnsupportedSaxResult() throws Exception { + SAXResult result = new SAXResult(); + GenericMessage message = new GenericMessage(result); + resToDocTransformer.transform(message); + } +} diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/transformer/XsltPayloadTransformerTest.java b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/transformer/XsltPayloadTransformerTest.java index ceff9495f6..fd8fe19a3d 100644 --- a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/transformer/XsltPayloadTransformerTest.java +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/transformer/XsltPayloadTransformerTest.java @@ -49,23 +49,12 @@ public class XsltPayloadTransformerTest { transformer = new XsltPayloadTransformer(getXslResource()); } - @Test - public void testDocumentAsPayload() throws Exception { + @Test(expected = MessagingException.class) + public void testDocumentAsPayloadShouldBeRejected() throws Exception { Document input = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse( new InputSource(new StringReader(getInputString()))); GenericMessage documentMessage = new GenericMessage(input); transformer.transform(documentMessage); - String rootNodeName = ((Document) documentMessage.getPayload()).getDocumentElement().getNodeName(); - assertEquals("Wrong name for root element after transform", "bob", rootNodeName); - } - - @Test - public void testXmlAsStringPayload() throws Exception { - StringMessage message = new StringMessage(getInputString()); - transformer.transform(message); - String rootNodeName = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse( - new InputSource(new StringReader(message.getPayload()))).getDocumentElement().getNodeName(); - assertEquals("Wrong name for root element after transform", "bob", rootNodeName); } @Test diff --git a/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/util/XmlTestUtil.java b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/util/XmlTestUtil.java new file mode 100644 index 0000000000..5faf61e4b0 --- /dev/null +++ b/org.springframework.integration.xml/src/test/java/org/springframework/integration/xml/util/XmlTestUtil.java @@ -0,0 +1,66 @@ +/* + * 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.util; + +import java.io.StringReader; + +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.Result; +import javax.xml.transform.Source; +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; + +/** + * Utill class for XML related testing + * @author Jonas Partner + * + */ +public class XmlTestUtil { + + public static Document getDocumentForString(String strDoc) throws Exception { + return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse( + new InputSource(new StringReader(strDoc))); + } + + public static DOMSource getDomSourceForString(String strDoc) throws Exception { + DOMSource domSource = new DOMSource(); + domSource.setNode(getDocumentForString(strDoc)); + return domSource; + } + + public static DOMResult getDomResultForString(String strDoc) throws Exception { + DOMResult res = new DOMResult(); + transform(getDomSourceForString(strDoc), res); + return res; + } + + public static StringResult getStringResultForString(String strDoc) throws Exception { + StringResult res = new StringResult(); + transform(getDomSourceForString(strDoc), res); + return res; + } + + public static void transform(Source source, Result res) throws Exception { + TransformerFactory.newInstance().newTransformer().transform(source, res); + } + +} diff --git a/org.springframework.integration.xml/template.mf b/org.springframework.integration.xml/template.mf index 1380a8a9b5..0b78d8f73a 100644 --- a/org.springframework.integration.xml/template.mf +++ b/org.springframework.integration.xml/template.mf @@ -4,11 +4,12 @@ Bundle-Vendor: SpringSource Bundle-ManifestVersion: 2 Import-Template: org.springframework.integration.*;version="[1.0.0, 1.0.1)", - org.springframework.beans.factory.*;version="[2.5.4.A, 3.0.0)", + org.springframework.beans.*;version="[2.5.4.A, 3.0.0)", org.springframework.core.*;version="[2.5.4.A, 3.0.0)", org.springframework.oxm;version="[1.5.1.A, 2.0.0)";resolution:=optional, org.springframework.util;version="[2.5.4.A, 3.0.0)", org.springframework.xml.transform;version="[1.5.1.A, 2.0.0)" Unversioned-Imports: org.w3c.dom, - javax.xml.transform.* + javax.xml.*, + org.xml.sax.* \ No newline at end of file