📄 namespacenode.java
字号:
* namespace-qualified.</p> * * @return null * @see #getLocalName */ public String getPrefix() { return null; } /** * Change the namespace prefix of this node (always fails). * * @param prefix the new prefix * @throws DOMException always thrown */ public void setPrefix(String prefix) throws DOMException { disallowModification(); } /** * Get the XPath name of the namespace node;; i.e. the * namespace prefix. * * @return the namespace prefix */ public String getLocalName () { return name; } /** * Test if this node has attributes. * * @return false */ public boolean hasAttributes () { return false; } /** * Throw a NO_MODIFICATION_ALLOWED_ERR DOMException. * * @throws DOMException always thrown */ private void disallowModification () throws DOMException { throw new DOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, "Namespace node may not be modified"); } //////////////////////////////////////////////////////////////////// // Override default methods from java.lang.Object. //////////////////////////////////////////////////////////////////// /** * Generate a hash code for a namespace node. * * @return a hash code for this node */ public int hashCode () { return hashCode(parent) + hashCode(name) + hashCode(value); } /** * Test for equivalence with another object. * * <p>Two Namespace nodes are considered equivalent if their parents, * names, and values are equal.</p> * * @param o the object to test for equality * @return true if the object is equivalent to this node, false * otherwise */ public boolean equals (Object o) { if (o == this) return true; else if (o == null) return false; else if (o instanceof NamespaceNode) { NamespaceNode ns = (NamespaceNode)o; return (equals(parent, ns.getParentNode()) && equals(name, ns.getNodeName()) && equals(value, ns.getNodeValue())); } else { return false; } } /** * Helper method for generating a hash code. * * @param o the object for generating a hash code (possibly null) * @return the object's hash code, or 0 if the object is null * @see java.lang.Object#hashCode */ private int hashCode (Object o) { return (o == null ? 0 : o.hashCode()); } /** * Helper method for comparing two objects. * * @param a the first object to compare (possibly null) * @param b the second object to compare (possibly null) * @return true if the objects are equivalent or are both null * @see java.lang.Object#equals */ private boolean equals (Object a, Object b) { return ((a == null && b == null) || (a != null && a.equals(b))); } //////////////////////////////////////////////////////////////////// // Internal state. //////////////////////////////////////////////////////////////////// private Node parent; private String name; private String value; //////////////////////////////////////////////////////////////////// // Inner class: empty node list. //////////////////////////////////////////////////////////////////// /** * A node list with no members. * * <p>This class is necessary for the {@link Node#getChildNodes} * method, which must return an empty node list rather than * null when there are no children.</p> */ private static class EmptyNodeList implements NodeList { /** * @see NodeList#getLength */ public int getLength () { return 0; } /** * @see NodeList#item */ public Node item(int index) { return null; } } //////////////////////////////////////////////////////////////////// // DOM Level 3 methods //////////////////////////////////////////////////////////////////// /** * Return the base URI of the document containing this node. * This only works in DOM 3. * * @return null */ public String getBaseURI() { Class clazz = Node.class; try { Class[] args = new Class[0]; Method getBaseURI = clazz.getMethod("getBaseURI", args); String base = (String) getBaseURI.invoke(this.getParentNode(), args); return base; } catch (Exception ex) { return null; } } /** * Compare relative position of this node to another nbode. (Always fails). * This method is included solely for compatibility with the superclass. * * @param other the node to compare to * * @return never * @throws DOMException NOT_SUPPORTED_ERR */ public short compareDocumentPosition(Node other) throws DOMException { DOMException ex = new DOMException( DOMException.NOT_SUPPORTED_ERR, "DOM level 3 interfaces are not fully implemented in Jaxen's NamespaceNode class" ); throw ex; } /** * Return the namespace URI. * * @return the namespace URI * @see #getNodeValue */ public String getTextContent() { return value; } /** * Change the value of this node (always fails). * This method is included solely for compatibility with the superclass. * * @param textContent the new content * @throws DOMException always */ public void setTextContent(String textContent) throws DOMException { disallowModification(); } /** * Returns true if and only if this object represents the same XPath namespace node * as the argument; that is, they have the same parent, the same prefix, and the * same URI. * * @param other the node to compare to * @return true if this object represents the same XPath namespace node * as other; false otherwise */ public boolean isSameNode(Node other) { boolean a = this.isEqualNode(other); // a bit flaky (should really be // this.getParentNode().isEqual(other.getParentNode()) // but we want this to compile in Java 1.4 without problems // Note that this will mess up code coverage since you can't cover both // branches in the same VM boolean b; Node thisParent = this.getParentNode(); Node thatParent = other.getParentNode(); try { Class clazz = Node.class; Class[] args = {clazz}; Method isEqual = clazz.getMethod("isEqual", args); Object[] args2 = new Object[1]; args2[0] = thatParent; Boolean result = (Boolean) isEqual.invoke(thisParent, args2); b = result.booleanValue(); } catch (NoSuchMethodException ex) { b = thisParent.equals(thatParent); } catch (InvocationTargetException ex) { b = thisParent.equals(thatParent); } catch (IllegalAccessException ex) { b = thisParent.equals(thatParent); } return a && b; } /** * Return the prefix bound to this namespace URI within the scope * of this node. * * @param namespaceURI the URI to find a prefix binding for * * @return a prefix matching this namespace URI * @throws UnsupportedOperationException in DOM 2 */ public String lookupPrefix(String namespaceURI) { // This could be fully implemented even in Java 1.4. See // http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/namespaces-algorithms.html#lookupNamespaceURIAlgo // It hardly seems worth the effort though. try { Class clazz = Node.class; Class[] argTypes = {String.class}; Method lookupPrefix = clazz.getMethod("lookupPrefix", argTypes); String[] args = {namespaceURI}; String result = (String) lookupPrefix.invoke(parent, args); return result; } catch (NoSuchMethodException ex) { throw new UnsupportedOperationException("Cannot lookup prefixes in DOM 2"); } catch (InvocationTargetException ex) { throw new UnsupportedOperationException("Cannot lookup prefixes in DOM 2"); } catch (IllegalAccessException ex) { throw new UnsupportedOperationException("Cannot lookup prefixes in DOM 2"); } } /** * Return true if the specified URI is the default namespace in * scope (always fails). This method is included solely for * compatibility with the superclass. * * @param namespaceURI the URI to check * * @return never * @throws UnsupportedOperationException always */ public boolean isDefaultNamespace(String namespaceURI) { return namespaceURI.equals(this.lookupNamespaceURI(null)); } /** * Return the namespace URI mapped to the specified * prefix within the scope of this namespace node. * * @param prefix the prefix to search for * * @return the namespace URI mapped to this prefix * @throws UnsupportedOperationException in DOM 2 */ public String lookupNamespaceURI(String prefix) { // This could be fully implemented even in Java 1.4. See // http://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/namespaces-algorithms.html#lookupNamespaceURIAlgo // It hardly seems worth the effort though. try { Class clazz = Node.class; Class[] argTypes = {String.class}; Method lookupNamespaceURI = clazz.getMethod("lookupNamespaceURI", argTypes); String[] args = {prefix}; String result = (String) lookupNamespaceURI.invoke(parent, args); return result; } catch (NoSuchMethodException ex) { throw new UnsupportedOperationException("Cannot lookup namespace URIs in DOM 2"); } catch (InvocationTargetException ex) { throw new UnsupportedOperationException("Cannot lookup namespace URIs in DOM 2"); } catch (IllegalAccessException ex) { throw new UnsupportedOperationException("Cannot lookup namespace URIs in DOM 2"); } } /** * Returns true if this object binds the same prefix to the same URI. * That is, this object has the same prefix and URI as the argument. * * @param arg the node to compare to * @return true if this object has the same prefix and URI as the argument; false otherwise */ public boolean isEqualNode(Node arg) { if (arg.getNodeType() == this.getNodeType()) { NamespaceNode other = (NamespaceNode) arg; if (other.name == null && this.name != null) return false; else if (other.name != null && this.name == null) return false; else if (other.value == null && this.value != null) return false; else if (other.value != null && this.value == null) return false; else if (other.name == null && this.name == null) { return other.value.equals(this.value); } return other.name.equals(this.name) && other.value.equals(this.value); } return false; } /** * Returns the value of the requested feature. Always returns null. * * @return null */ public Object getFeature(String feature, String version) { return null; } // XXX userdata needs testing private HashMap userData = new HashMap(); /** * Associates an object with a key. * * @param key the key by which the data will be retrieved * @param data the object to store with the key * @param handler ignored since namespace nodes cannot be imported, cloned, or renamed * * @return the value previously associated with this key; or null * if there isn't any such previous value */ public Object setUserData(String key, Object data, UserDataHandler handler) { Object oldValue = getUserData(key); userData.put(key, data); return oldValue; } /** * Returns the user data associated with the given key. * * @param key the lookup key * * @return the object associated with the key; or null if no such object is available */ public Object getUserData(String key) { return userData.get(key); } }// end of NamespaceNode.java
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -