From: <bo...@us...> - 2010-04-29 09:38:26
|
Revision: 367 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=367&view=rev Author: bodewig Date: 2010-04-29 09:38:19 +0000 (Thu, 29 Apr 2010) Log Message: ----------- rename class to leave door open for different implementations (at which point we may want to extract an interface) Modified Paths: -------------- trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/jaxp13/Jaxp13XpathEngine.java Added Paths: ----------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/JAXPXPathEngine.java Removed Paths: ------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/XPathEngine.java Copied: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/JAXPXPathEngine.java (from rev 363, trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/XPathEngine.java) =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/JAXPXPathEngine.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/JAXPXPathEngine.java 2010-04-29 09:38:19 UTC (rev 367) @@ -0,0 +1,165 @@ +/* + This file is licensed to You 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 net.sf.xmlunit.xpath; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import javax.xml.XMLConstants; +import javax.xml.namespace.NamespaceContext; +import javax.xml.transform.Source; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; +import net.sf.xmlunit.exceptions.ConfigurationException; +import net.sf.xmlunit.exceptions.XMLUnitException; +import net.sf.xmlunit.util.Convert; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** + * Simplified access to JAXP's XPath API. + */ +public class JAXPXPathEngine { + private final XPath xpath; + + public JAXPXPathEngine(XPathFactory fac) { + try { + xpath = fac.newXPath(); + } catch (Exception e) { + throw new ConfigurationException(e); + } + } + + /** + * Create an XPathEngine that uses JAXP's default XPathFactory + * under the covers. + */ + public JAXPXPathEngine() { + this(XPathFactory.newInstance()); + } + + /** + * Returns a potentially empty collection of Nodes matching an + * XPath expression. + */ + public Iterable<Node> selectNodes(String xPath, Source s) { + try { + NodeList nl = (NodeList) xpath.evaluate(xPath, + Convert.toInputSource(s), + XPathConstants.NODESET); + return new IterableNodeList(nl); + } catch (XPathExpressionException ex) { + throw new XMLUnitException(ex); + } + } + + /** + * Evaluates an XPath expression and stringifies the result. + */ + public String evaluate(String xPath, Source s) { + try { + return xpath.evaluate(xPath, Convert.toInputSource(s)); + } catch (XPathExpressionException ex) { + throw new XMLUnitException(ex); + } + } + + /** + * Establish a namespace context. + * + * @param prefix2Uri maps from prefix to namespace URI. + */ + public void setNamespaceContext(Map<String, String> prefix2Uri) { + xpath.setNamespaceContext(new NC(prefix2Uri)); + } + + private static class NC implements NamespaceContext { + private final Map<String, String> prefix2Uri; + + private NC(Map<String, String> prefix2Uri) { + this.prefix2Uri = prefix2Uri; + } + + public String getNamespaceURI(String prefix) { + if (prefix == null) { + throw new IllegalArgumentException("prefix must not be null"); + } + if (XMLConstants.XML_NS_PREFIX.equals(prefix)) { + return XMLConstants.XML_NS_URI; + } + if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) { + return XMLConstants.XMLNS_ATTRIBUTE_NS_URI; + } + String uri = prefix2Uri.get(prefix); + return uri != null ? uri : XMLConstants.NULL_NS_URI; + } + + public String getPrefix(String uri) { + Iterator i = getPrefixes(uri); + return i.hasNext() ? (String) i.next() : null; + } + + public Iterator getPrefixes(String uri) { + if (uri == null) { + throw new IllegalArgumentException("uri must not be null"); + } + Collection<String> c = new HashSet<String>(); + boolean done = false; + if (XMLConstants.XML_NS_URI.equals(uri)) { + c.add(XMLConstants.XML_NS_PREFIX); + done = true; + } + if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(uri)) { + c.add(XMLConstants.XMLNS_ATTRIBUTE); + done = true; + } + if (!done) { + for (Map.Entry<String, String> entry : prefix2Uri.entrySet()) { + if (uri.equals(entry.getValue())) { + c.add(entry.getKey()); + } + } + } + return c.iterator(); + } + } + + private static class IterableNodeList implements Iterable<Node> { + private final NodeList nl; + private final int length; + private int current = 0; + private IterableNodeList(NodeList nl) { + this.nl = nl; + length = nl.getLength(); + } + public Iterator<Node> iterator() { + return new Iterator<Node>() { + public void remove() { + throw new UnsupportedOperationException(); + } + public Node next() { + return nl.item(current++); + } + public boolean hasNext() { + return current < length; + } + }; + } + } + +} Deleted: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/XPathEngine.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/XPathEngine.java 2010-04-29 08:58:05 UTC (rev 366) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/XPathEngine.java 2010-04-29 09:38:19 UTC (rev 367) @@ -1,165 +0,0 @@ -/* - This file is licensed to You 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 net.sf.xmlunit.xpath; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Map; -import javax.xml.XMLConstants; -import javax.xml.namespace.NamespaceContext; -import javax.xml.transform.Source; -import javax.xml.xpath.XPath; -import javax.xml.xpath.XPathConstants; -import javax.xml.xpath.XPathExpressionException; -import javax.xml.xpath.XPathFactory; -import net.sf.xmlunit.exceptions.ConfigurationException; -import net.sf.xmlunit.exceptions.XMLUnitException; -import net.sf.xmlunit.util.Convert; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; - -/** - * Simplified access to JAXP's XPath API. - */ -public class XPathEngine { - private final XPath xpath; - - public XPathEngine(XPathFactory fac) { - try { - xpath = fac.newXPath(); - } catch (Exception e) { - throw new ConfigurationException(e); - } - } - - /** - * Create an XPathEngine that uses JAXP's default XPathFactory - * under the covers. - */ - public XPathEngine() { - this(XPathFactory.newInstance()); - } - - /** - * Returns a potentially empty collection of Nodes matching an - * XPath expression. - */ - public Iterable<Node> selectNodes(String xPath, Source s) { - try { - NodeList nl = (NodeList) xpath.evaluate(xPath, - Convert.toInputSource(s), - XPathConstants.NODESET); - return new IterableNodeList(nl); - } catch (XPathExpressionException ex) { - throw new XMLUnitException(ex); - } - } - - /** - * Evaluates an XPath expression and stringifies the result. - */ - public String evaluate(String xPath, Source s) { - try { - return xpath.evaluate(xPath, Convert.toInputSource(s)); - } catch (XPathExpressionException ex) { - throw new XMLUnitException(ex); - } - } - - /** - * Establish a namespace context. - * - * @param prefix2Uri maps from prefix to namespace URI. - */ - public void setNamespaceContext(Map<String, String> prefix2Uri) { - xpath.setNamespaceContext(new NC(prefix2Uri)); - } - - private static class NC implements NamespaceContext { - private final Map<String, String> prefix2Uri; - - private NC(Map<String, String> prefix2Uri) { - this.prefix2Uri = prefix2Uri; - } - - public String getNamespaceURI(String prefix) { - if (prefix == null) { - throw new IllegalArgumentException("prefix must not be null"); - } - if (XMLConstants.XML_NS_PREFIX.equals(prefix)) { - return XMLConstants.XML_NS_URI; - } - if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) { - return XMLConstants.XMLNS_ATTRIBUTE_NS_URI; - } - String uri = prefix2Uri.get(prefix); - return uri != null ? uri : XMLConstants.NULL_NS_URI; - } - - public String getPrefix(String uri) { - Iterator i = getPrefixes(uri); - return i.hasNext() ? (String) i.next() : null; - } - - public Iterator getPrefixes(String uri) { - if (uri == null) { - throw new IllegalArgumentException("uri must not be null"); - } - Collection<String> c = new HashSet<String>(); - boolean done = false; - if (XMLConstants.XML_NS_URI.equals(uri)) { - c.add(XMLConstants.XML_NS_PREFIX); - done = true; - } - if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(uri)) { - c.add(XMLConstants.XMLNS_ATTRIBUTE); - done = true; - } - if (!done) { - for (Map.Entry<String, String> entry : prefix2Uri.entrySet()) { - if (uri.equals(entry.getValue())) { - c.add(entry.getKey()); - } - } - } - return c.iterator(); - } - } - - private static class IterableNodeList implements Iterable<Node> { - private final NodeList nl; - private final int length; - private int current = 0; - private IterableNodeList(NodeList nl) { - this.nl = nl; - length = nl.getLength(); - } - public Iterator<Node> iterator() { - return new Iterator<Node>() { - public void remove() { - throw new UnsupportedOperationException(); - } - public Node next() { - return nl.item(current++); - } - public boolean hasNext() { - return current < length; - } - }; - } - } - -} Modified: trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/jaxp13/Jaxp13XpathEngine.java =================================================================== --- trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/jaxp13/Jaxp13XpathEngine.java 2010-04-29 08:58:05 UTC (rev 366) +++ trunk/xmlunit/src/main/java-legacy/org/custommonkey/xmlunit/jaxp13/Jaxp13XpathEngine.java 2010-04-29 09:38:19 UTC (rev 367) @@ -53,24 +53,24 @@ import org.w3c.dom.NodeList; import net.sf.xmlunit.exceptions.XMLUnitException; -import net.sf.xmlunit.xpath.XPathEngine; +import net.sf.xmlunit.xpath.JAXPXPathEngine; /** * XPath engine based on javax.xml.xpath. */ public class Jaxp13XpathEngine implements XpathEngine { - private final XPathEngine engine; + private final JAXPXPathEngine engine; public Jaxp13XpathEngine() throws ConfigurationException { try { - XPathEngine e = null; + JAXPXPathEngine e = null; if (XMLUnit.getXPathFactory() != null) { - e = new XPathEngine((XPathFactory) Class - .forName(XMLUnit.getXPathFactory()) - .newInstance()); + e = new JAXPXPathEngine((XPathFactory) Class + .forName(XMLUnit.getXPathFactory()) + .newInstance()); } else { - e = new XPathEngine(); + e = new JAXPXPathEngine(); } engine = e; } catch (net.sf.xmlunit.exceptions.ConfigurationException ex) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |