KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > html > xpath > NodeRelativeNavigator


1 /*
2  * Copyright (c) 2002, 2005 Gargoyle Software Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright notice,
10  * this list of conditions and the following disclaimer in the documentation
11  * and/or other materials provided with the distribution.
12  * 3. The end-user documentation included with the redistribution, if any, must
13  * include the following acknowledgment:
14  *
15  * "This product includes software developed by Gargoyle Software Inc.
16  * (http://www.GargoyleSoftware.com/)."
17  *
18  * Alternately, this acknowledgment may appear in the software itself, if
19  * and wherever such third-party acknowledgments normally appear.
20  * 4. The name "Gargoyle Software" must not be used to endorse or promote
21  * products derived from this software without prior written permission.
22  * For written permission, please contact info@GargoyleSoftware.com.
23  * 5. Products derived from this software may not be called "HtmlUnit", nor may
24  * "HtmlUnit" appear in their name, without prior written permission of
25  * Gargoyle Software Inc.
26  *
27  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
28  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
29  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARGOYLE
30  * SOFTWARE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
33  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
36  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37  */

38 package com.gargoylesoftware.htmlunit.html.xpath;
39
40 import org.jaxen.BaseXPath;
41 import org.jaxen.JaxenException;
42 import org.jaxen.XPath;
43
44 import com.gargoylesoftware.htmlunit.html.DomNode;
45
46 /**
47  * Implementation of the Jaxen Navigator to navigate through part of the HtmlUnit DOM object model
48  * in the context of XPath evaluation.
49  * <p>
50  * <em>This class allows to make evaluation of xpath be relative to a special node that is
51  * considered as the root. The rest of the html page is not considered.
52  * </em>
53  * <em>This class is not intended for direct usage, but is used by the Jaxen engine
54  * during evaluation.
55  * </em>
56  *
57  * @version $Revision: 100 $
58  * @author Marc Guillemot
59  * @author Mike Bowler
60  * @see HtmlUnitXPath
61  */

62 class NodeRelativeNavigator extends DocumentNavigator {
63     
64     private static final long serialVersionUID = 3833748784969691447L;
65     private final DomNode rootNode_;
66
67     /**
68      * Constructs a navigator that will see the provided node as the root
69      * of the hierarchy it has to walk through.
70      * @param node the node to consider as the root when navigating
71      */

72     public NodeRelativeNavigator(final DomNode node) {
73         rootNode_ = node;
74     }
75
76     /**
77      * @param xpath an xpath expression
78      * @return a parsed form of the given xpath string, which will be suitable
79      * for queries on DOM documents.
80      * @throws JaxenException if the expression could not be parsed
81      */

82     public XPath parseXPath (final String JavaDoc xpath) throws JaxenException {
83         return new BaseXPath(xpath, this);
84     }
85
86     /**
87      * Get the top-level document node.
88      *
89      * @param contextNode Any node in the document.
90      * @return The root node.
91      */

92     public Object JavaDoc getDocumentNode (final Object JavaDoc contextNode) {
93         return rootNode_;
94     }
95
96     /**
97      * Test if a node is a top-level document.
98      *
99      * @param object The target node.
100      * @return true if the node is the document root, false otherwise.
101      */

102     public boolean isDocument (final Object JavaDoc object) {
103         return (object == rootNode_);
104     }
105
106     /**
107      * Test if a node is an element.
108      *
109      * @param object The target node.
110      * @return true if the node is an element, false otherwise.
111      */

112     public boolean isElement (final Object JavaDoc object) {
113         return super.isElement(object) && (object != rootNode_);
114     }
115
116     /**
117      * Returns the element whose ID is given by elementId.
118      * If no such element exists, returns null.
119      * Attributes with the name "ID" are not of type ID unless so defined.
120      * Atribute types are only known if when the parser understands DTD's or
121      * schemas that declare attributes of type ID. When JAXP is used, you
122      * must call <code>setValidating(true)</code> on the
123      * DocumentBuilderFactory.
124      *
125      * @param contextNode a node from the document in which to look for the
126      * id
127      * @param elementId id to look for
128      *
129      * @return element whose ID is given by elementId, or null if no such
130      * element exists in the document or if the implementation
131      * does not know about attribute types
132      * @see javax.xml.parsers.DocumentBuilderFactory
133      */

134     public Object JavaDoc getElementById(final Object JavaDoc contextNode, final String JavaDoc elementId) {
135         final DomNode node = (DomNode) super.getElementById(contextNode, elementId);
136         if (isChild(node)) {
137             return node;
138         }
139         return null;
140     }
141
142     /**
143      * Tests if the document's node is a child of the node considered as the root by this Navigator
144      * @param node the node to test
145      * @return <code>true</code> if it is a child of the root.
146      */

147     private boolean isChild(final DomNode node) {
148         DomNode parent = node;
149         while (parent != null) {
150             if (parent == rootNode_) {
151                 return true;
152             }
153             parent = parent.getParentNode();
154         }
155         
156         return false;
157     }
158 }
159
Popular Tags