diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/DefaultXmlPayloadConverter.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/DefaultXmlPayloadConverter.java index 625c9f4cc7..ebaa35377e 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/DefaultXmlPayloadConverter.java +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/DefaultXmlPayloadConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2008 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.integration.xml; import java.io.StringReader; @@ -21,21 +22,23 @@ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; -import org.springframework.integration.core.MessagingException; - import org.w3c.dom.Document; import org.w3c.dom.Node; import org.xml.sax.InputSource; +import org.springframework.integration.core.MessagingException; + /** - * Supports {@link Document} and {@link String} + * Default implementation of {@link XmlPayloadConverter}. + * Supports {@link Document} and {@link String}. + * * @author Jonas Partner - * */ public class DefaultXmlPayloadConverter implements XmlPayloadConverter { private DocumentBuilderFactory documentBuilderFactory; + public DefaultXmlPayloadConverter() { this.documentBuilderFactory = DocumentBuilderFactory.newInstance(); this.documentBuilderFactory.setNamespaceAware(true); @@ -45,44 +48,36 @@ public class DefaultXmlPayloadConverter implements XmlPayloadConverter { this.documentBuilderFactory = documentBuilderFactory; } + public Document convertToDocument(Object object) { - Document doc; if (object instanceof Document) { - doc = (Document) object; + return (Document) object; } - else if (object instanceof String) { + if (object instanceof String) { try { - doc = getDocumentBuilder().parse(new InputSource(new StringReader((String) object))); + return getDocumentBuilder().parse(new InputSource(new StringReader((String) object))); } catch (Exception e) { - throw new MessagingException("Failed to parse String payload " + object, e); + throw new MessagingException("failed to parse String payload '" + object + "'", e); } } - else { - throw new MessagingException("Unsupported payload type " + object.getClass().getName()); - } - return doc; + throw new MessagingException("unsupported payload type [" + object.getClass().getName() + "]"); } public Node convertToNode(Object object) { - Node node; - if(object instanceof Node){ - node = (Node)object; - } else { - node = convertToDocument(object); + if (object instanceof Node){ + return (Node) object; } - return node; + return convertToDocument(object); } - protected synchronized DocumentBuilder getDocumentBuilder() { try { return this.documentBuilderFactory.newDocumentBuilder(); } catch (ParserConfigurationException e) { - throw new MessagingException("Failed to create a new DocumentBuilder", e); + throw new MessagingException("failed to create a new DocumentBuilder", e); } } - } diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/XmlPayloadConverter.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/XmlPayloadConverter.java index 018e066aaf..695cdc6a0d 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/XmlPayloadConverter.java +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/XmlPayloadConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2008 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,21 +13,21 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.integration.xml; import org.w3c.dom.Document; import org.w3c.dom.Node; - /** * Converter for creating XML {@link Document} instances + * * @author Jonas Partner - * */ public interface XmlPayloadConverter { - + public Document convertToDocument(Object object); - + public Node convertToNode(Object object); } diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathExpressionParser.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathExpressionParser.java index 5197899f8d..69bf51bbe5 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathExpressionParser.java +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathExpressionParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2008 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,11 +13,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.integration.xml.config; import java.util.HashMap; import java.util.Map; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; @@ -26,17 +31,14 @@ import org.springframework.beans.factory.xml.ParserContext; import org.springframework.util.Assert; import org.springframework.util.StringUtils; import org.springframework.xml.xpath.XPathExpressionFactory; -import org.w3c.dom.Element; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; /** + * Parser for the <xpath-expression> element. * * @author Jonas Partner - * */ public class XPathExpressionParser extends AbstractSingleBeanDefinitionParser { - + @Override protected boolean shouldGenerateId() { return false; @@ -46,44 +48,41 @@ public class XPathExpressionParser extends AbstractSingleBeanDefinitionParser { protected boolean shouldGenerateIdAsFallback() { return true; } - - - @SuppressWarnings("unchecked") + @Override - protected Class getBeanClass(Element element) { + protected Class getBeanClass(Element element) { return XPathExpressionFactory.class; } - @SuppressWarnings("unchecked") @Override protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { - String strXpathExpression = element.getAttribute("expression"); - String strXpathExpressionPrefix = element.getAttribute("ns-prefix"); - String strXpathExpressionNamespace = element.getAttribute("ns-uri"); - String nameSpaceMapRef = element.getAttribute("namespace-map"); + String expression = element.getAttribute("expression"); + Assert.hasText(expression, "The 'expression' attribute is required."); - Assert.hasText(strXpathExpression, "xpath-expression attribute is required"); + String nsPrefix = element.getAttribute("ns-prefix"); + String nsUri = element.getAttribute("ns-uri"); + String namespaceMapRef = element.getAttribute("namespace-map"); - boolean prefixProvided = StringUtils.hasText(strXpathExpressionPrefix); - boolean namespaceProvided = StringUtils.hasText(strXpathExpressionNamespace); - boolean namespaceMapProvided = StringUtils.hasText(nameSpaceMapRef); + boolean prefixProvided = StringUtils.hasText(nsPrefix); + boolean namespaceProvided = StringUtils.hasText(nsUri); + boolean namespaceMapProvided = StringUtils.hasText(namespaceMapRef); if (prefixProvided || namespaceProvided) { Assert.isTrue(prefixProvided && namespaceProvided, - "Both xpath-prefix and xpath-namespace must be specified if one is specified"); - Assert.isTrue(!namespaceMapProvided, "It is not valid to sepcify both xpath-namespace and namespace-map"); + "Both 'ns-prefix' and 'ns-uri' must be specified if one is specified."); + Assert.isTrue(!namespaceMapProvided, "It is not valid to specify both namespace and namespace-map."); } builder.setFactoryMethod("createXPathExpression"); - builder.addConstructorArgValue(strXpathExpression); + builder.addConstructorArgValue(expression); if (prefixProvided) { Map namespaceMap = new HashMap(); - namespaceMap.put(strXpathExpressionPrefix, strXpathExpressionNamespace); + namespaceMap.put(nsPrefix, nsUri); builder.addConstructorArgValue(namespaceMap); } - else if (StringUtils.hasText(nameSpaceMapRef)) { - builder.addConstructorArgReference(nameSpaceMapRef); + else if (StringUtils.hasText(namespaceMapRef)) { + builder.addConstructorArgReference(namespaceMapRef); } else if (element.getChildNodes().getLength() > 0) { NodeList nodeList = element.getChildNodes(); @@ -96,17 +95,14 @@ public class XPathExpressionParser extends AbstractSingleBeanDefinitionParser { elementCount++; } } - Assert.isTrue(elementCount == 1, "Only one namespace map child allowed"); + Assert.isTrue(elementCount == 1, "only one namespace map child allowed"); if (mapElement != null) { - Map namespaceMap = parseNamespaceMapElement(mapElement, parserContext, builder.getBeanDefinition()); - builder.getBeanDefinition().getConstructorArgumentValues().addGenericArgumentValue(namespaceMap); + builder.addConstructorArgValue(this.parseNamespaceMapElement( + mapElement, parserContext, builder.getBeanDefinition())); } } - } - - @SuppressWarnings("unchecked") protected Map parseNamespaceMapElement(Element element, ParserContext parserContext, BeanDefinition parentDefinition) { BeanDefinitionParserDelegate beanParser = new BeanDefinitionParserDelegate(parserContext.getReaderContext()); diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathMessageSplitterParser.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathMessageSplitterParser.java index d950002bbd..ed5f2b7c24 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathMessageSplitterParser.java +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathMessageSplitterParser.java @@ -16,6 +16,9 @@ package org.springframework.integration.xml.config; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.ParserContext; @@ -23,17 +26,15 @@ import org.springframework.integration.config.xml.AbstractConsumerEndpointParser import org.springframework.integration.xml.splitter.XPathMessageSplitter; import org.springframework.util.Assert; import org.springframework.util.StringUtils; -import org.w3c.dom.Element; -import org.w3c.dom.NodeList; /** * @author Jonas Partner */ public class XPathMessageSplitterParser extends AbstractConsumerEndpointParser { + private final XPathExpressionParser xpathParser = new XPathExpressionParser(); + - private XPathExpressionParser xpathParser = new XPathExpressionParser(); - @Override protected boolean shouldGenerateId() { return false; @@ -44,30 +45,23 @@ public class XPathMessageSplitterParser extends AbstractConsumerEndpointParser { return true; } - @Override protected BeanDefinitionBuilder parseConsumer(Element element, ParserContext parserContext) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(XPathMessageSplitter.class); String xPathExpressionRef = element.getAttribute("xpath-expression-ref"); - NodeList xPathExpressionNodes = element.getElementsByTagNameNS(element.getNamespaceURI(), "xpath-expression"); - Assert.isTrue(xPathExpressionNodes.getLength() < 2, "Only one xpath-expression child can be specified"); - boolean xPathExpressionChildPresent = xPathExpressionNodes.getLength() == 1; - boolean xPathReferencePresent = StringUtils.hasText(xPathExpressionRef); - - boolean exactlyOneSpecified = !(xPathExpressionChildPresent && xPathReferencePresent) && (!xPathExpressionChildPresent ^ !xPathReferencePresent); - - Assert.isTrue( exactlyOneSpecified,"Exactly one of 'xpath-expression' or 'xpath-expression-ref' is required."); - - if (xPathExpressionChildPresent) { - BeanDefinition beanDefinition = xpathParser.parse((Element) xPathExpressionNodes.item(0), parserContext); + Assert.isTrue(xPathExpressionNodes.getLength() <= 1, "only one xpath-expression child can be specified"); + boolean hasChild = xPathExpressionNodes.getLength() == 1; + boolean hasReference = StringUtils.hasText(xPathExpressionRef); + Assert.isTrue(hasChild ^ hasReference, "Exactly one of 'xpath-expression' or 'xpath-expression-ref' is required."); + if (hasChild) { + BeanDefinition beanDefinition = this.xpathParser.parse((Element) xPathExpressionNodes.item(0), parserContext); builder.addConstructorArgValue(beanDefinition); - } else { + } + else { builder.addConstructorArgReference(xPathExpressionRef); } return builder; } - - } diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathRouterParser.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathRouterParser.java index 1a2bbcaa6c..68064dd597 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathRouterParser.java +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XPathRouterParser.java @@ -16,6 +16,9 @@ package org.springframework.integration.xml.config; +import org.w3c.dom.Element; +import org.w3c.dom.NodeList; + import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.xml.ParserContext; @@ -24,8 +27,6 @@ import org.springframework.integration.xml.router.XPathMultiChannelRouter; import org.springframework.integration.xml.router.XPathSingleChannelRouter; import org.springframework.util.Assert; import org.springframework.util.StringUtils; -import org.w3c.dom.Element; -import org.w3c.dom.NodeList; /** * @author Jonas Partner @@ -33,7 +34,8 @@ import org.w3c.dom.NodeList; public class XPathRouterParser extends AbstractConsumerEndpointParser { private XPathExpressionParser xpathParser = new XPathExpressionParser(); - + + @Override protected boolean shouldGenerateId() { return false; @@ -47,7 +49,7 @@ public class XPathRouterParser extends AbstractConsumerEndpointParser { @Override protected BeanDefinitionBuilder parseConsumer(Element element, ParserContext parserContext) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(); - + boolean multiChannel = Boolean.parseBoolean(element.getAttribute("multi-channel")); String xPathExpressionRef = element.getAttribute("xpath-expression-ref"); NodeList xPathExpressionNodes = element.getElementsByTagNameNS( 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 0f31cce855..2b66e53be6 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 @@ -32,9 +32,6 @@ import org.springframework.util.StringUtils; */ public class XmlMarshallingTransformerParser extends AbstractTransformerParser { - private ResultFactoryResultTypeHelper resultFactoryHelper = new ResultFactoryResultTypeHelper(); - - @Override protected Class getTransformerClass() { return XmlPayloadMarshallingTransformer.class; @@ -51,8 +48,7 @@ public class XmlMarshallingTransformerParser extends AbstractTransformerParser { if (StringUtils.hasText(resultTransformer)) { builder.addConstructorArgReference(resultTransformer); } - resultFactoryHelper.assertResultFactoryAndTypeValid(resultFactory, resultType); - resultFactoryHelper.addResultFactory(builder, resultType, resultFactory); + XmlNamespaceUtils.configureResultFactory(builder, resultType, resultFactory); } } diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/ResultFactoryResultTypeHelper.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XmlNamespaceUtils.java similarity index 69% rename from org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/ResultFactoryResultTypeHelper.java rename to org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XmlNamespaceUtils.java index b95071ac58..355a568e35 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/ResultFactoryResultTypeHelper.java +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XmlNamespaceUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2008 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,6 +13,7 @@ * 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.support.BeanDefinitionBuilder; @@ -23,37 +24,39 @@ import org.springframework.util.Assert; import org.springframework.util.StringUtils; /** - * helper class which encapsulates common logic for validating and building a - * bean definition for a {@link ResultFactory} based on either the - * result-factory or result-type + * Utility methods for the XML namespace. * * @author Jonas Partner - * + * @author Mark Fisher */ -public class ResultFactoryResultTypeHelper { +public class XmlNamespaceUtils { public static final String DOM_RESULT = "DOMResult"; public static final String STRING_RESULT = "StringResult"; - protected void assertResultFactoryAndTypeValid(String resultFactory, String resultType) { + + /** + * Helper method that encapsulates common logic for validating and building + * a bean definition for a {@link ResultFactory} based on either the + * 'result-factory' or 'result-type' attributes. + */ + public static void configureResultFactory(BeanDefinitionBuilder builder, String resultType, String resultFactory) { boolean bothHaveText = StringUtils.hasText(resultFactory) && StringUtils.hasText(resultType); - Assert.state(!bothHaveText, "Exactly one of result-factory or result-type should be specified"); + Assert.state(!bothHaveText, "Only one of 'result-factory' or 'result-type' should be specified."); if (StringUtils.hasText(resultType)) { Assert.state(resultType.equals(DOM_RESULT) || resultType.equals(STRING_RESULT), - "Result type must be either DOMResult or StringResult"); + "Result type must be either 'DOMResult' or 'StringResult'"); } - } - - protected void addResultFactory(BeanDefinitionBuilder builder, String resultType, String resultFactory) { if (StringUtils.hasText(resultFactory)) { builder.addPropertyReference("resultFactory", resultFactory); } - else if (resultType.equals(DOM_RESULT) || !StringUtils.hasText(resultType)) { - builder.addPropertyValue("resultFactory", new DomResultFactory()); - } else if (resultType.equals(STRING_RESULT)) { builder.addPropertyValue("resultFactory", new StringResultFactory()); } + else { + builder.addPropertyValue("resultFactory", new DomResultFactory()); + } } + } diff --git a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XsltPayloadTransformerParser.java b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XsltPayloadTransformerParser.java index 7a01d82043..edae86e2c1 100644 --- a/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XsltPayloadTransformerParser.java +++ b/org.springframework.integration.xml/src/main/java/org/springframework/integration/xml/config/XsltPayloadTransformerParser.java @@ -33,9 +33,6 @@ import org.springframework.util.StringUtils; */ public class XsltPayloadTransformerParser extends AbstractTransformerParser { - private ResultFactoryResultTypeHelper resultFactoryHelper = new ResultFactoryResultTypeHelper(); - - @Override protected Class getTransformerClass() { return XsltPayloadTransformer.class; @@ -56,8 +53,7 @@ public class XsltPayloadTransformerParser extends AbstractTransformerParser { else if (StringUtils.hasText(xslTemplates)) { builder.addConstructorArgReference(xslTemplates); } - resultFactoryHelper.assertResultFactoryAndTypeValid(resultFactory, resultType); - resultFactoryHelper.addResultFactory(builder, resultType, resultFactory); + XmlNamespaceUtils.configureResultFactory(builder, resultType, resultFactory); if (StringUtils.hasText(resultTransformer)) { builder.addConstructorArgReference(resultTransformer); }