diff --git a/spring-xml/src/main/java/org/springframework/xml/xpath/JaxenXPathExpressionFactory.java b/spring-xml/src/main/java/org/springframework/xml/xpath/JaxenXPathExpressionFactory.java index a2aa2fa9..d9265504 100644 --- a/spring-xml/src/main/java/org/springframework/xml/xpath/JaxenXPathExpressionFactory.java +++ b/spring-xml/src/main/java/org/springframework/xml/xpath/JaxenXPathExpressionFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2014 the original author or authors. + * Copyright 2005-2016 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. @@ -31,6 +31,7 @@ import org.w3c.dom.Node; * Jaxen-specific factory for creating {@code XPathExpression}s. * * @author Arjen Poutsma + * @author Greg Turnquist * @see #createXPathExpression(String) * @since 1.0.0 */ @@ -46,7 +47,7 @@ abstract class JaxenXPathExpressionFactory { static XPathExpression createXPathExpression(String expression) { try { XPath xpath = new DOMXPath(expression); - return new JaxenXpathExpression(xpath); + return new JaxenXpathExpression(xpath, expression); } catch (JaxenException ex) { throw new org.springframework.xml.xpath.XPathParseException( @@ -66,7 +67,7 @@ abstract class JaxenXPathExpressionFactory { try { XPath xpath = new DOMXPath(expression); xpath.setNamespaceContext(new SimpleNamespaceContext(namespaces)); - return new JaxenXpathExpression(xpath); + return new JaxenXpathExpression(xpath, expression); } catch (JaxenException ex) { throw new org.springframework.xml.xpath.XPathParseException( @@ -78,9 +79,16 @@ abstract class JaxenXPathExpressionFactory { private static class JaxenXpathExpression implements XPathExpression { private XPath xpath; + private final String expression; - private JaxenXpathExpression(XPath xpath) { + private JaxenXpathExpression(XPath xpath, String expression) { this.xpath = xpath; + this.expression = expression; + } + + @Override + public String toString() { + return expression; } @Override diff --git a/spring-xml/src/main/java/org/springframework/xml/xpath/Jaxp13XPathExpressionFactory.java b/spring-xml/src/main/java/org/springframework/xml/xpath/Jaxp13XPathExpressionFactory.java index 694f51a0..3b06e2db 100644 --- a/spring-xml/src/main/java/org/springframework/xml/xpath/Jaxp13XPathExpressionFactory.java +++ b/spring-xml/src/main/java/org/springframework/xml/xpath/Jaxp13XPathExpressionFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2014 the original author or authors. + * Copyright 2005-2016 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. @@ -35,6 +35,7 @@ import org.springframework.xml.namespace.SimpleNamespaceContext; * JAXP 1.3-specific factory creating {@link XPathExpression} objects. * * @author Arjen Poutsma + * @author Greg Turnquist * @see #createXPathExpression(String) * @since 1.0.0 */ @@ -53,7 +54,7 @@ abstract class Jaxp13XPathExpressionFactory { try { XPath xpath = createXPath(); javax.xml.xpath.XPathExpression xpathExpression = xpath.compile(expression); - return new Jaxp13XPathExpression(xpathExpression); + return new Jaxp13XPathExpression(xpathExpression, expression); } catch (XPathExpressionException ex) { throw new org.springframework.xml.xpath.XPathParseException( @@ -76,7 +77,7 @@ abstract class Jaxp13XPathExpressionFactory { namespaceContext.setBindings(namespaces); xpath.setNamespaceContext(namespaceContext); javax.xml.xpath.XPathExpression xpathExpression = xpath.compile(expression); - return new Jaxp13XPathExpression(xpathExpression); + return new Jaxp13XPathExpression(xpathExpression, expression); } catch (XPathExpressionException ex) { throw new org.springframework.xml.xpath.XPathParseException( @@ -93,9 +94,16 @@ abstract class Jaxp13XPathExpressionFactory { private static class Jaxp13XPathExpression implements XPathExpression { private final javax.xml.xpath.XPathExpression xpathExpression; + private final String expression; - private Jaxp13XPathExpression(javax.xml.xpath.XPathExpression xpathExpression) { + private Jaxp13XPathExpression(javax.xml.xpath.XPathExpression xpathExpression, String expression) { this.xpathExpression = xpathExpression; + this.expression = expression; + } + + @Override + public String toString() { + return expression; } @Override diff --git a/spring-xml/src/test/java/org/springframework/xml/xpath/AbstractXPathExpressionFactoryTestCase.java b/spring-xml/src/test/java/org/springframework/xml/xpath/AbstractXPathExpressionFactoryTestCase.java index c9e075ba..be946713 100644 --- a/spring-xml/src/test/java/org/springframework/xml/xpath/AbstractXPathExpressionFactoryTestCase.java +++ b/spring-xml/src/test/java/org/springframework/xml/xpath/AbstractXPathExpressionFactoryTestCase.java @@ -1,5 +1,5 @@ /* - * Copyright 2005-2010 the original author or authors. + * Copyright 2005-2016 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. @@ -24,8 +24,6 @@ import java.util.Map; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; -import org.springframework.util.StringUtils; - import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -34,6 +32,8 @@ import org.w3c.dom.Document; import org.w3c.dom.Node; import org.xml.sax.SAXException; +import org.springframework.util.StringUtils; + public abstract class AbstractXPathExpressionFactoryTestCase { private Document noNamespacesDocument; @@ -65,6 +65,12 @@ public abstract class AbstractXPathExpressionFactoryTestCase { } } + @Test + public void testThatToStringReturnsOriginalXpathExpression() { + XPathExpression expression = createXPathExpression("/prefix1:root/prefix2:otherchild", namespaces); + Assert.assertEquals("/prefix1:root/prefix2:otherchild", expression.toString()); + } + @Test public void testEvaluateAsBooleanInvalidNamespaces() throws IOException, SAXException { XPathExpression expression = createXPathExpression("/prefix1:root/prefix2:otherchild", namespaces);