KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > gargoylesoftware > htmlunit > html > Util


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;
39
40 import java.util.Iterator JavaDoc;
41 import java.util.NoSuchElementException JavaDoc;
42
43 /**
44  * Provides some utilities for working on the Html document.
45  * @version $Revision: 100 $
46  * @author Marc Guillemot
47  */

48 public final class Util {
49
50     /** Private constructor to prevent instantiation */
51     private Util() {
52         // does nothing
53
}
54     
55     /**
56      * Get an iterator over all following nodes, depth-first.
57      *
58      * @param contextNode The context node for the following axis.
59      * @return A possibly-empty iterator (not null).
60      */

61     public static Iterator JavaDoc getFollowingSiblingAxisIterator(final DomNode contextNode) {
62         return new NodeIterator (contextNode) {
63             protected DomNode getFirstNode (final DomNode node) {
64                 return getNextNode(node);
65             }
66             protected DomNode getNextNode (final DomNode node) {
67                 return node.getNextSibling();
68             }
69         };
70     }
71
72     /**
73      * Get an iterator over all preceding siblings.
74      *
75      * @param contextNode The context node for the preceding sibling axis.
76      * @return A possibly-empty iterator (not null).
77      */

78     public static Iterator JavaDoc getPrecedingSiblingAxisIterator (final DomNode contextNode) {
79         return new NodeIterator (contextNode) {
80             protected DomNode getFirstNode (final DomNode node) {
81                 return getNextNode(node);
82             }
83             protected DomNode getNextNode (final DomNode node) {
84                 return node.getPreviousSibling();
85             }
86         };
87     }
88     
89     /**
90      * Get an iterator over all following nodes, depth-first.
91      *
92      * @param contextNode The context node for the following axis.
93      * @return A possibly-empty iterator (not null).
94      */

95     public static Iterator JavaDoc getFollowingAxisIterator (final DomNode contextNode) {
96         return new NodeIterator (contextNode) {
97             protected DomNode getFirstNode (final DomNode node) {
98                 return getNextNode(node);
99             }
100             protected DomNode getNextNode (final DomNode node) {
101                 if (node == null) {
102                     return null;
103                 }
104                 else {
105                     DomNode n = node.getFirstChild();
106                     if (n == null) {
107                         n = node.getNextSibling();
108                     }
109                     if (n == null) {
110                         return getParentNext(node.getParentNode());
111                     }
112                     else {
113                         return n;
114                     }
115                 }
116             }
117             
118             protected DomNode getParentNext(final DomNode node) {
119                 if (node == null) {
120                     return null;
121                 }
122
123                 DomNode n = node.getNextSibling();
124                 if (n == null) {
125                     n = getParentNext(node.getParentNode());
126                 }
127
128                 return n;
129             }
130         };
131     }
132
133     /**
134      * Get an iterator over all preceding nodes, depth-first.
135      *
136      * @param contextNode The context node for the preceding axis.
137      * @return A possibly-empty iterator (not null).
138      */

139     public static Iterator JavaDoc getPrecedingAxisIterator (final DomNode contextNode) {
140         return new NodeIterator(contextNode) {
141             protected DomNode getFirstNode (final DomNode node) {
142                 if (node == null) {
143                     return null;
144                 }
145                 else {
146                     final DomNode sibling = node.getPreviousSibling();
147                     if (sibling == null) {
148                         return getFirstNode(node.getParentNode());
149                     }
150                     else {
151                         return sibling;
152                     }
153                 }
154             }
155             protected DomNode getNextNode (final DomNode node) {
156                 if (node == null) {
157                     return null;
158                 }
159                 else {
160                     DomNode n = node.getLastChild();
161                     if (n == null) {
162                         n = node.getPreviousSibling();
163                     }
164                     if (n == null) {
165                         return getFirstNode(node.getParentNode());
166                     }
167                     else {
168                         return n;
169                     }
170                 }
171             }
172         };
173     }
174 }
175
176 /**
177  * A generic iterator over DOM nodes.
178  *
179  * <p>Concrete subclasses must implement the {@link #getFirstNode}
180  * and {@link #getNextNode} methods for a specific iteration
181  * strategy.</p>
182  */

183 abstract class NodeIterator implements Iterator JavaDoc {
184
185     private DomNode node_;
186
187     /**
188      * @param contextNode The starting node.
189      */

190     public NodeIterator (final DomNode contextNode) {
191         node_ = getFirstNode(contextNode);
192     }
193     /** @inheritDoc Iterator#hasNext() */
194     public boolean hasNext () {
195         return (node_ != null);
196     }
197     /** @inheritDoc Iterator#next() */
198     public Object JavaDoc next () {
199         if (node_ == null) {
200             throw new NoSuchElementException JavaDoc();
201         }
202         final DomNode ret = node_;
203         node_ = getNextNode(node_);
204         return ret;
205     }
206     /** @inheritDoc Iterator#remove() */
207     public void remove () {
208         throw new UnsupportedOperationException JavaDoc();
209     }
210     /**
211      * Get the first node for iteration.
212      *
213      * <p>This method must derive an initial node for iteration
214      * from a context node.</p>
215      *
216      * @param contextNode The starting node.
217      * @return The first node in the iteration.
218      * @see #getNextNode
219      */

220     protected abstract DomNode getFirstNode (final DomNode contextNode);
221     /**
222      * Get the next node for iteration.
223      *
224      * <p>This method must locate a following node from the
225      * current context node.</p>
226      *
227      * @param contextNode The current node in the iteration.
228      * @return The following node in the iteration, or null
229      * if there is none.
230      * @see #getFirstNode
231      */

232     protected abstract DomNode getNextNode (final DomNode contextNode);
233 }
234
235
236
Popular Tags