KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > util > NodeList


1 // HTMLParser Library $Name: v1_5_20050313 $ - A java-based parser for HTML
2
// http://sourceforge.org/projects/htmlparser
3
// Copyright (C) 2004 Somik Raha
4
//
5
// Revision Control Information
6
//
7
// $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/util/NodeList.java,v $
8
// $Author: derrickoswald $
9
// $Date: 2005/03/12 13:39:47 $
10
// $Revision: 1.58 $
11
//
12
// This library is free software; you can redistribute it and/or
13
// modify it under the terms of the GNU Lesser General Public
14
// License as published by the Free Software Foundation; either
15
// version 2.1 of the License, or (at your option) any later version.
16
//
17
// This library is distributed in the hope that it will be useful,
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
// Lesser General Public License for more details.
21
//
22
// You should have received a copy of the GNU Lesser General Public
23
// License along with this library; if not, write to the Free Software
24
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
//
26

27 package org.htmlparser.util;
28
29 import java.io.Serializable JavaDoc;
30 import java.util.NoSuchElementException JavaDoc;
31
32 import org.htmlparser.Node;
33 import org.htmlparser.NodeFilter;
34 import org.htmlparser.filters.NodeClassFilter;
35 import org.htmlparser.visitors.NodeVisitor;
36
37 public class NodeList implements Serializable JavaDoc {
38     private static final int INITIAL_CAPACITY=10;
39     //private static final int CAPACITY_INCREMENT=20;
40
private Node nodeData[];
41     private int size;
42     private int capacity;
43     private int capacityIncrement;
44     private int numberOfAdjustments;
45
46     public NodeList() {
47         size = 0;
48         capacity = INITIAL_CAPACITY;
49         nodeData = newNodeArrayFor(capacity);
50         capacityIncrement = capacity*2;
51         numberOfAdjustments = 0;
52     }
53
54     /**
55      * Create a one element node list.
56      * @param node The initial node to add.
57      */

58     public NodeList(Node node)
59     {
60         this ();
61         add (node);
62     }
63         
64     public void add(Node node) {
65         if (size==capacity)
66             adjustVectorCapacity();
67         nodeData[size++]=node;
68     }
69
70     /**
71      * Add another node list to this one.
72      * @param list The list to add.
73      */

74     public void add (NodeList list)
75     {
76         for (int i = 0; i < list.size; i++)
77             add (list.nodeData[i]);
78     }
79
80     /**
81      * Insert the given node at the head of the list.
82      * @param node The new first element.
83      */

84     public void prepend(Node node)
85     {
86         if (size==capacity)
87             adjustVectorCapacity();
88         System.arraycopy (nodeData, 0, nodeData, 1, size);
89         size++;
90         nodeData[0]=node;
91     }
92
93     private void adjustVectorCapacity() {
94         capacity += capacityIncrement;
95         capacityIncrement *= 2;
96         Node oldData [] = nodeData;
97         nodeData = newNodeArrayFor(capacity);
98         System.arraycopy(oldData, 0, nodeData, 0, size);
99         numberOfAdjustments++;
100     }
101
102     private Node[] newNodeArrayFor(int capacity) {
103         return new Node[capacity];
104     }
105
106     public int size() {
107         return size;
108     }
109
110     public Node elementAt(int i) {
111         return nodeData[i];
112     }
113
114     public int getNumberOfAdjustments() {
115         return numberOfAdjustments;
116     }
117
118     public SimpleNodeIterator elements() {
119         return new SimpleNodeIterator() {
120             int count = 0;
121
122             public boolean hasMoreNodes() {
123                 return count < size;
124             }
125
126             public Node nextNode() {
127             synchronized (NodeList.this) {
128                 if (count < size) {
129                 return nodeData[count++];
130                 }
131             }
132             throw new NoSuchElementException JavaDoc("Vector Enumeration");
133             }
134         };
135     }
136
137     public Node [] toNodeArray() {
138         Node [] nodeArray = newNodeArrayFor(size);
139         System.arraycopy(nodeData, 0, nodeArray, 0, size);
140         return nodeArray;
141     }
142
143     public void copyToNodeArray(Node[] array) {
144         System.arraycopy(nodeData, 0, array, 0, size);
145     }
146
147     public String JavaDoc asString() {
148         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
149         for (int i=0;i<size;i++)
150             buff.append(nodeData[i].toPlainTextString());
151         return buff.toString();
152     }
153
154     /**
155      * Convert this nodelist into the equivalent HTML.
156      * @deprecated Use {@link #toHtml}.
157      * @return The contents of the list as HTML text.
158      */

159     public String JavaDoc asHtml()
160     {
161         return (toHtml ());
162     }
163
164     /**
165      * Convert this nodelist into the equivalent HTML.
166      * @return The contents of the list as HTML text.
167      */

168     public String JavaDoc toHtml()
169     {
170         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
171         for (int i=0;i<size;i++)
172             buff.append(nodeData[i].toHtml());
173         return buff.toString();
174     }
175
176     public Node remove(int index) {
177         Node ret;
178         ret = nodeData[index];
179         System.arraycopy(nodeData, index+1, nodeData, index, size-index-1);
180         nodeData[size-1] = null;
181         size--;
182         return (ret);
183     }
184
185     public void removeAll() {
186         size = 0;
187         capacity = INITIAL_CAPACITY;
188         nodeData = newNodeArrayFor(capacity);
189         capacityIncrement = capacity*2;
190         numberOfAdjustments = 0;
191     }
192
193     /**
194      * Return the contents of the list as a string.
195      * Suitable for debugging.
196      * @return A string representation of the list.
197      */

198     public String JavaDoc toString()
199     {
200         StringBuffer JavaDoc text = new StringBuffer JavaDoc();
201         for (int i=0;i<size;i++)
202             text.append (nodeData[i]);
203         return (text.toString ());
204     }
205
206     /**
207      * Filter the list with the given filter non-recursively.
208      * @param filter The filter to use.
209      * @return A new node array containing the nodes accepted by the filter.
210      * This is a linear list and preserves the nested structure of the returned
211      * nodes only.
212      */

213     public NodeList extractAllNodesThatMatch (NodeFilter filter)
214     {
215         return (extractAllNodesThatMatch (filter, false));
216     }
217
218     /**
219      * Filter the list with the given filter.
220      * @param filter The filter to use.
221      * @param recursive If <code>true<code> digs into the children recursively.
222      * @return A new node array containing the nodes accepted by the filter.
223      * This is a linear list and preserves the nested structure of the returned
224      * nodes only.
225      */

226     public NodeList extractAllNodesThatMatch (NodeFilter filter, boolean recursive)
227     {
228         Node node;
229         NodeList children;
230         NodeList ret;
231
232         ret = new NodeList ();
233         for (int i = 0; i < size; i++)
234         {
235             node = nodeData[i];
236             if (filter.accept (node))
237                 ret.add (node);
238             if (recursive)
239             {
240                 children = node.getChildren ();
241                 if (null != children)
242                     ret.add (children.extractAllNodesThatMatch (filter, recursive));
243             }
244         }
245
246         return (ret);
247     }
248
249     /**
250      * Remove nodes not matching the given filter non-recursively.
251      * @param filter The filter to use.
252      */

253     public void keepAllNodesThatMatch (NodeFilter filter)
254     {
255         keepAllNodesThatMatch (filter, false);
256     }
257
258     /**
259      * Remove nodes not matching the given filter.
260      * @param filter The filter to use.
261      * @param recursive If <code>true<code> digs into the children recursively.
262      */

263     public void keepAllNodesThatMatch (NodeFilter filter, boolean recursive)
264     {
265         Node node;
266         NodeList children;
267
268         for (int i = 0; i < size; )
269         {
270             node = nodeData[i];
271             if (!filter.accept (node))
272                 remove (i);
273             else
274             {
275                 if (recursive)
276                 {
277                     children = node.getChildren ();
278                     if (null != children)
279                         children.keepAllNodesThatMatch (filter, recursive);
280                 }
281                 i++;
282             }
283         }
284     }
285
286     /**
287      * Convenience method to search for nodes of the given type non-recursively.
288      * @param classType The class to search for.
289      */

290     public NodeList searchFor (Class JavaDoc classType)
291     {
292         return (searchFor (classType, false));
293     }
294
295     /**
296      * Convenience method to search for nodes of the given type.
297      * @param classType The class to search for.
298      * @param recursive If <code>true<code> digs into the children recursively.
299      */

300     public NodeList searchFor (Class JavaDoc classType, boolean recursive)
301     {
302         return (extractAllNodesThatMatch (new NodeClassFilter (classType), recursive));
303     }
304
305     /**
306      * Utility to apply a visitor to a node list.
307      * Provides for a visitor to modify the contents of a page and get the
308      * modified HTML as a string with code like this:
309      * <pre>
310      * Parser parser = new Parser ("http://whatever");
311      * NodeList list = parser.parse (null); // no filter
312      * list.visitAllNodesWith (visitor);
313      * System.out.println (list.toHtml ());
314      * </pre>
315      */

316     public void visitAllNodesWith (NodeVisitor visitor)
317         throws
318             ParserException
319     {
320         Node node;
321
322         visitor.beginParsing ();
323         for (int i = 0; i < size; i++)
324             nodeData[i].accept (visitor);
325         visitor.finishedParsing ();
326     }
327 }
328
Popular Tags