1 27 package org.htmlparser.util; 28 29 import java.io.Serializable ; 30 import java.util.NoSuchElementException ; 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 { 38 private static final int INITIAL_CAPACITY=10; 39 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 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 74 public void add (NodeList list) 75 { 76 for (int i = 0; i < list.size; i++) 77 add (list.nodeData[i]); 78 } 79 80 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 ("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 asString() { 148 StringBuffer buff = new StringBuffer (); 149 for (int i=0;i<size;i++) 150 buff.append(nodeData[i].toPlainTextString()); 151 return buff.toString(); 152 } 153 154 159 public String asHtml() 160 { 161 return (toHtml ()); 162 } 163 164 168 public String toHtml() 169 { 170 StringBuffer buff = new StringBuffer (); 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 198 public String toString() 199 { 200 StringBuffer text = new StringBuffer (); 201 for (int i=0;i<size;i++) 202 text.append (nodeData[i]); 203 return (text.toString ()); 204 } 205 206 213 public NodeList extractAllNodesThatMatch (NodeFilter filter) 214 { 215 return (extractAllNodesThatMatch (filter, false)); 216 } 217 218 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 253 public void keepAllNodesThatMatch (NodeFilter filter) 254 { 255 keepAllNodesThatMatch (filter, false); 256 } 257 258 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 290 public NodeList searchFor (Class classType) 291 { 292 return (searchFor (classType, false)); 293 } 294 295 300 public NodeList searchFor (Class classType, boolean recursive) 301 { 302 return (extractAllNodesThatMatch (new NodeClassFilter (classType), recursive)); 303 } 304 305 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 |