KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Iterator JavaDoc;
41 import java.util.Map JavaDoc;
42 import java.util.Collections JavaDoc;
43
44 import org.jaxen.DefaultNavigator;
45 import org.jaxen.XPath;
46 import org.jaxen.JaxenException;
47 import com.gargoylesoftware.htmlunit.html.DomNode;
48 import com.gargoylesoftware.htmlunit.html.HtmlElement;
49 import com.gargoylesoftware.htmlunit.html.DomText;
50 import com.gargoylesoftware.htmlunit.html.HtmlPage;
51 import com.gargoylesoftware.htmlunit.html.Util;
52
53 /**
54  * Jaxen Navigator implementation for navigating around the HtmlUnit DOM object model
55  * in the context of XPath evaluation. The implementation is closely modeled after the
56  * W3C DOM Navigator that comes with Jaxen.
57  * <p>
58  * <em>This class is not intended for direct usage, but is used by the Jaxen engine
59  * during evaluation.
60  * </em>
61  *
62  * @version $Revision: 100 $
63  * @author <a HREF="mailto:cse@dynabean.de">Christian Sell</a>
64  * @author Mike Bowler
65  * @see HtmlUnitXPath
66  */

67 public class DocumentNavigator extends DefaultNavigator {
68     private static final long serialVersionUID = -5323715453687261210L;
69     
70     /**
71      * Constant: singleton navigator.
72      */

73     public static final DocumentNavigator instance = new DocumentNavigator();
74
75     /**
76      * Get an iterator over all of this node's children.
77      *
78      * @param contextNode The context node for the child axis.
79      * @return A possibly-empty iterator (not null).
80      */

81     public Iterator JavaDoc getChildAxisIterator (final Object JavaDoc contextNode) {
82         return ((DomNode)contextNode).getChildIterator();
83     }
84
85     /**
86      * Get a (single-member) iterator over this node's parent.
87      *
88      * @param contextNode the context node for the parent axis.
89      * @return A possibly-empty iterator (not null).
90      */

91     public Iterator JavaDoc getParentAxisIterator (final Object JavaDoc contextNode) {
92         return new Iterator JavaDoc() {
93             private DomNode parent_ = ((DomNode)contextNode).getParentNode();
94
95             public boolean hasNext() {
96                 return parent_ != null;
97             }
98             public Object JavaDoc next() {
99                 final DomNode next = parent_;
100                 parent_ = null;
101                 return next;
102             }
103             public void remove() {
104                 throw new UnsupportedOperationException JavaDoc();
105             }
106         };
107     }
108
109     /**
110      * Get an iterator over all following siblings.
111      *
112      * @param contextNode the context node for the sibling iterator.
113      * @return A possibly-empty iterator (not null).
114      */

115     public Iterator JavaDoc getFollowingSiblingAxisIterator (final Object JavaDoc contextNode) {
116         return Util.getFollowingSiblingAxisIterator((DomNode) contextNode);
117     }
118
119     /**
120      * Get an iterator over all preceding siblings.
121      *
122      * @param contextNode The context node for the preceding sibling axis.
123      * @return A possibly-empty iterator (not null).
124      */

125     public Iterator JavaDoc getPrecedingSiblingAxisIterator (final Object JavaDoc contextNode) {
126         return Util.getPrecedingSiblingAxisIterator((DomNode)contextNode);
127     }
128
129     /**
130      * Get an iterator over all following nodes, depth-first.
131      *
132      * @param contextNode The context node for the following axis.
133      * @return A possibly-empty iterator (not null).
134      */

135     public Iterator JavaDoc getFollowingAxisIterator (final Object JavaDoc contextNode) {
136         return Util.getFollowingAxisIterator((DomNode)contextNode);
137     }
138
139     /**
140      * Get an iterator over all preceding nodes, depth-first.
141      *
142      * @param contextNode The context node for the preceding axis.
143      * @return A possibly-empty iterator (not null).
144      */

145     public Iterator JavaDoc getPrecedingAxisIterator (final Object JavaDoc contextNode) {
146         return Util.getPrecedingAxisIterator((DomNode)contextNode);
147     }
148
149     /**
150      * Get an iterator over all attributes.
151      *
152      * @param contextNode The context node for the attribute axis.
153      * @return A possibly-empty iterator (not null).
154      */

155     public Iterator JavaDoc getAttributeAxisIterator (final Object JavaDoc contextNode) {
156         if(contextNode instanceof HtmlElement) {
157             return ((HtmlElement)contextNode).getAttributeEntriesIterator();
158         }
159         else {
160             return Collections.EMPTY_LIST.iterator();
161         }
162     }
163
164     /**
165      * @param xpath an xpath expression
166      * @return a parsed form of the given xpath string, which will be suitable
167      * for queries on DOM documents.
168      * @throws JaxenException if the expression could not be parsed
169      */

170     public XPath parseXPath (final String JavaDoc xpath) throws JaxenException {
171         return new HtmlUnitXPath(xpath);
172     }
173
174     /**
175      * Get the top-level document node.
176      *
177      * @param contextNode Any node in the document.
178      * @return The root node.
179      */

180     public Object JavaDoc getDocumentNode (final Object JavaDoc contextNode) {
181         return ((DomNode)contextNode).getPage();
182     }
183
184     /**
185      * Get the Namespace URI of an element.
186      *
187      * @param object The target node.
188      * @return A string (possibly empty) if the node is an element,
189      * and null otherwise.
190      */

191     public String JavaDoc getElementNamespaceUri (final Object JavaDoc object) {
192
193         //return object instanceof HtmlElement ? "" : null;
194
if(object instanceof HtmlElement) {
195             return "";
196         }
197         else {
198             return null;
199         }
200     }
201
202     /**
203      * Get the local name of an element.
204      *
205      * @param object The target node.
206      * @return A string representing the unqualified local name
207      * if the node is an element, or null otherwise.
208      */

209     public String JavaDoc getElementName (final Object JavaDoc object) {
210         return ((DomNode)object).getNodeName();
211     }
212
213     /**
214      * Get the qualified name of an element.
215      *
216      * @param object The target node.
217      * @return A string representing the qualified (i.e. possibly
218      * prefixed) name if the node is an element, or null otherwise.
219      */

220     public String JavaDoc getElementQName (final Object JavaDoc object) {
221         return ((DomNode)object).getNodeName();
222     }
223
224     /**
225      * @param object The target node.
226      * @return the Namespace URI of an attribute.
227      */

228     public String JavaDoc getAttributeNamespaceUri (final Object JavaDoc object) {
229         return "";
230     }
231
232     /**
233      * Get the local name of an attribute.
234      *
235      * @param object The target node.
236      * @return A string representing the unqualified local name
237      * if the node is an attribute, or null otherwise.
238      */

239     public String JavaDoc getAttributeName (final Object JavaDoc object) {
240         return (String JavaDoc)((Map.Entry JavaDoc)object).getKey();
241     }
242
243     /**
244      * Get the qualified name of an attribute.
245      *
246      * @param object The target node.
247      * @return A string representing the qualified (i.e. possibly
248      * prefixed) name if the node is an attribute, or null otherwise.
249      */

250     public String JavaDoc getAttributeQName (final Object JavaDoc object) {
251         return (String JavaDoc)((Map.Entry JavaDoc)object).getKey();
252     }
253
254     /**
255      * Test if a node is a top-level document.
256      *
257      * @param object The target node.
258      * @return true if the node is the document root, false otherwise.
259      */

260     public boolean isDocument (final Object JavaDoc object) {
261         return (object instanceof HtmlPage);
262     }
263
264     /**
265      * Test if a node is a Namespace.
266      *
267      * @param object The target node.
268      * @return true if the node is a Namespace, false otherwise.
269      */

270     public boolean isNamespace (final Object JavaDoc object) {
271         return false;
272     }
273
274     /**
275      * Test if a node is an element.
276      *
277      * @param object The target node.
278      * @return true if the node is an element, false otherwise.
279      */

280     public boolean isElement (final Object JavaDoc object) {
281         return (object instanceof HtmlElement);
282     }
283
284     /**
285      * Test if a node is an attribute.
286      *
287      * @param object The target node.
288      * @return true if the node is an attribute, false otherwise.
289      */

290     public boolean isAttribute (final Object JavaDoc object) {
291         return (object instanceof Map.Entry JavaDoc);
292     }
293
294     /**
295      * Test if a node is a comment.
296      *
297      * @param object The target node.
298      * @return true if the node is a comment, false otherwise.
299      */

300     public boolean isComment (final Object JavaDoc object) {
301         return false;
302     }
303
304     /**
305      * Test if a node is plain text.
306      *
307      * @param object The target node.
308      * @return true if the node is a text node, false otherwise.
309      */

310     public boolean isText (final Object JavaDoc object) {
311         return (object instanceof DomText);
312     }
313
314     /**
315      * Test if a node is a processing instruction.
316      *
317      * @param object The target node.
318      * @return true if the node is a processing instruction, false otherwise.
319      */

320     public boolean isProcessingInstruction (final Object JavaDoc object) {
321         return false;
322     }
323
324     /**
325      * Get the string value of an element node.
326      *
327      * @param object The target node.
328      * @return The text inside the node and its descendants if the node
329      * is an element, null otherwise.
330      */

331     public String JavaDoc getElementStringValue (final Object JavaDoc object) {
332         return ((DomNode)object).asText();
333     }
334
335     /**
336      * Get the string value of an attribute node.
337      *
338      * @param object The target node.
339      * @return The text of the attribute value if the node is an
340      * attribute, null otherwise.
341      */

342     public String JavaDoc getAttributeStringValue (final Object JavaDoc object) {
343         return (String JavaDoc)((Map.Entry JavaDoc)object).getValue();
344     }
345
346     /**
347      * Get the string value of text.
348      *
349      * @param object The target node.
350      * @return The string of text if the node is text, null otherwise.
351      */

352     public String JavaDoc getTextStringValue (final Object JavaDoc object) {
353         return ((DomText)object).asText();
354     }
355
356     /**
357      * Get the string value of a comment node.
358      *
359      * @param object The target node.
360      * @return The text of the comment if the node is a comment,
361      * null otherwise.
362      */

363     public String JavaDoc getCommentStringValue (final Object JavaDoc object) {
364         return null;
365     }
366
367     /**
368      * Get the string value of a Namespace node.
369      *
370      * @param object The target node.
371      * @return The Namespace URI as a (possibly empty) string if the
372      * node is a namespace node, null otherwise.
373      */

374     public String JavaDoc getNamespaceStringValue (final Object JavaDoc object) {
375         return null;
376     }
377
378     /**
379      * Get the prefix value of a Namespace node.
380      *
381      * @param object The target node.
382      * @return The Namespace prefix a (possibly empty) string if the
383      * node is a namespace node, null otherwise.
384      */

385     public String JavaDoc getNamespacePrefix (final Object JavaDoc object) {
386         return null;
387     }
388
389     /**
390      * Returns the element whose ID is given by elementId.
391      * If no such element exists, returns null.
392      * Attributes with the name "ID" are not of type ID unless so defined.
393      * Atribute types are only known if when the parser understands DTD's or
394      * schemas that declare attributes of type ID. When JAXP is used, you
395      * must call <code>setValidating(true)</code> on the
396      * DocumentBuilderFactory.
397      *
398      * @param contextNode a node from the document in which to look for the
399      * id
400      * @param elementId id to look for
401      *
402      * @return element whose ID is given by elementId, or null if no such
403      * element exists in the document or if the implementation
404      * does not know about attribute types
405      * @see javax.xml.parsers.DocumentBuilderFactory
406      */

407     public Object JavaDoc getElementById(final Object JavaDoc contextNode, final String JavaDoc elementId) {
408         final HtmlPage page = ((DomNode)contextNode).getPage();
409         return page.getHtmlElementById(elementId);
410     }
411
412 }
413
Popular Tags