From 99d26a5b04a5132eb7b348cb30bfbba92b71b8cd Mon Sep 17 00:00:00 2001 From: Jonas Partner Date: Thu, 7 Aug 2008 14:48:32 +0000 Subject: [PATCH] fix for INT-324 --- .../XmlMarshallingTransformerParser.java | 30 +++++--- .../xml/result/DomResultFactory.java | 17 ++++- .../xml/source/StringSourceFactory.java | 76 +++++++++++++++++++ .../XmlPayloadMarshallingTransformer.java | 6 +- .../transformer/XsltPayloadTransformer.java | 8 +- ...XmlPayloadMarshallingTransformerTests.java | 14 ++-- 6 files changed, 129 insertions(+), 22 deletions(-) create mode 100644 org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/source/StringSourceFactory.java 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 index a033242872..c0cd5f3c55 100644 --- 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 @@ -16,7 +16,7 @@ package org.springframework.integration.xml.config; -import org.w3c.dom.Element; +import javax.xml.parsers.ParserConfigurationException; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.ParserContext; @@ -26,12 +26,14 @@ 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 AbstractPayloadTransformerParser { +public class XmlMarshallingTransformerParser extends + AbstractPayloadTransformerParser { @Override protected Class> getTransformerClass() { @@ -39,18 +41,28 @@ public class XmlMarshallingTransformerParser extends AbstractPayloadTransformerP } @Override - protected void parsePayloadTransformer(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { + protected void parsePayloadTransformer(Element element, + ParserContext parserContext, BeanDefinitionBuilder builder) { String resultFactory = element.getAttribute("result-factory"); String marshaller = element.getAttribute("marshaller"); Assert.hasText(marshaller, "the 'marshaller' attribute is required"); - Assert.hasText(resultFactory, "the 'result-factory' 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 (resultFactory.equals("StringResult")) { - builder.addPropertyValue("resultFactory", new StringResultFactory()); + try { + 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()); } } - } 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 8fd0821e9a..30952d6f89 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 @@ -16,6 +16,9 @@ package org.springframework.integration.xml.result; +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; @@ -24,8 +27,18 @@ import javax.xml.transform.dom.DOMResult; */ public class DomResultFactory implements ResultFactory { - public Result createResult(Object payload) { - return new DOMResult(); + private final DocumentBuilder documentBuilder; + + public DomResultFactory(DocumentBuilder documentBuilder){ + this.documentBuilder = documentBuilder; + } + + public DomResultFactory() throws ParserConfigurationException{ + this(DocumentBuilderFactory.newInstance().newDocumentBuilder()); + } + + public synchronized Result createResult(Object payload) { + return new DOMResult(documentBuilder.newDocument()); } } diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/source/StringSourceFactory.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/source/StringSourceFactory.java new file mode 100644 index 0000000000..e80395f961 --- /dev/null +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/source/StringSourceFactory.java @@ -0,0 +1,76 @@ +/* + * 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 javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; + +import org.springframework.integration.message.MessagingException; +import org.springframework.xml.transform.StringResult; +import org.springframework.xml.transform.StringSource; +import org.w3c.dom.Document; + +/** + * + * @author Jonas Partner + * + */ +public class StringSourceFactory implements SourceFactory { + + private final TransformerFactory transformerFactory; + + public StringSourceFactory() { + this(TransformerFactory.newInstance()); + } + + public StringSourceFactory(TransformerFactory transformerFactory) { + this.transformerFactory = transformerFactory; + } + + public Source createSource(Object payload) { + if (Document.class.isAssignableFrom(payload.getClass())) { + return createStringSourceForDocument((Document) payload); + } else if (payload instanceof String) { + return new StringSource((String) payload); + } + throw new MessagingException( + "Failed to create Source for payload type [" + + payload.getClass().getName() + "]"); + } + + protected StringSource createStringSourceForDocument(Document doc) { + try { + StringResult result = new StringResult(); + Transformer transformer = getTransformer(); + transformer.transform(new DOMSource(doc), result); + return new StringSource(result.toString()); + } catch (Exception e) { + throw new MessagingException( + "Exception creating StringSource from document", e); + } + } + + protected synchronized Transformer getTransformer() { + try { + return transformerFactory.newTransformer(); + } catch (Exception e) { + throw new MessagingException("Exception creating transformer", e); + } + } + +} 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 7c19d4f5a0..6e8093ea6e 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 @@ -18,6 +18,7 @@ package org.springframework.integration.xml.transformer; import java.io.IOException; +import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Result; import org.springframework.integration.message.MessagingException; @@ -38,12 +39,13 @@ public class XmlPayloadMarshallingTransformer implements PayloadTransformer payloads = new ArrayList(); @@ -66,7 +67,8 @@ public class XmlPayloadMarshallingTransformerTests { return true; } - public void marshal(Object originalPayload, Result result) throws XmlMappingException, IOException { + public void marshal(Object originalPayload, Result result) + throws XmlMappingException, IOException { payloads.add(originalPayload); if (result instanceof StringResult) { ((StringResult) result).getWriter().write("hello world");