From: <bo...@us...> - 2010-04-29 10:01:44
|
Revision: 368 http://xmlunit.svn.sourceforge.net/xmlunit/?rev=368&view=rev Author: bodewig Date: 2010-04-29 10:01:38 +0000 (Thu, 29 Apr 2010) Log Message: ----------- extract reusable helpers Modified Paths: -------------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Convert.java trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/JAXPXPathEngine.java Added Paths: ----------- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/IterableNodeList.java Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Convert.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Convert.java 2010-04-29 09:38:19 UTC (rev 367) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/Convert.java 2010-04-29 10:01:38 UTC (rev 368) @@ -15,6 +15,14 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.LinkedHashMap; +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.transform.Transformer; import javax.xml.transform.TransformerFactory; @@ -57,4 +65,57 @@ throw new XMLUnitException(e); } } + + /** + * Creates a JAXP NamespaceContext from a Map prefix => Namespace URI. + */ + public static NamespaceContext + toNamespaceContext(Map<String, String> prefix2URI) { + final Map<String, String> copy = + new LinkedHashMap<String, String>(prefix2URI); + return new NamespaceContext() { + 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 = copy.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 : copy.entrySet()) { + if (uri.equals(entry.getValue())) { + c.add(entry.getKey()); + } + } + } + return c.iterator(); + } + }; + } } \ No newline at end of file Added: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/IterableNodeList.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/IterableNodeList.java (rev 0) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/IterableNodeList.java 2010-04-29 10:01:38 UTC (rev 368) @@ -0,0 +1,49 @@ +/* + 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.util; + +import java.util.Iterator; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +/** + * Provides an iterable view to a NodeList, the Iterator that can be + * obtained from this Iterable will be read-only. + */ +public final class IterableNodeList implements Iterable<Node> { + private final NodeList nl; + private final int length; + + public IterableNodeList(NodeList nl) { + this.nl = nl; + length = nl.getLength(); + } + + public Iterator<Node> iterator() { + return new NodeListIterator(); + } + + private class NodeListIterator implements Iterator<Node> { + private int current = 0; + public void remove() { + throw new UnsupportedOperationException(); + } + public Node next() { + return nl.item(current++); + } + public boolean hasNext() { + return current < length; + } + } +} Property changes on: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/util/IterableNodeList.java ___________________________________________________________________ Added: svn:eol-style + native Modified: trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/JAXPXPathEngine.java =================================================================== --- trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/JAXPXPathEngine.java 2010-04-29 09:38:19 UTC (rev 367) +++ trunk/xmlunit/src/main/java-core/net/sf/xmlunit/xpath/JAXPXPathEngine.java 2010-04-29 10:01:38 UTC (rev 368) @@ -13,13 +13,7 @@ */ 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; @@ -28,6 +22,7 @@ import net.sf.xmlunit.exceptions.ConfigurationException; import net.sf.xmlunit.exceptions.XMLUnitException; import net.sf.xmlunit.util.Convert; +import net.sf.xmlunit.util.IterableNodeList; import org.w3c.dom.Node; import org.w3c.dom.NodeList; @@ -59,10 +54,10 @@ */ public Iterable<Node> selectNodes(String xPath, Source s) { try { - NodeList nl = (NodeList) xpath.evaluate(xPath, - Convert.toInputSource(s), - XPathConstants.NODESET); - return new IterableNodeList(nl); + return new IterableNodeList( + (NodeList) xpath.evaluate(xPath, Convert.toInputSource(s), + XPathConstants.NODESET) + ); } catch (XPathExpressionException ex) { throw new XMLUnitException(ex); } @@ -85,81 +80,7 @@ * @param prefix2Uri maps from prefix to namespace URI. */ public void setNamespaceContext(Map<String, String> prefix2Uri) { - xpath.setNamespaceContext(new NC(prefix2Uri)); + xpath.setNamespaceContext(Convert.toNamespaceContext(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; - } - }; - } - } - } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |