⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 documentnavigator.java

📁 XML的解析、编译、查询等技术的JAVA源代码。
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package org.jaxen.dom;/* * $Header: /home/projects/jaxen/scm/jaxen/src/java/main/org/jaxen/dom/DocumentNavigator.java,v 1.56 2006/07/03 13:08:43 elharo Exp $ * $Revision: 1.56 $ * $Date: 2006/07/03 13:08:43 $ * * ==================================================================== * * Copyright 2000-2005 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: DocumentNavigator.java,v 1.56 2006/07/03 13:08:43 elharo Exp $*/import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import java.io.IOException;import java.util.HashMap;import java.util.Iterator;import java.util.NoSuchElementException;import org.jaxen.DefaultNavigator;import org.jaxen.FunctionCallException;import org.jaxen.Navigator;import org.jaxen.XPath;import org.jaxen.JaxenConstants;import org.w3c.dom.Attr;import org.w3c.dom.Document;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.w3c.dom.ProcessingInstruction;import org.xml.sax.SAXException;/** Interface for navigating around the W3C DOM Level 2 object model. * *  <p> *  This class is not intended for direct usage, but is *  used by the Jaxen engine during evaluation. *  </p> * *  <p>This class implements the {@link org.jaxen.DefaultNavigator} interface *  for the Jaxen XPath library.  This adapter allows the Jaxen *  library to be used to execute XPath queries against any object tree *  that implements the DOM level 2 interfaces.</p> * *  <p>Note: DOM level 2 does not include a node representing an XPath *  namespace node.  This navigator will return namespace nodes *  as instances of the custom {@link NamespaceNode} class, and *  users will have to check result sets to locate and isolate *  these.</p> * *  @author David Megginson *  @author James Strachan * *  @see XPath *  @see NamespaceNode */public class DocumentNavigator extends DefaultNavigator{        ////////////////////////////////////////////////////////////////////    // Constants.    ////////////////////////////////////////////////////////////////////    /**     *      */    private static final long serialVersionUID = 8460943068889528115L;         private final static DocumentNavigator SINGLETON = new DocumentNavigator();        ////////////////////////////////////////////////////////////////////    // Constructor.    ////////////////////////////////////////////////////////////////////    /**     * Default constructor.     */    public DocumentNavigator ()    {    }    /**     * Get a constant DocumentNavigator for efficiency.     *     * @return a constant instance of a DocumentNavigator.     */    public static Navigator getInstance ()    {        return SINGLETON;    }        ////////////////////////////////////////////////////////////////////    // Implementation of org.jaxen.DefaultNavigator.    ////////////////////////////////////////////////////////////////////    /**     * Get an iterator over all of this node's children.     *     * @param contextNode the context node for the child axis.     * @return a possibly-empty iterator (not null)     */    public Iterator getChildAxisIterator (Object contextNode)    {        return new NodeIterator ((Node)contextNode) {                protected Node getFirstNode (Node node)                {                    return node.getFirstChild();                }                protected Node getNextNode (Node node)                {                    return node.getNextSibling();                }            };    }    /**     * Get a (single-member) iterator over this node's parent.     *     * @param contextNode the context node for the parent axis     * @return a possibly-empty iterator (not null)     */    public Iterator getParentAxisIterator (Object contextNode)    {        Node node = (Node)contextNode;        if (node.getNodeType() == Node.ATTRIBUTE_NODE) {            return new NodeIterator (node) {                    protected Node getFirstNode (Node n)                    {                        // We can assume castability here because we've already                        // tested the node type.                        return ((Attr)n).getOwnerElement();                    }                    protected Node getNextNode (Node n) {                        return null;                    }                };        } else {            return new NodeIterator (node) {                    protected Node getFirstNode (Node n)                    {                        return n.getParentNode();                    }                    protected Node getNextNode (Node n) {                        return null;                    }                };        }    }            /**      * Return the XPath parent of this DOM node.     * XPath has slightly different definition of parent than DOM does.     * In particular, the parent of an attribute is not null.     *      * @param o      *      * @return the parent of the specified node; or null if     *     the node does not have a parent     */    public Object getParentNode(Object o) {        Node node = (Node) o;        if (node.getNodeType() == Node.ATTRIBUTE_NODE) {            return ((Attr) node).getOwnerElement();        }        return node.getParentNode();    }    /**     * Get an iterator over all following siblings.     *     * @param contextNode the context node for the sibling iterator     * @return a possibly-empty iterator (not null)     */    public Iterator getFollowingSiblingAxisIterator (Object contextNode)    {        return new NodeIterator ((Node)contextNode) {                protected Node getFirstNode (Node node)                {                    return getNextNode(node);                }                protected Node getNextNode (Node node) {                    return node.getNextSibling();                }            };    }    /**     * Get an iterator over all preceding siblings.     *     * @param contextNode the context node for the preceding sibling axis     * @return a possibly-empty iterator (not null)     */    public Iterator getPrecedingSiblingAxisIterator (Object contextNode)    {        return new NodeIterator ((Node)contextNode) {                protected Node getFirstNode (Node node)                {                    return getNextNode(node);                }                protected Node getNextNode (Node node) {                    return node.getPreviousSibling();                }            };    }    /**     * Get an iterator over all following nodes, depth-first.     *     * @param contextNode the context node for the following axis     * @return a possibly-empty iterator (not null)     */    public Iterator getFollowingAxisIterator (Object contextNode)    {        return new NodeIterator ((Node)contextNode) {                protected Node getFirstNode (Node node)                {                    if (node == null) {                        return null;                    }                    else {                        Node sibling = node.getNextSibling();                        if (sibling == null) {                            return getFirstNode(node.getParentNode());                        }                        else {                            return sibling;                        }                    }                }                protected Node getNextNode (Node node) {                    if (node == null) {                        return null;                    }                    else {                        Node n = node.getFirstChild();                        if (n == null) n = node.getNextSibling();                        if (n == null) return getFirstNode(node.getParentNode());                        else return n;                    }                }            };    }    /**     * Get an iterator over all attributes.     *     * @param contextNode the context node for the attribute axis     * @return a possibly-empty iterator (not null)     */    public Iterator getAttributeAxisIterator (Object contextNode)    {        if (isElement(contextNode)) {            return new AttributeIterator((Node)contextNode);        }         else {            return JaxenConstants.EMPTY_ITERATOR;        }    }    /**     * Get an iterator over all declared namespaces.     *     * <p>Note: this iterator is not live: it takes a snapshot     * and that snapshot remains static during the life of     * the iterator (i.e. it won't reflect subsequent changes     * to the DOM).</p>     *      * <p>     * In the event that the DOM is inconsistent; for instance a      * <code>pre:foo</code> element is declared by DOM to be in the      * http://www.a.com/ namespace but also has an      * <code>xmlns:pre="http://www.b.com"</code> attribute; then only      * one of the namespaces will be counted. This will be the intrinsic     * namespace of the <code>Element</code> or <code>Attr</code> object     * rather than the one provide by the contradictory namespace      * declaration attribute. In the event of a contradiction between two     * attributes on the same element--e.g. <code>pre:foo</code> in the     * http://www.a.com/ namespace and <code>pre:bar</code> in the      * http://www.b.com/ namespace--it is undefined which namespace     * will be returned.      * </p>     *     * @param contextNode the context node for the namespace axis     * @return a possibly-empty iterator (not null)     */    public Iterator getNamespaceAxisIterator (Object contextNode)    {        // Only elements have namespace nodes        if (isElement(contextNode)) {            HashMap nsMap = new HashMap();            // Starting at the current node, walk            // up to the root, noting the namespace            // declarations in scope.            for (Node n = (Node) contextNode;                 n != null;                 n = n.getParentNode()) {                

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -