1 package org.apache.velocity.anakia; 2 3 18 19 import java.io.Writer ; 20 import java.io.IOException ; 21 import java.io.StringWriter ; 22 import java.util.*; 23 import org.jdom.*; 24 import org.jdom.output.*; 25 26 40 public class NodeList implements List, Cloneable 41 { 42 private static final AttributeXMLOutputter DEFAULT_OUTPUTTER = 43 new AttributeXMLOutputter(); 44 45 46 private List nodes; 47 48 51 public NodeList() 52 { 53 nodes = new ArrayList(); 54 } 55 56 59 public NodeList(Document document) 60 { 61 this((Object )document); 62 } 63 64 67 public NodeList(Element element) 68 { 69 this((Object )element); 70 } 71 72 private NodeList(Object object) 73 { 74 if(object == null) 75 { 76 throw new IllegalArgumentException ( 77 "Cannot construct NodeList with null."); 78 } 79 nodes = new ArrayList(1); 80 nodes.add(object); 81 } 82 83 89 public NodeList(List nodes) 90 { 91 this(nodes, true); 92 } 93 94 102 public NodeList(List nodes, boolean copy) 103 { 104 if(nodes == null) 105 { 106 throw new IllegalArgumentException ( 107 "Cannot initialize NodeList with null list"); 108 } 109 this.nodes = copy ? new ArrayList(nodes) : nodes; 110 } 111 112 118 public List getList() 119 { 120 return nodes; 121 } 122 123 131 public String toString() 132 { 133 if(nodes.isEmpty()) 134 { 135 return ""; 136 } 137 138 StringWriter sw = new StringWriter (nodes.size() * 128); 139 try 140 { 141 for(Iterator i = nodes.iterator(); i.hasNext();) 142 { 143 Object node = i.next(); 144 if(node instanceof Element) 145 { 146 DEFAULT_OUTPUTTER.output((Element)node, sw); 147 } 148 else if(node instanceof Attribute) 149 { 150 DEFAULT_OUTPUTTER.output((Attribute)node, sw); 151 } 152 else if(node instanceof Text) 153 { 154 DEFAULT_OUTPUTTER.output((Text)node, sw); 155 } 156 else if(node instanceof Document) 157 { 158 DEFAULT_OUTPUTTER.output((Document)node, sw); 159 } 160 else if(node instanceof ProcessingInstruction) 161 { 162 DEFAULT_OUTPUTTER.output((ProcessingInstruction)node, sw); 163 } 164 else if(node instanceof Comment) 165 { 166 DEFAULT_OUTPUTTER.output((Comment)node, sw); 167 } 168 else if(node instanceof CDATA) 169 { 170 DEFAULT_OUTPUTTER.output((CDATA)node, sw); 171 } 172 else if(node instanceof DocType) 173 { 174 DEFAULT_OUTPUTTER.output((DocType)node, sw); 175 } 176 else if(node instanceof EntityRef) 177 { 178 DEFAULT_OUTPUTTER.output((EntityRef)node, sw); 179 } 180 else 181 { 182 throw new IllegalArgumentException ( 183 "Cannot process a " + 184 (node == null 185 ? "null node" 186 : "node of class " + node.getClass().getName())); 187 } 188 } 189 } 190 catch(IOException e) 191 { 192 throw new Error (); 194 } 195 return sw.toString(); 196 } 197 198 203 public Object clone() 204 throws CloneNotSupportedException 205 { 206 NodeList clonedList = (NodeList)super.clone(); 207 clonedList.cloneNodes(); 208 return clonedList; 209 } 210 211 private void cloneNodes() 212 throws CloneNotSupportedException 213 { 214 Class listClass = nodes.getClass(); 215 try 216 { 217 List clonedNodes = (List)listClass.newInstance(); 218 clonedNodes.addAll(nodes); 219 nodes = clonedNodes; 220 } 221 catch(IllegalAccessException e) 222 { 223 throw new CloneNotSupportedException ("Cannot clone NodeList since" 224 + " there is no accessible no-arg constructor on class " 225 + listClass.getName()); 226 } 227 catch(InstantiationException e) 228 { 229 throw new Error (); 233 } 234 } 235 236 239 public int hashCode() 240 { 241 return nodes.hashCode(); 242 } 243 244 250 public boolean equals(Object o) 251 { 252 return o instanceof NodeList 253 ? ((NodeList)o).nodes.equals(nodes) 254 : false; 255 } 256 257 271 public NodeList selectNodes(String xpathString) 272 { 273 return new NodeList(XPathCache.getXPath(xpathString).applyTo(nodes), false); 274 } 275 276 278 public boolean add(Object o) 279 { 280 return nodes.add(o); 281 } 282 283 public void add(int index, Object o) 284 { 285 nodes.add(index, o); 286 } 287 288 public boolean addAll(Collection c) 289 { 290 return nodes.addAll(c); 291 } 292 293 public boolean addAll(int index, Collection c) 294 { 295 return nodes.addAll(index, c); 296 } 297 298 public void clear() 299 { 300 nodes.clear(); 301 } 302 303 public boolean contains(Object o) 304 { 305 return nodes.contains(o); 306 } 307 308 public boolean containsAll(Collection c) 309 { 310 return nodes.containsAll(c); 311 } 312 313 public Object get(int index) 314 { 315 return nodes.get(index); 316 } 317 318 public int indexOf(Object o) 319 { 320 return nodes.indexOf(o); 321 } 322 323 public boolean isEmpty() 324 { 325 return nodes.isEmpty(); 326 } 327 328 public Iterator iterator() 329 { 330 return nodes.iterator(); 331 } 332 333 public int lastIndexOf(Object o) 334 { 335 return nodes.lastIndexOf(o); 336 } 337 338 public ListIterator listIterator() 339 { 340 return nodes.listIterator(); 341 } 342 343 public ListIterator listIterator(int index) 344 { 345 return nodes.listIterator(index); 346 } 347 348 public Object remove(int index) 349 { 350 return nodes.remove(index); 351 } 352 353 public boolean remove(Object o) 354 { 355 return nodes.remove(o); 356 } 357 358 public boolean removeAll(Collection c) 359 { 360 return nodes.removeAll(c); 361 } 362 363 public boolean retainAll(Collection c) 364 { 365 return nodes.retainAll(c); 366 } 367 368 public Object set(int index, Object o) 369 { 370 return nodes.set(index, o); 371 } 372 373 public int size() 374 { 375 return nodes.size(); 376 } 377 378 public List subList(int fromIndex, int toIndex) 379 { 380 return new NodeList(nodes.subList(fromIndex, toIndex)); 381 } 382 383 public Object [] toArray() 384 { 385 return nodes.toArray(); 386 } 387 388 public Object [] toArray(Object [] a) 389 { 390 return nodes.toArray(a); 391 } 392 393 399 private static final class AttributeXMLOutputter extends XMLOutputter 400 { 401 public void output(Attribute attribute, Writer out) 402 throws IOException 403 { 404 out.write(" "); 405 out.write(attribute.getQualifiedName()); 406 out.write("="); 407 408 out.write("\""); 409 out.write(escapeAttributeEntities(attribute.getValue())); 410 out.write("\""); 411 } 412 } 413 } 414 | Popular Tags |