[Practicalxml-commits] SF.net SVN: practicalxml:[54] trunk/src
Brought to you by:
kdgregory
|
From: Auto-Generated S. C. M. <pra...@li...> - 2008-12-16 02:56:50
|
Revision: 54
http://practicalxml.svn.sourceforge.net/practicalxml/?rev=54&view=rev
Author: kdgregory
Date: 2008-12-16 02:56:39 +0000 (Tue, 16 Dec 2008)
Log Message:
-----------
Package changes:
renamed "misc" to "util"
added "xpath", moving XPath support classes from "util"
Modified Paths:
--------------
trunk/src/main/java/net/sf/practicalxml/DomUtil.java
trunk/src/main/java/net/sf/practicalxml/ParseUtil.java
trunk/src/main/java/net/sf/practicalxml/XPathWrapper.java
trunk/src/main/java/net/sf/practicalxml/util/ErrorHandlerAdapter.java
trunk/src/main/java/net/sf/practicalxml/util/ExceptionErrorHandler.java
trunk/src/main/java/net/sf/practicalxml/util/NodeListIterable.java
trunk/src/test/java/net/sf/practicalxml/TestDomUtilGetPath.java
trunk/src/test/java/net/sf/practicalxml/TestSchemaUtil.java
trunk/src/test/java/net/sf/practicalxml/util/TestNodeListIterable.java
Added Paths:
-----------
trunk/src/main/java/net/sf/practicalxml/util/
trunk/src/main/java/net/sf/practicalxml/xpath/
trunk/src/main/java/net/sf/practicalxml/xpath/NamespaceResolver.java
trunk/src/main/java/net/sf/practicalxml/xpath/SimpleNamespaceResolver.java
trunk/src/main/java/net/sf/practicalxml/xpath/package.html
trunk/src/test/java/net/sf/practicalxml/util/
trunk/src/test/java/net/sf/practicalxml/xpath/
trunk/src/test/java/net/sf/practicalxml/xpath/TestNamespaceResolver.java
trunk/src/test/java/net/sf/practicalxml/xpath/TestSimpleNamespaceResolver.java
Removed Paths:
-------------
trunk/src/main/java/net/sf/practicalxml/misc/
trunk/src/main/java/net/sf/practicalxml/util/NamespaceResolver.java
trunk/src/main/java/net/sf/practicalxml/util/SimpleNamespaceResolver.java
trunk/src/test/java/net/sf/practicalxml/misc/
trunk/src/test/java/net/sf/practicalxml/util/TestNamespaceResolver.java
trunk/src/test/java/net/sf/practicalxml/util/TestSimpleNamespaceResolver.java
Modified: trunk/src/main/java/net/sf/practicalxml/DomUtil.java
===================================================================
--- trunk/src/main/java/net/sf/practicalxml/DomUtil.java 2008-12-14 17:22:13 UTC (rev 53)
+++ trunk/src/main/java/net/sf/practicalxml/DomUtil.java 2008-12-16 02:56:39 UTC (rev 54)
@@ -13,7 +13,7 @@
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
-import net.sf.practicalxml.misc.NamespaceResolver;
+import net.sf.practicalxml.xpath.NamespaceResolver;
import org.apache.commons.lang.StringUtils;
Modified: trunk/src/main/java/net/sf/practicalxml/ParseUtil.java
===================================================================
--- trunk/src/main/java/net/sf/practicalxml/ParseUtil.java 2008-12-14 17:22:13 UTC (rev 53)
+++ trunk/src/main/java/net/sf/practicalxml/ParseUtil.java 2008-12-16 02:56:39 UTC (rev 54)
@@ -8,7 +8,7 @@
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.validation.Schema;
-import net.sf.practicalxml.misc.ExceptionErrorHandler;
+import net.sf.practicalxml.util.ExceptionErrorHandler;
import org.w3c.dom.Document;
import org.xml.sax.EntityResolver;
Modified: trunk/src/main/java/net/sf/practicalxml/XPathWrapper.java
===================================================================
--- trunk/src/main/java/net/sf/practicalxml/XPathWrapper.java 2008-12-14 17:22:13 UTC (rev 53)
+++ trunk/src/main/java/net/sf/practicalxml/XPathWrapper.java 2008-12-16 02:56:39 UTC (rev 54)
@@ -19,7 +19,7 @@
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
-import net.sf.practicalxml.misc.NamespaceResolver;
+import net.sf.practicalxml.xpath.NamespaceResolver;
Modified: trunk/src/main/java/net/sf/practicalxml/util/ErrorHandlerAdapter.java
===================================================================
--- trunk/src/main/java/net/sf/practicalxml/misc/ErrorHandlerAdapter.java 2008-12-14 17:22:13 UTC (rev 53)
+++ trunk/src/main/java/net/sf/practicalxml/util/ErrorHandlerAdapter.java 2008-12-16 02:56:39 UTC (rev 54)
@@ -1,4 +1,4 @@
-package net.sf.practicalxml.misc;
+package net.sf.practicalxml.util;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
Modified: trunk/src/main/java/net/sf/practicalxml/util/ExceptionErrorHandler.java
===================================================================
--- trunk/src/main/java/net/sf/practicalxml/misc/ExceptionErrorHandler.java 2008-12-14 17:22:13 UTC (rev 53)
+++ trunk/src/main/java/net/sf/practicalxml/util/ExceptionErrorHandler.java 2008-12-16 02:56:39 UTC (rev 54)
@@ -1,4 +1,4 @@
-package net.sf.practicalxml.misc;
+package net.sf.practicalxml.util;
import java.util.ArrayList;
import java.util.List;
Deleted: trunk/src/main/java/net/sf/practicalxml/util/NamespaceResolver.java
===================================================================
--- trunk/src/main/java/net/sf/practicalxml/misc/NamespaceResolver.java 2008-12-14 17:22:13 UTC (rev 53)
+++ trunk/src/main/java/net/sf/practicalxml/util/NamespaceResolver.java 2008-12-16 02:56:39 UTC (rev 54)
@@ -1,261 +0,0 @@
-package net.sf.practicalxml.misc;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.SortedSet;
-import java.util.TreeMap;
-import java.util.TreeSet;
-import javax.xml.XMLConstants;
-import javax.xml.namespace.NamespaceContext;
-
-
-/**
- * Maintains a bi-directional lookup table for mappings between namespace URIs
- * and prefixes. Follows the "builder" pattern, in that the methods to add
- * mappings may be chained: a resolver can be created and fully configured in
- * a single expression.
- * <p>
- * Usage note: <code>NamespaceContext</code> allows multiple prefixes per URI,
- * in keeping with the Namespace spec. This implementation supports that, but
- * it's a bad idea to actually use this feature when writing an XPath. You'll
- * be much happier if you limit yourself to a 1:1 mapping.
- * <p>
- * If you have a single namespace mapping, this implementation is overkill.
- * Instead, use {@link SimpleNamespaceResolver}.
- */
-public class NamespaceResolver
-implements NamespaceContext
-{
- private final static SortedSet<String> DEFAULT_PREFIXES =
- new TreeSet<String>();
- private final static SortedSet<String> XML_NS_URI_PREFIXES =
- new TreeSet<String>();
- private final static SortedSet<String> XML_NS_ATTR_PREFIXES =
- new TreeSet<String>();
- static
- {
- DEFAULT_PREFIXES.add("");
- XML_NS_URI_PREFIXES.add(XMLConstants.XML_NS_PREFIX);
- XML_NS_ATTR_PREFIXES.add(XMLConstants.XMLNS_ATTRIBUTE);
- }
-
- private TreeMap<String,String> _prefix2ns = new TreeMap<String,String>();
- private Map<String,SortedSet<String>> _ns2prefix = new HashMap<String,SortedSet<String>>();
- private String _defaultNS = "";
-
-
-//----------------------------------------------------------------------------
-// Public methods
-//----------------------------------------------------------------------------
-
- /**
- * Adds a namespace to this resolver.
- *
- * @return The resolver instance, so that calls may be chained.
- *
- * @throws IllegalArgumentException if either <code>prefix</code>
- * or <code>nsURI</code> is <code>null</code>.
- */
- public NamespaceResolver addNamespace(String prefix, String nsURI)
- {
- if (prefix == null)
- throw new IllegalArgumentException("prefix may not be null");
- if (nsURI == null)
- throw new IllegalArgumentException("nsURI may not be null");
-
- _prefix2ns.put(prefix, nsURI);
- getPrefixSet(nsURI).add(prefix);
- return this;
- }
-
-
- /**
- * Sets the default namespace -- the namespace that will be returned
- * when an empty string is passed to the resolver.
- *
- * @return The resolver instance, so that calls may be chained.
- *
- * @throws IllegalArgumentException if <code>nsURI</code> is
- * <code>null</code>.
- */
- public NamespaceResolver setDefaultNamespace(String nsURI)
- {
- if (nsURI == null)
- throw new IllegalArgumentException("nsURI may not be null");
-
- _defaultNS = nsURI;
- return this;
- }
-
-
- /**
- * Returns the default namespace, an empty string if one has not yet
- * been set.
- */
- public String getDefaultNamespace()
- {
- return _defaultNS;
- }
-
-
- /**
- * Returns a list of all prefixes known to this resolver, in alphabetical
- * order.
- */
- public List<String> getAllPrefixes()
- {
- return new ArrayList<String>(_prefix2ns.keySet());
- }
-
-
-//----------------------------------------------------------------------------
-// NamespaceContext implementation
-//----------------------------------------------------------------------------
-
- /**
- * Returns the namespace URI bound to a given prefix, <code>null</code>
- * if no URI is bound to the specified prefix. See interface doc for
- * default bindings.
- */
- public String getNamespaceURI(String prefix)
- {
- if (prefix == null)
- throw new IllegalArgumentException("prefix may not be null");
- else if ("".equals(prefix))
- return _defaultNS;
- else if (XMLConstants.XML_NS_PREFIX.equals(prefix))
- return XMLConstants.XML_NS_URI;
- else if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix))
- return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
- else
- return _prefix2ns.get(prefix);
- }
-
-
- /**
- * Returns the first prefix in alphabetical order bound to this namespace
- * URI, <code>null</code> if there is no binding for the namespace. See
- * interface doc for default bindings.
- */
- public String getPrefix(String nsURI)
- {
- Iterator<String> itx = getPrefixes(nsURI);
- return itx.hasNext() ? itx.next() : null;
- }
-
-
- /**
- * Returns an iterator over all prefixes bound to this namespace URI, in
- * alphabetical order, an empty iterator if there are no bindings. See
- * interface doc for default bindings.
- */
- public Iterator<String> getPrefixes(String nsURI)
- {
- if (nsURI == null)
- throw new IllegalArgumentException("nsURI may not be null");
- else if (_defaultNS.equals(nsURI))
- return DEFAULT_PREFIXES.iterator();
- else if (XMLConstants.XML_NS_URI.equals(nsURI))
- return XML_NS_URI_PREFIXES.iterator();
- else if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(nsURI))
- return XML_NS_ATTR_PREFIXES.iterator();
- else
- return getPrefixSet(nsURI).iterator();
- }
-
-
-//----------------------------------------------------------------------------
-// Object overrides
-//----------------------------------------------------------------------------
-
- /**
- * Two instances are considered equal if they have the same mappings,
- * including default namespace.
- */
- @Override
- public final boolean equals(Object obj)
- {
- if (this == obj)
- return true;
-
- if (obj instanceof NamespaceResolver)
- {
- NamespaceResolver that = (NamespaceResolver)obj;
- return this._prefix2ns.equals(that._prefix2ns)
- && this._defaultNS.equals(that._defaultNS);
- }
- return false;
- }
-
-
- @Override
- public int hashCode()
- {
- // rely on these objects caching their hashcode
- return _prefix2ns.hashCode() ^ _defaultNS.hashCode();
- }
-
-
- /**
- * Returns a string containing the the <code>xmlns</code> attribute
- * specifications that would result from this resolver.
- */
- @Override
- public String toString()
- {
- StringBuilder buf = new StringBuilder(50 * _prefix2ns.size());
- if (!"".equals(_defaultNS))
- {
- buf.append("xmlns=\"").append(_defaultNS).append("\"");
- }
- for (String prefix : getAllPrefixes())
- {
- if (buf.length() > 0)
- buf.append(" ");
- buf.append("xmlns:").append(prefix).append("=\"")
- .append(_prefix2ns.get(prefix)).append("\"");
- }
- return buf.toString();
- }
-
-
- /**
- * Returns a deep clone of this object, that can then be independently
- * manipulated.
- */
- @Override
- protected NamespaceResolver clone()
- {
- NamespaceResolver that = new NamespaceResolver()
- .setDefaultNamespace(getDefaultNamespace());
- for (String prefix : getAllPrefixes())
- {
- that.addNamespace(prefix, getNamespaceURI(prefix));
- }
- return that;
- }
-
-
-//----------------------------------------------------------------------------
-// Internals
-//----------------------------------------------------------------------------
-
-
- /**
- * Returns the set of prefixes for a given namespace, creating a new
- * entry if one doesn't already exist.
- */
- private SortedSet<String> getPrefixSet(String nsURI)
- {
- SortedSet<String> prefixes = _ns2prefix.get(nsURI);
- if (prefixes == null)
- {
- prefixes = new TreeSet<String>();
- _ns2prefix.put(nsURI, prefixes);
- }
- return prefixes;
- }
-}
Modified: trunk/src/main/java/net/sf/practicalxml/util/NodeListIterable.java
===================================================================
--- trunk/src/main/java/net/sf/practicalxml/misc/NodeListIterable.java 2008-12-14 17:22:13 UTC (rev 53)
+++ trunk/src/main/java/net/sf/practicalxml/util/NodeListIterable.java 2008-12-16 02:56:39 UTC (rev 54)
@@ -1,4 +1,4 @@
-package net.sf.practicalxml.misc;
+package net.sf.practicalxml.util;
import java.util.Iterator;
import java.util.NoSuchElementException;
Deleted: trunk/src/main/java/net/sf/practicalxml/util/SimpleNamespaceResolver.java
===================================================================
--- trunk/src/main/java/net/sf/practicalxml/misc/SimpleNamespaceResolver.java 2008-12-14 17:22:13 UTC (rev 53)
+++ trunk/src/main/java/net/sf/practicalxml/util/SimpleNamespaceResolver.java 2008-12-16 02:56:39 UTC (rev 54)
@@ -1,145 +0,0 @@
-package net.sf.practicalxml.misc;
-
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
-
-import javax.xml.XMLConstants;
-import javax.xml.namespace.NamespaceContext;
-
-
-/**
- * Implements a bidirectional lookup between a single namespace URI and its
- * prefix, for use with XPath expressions. Does not support the "default"
- * namespace, unless explicitly created with a prefix "".
- */
-public class SimpleNamespaceResolver
-implements NamespaceContext
-{
- private final String _prefix;
- private final String _nsURI;
- private final List<String> _prefixes;
-
- public SimpleNamespaceResolver(String prefix, String nsURI)
- {
- if (prefix == null)
- throw new IllegalArgumentException("prefix may not be null");
- if (nsURI == null)
- throw new IllegalArgumentException("nsURI may not be null");
-
- _prefix = prefix;
- _nsURI = nsURI;
- _prefixes = Arrays.asList(prefix);
- }
-
-
-//----------------------------------------------------------------------------
-// NamespaceContext implementation
-//----------------------------------------------------------------------------
-
- /**
- * Returns the namespace URI bound to the passed prefix, <code>null</code>
- * if the prefix does not correspond to the binding defined by this
- * instance. Also supports "standard" bindings; see JDK doc for details.
- */
- public String getNamespaceURI(String prefix)
- {
- if (prefix == null)
- throw new IllegalArgumentException("prefix may not be null");
- else if (_prefix.equals(prefix))
- return _nsURI;
- else if (XMLConstants.XML_NS_PREFIX.equals(prefix))
- return XMLConstants.XML_NS_URI;
- else if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix))
- return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
- else
- return null;
- }
-
-
- /**
- * Returns the prefix bound to the passed namespace URI, <code>null</code>
- * if the URI does not correspond to the binding defined by this instance.
- * Also supports "standard" bindings; see JDK doc for details.
- */
- public String getPrefix(String nsURI)
- {
- if (nsURI == null)
- throw new IllegalArgumentException("nsURI may not be null");
- else if (nsURI.equals(_nsURI))
- return _prefix;
- else if (nsURI.equals(XMLConstants.XML_NS_URI))
- return XMLConstants.XML_NS_PREFIX;
- else if (nsURI.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI))
- return XMLConstants.XMLNS_ATTRIBUTE;
- else
- return null;
- }
-
-
- /**
- * Returns an iterator over prefixes for the passed URI, an empty iterator
- * if the URI does not correspond to the binding defined by this instance.
- * Also supports "standard" bindings; see JDK doc for details.
- */
- public Iterator<String> getPrefixes(String nsURI)
- {
- String prefix = getPrefix(nsURI);
- if (_prefix.equals(prefix))
- return _prefixes.iterator();
- else if (prefix == null)
- return Collections.<String>emptyList().iterator();
- else
- return Arrays.asList(prefix).iterator();
- }
-
-
-//----------------------------------------------------------------------------
-// Object overrides
-//----------------------------------------------------------------------------
-
- /**
- * Two instances are considered equal if they have the same mappings,
- * including default namespace.
- */
- @Override
- public final boolean equals(Object obj)
- {
- if (obj instanceof SimpleNamespaceResolver)
- {
- SimpleNamespaceResolver that = (SimpleNamespaceResolver)obj;
- return this._prefix.equals(that._prefix)
- && this._nsURI.equals(that._nsURI);
- }
- return false;
- }
-
-
- @Override
- public int hashCode()
- {
- return _prefix.hashCode() ^ _nsURI.hashCode();
- }
-
-
- /**
- * Returns a string containing the the <code>xmlns</code> attribute
- * specifications that would result from this resolver.
- */
- @Override
- public String toString()
- {
- StringBuilder buf = new StringBuilder(_prefix.length() + _nsURI.length() + 10);
- if ("".equals(_prefix))
- {
- buf.append("xmlns=\"").append(_nsURI).append("\"");
- }
- else
- {
- buf.append("xmlns:").append(_prefix).append("=\"")
- .append(_nsURI).append("\"");
- }
- return buf.toString();
- }
-}
Copied: trunk/src/main/java/net/sf/practicalxml/xpath/NamespaceResolver.java (from rev 53, trunk/src/main/java/net/sf/practicalxml/misc/NamespaceResolver.java)
===================================================================
--- trunk/src/main/java/net/sf/practicalxml/xpath/NamespaceResolver.java (rev 0)
+++ trunk/src/main/java/net/sf/practicalxml/xpath/NamespaceResolver.java 2008-12-16 02:56:39 UTC (rev 54)
@@ -0,0 +1,261 @@
+package net.sf.practicalxml.xpath;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.SortedSet;
+import java.util.TreeMap;
+import java.util.TreeSet;
+import javax.xml.XMLConstants;
+import javax.xml.namespace.NamespaceContext;
+
+
+/**
+ * Maintains a bi-directional lookup table for mappings between namespace URIs
+ * and prefixes. Follows the "builder" pattern, in that the methods to add
+ * mappings may be chained: a resolver can be created and fully configured in
+ * a single expression.
+ * <p>
+ * Usage note: <code>NamespaceContext</code> allows multiple prefixes per URI,
+ * in keeping with the Namespace spec. This implementation supports that, but
+ * it's a bad idea to actually use this feature when writing an XPath. You'll
+ * be much happier if you limit yourself to a 1:1 mapping.
+ * <p>
+ * If you have a single namespace mapping, this implementation is overkill.
+ * Instead, use {@link SimpleNamespaceResolver}.
+ */
+public class NamespaceResolver
+implements NamespaceContext
+{
+ private final static SortedSet<String> DEFAULT_PREFIXES =
+ new TreeSet<String>();
+ private final static SortedSet<String> XML_NS_URI_PREFIXES =
+ new TreeSet<String>();
+ private final static SortedSet<String> XML_NS_ATTR_PREFIXES =
+ new TreeSet<String>();
+ static
+ {
+ DEFAULT_PREFIXES.add("");
+ XML_NS_URI_PREFIXES.add(XMLConstants.XML_NS_PREFIX);
+ XML_NS_ATTR_PREFIXES.add(XMLConstants.XMLNS_ATTRIBUTE);
+ }
+
+ private TreeMap<String,String> _prefix2ns = new TreeMap<String,String>();
+ private Map<String,SortedSet<String>> _ns2prefix = new HashMap<String,SortedSet<String>>();
+ private String _defaultNS = "";
+
+
+//----------------------------------------------------------------------------
+// Public methods
+//----------------------------------------------------------------------------
+
+ /**
+ * Adds a namespace to this resolver.
+ *
+ * @return The resolver instance, so that calls may be chained.
+ *
+ * @throws IllegalArgumentException if either <code>prefix</code>
+ * or <code>nsURI</code> is <code>null</code>.
+ */
+ public NamespaceResolver addNamespace(String prefix, String nsURI)
+ {
+ if (prefix == null)
+ throw new IllegalArgumentException("prefix may not be null");
+ if (nsURI == null)
+ throw new IllegalArgumentException("nsURI may not be null");
+
+ _prefix2ns.put(prefix, nsURI);
+ getPrefixSet(nsURI).add(prefix);
+ return this;
+ }
+
+
+ /**
+ * Sets the default namespace -- the namespace that will be returned
+ * when an empty string is passed to the resolver.
+ *
+ * @return The resolver instance, so that calls may be chained.
+ *
+ * @throws IllegalArgumentException if <code>nsURI</code> is
+ * <code>null</code>.
+ */
+ public NamespaceResolver setDefaultNamespace(String nsURI)
+ {
+ if (nsURI == null)
+ throw new IllegalArgumentException("nsURI may not be null");
+
+ _defaultNS = nsURI;
+ return this;
+ }
+
+
+ /**
+ * Returns the default namespace, an empty string if one has not yet
+ * been set.
+ */
+ public String getDefaultNamespace()
+ {
+ return _defaultNS;
+ }
+
+
+ /**
+ * Returns a list of all prefixes known to this resolver, in alphabetical
+ * order.
+ */
+ public List<String> getAllPrefixes()
+ {
+ return new ArrayList<String>(_prefix2ns.keySet());
+ }
+
+
+//----------------------------------------------------------------------------
+// NamespaceContext implementation
+//----------------------------------------------------------------------------
+
+ /**
+ * Returns the namespace URI bound to a given prefix, <code>null</code>
+ * if no URI is bound to the specified prefix. See interface doc for
+ * default bindings.
+ */
+ public String getNamespaceURI(String prefix)
+ {
+ if (prefix == null)
+ throw new IllegalArgumentException("prefix may not be null");
+ else if ("".equals(prefix))
+ return _defaultNS;
+ else if (XMLConstants.XML_NS_PREFIX.equals(prefix))
+ return XMLConstants.XML_NS_URI;
+ else if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix))
+ return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
+ else
+ return _prefix2ns.get(prefix);
+ }
+
+
+ /**
+ * Returns the first prefix in alphabetical order bound to this namespace
+ * URI, <code>null</code> if there is no binding for the namespace. See
+ * interface doc for default bindings.
+ */
+ public String getPrefix(String nsURI)
+ {
+ Iterator<String> itx = getPrefixes(nsURI);
+ return itx.hasNext() ? itx.next() : null;
+ }
+
+
+ /**
+ * Returns an iterator over all prefixes bound to this namespace URI, in
+ * alphabetical order, an empty iterator if there are no bindings. See
+ * interface doc for default bindings.
+ */
+ public Iterator<String> getPrefixes(String nsURI)
+ {
+ if (nsURI == null)
+ throw new IllegalArgumentException("nsURI may not be null");
+ else if (_defaultNS.equals(nsURI))
+ return DEFAULT_PREFIXES.iterator();
+ else if (XMLConstants.XML_NS_URI.equals(nsURI))
+ return XML_NS_URI_PREFIXES.iterator();
+ else if (XMLConstants.XMLNS_ATTRIBUTE_NS_URI.equals(nsURI))
+ return XML_NS_ATTR_PREFIXES.iterator();
+ else
+ return getPrefixSet(nsURI).iterator();
+ }
+
+
+//----------------------------------------------------------------------------
+// Object overrides
+//----------------------------------------------------------------------------
+
+ /**
+ * Two instances are considered equal if they have the same mappings,
+ * including default namespace.
+ */
+ @Override
+ public final boolean equals(Object obj)
+ {
+ if (this == obj)
+ return true;
+
+ if (obj instanceof NamespaceResolver)
+ {
+ NamespaceResolver that = (NamespaceResolver)obj;
+ return this._prefix2ns.equals(that._prefix2ns)
+ && this._defaultNS.equals(that._defaultNS);
+ }
+ return false;
+ }
+
+
+ @Override
+ public int hashCode()
+ {
+ // rely on these objects caching their hashcode
+ return _prefix2ns.hashCode() ^ _defaultNS.hashCode();
+ }
+
+
+ /**
+ * Returns a string containing the the <code>xmlns</code> attribute
+ * specifications that would result from this resolver.
+ */
+ @Override
+ public String toString()
+ {
+ StringBuilder buf = new StringBuilder(50 * _prefix2ns.size());
+ if (!"".equals(_defaultNS))
+ {
+ buf.append("xmlns=\"").append(_defaultNS).append("\"");
+ }
+ for (String prefix : getAllPrefixes())
+ {
+ if (buf.length() > 0)
+ buf.append(" ");
+ buf.append("xmlns:").append(prefix).append("=\"")
+ .append(_prefix2ns.get(prefix)).append("\"");
+ }
+ return buf.toString();
+ }
+
+
+ /**
+ * Returns a deep clone of this object, that can then be independently
+ * manipulated.
+ */
+ @Override
+ protected NamespaceResolver clone()
+ {
+ NamespaceResolver that = new NamespaceResolver()
+ .setDefaultNamespace(getDefaultNamespace());
+ for (String prefix : getAllPrefixes())
+ {
+ that.addNamespace(prefix, getNamespaceURI(prefix));
+ }
+ return that;
+ }
+
+
+//----------------------------------------------------------------------------
+// Internals
+//----------------------------------------------------------------------------
+
+
+ /**
+ * Returns the set of prefixes for a given namespace, creating a new
+ * entry if one doesn't already exist.
+ */
+ private SortedSet<String> getPrefixSet(String nsURI)
+ {
+ SortedSet<String> prefixes = _ns2prefix.get(nsURI);
+ if (prefixes == null)
+ {
+ prefixes = new TreeSet<String>();
+ _ns2prefix.put(nsURI, prefixes);
+ }
+ return prefixes;
+ }
+}
Copied: trunk/src/main/java/net/sf/practicalxml/xpath/SimpleNamespaceResolver.java (from rev 53, trunk/src/main/java/net/sf/practicalxml/misc/SimpleNamespaceResolver.java)
===================================================================
--- trunk/src/main/java/net/sf/practicalxml/xpath/SimpleNamespaceResolver.java (rev 0)
+++ trunk/src/main/java/net/sf/practicalxml/xpath/SimpleNamespaceResolver.java 2008-12-16 02:56:39 UTC (rev 54)
@@ -0,0 +1,145 @@
+package net.sf.practicalxml.xpath;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.NamespaceContext;
+
+
+/**
+ * Implements a bidirectional lookup between a single namespace URI and its
+ * prefix, for use with XPath expressions. Does not support the "default"
+ * namespace, unless explicitly created with a prefix "".
+ */
+public class SimpleNamespaceResolver
+implements NamespaceContext
+{
+ private final String _prefix;
+ private final String _nsURI;
+ private final List<String> _prefixes;
+
+ public SimpleNamespaceResolver(String prefix, String nsURI)
+ {
+ if (prefix == null)
+ throw new IllegalArgumentException("prefix may not be null");
+ if (nsURI == null)
+ throw new IllegalArgumentException("nsURI may not be null");
+
+ _prefix = prefix;
+ _nsURI = nsURI;
+ _prefixes = Arrays.asList(prefix);
+ }
+
+
+//----------------------------------------------------------------------------
+// NamespaceContext implementation
+//----------------------------------------------------------------------------
+
+ /**
+ * Returns the namespace URI bound to the passed prefix, <code>null</code>
+ * if the prefix does not correspond to the binding defined by this
+ * instance. Also supports "standard" bindings; see JDK doc for details.
+ */
+ public String getNamespaceURI(String prefix)
+ {
+ if (prefix == null)
+ throw new IllegalArgumentException("prefix may not be null");
+ else if (_prefix.equals(prefix))
+ return _nsURI;
+ else if (XMLConstants.XML_NS_PREFIX.equals(prefix))
+ return XMLConstants.XML_NS_URI;
+ else if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix))
+ return XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
+ else
+ return null;
+ }
+
+
+ /**
+ * Returns the prefix bound to the passed namespace URI, <code>null</code>
+ * if the URI does not correspond to the binding defined by this instance.
+ * Also supports "standard" bindings; see JDK doc for details.
+ */
+ public String getPrefix(String nsURI)
+ {
+ if (nsURI == null)
+ throw new IllegalArgumentException("nsURI may not be null");
+ else if (nsURI.equals(_nsURI))
+ return _prefix;
+ else if (nsURI.equals(XMLConstants.XML_NS_URI))
+ return XMLConstants.XML_NS_PREFIX;
+ else if (nsURI.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI))
+ return XMLConstants.XMLNS_ATTRIBUTE;
+ else
+ return null;
+ }
+
+
+ /**
+ * Returns an iterator over prefixes for the passed URI, an empty iterator
+ * if the URI does not correspond to the binding defined by this instance.
+ * Also supports "standard" bindings; see JDK doc for details.
+ */
+ public Iterator<String> getPrefixes(String nsURI)
+ {
+ String prefix = getPrefix(nsURI);
+ if (_prefix.equals(prefix))
+ return _prefixes.iterator();
+ else if (prefix == null)
+ return Collections.<String>emptyList().iterator();
+ else
+ return Arrays.asList(prefix).iterator();
+ }
+
+
+//----------------------------------------------------------------------------
+// Object overrides
+//----------------------------------------------------------------------------
+
+ /**
+ * Two instances are considered equal if they have the same mappings,
+ * including default namespace.
+ */
+ @Override
+ public final boolean equals(Object obj)
+ {
+ if (obj instanceof SimpleNamespaceResolver)
+ {
+ SimpleNamespaceResolver that = (SimpleNamespaceResolver)obj;
+ return this._prefix.equals(that._prefix)
+ && this._nsURI.equals(that._nsURI);
+ }
+ return false;
+ }
+
+
+ @Override
+ public int hashCode()
+ {
+ return _prefix.hashCode() ^ _nsURI.hashCode();
+ }
+
+
+ /**
+ * Returns a string containing the the <code>xmlns</code> attribute
+ * specifications that would result from this resolver.
+ */
+ @Override
+ public String toString()
+ {
+ StringBuilder buf = new StringBuilder(_prefix.length() + _nsURI.length() + 10);
+ if ("".equals(_prefix))
+ {
+ buf.append("xmlns=\"").append(_nsURI).append("\"");
+ }
+ else
+ {
+ buf.append("xmlns:").append(_prefix).append("=\"")
+ .append(_nsURI).append("\"");
+ }
+ return buf.toString();
+ }
+}
Added: trunk/src/main/java/net/sf/practicalxml/xpath/package.html
===================================================================
--- trunk/src/main/java/net/sf/practicalxml/xpath/package.html (rev 0)
+++ trunk/src/main/java/net/sf/practicalxml/xpath/package.html 2008-12-16 02:56:39 UTC (rev 54)
@@ -0,0 +1,5 @@
+<html>
+<body>
+ This package support classes that are primarily used for XPath evaluation.
+</body>
+</html>
\ No newline at end of file
Modified: trunk/src/test/java/net/sf/practicalxml/TestDomUtilGetPath.java
===================================================================
--- trunk/src/test/java/net/sf/practicalxml/TestDomUtilGetPath.java 2008-12-14 17:22:13 UTC (rev 53)
+++ trunk/src/test/java/net/sf/practicalxml/TestDomUtilGetPath.java 2008-12-16 02:56:39 UTC (rev 54)
@@ -7,8 +7,8 @@
import org.w3c.dom.Document;
import org.w3c.dom.Element;
-import net.sf.practicalxml.misc.NamespaceResolver;
-import net.sf.practicalxml.misc.SimpleNamespaceResolver;
+import net.sf.practicalxml.xpath.NamespaceResolver;
+import net.sf.practicalxml.xpath.SimpleNamespaceResolver;
/**
Modified: trunk/src/test/java/net/sf/practicalxml/TestSchemaUtil.java
===================================================================
--- trunk/src/test/java/net/sf/practicalxml/TestSchemaUtil.java 2008-12-14 17:22:13 UTC (rev 53)
+++ trunk/src/test/java/net/sf/practicalxml/TestSchemaUtil.java 2008-12-16 02:56:39 UTC (rev 54)
@@ -6,7 +6,7 @@
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
-import net.sf.practicalxml.misc.ExceptionErrorHandler;
+import net.sf.practicalxml.util.ExceptionErrorHandler;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
Deleted: trunk/src/test/java/net/sf/practicalxml/util/TestNamespaceResolver.java
===================================================================
--- trunk/src/test/java/net/sf/practicalxml/misc/TestNamespaceResolver.java 2008-12-14 17:22:13 UTC (rev 53)
+++ trunk/src/test/java/net/sf/practicalxml/util/TestNamespaceResolver.java 2008-12-16 02:56:39 UTC (rev 54)
@@ -1,308 +0,0 @@
-package net.sf.practicalxml.misc;
-
-import java.util.Iterator;
-import java.util.List;
-
-import javax.xml.XMLConstants;
-import javax.xml.namespace.NamespaceContext;
-
-import net.sf.practicalxml.AbstractTestCase;
-
-
-public class TestNamespaceResolver extends AbstractTestCase
-{
- public void testSingleNamespace() throws Exception
- {
- final String prefix = "foo";
- final String nsURI = "bar";
-
- NamespaceResolver resolv = new NamespaceResolver();
-
- assertSame(resolv, resolv.addNamespace(prefix, nsURI));
-
- assertEquals(nsURI, resolv.getNamespaceURI(prefix));
- assertEquals(prefix, resolv.getPrefix(nsURI));
-
- Iterator<String> itx = resolv.getPrefixes(nsURI);
- assertEquals(prefix, itx.next());
- assertFalse(itx.hasNext());
- }
-
-
- public void testTwoNamespaces() throws Exception
- {
- final String prefix1 = "foo";
- final String nsURI1 = "bar";
- final String prefix2 = "argle";
- final String nsURI2 = "bargle";
-
- NamespaceContext resolv = new NamespaceResolver()
- .addNamespace(prefix1, nsURI1)
- .addNamespace(prefix2, nsURI2);
-
- assertEquals(nsURI1, resolv.getNamespaceURI(prefix1));
- assertEquals(nsURI2, resolv.getNamespaceURI(prefix2));
-
- assertEquals(prefix1, resolv.getPrefix(nsURI1));
- assertEquals(prefix2, resolv.getPrefix(nsURI2));
-
- Iterator<String> itx1 = resolv.getPrefixes(nsURI1);
- assertEquals(prefix1, itx1.next());
- assertFalse(itx1.hasNext());
-
- Iterator<String> itx2 = resolv.getPrefixes(nsURI2);
- assertEquals(prefix2, itx2.next());
- assertFalse(itx2.hasNext());
- }
-
-
- public void testOneNamespaceTwoPrefixes() throws Exception
- {
- final String prefix1 = "foo";
- final String prefix2 = "argle";
- final String nsURI = "bargle";
-
- NamespaceContext resolv = new NamespaceResolver()
- .addNamespace(prefix1, nsURI)
- .addNamespace(prefix2, nsURI);
-
- assertEquals(nsURI, resolv.getNamespaceURI(prefix1));
- assertEquals(nsURI, resolv.getNamespaceURI(prefix2));
-
- assertEquals(prefix2, resolv.getPrefix(nsURI));
-
- Iterator<String> itx1 = resolv.getPrefixes(nsURI);
- assertEquals(prefix2, itx1.next());
- assertEquals(prefix1, itx1.next());
- assertFalse(itx1.hasNext());
- }
-
-
- public void testUnboundNamespace() throws Exception
- {
- NamespaceContext resolv = new NamespaceResolver();
-
- assertNull(resolv.getNamespaceURI("argle"));
- assertNull(resolv.getPrefix("argle"));
- assertFalse(resolv.getPrefixes("argle").hasNext());
- }
-
-
- public void testInvalidNamespace() throws Exception
- {
- NamespaceResolver resolv = new NamespaceResolver();
-
- try
- {
- resolv.addNamespace(null, "foo");
- fail("accepted null prefix");
- }
- catch (IllegalArgumentException e)
- {
- // success
- }
-
- try
- {
- resolv.addNamespace("foo", null);
- fail("accepted null nsURI");
- }
- catch (IllegalArgumentException e)
- {
- // success
- }
-
- try
- {
- resolv.setDefaultNamespace(null);
- fail("accepted null nsURI");
- }
- catch (IllegalArgumentException e)
- {
- // success
- }
- }
-
-
- public void testStandardMappings() throws Exception
- {
- NamespaceContext resolv = new NamespaceResolver();
-
- assertEquals(XMLConstants.XML_NS_URI, resolv.getNamespaceURI(XMLConstants.XML_NS_PREFIX));
- assertEquals(XMLConstants.XML_NS_PREFIX, resolv.getPrefix(XMLConstants.XML_NS_URI));
- Iterator<String> itx1 = resolv.getPrefixes(XMLConstants.XML_NS_URI);
- assertEquals(XMLConstants.XML_NS_PREFIX, itx1.next());
- assertFalse(itx1.hasNext());
-
- assertEquals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, resolv.getNamespaceURI(XMLConstants.XMLNS_ATTRIBUTE));
- assertEquals(XMLConstants.XMLNS_ATTRIBUTE, resolv.getPrefix(XMLConstants.XMLNS_ATTRIBUTE_NS_URI));
- Iterator<String> itx2 = resolv.getPrefixes(XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
- assertEquals(XMLConstants.XMLNS_ATTRIBUTE, itx2.next());
- assertFalse(itx2.hasNext());
-
- try
- {
- resolv.getNamespaceURI(null);
- fail("should throw IllegalArgumentException");
- }
- catch (IllegalArgumentException e)
- {
- // success
- }
-
- try
- {
- resolv.getPrefix(null);
- fail("should throw IllegalArgumentException");
- }
- catch (IllegalArgumentException e)
- {
- // success
- }
-
- try
- {
- resolv.getPrefixes(null);
- fail("should throw IllegalArgumentException");
- }
- catch (IllegalArgumentException e)
- {
- // success
- }
- }
-
-
- public void testDefaultNamespace() throws Exception
- {
- NamespaceResolver resolv = new NamespaceResolver();
-
- assertEquals("", resolv.getDefaultNamespace());
- assertEquals("", resolv.getNamespaceURI(""));
- assertEquals("", resolv.getPrefix(""));
- Iterator<String> itx1 = resolv.getPrefixes("");
- assertEquals("", itx1.next());
- assertFalse(itx1.hasNext());
-
- assertSame(resolv, resolv.setDefaultNamespace("foo"));
-
- assertEquals("foo", resolv.getDefaultNamespace());
- assertEquals("foo", resolv.getNamespaceURI(""));
- assertEquals("", resolv.getPrefix("foo"));
- Iterator<String> itx2 = resolv.getPrefixes("foo");
- assertEquals("", itx2.next());
- assertFalse(itx2.hasNext());
-
- assertSame(resolv, resolv.setDefaultNamespace("bar"));
-
- assertEquals("bar", resolv.getDefaultNamespace());
- assertEquals("bar", resolv.getNamespaceURI(""));
- assertEquals("", resolv.getPrefix("bar"));
- Iterator<String> itx3 = resolv.getPrefixes("bar");
- assertEquals("", itx3.next());
- assertFalse(itx3.hasNext());
- }
-
-
- public void testGetAllPrefixes() throws Exception
- {
- NamespaceResolver resolv = new NamespaceResolver()
- .addNamespace("foo", "bar")
- .addNamespace("baz", "bar")
- .addNamespace("baz", "biggles") // intentional overwrite
- .addNamespace("argle", "bargle");
-
- List<String> prefixes = resolv.getAllPrefixes();
- assertEquals(3, prefixes.size());
-
- Iterator<String> itx = prefixes.iterator();
- assertEquals("argle", itx.next());
- assertEquals("baz", itx.next());
- assertEquals("foo", itx.next());
- }
-
-
- public void testEqualsAndHashCode() throws Exception
- {
- Object obj1 = new NamespaceResolver()
- .addNamespace("foo", "bar")
- .setDefaultNamespace("zippy");
- Object obj2 = new NamespaceResolver()
- .addNamespace("foo", "bar")
- .setDefaultNamespace("zippy");
- Object obj3 = new NamespaceResolver()
- .addNamespace("foo", "bar");
- Object obj4 = new NamespaceResolver()
- .addNamespace("argle", "bargle");
-
- assertFalse(obj1.equals(new Object()));
-
- assertTrue(obj1.equals(obj2));
- assertTrue(obj2.equals(obj1));
- assertEquals(obj1.hashCode(), obj2.hashCode());
-
- assertFalse(obj1.equals(obj3));
- assertFalse(obj3.equals(obj1));
-
- assertFalse(obj3.equals(obj4));
- assertFalse(obj4.equals(obj3));
-
- // this works today ... assume that the underlying calcs don't change
- assertFalse(obj3.hashCode() == obj4.hashCode());
- }
-
-
- public void testToString() throws Exception
- {
- NamespaceResolver resolv = new NamespaceResolver();
- String str0 = resolv.toString();
- assertEquals(0, str0.length());
-
- resolv.setDefaultNamespace("foo");
- String str1 = resolv.toString();
- assertTrue(str1.contains("xmlns=\"foo\""));
- assertEquals(1, str1.split(" +").length);
-
- resolv.addNamespace("argle", "bargle");
- String str2 = resolv.toString();
- assertTrue(str2.contains("xmlns=\"foo\""));
- assertTrue(str2.contains("xmlns:argle=\"bargle\""));
- assertEquals(2, str2.split(" +").length);
-
- resolv.addNamespace("zippy", "pinhead");
- String str3 = resolv.toString();
- assertTrue(str3.contains("xmlns=\"foo\""));
- assertTrue(str3.contains("xmlns:argle=\"bargle\""));
- assertTrue(str3.contains("xmlns:zippy=\"pinhead\""));
- assertEquals(3, str3.split(" +").length);
- }
-
-
- public void testClone() throws Exception
- {
- NamespaceResolver resolv1 = new NamespaceResolver()
- .setDefaultNamespace("foo")
- .addNamespace("argle", "bargle");
-
- NamespaceResolver resolv2 = resolv1.clone();
- assertNotSame(resolv1, resolv2);
- assertEquals(resolv1, resolv2);
-
- resolv2.setDefaultNamespace("bar");
- assertFalse(resolv1.equals(resolv2));
- assertEquals("foo", resolv1.getDefaultNamespace());
- assertEquals("bar", resolv2.getDefaultNamespace());
-
- resolv2.addNamespace("argle", "zargle");
- assertEquals("bargle", resolv1.getNamespaceURI("argle"));
- assertEquals("zargle", resolv2.getNamespaceURI("argle"));
-
- resolv2.addNamespace("wargle", "qwerty");
- assertNull(resolv1.getPrefix("qwerty"));
- assertNull(resolv1.getNamespaceURI("wargle"));
-
- resolv1.addNamespace("wargle", "asdfg");
- assertNull(resolv2.getPrefix("asdfg"));
- assertEquals("qwerty", resolv2.getNamespaceURI("wargle"));
- }
-
-}
Modified: trunk/src/test/java/net/sf/practicalxml/util/TestNodeListIterable.java
===================================================================
--- trunk/src/test/java/net/sf/practicalxml/misc/TestNodeListIterable.java 2008-12-14 17:22:13 UTC (rev 53)
+++ trunk/src/test/java/net/sf/practicalxml/util/TestNodeListIterable.java 2008-12-16 02:56:39 UTC (rev 54)
@@ -1,11 +1,10 @@
-package net.sf.practicalxml.misc;
+package net.sf.practicalxml.util;
import java.util.Iterator;
import java.util.NoSuchElementException;
import net.sf.practicalxml.AbstractTestCase;
import net.sf.practicalxml.DomUtil;
-import net.sf.practicalxml.misc.NodeListIterable;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
Deleted: trunk/src/test/java/net/sf/practicalxml/util/TestSimpleNamespaceResolver.java
===================================================================
--- trunk/src/test/java/net/sf/practicalxml/misc/TestSimpleNamespaceResolver.java 2008-12-14 17:22:13 UTC (rev 53)
+++ trunk/src/test/java/net/sf/practicalxml/util/TestSimpleNamespaceResolver.java 2008-12-16 02:56:39 UTC (rev 54)
@@ -1,153 +0,0 @@
-package net.sf.practicalxml.misc;
-
-import java.util.Iterator;
-import javax.xml.XMLConstants;
-import javax.xml.namespace.NamespaceContext;
-
-import net.sf.practicalxml.AbstractTestCase;
-
-
-public class TestSimpleNamespaceResolver extends AbstractTestCase
-{
- public void testInvalidConstruction() throws Exception
- {
- try
- {
- new SimpleNamespaceResolver(null, "foo");
- fail("accepted null prefix");
- }
- catch (IllegalArgumentException e)
- {
- // success
- }
-
- try
- {
- new SimpleNamespaceResolver("foo", null);
- fail("accepted null nsURI");
- }
- catch (IllegalArgumentException e)
- {
- // success
- }
- }
-
-
- public void testLookup() throws Exception
- {
- final String prefix = "foo";
- final String nsURI = "bar";
-
- NamespaceContext resolv = new SimpleNamespaceResolver(prefix, nsURI);
-
- assertEquals(nsURI, resolv.getNamespaceURI(prefix));
- assertEquals(prefix, resolv.getPrefix(nsURI));
-
- Iterator<String> itx = resolv.getPrefixes(nsURI);
- assertEquals(prefix, itx.next());
- assertFalse(itx.hasNext());
- }
-
-
- public void testDefaultNamespace() throws Exception
- {
- final String prefix = "";
- final String nsURI = "bar";
-
- NamespaceContext resolv = new SimpleNamespaceResolver(prefix, nsURI);
-
- assertEquals(nsURI, resolv.getNamespaceURI(prefix));
- assertEquals(prefix, resolv.getPrefix(nsURI));
-
- Iterator<String> itx = resolv.getPrefixes(nsURI);
- assertEquals(prefix, itx.next());
- assertFalse(itx.hasNext());
- }
-
-
- public void testUnboundNamespace() throws Exception
- {
- NamespaceContext resolv = new SimpleNamespaceResolver("foo", "bar");
-
- assertNull(resolv.getNamespaceURI("argle"));
- assertNull(resolv.getPrefix("argle"));
- assertFalse(resolv.getPrefixes("argle").hasNext());
- }
-
-
- public void testStandardMappings() throws Exception
- {
- NamespaceContext resolv = new SimpleNamespaceResolver("foo", "bar");
-
- assertEquals(XMLConstants.XML_NS_URI, resolv.getNamespaceURI(XMLConstants.XML_NS_PREFIX));
- assertEquals(XMLConstants.XML_NS_PREFIX, resolv.getPrefix(XMLConstants.XML_NS_URI));
- Iterator<String> itx1 = resolv.getPrefixes(XMLConstants.XML_NS_URI);
- assertEquals(XMLConstants.XML_NS_PREFIX, itx1.next());
- assertFalse(itx1.hasNext());
-
- assertEquals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, resolv.getNamespaceURI(XMLConstants.XMLNS_ATTRIBUTE));
- assertEquals(XMLConstants.XMLNS_ATTRIBUTE, resolv.getPrefix(XMLConstants.XMLNS_ATTRIBUTE_NS_URI));
- Iterator<String> itx2 = resolv.getPrefixes(XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
- assertEquals(XMLConstants.XMLNS_ATTRIBUTE, itx2.next());
- assertFalse(itx2.hasNext());
-
- try
- {
- resolv.getNamespaceURI(null);
- fail("should throw IllegalArgumentException");
- }
- catch (IllegalArgumentException e)
- {
- // success
- }
-
- try
- {
- resolv.getPrefix(null);
- fail("should throw IllegalArgumentException");
- }
- catch (IllegalArgumentException e)
- {
- // success
- }
-
- try
- {
- resolv.getPrefixes(null);
- fail("should throw IllegalArgumentException");
- }
- catch (IllegalArgumentException e)
- {
- // success
- }
- }
-
-
- public void testEqualsAndHashCode() throws Exception
- {
- Object obj1 = new SimpleNamespaceResolver("foo", "bar");
- Object obj2 = new SimpleNamespaceResolver("foo", "bar");
- Object obj3 = new SimpleNamespaceResolver("argle", "bargle");
-
- assertFalse(obj1.equals(new Object()));
-
- assertTrue(obj1.equals(obj2));
- assertTrue(obj2.equals(obj1));
- assertTrue(obj1.hashCode() == obj2.hashCode());
-
- assertFalse(obj1.equals(obj3));
- assertFalse(obj3.equals(obj1));
- // this works today ... assume that the underlying calcs don't change
- assertFalse(obj1.hashCode() == obj3.hashCode());
- }
-
-
- public void testToString() throws Exception
- {
- String str1 = new SimpleNamespaceResolver("", "foo").toString();
- assertEquals("xmlns=\"foo\"", str1);
-
- String str2 = new SimpleNamespaceResolver("foo", "bar").toString();
- assertEquals("xmlns:foo=\"bar\"", str2);
- }
-}
Copied: trunk/src/test/java/net/sf/practicalxml/xpath/TestNamespaceResolver.java (from rev 53, trunk/src/test/java/net/sf/practicalxml/misc/TestNamespaceResolver.java)
===================================================================
--- trunk/src/test/java/net/sf/practicalxml/xpath/TestNamespaceResolver.java (rev 0)
+++ trunk/src/test/java/net/sf/practicalxml/xpath/TestNamespaceResolver.java 2008-12-16 02:56:39 UTC (rev 54)
@@ -0,0 +1,308 @@
+package net.sf.practicalxml.xpath;
+
+import java.util.Iterator;
+import java.util.List;
+
+import javax.xml.XMLConstants;
+import javax.xml.namespace.NamespaceContext;
+
+import net.sf.practicalxml.AbstractTestCase;
+
+
+public class TestNamespaceResolver extends AbstractTestCase
+{
+ public void testSingleNamespace() throws Exception
+ {
+ final String prefix = "foo";
+ final String nsURI = "bar";
+
+ NamespaceResolver resolv = new NamespaceResolver();
+
+ assertSame(resolv, resolv.addNamespace(prefix, nsURI));
+
+ assertEquals(nsURI, resolv.getNamespaceURI(prefix));
+ assertEquals(prefix, resolv.getPrefix(nsURI));
+
+ Iterator<String> itx = resolv.getPrefixes(nsURI);
+ assertEquals(prefix, itx.next());
+ assertFalse(itx.hasNext());
+ }
+
+
+ public void testTwoNamespaces() throws Exception
+ {
+ final String prefix1 = "foo";
+ final String nsURI1 = "bar";
+ final String prefix2 = "argle";
+ final String nsURI2 = "bargle";
+
+ NamespaceContext resolv = new NamespaceResolver()
+ .addNamespace(prefix1, nsURI1)
+ .addNamespace(prefix2, nsURI2);
+
+ assertEquals(nsURI1, resolv.getNamespaceURI(prefix1));
+ assertEquals(nsURI2, resolv.getNamespaceURI(prefix2));
+
+ assertEquals(prefix1, resolv.getPrefix(nsURI1));
+ assertEquals(prefix2, resolv.getPrefix(nsURI2));
+
+ Iterator<String> itx1 = resolv.getPrefixes(nsURI1);
+ assertEquals(prefix1, itx1.next());
+ assertFalse(itx1.hasNext());
+
+ Iterator<String> itx2 = resolv.getPrefixes(nsURI2);
+ assertEquals(prefix2, itx2.next());
+ assertFalse(itx2.hasNext());
+ }
+
+
+ public void testOneNamespaceTwoPrefixes() throws Exception
+ {
+ final String prefix1 = "foo";
+ final String prefix2 = "argle";
+ final String nsURI = "bargle";
+
+ NamespaceContext resolv = new NamespaceResolver()
+ .addNamespace(prefix1, nsURI)
+ .addNamespace(prefix2, nsURI);
+
+ assertEquals(nsURI, resolv.getNamespaceURI(prefix1));
+ assertEquals(nsURI, resolv.getNamespaceURI(prefix2));
+
+ assertEquals(prefix2, resolv.getPrefix(nsURI));
+
+ Iterator<String> itx1 = resolv.getPrefixes(nsURI);
+ assertEquals(prefix2, itx1.next());
+ assertEquals(prefix1, itx1.next());
+ assertFalse(itx1.hasNext());
+ }
+
+
+ public void testUnboundNamespace() throws Exception
+ {
+ NamespaceContext resolv = new NamespaceResolver();
+
+ assertNull(resolv.getNamespaceURI("argle"));
+ assertNull(resolv.getPrefix("argle"));
+ assertFalse(resolv.getPrefixes("argle").hasNext());
+ }
+
+
+ public void testInvalidNamespace() throws Exception
+ {
+ NamespaceResolver resolv = new NamespaceResolver();
+
+ try
+ {
+ resolv.addNamespace(null, "foo");
+ fail("accepted null prefix");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // success
+ }
+
+ try
+ {
+ resolv.addNamespace("foo", null);
+ fail("accepted null nsURI");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // success
+ }
+
+ try
+ {
+ resolv.setDefaultNamespace(null);
+ fail("accepted null nsURI");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // success
+ }
+ }
+
+
+ public void testStandardMappings() throws Exception
+ {
+ NamespaceContext resolv = new NamespaceResolver();
+
+ assertEquals(XMLConstants.XML_NS_URI, resolv.getNamespaceURI(XMLConstants.XML_NS_PREFIX));
+ assertEquals(XMLConstants.XML_NS_PREFIX, resolv.getPrefix(XMLConstants.XML_NS_URI));
+ Iterator<String> itx1 = resolv.getPrefixes(XMLConstants.XML_NS_URI);
+ assertEquals(XMLConstants.XML_NS_PREFIX, itx1.next());
+ assertFalse(itx1.hasNext());
+
+ assertEquals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, resolv.getNamespaceURI(XMLConstants.XMLNS_ATTRIBUTE));
+ assertEquals(XMLConstants.XMLNS_ATTRIBUTE, resolv.getPrefix(XMLConstants.XMLNS_ATTRIBUTE_NS_URI));
+ Iterator<String> itx2 = resolv.getPrefixes(XMLConstants.XMLNS_ATTRIBUTE_NS_URI);
+ assertEquals(XMLConstants.XMLNS_ATTRIBUTE, itx2.next());
+ assertFalse(itx2.hasNext());
+
+ try
+ {
+ resolv.getNamespaceURI(null);
+ fail("should throw IllegalArgumentException");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // success
+ }
+
+ try
+ {
+ resolv.getPrefix(null);
+ fail("should throw IllegalArgumentException");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // success
+ }
+
+ try
+ {
+ resolv.getPrefixes(null);
+ fail("should throw IllegalArgumentException");
+ }
+ catch (IllegalArgumentException e)
+ {
+ // success
+ }
+ }
+
+
+ public void testDefaultNamespace() throws Exception
+ {
+ NamespaceResolver resolv = new NamespaceResolver();
+
+ assertEquals("", resolv.getDefaultNamespace());
+ assertEquals("", resolv.getNamespaceURI(""));
+ assertEquals("", resolv.getPrefix(""));
+ Iterator<String> itx1 = resolv.getPrefixes("");
+ assertEquals("", itx1.next());
+ assertFalse(itx1.hasNext());
+
+ assertSame(resolv, resolv.setDefaultNamespace("foo"));
+
+ assertEquals("foo", resolv.getDefaultNamespace());
+ assertEquals("foo", resolv.getNamespaceURI(""));
+ assertEquals("", resolv.getPrefix("foo"));
+ Iterator<String> itx2 = resolv.getPrefixes("foo");
+ assertEquals("", itx2.next());
+ assertFalse(itx2.hasNext());
+
+ assertSame(resolv, resolv.setDefaultNamespace("bar"));
+
+ assertEquals("bar", resolv.getDefaultNamespace());
+ assertEquals("bar", resolv.getNamespaceURI(""));
+ assertEquals("", resolv.getPrefix("bar"));
+ Iterator<String> itx3 = resolv.getPrefixes("bar");
+ assertEquals("", itx3.next());
+ assertFalse(itx3.hasNext());
+ }
+
+
+ public void testGetAllPrefixes() throws Exception
+ {
+ NamespaceResolver resolv = new NamespaceResolver()
+ .addNamespace("foo", "bar")
+ .addNamespace("baz", "bar")
+ .addNamespace("baz", "biggles") // intentional overwrite
+ .addNamespace("argle", "bargle");
+
+ List<String> prefixes = resolv.getAllPrefixes();
+ assertEquals(3, prefixes.size());
+
+ Iterator<String> itx = prefixes.iterator();
+ assertEquals("argle", itx.next());
+ assertEquals("baz", itx.next());
+ assertEquals("foo", itx.next());
+ }
+
+
+ public void testEqualsAndHashCode() throws Exception
+ {
+ Object obj1 = new NamespaceResolver()
+ .addNamespace("foo", "bar")
+ .setDefaultNamespace("zippy");
+ Object obj2 = new NamespaceResolver()
+ .addNamespace("foo", "bar")
+ .setDefaultNamespace("zippy");
+ Object obj3 = new NamespaceResolver()
+ .addNamespace("foo", "bar");
+ Object obj4 = new NamespaceResolver()
+ .addNamespace("argle", "bargle");
+
+ assertFalse(obj1.equals(new Object()));
+
+ assertTrue(obj1.equals(obj2));
+ assertTrue(obj2.equals(obj1));
+ assertEquals(obj1.hashCode(), obj2.hashCode());
+
+ assertFalse(obj1.equals(obj3));
+ assertFalse(obj3.equals(obj1));
+
+ assertFalse(obj3.equals(obj4));
+ assertFalse(obj4.equals(obj3));
+
+ // this works today ... assume that the underlying calcs don't change
+ assertFalse(obj3.hashCode() == obj4.hashCode());
+ }
+
+
+ public void testToString() throws Exception
+ {
+ NamespaceResolver resolv = new NamespaceResolver();
+ String str0 = resolv.toString();
+ assertEquals(0, str0.length());
+
+ resolv.setDefaultNamespace("foo"...
[truncated message content] |