📄 namespacenode.java
字号:
/* * $Header: /home/projects/jaxen/scm/jaxen/src/java/main/org/jaxen/dom/NamespaceNode.java,v 1.25 2006/07/03 11:14:05 elharo Exp $ * $Revision: 1.25 $ * $Date: 2006/07/03 11:14:05 $ * * ==================================================================== * * Copyright 2000-2002 bob mcwhirter & James Strachan. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * * Neither the name of the Jaxen Project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * ==================================================================== * This software consists of voluntary contributions made by many * individuals on behalf of the Jaxen Project and was originally * created by bob mcwhirter <bob@werken.com> and * James Strachan <jstrachan@apache.org>. For more information on the * Jaxen Project, please see <http://www.jaxen.org/>. * * $Id: NamespaceNode.java,v 1.25 2006/07/03 11:14:05 elharo Exp $ */////////////////////////////////////////////////////////////////////// Inner class for a Namespace node.////////////////////////////////////////////////////////////////////package org.jaxen.dom;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.HashMap;import org.jaxen.pattern.Pattern;import org.w3c.dom.DOMException;import org.w3c.dom.Document;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.w3c.dom.UserDataHandler;/** * Extension DOM2/DOM3 node type for a namespace node. * * <p>This class implements the DOM2 and DOM3 {@link Node} interface * to allow namespace nodes to be included in the result * set of an XPath selectNodes operation, even though DOM does * not model namespaces in scope as separate nodes.</p> * * <p> * While all of the DOM2 methods are implemented with reasonable * defaults, there will be some unexpected surprises, so users are * advised to test for NamespaceNodes and filter them out from the * result sets as early as possible. * </p> * * <ol> * * <li>The {@link #getNodeType} method returns {@link #NAMESPACE_NODE}, * which is not one of the usual DOM2 node types. Generic code may * fall unexpectedly out of switch statements, for example.</li> * * <li>The {@link #getOwnerDocument} method returns the owner document * of the parent node, but that owner document will know nothing about * the namespace node.</p> * * <li>The {@link #isSupported} method always returns false.</li> * * <li> The DOM3 methods sometimes throw UnsupportedOperationException. * They're here only to allow this class to be compiled with Java 1.5. * Do not call or rely on them.</li> * </ol> * * <p>All attempts to modify a <code>NamespaceNode</code> will fail with a {@link * DOMException} ({@link * DOMException#NO_MODIFICATION_ALLOWED_ERR}).</p> * * @author David Megginson * @author Elliotte Rusty Harold * @see DocumentNavigator */public class NamespaceNode implements Node{ /** * Constant: this is a NamespaceNode. * * @see #getNodeType */ public final static short NAMESPACE_NODE = Pattern.NAMESPACE_NODE; // FIXME "Note: Numeric codes up to 200 are reserved to W3C for possible future use." // We should be using higher codes. Here we're using 13, the same as DOM 3's type for XPathNamespace. // However, that's only a note not a recommendation. /** * Create a new NamespaceNode. * * @param parent the DOM node to which the namespace is attached * @param name the namespace prefix * @param value the namespace URI */ public NamespaceNode (Node parent, String name, String value) { this.parent = parent; this.name = name; this.value = value; } /** * Constructor. * * @param parent the DOM node to which the namespace is attached * @param attribute the DOM attribute object containing the * namespace declaration */ NamespaceNode (Node parent, Node attribute) { String attributeName = attribute.getNodeName(); if (attributeName.equals("xmlns")) { this.name = ""; } else if (attributeName.startsWith("xmlns:")) { this.name = attributeName.substring(6); // the part after "xmlns:" } else { // workaround for Crimson bug; Crimson incorrectly reports the prefix as the node name this.name = attributeName; } this.parent = parent; this.value = attribute.getNodeValue(); } //////////////////////////////////////////////////////////////////// // Implementation of org.w3c.dom.Node. //////////////////////////////////////////////////////////////////// /** * Get the namespace prefix. * * @return the namespace prefix, or "" for the default namespace */ public String getNodeName () { return name; } /** * Get the namespace URI. * * @return the namespace URI */ public String getNodeValue () { return value; } /** * Change the namespace URI (always fails). * * @param value the new URI * @throws DOMException always */ public void setNodeValue (String value) throws DOMException { disallowModification(); } /** * Get the node type. * * @return always {@link #NAMESPACE_NODE}. */ public short getNodeType () { return NAMESPACE_NODE; } /** * Get the parent node. * * <p>This method returns the element that was queried for Namespaces * in effect, <em>not</em> necessarily the actual element containing * the Namespace declaration.</p> * * @return the parent node (not null) */ public Node getParentNode () { return parent; } /** * Get the list of child nodes. * * @return an empty node list */ public NodeList getChildNodes () { return new EmptyNodeList(); } /** * Get the first child node. * * @return null */ public Node getFirstChild () { return null; } /** * Get the last child node. * * @return null */ public Node getLastChild () { return null; } /** * Get the previous sibling node. * * @return null */ public Node getPreviousSibling () { return null; } /** * Get the next sibling node. * * @return null */ public Node getNextSibling () { return null; } /** * Get the attribute nodes. * * @return null */ public NamedNodeMap getAttributes () { return null; } /** * Get the owner document. * * @return the owner document <em>of the parent node</em> */ public Document getOwnerDocument () { if (parent == null) return null; return parent.getOwnerDocument(); } /** * Insert a new child node (always fails). * * @param newChild the node to add * @param refChild ignored * @return never * @throws DOMException always * @see Node#insertBefore */ public Node insertBefore (Node newChild, Node refChild) throws DOMException { disallowModification(); return null; } /** * Replace a child node (always fails). * * @param newChild the node to add * @param oldChild the child node to replace * @return never * @throws DOMException always * @see Node#replaceChild */ public Node replaceChild (Node newChild, Node oldChild) throws DOMException { disallowModification(); return null; } /** * Remove a child node (always fails). * * @param oldChild the child node to remove * @return never * @throws DOMException always * @see Node#removeChild */ public Node removeChild(Node oldChild) throws DOMException { disallowModification(); return null; } /** * Append a new child node (always fails). * * @param newChild the node to add * @return never * @throws DOMException always * @see Node#appendChild */ public Node appendChild(Node newChild) throws DOMException { disallowModification(); return null; } /** * Test for child nodes. * * @return false */ public boolean hasChildNodes() { return false; } /** * Create a copy of this node. * * @param deep make a deep copy (no effect, since namespace nodes * don't have children). * @return a new copy of this namespace node */ public Node cloneNode (boolean deep) { return new NamespaceNode(parent, name, value); } /** * Normalize the text descendants of this node. * * <p>This method has no effect, since namespace nodes have no * descendants.</p> */ public void normalize () { // no op } /** * Test if a DOM2 feature is supported. (None are.) * * @param feature the feature name * @param version the feature version * @return false */ public boolean isSupported(String feature, String version) { return false; } /** * Get the namespace URI of this node. * * <p>Namespace declarations are not themselves * Namespace-qualified.</p> * * @return null */ public String getNamespaceURI() { return null; } /** * Get the namespace prefix of this node. * * <p>Namespace declarations are not themselves
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -