1 17 package org.apache.excalibur.xml.xpath; 18 19 import org.w3c.dom.DocumentFragment ; 20 import org.w3c.dom.Element ; 21 import org.w3c.dom.Node ; 22 import org.w3c.dom.NodeList ; 23 24 30 public class NodeListImpl implements NodeList { 31 32 private Node [] nodelist; 33 34 37 public NodeListImpl(NodeList list) { 38 if (list == null || list.getLength() == 0) { 39 nodelist = null; 40 } else { 41 nodelist = new Node [list.getLength()]; 42 for(int i = 0; i < list.getLength(); i++) { 43 nodelist[i] = list.item(i).cloneNode(true); 44 } 45 } 46 } 47 48 51 public NodeListImpl(Node [] nodes) { 52 this.nodelist = nodes; 53 } 54 55 58 public NodeListImpl() {} 59 60 63 public NodeListImpl(DocumentFragment fragment, String rootName) { 64 if (fragment != null) { 65 Element root = fragment.getOwnerDocument().createElementNS(null, rootName); 66 Node current; 67 while (fragment.hasChildNodes() == true) { 68 current = fragment.getFirstChild(); 69 fragment.removeChild(current); 70 root.appendChild(current); 71 } 72 nodelist = new Node [1]; 73 nodelist[0] = root; 74 } 75 } 76 77 80 public void addNode(Node node) { 81 if (this.nodelist == null) { 82 this.nodelist = new Node [1]; 83 this.nodelist[0] = node; 84 } else { 85 Node [] copy = new Node [this.nodelist.length+1]; 86 System.arraycopy(this.nodelist, 0, copy, 0, this.nodelist.length); 87 copy[copy.length-1] = node; 88 this.nodelist = copy; 89 } 90 } 91 92 101 public Node item(int index) { 102 if (nodelist == null || index >= nodelist.length) { 103 return null; 104 } else { 105 return nodelist[index]; 106 } 107 } 108 109 113 public int getLength() { 114 return (nodelist == null ? 0 : nodelist.length); 115 } 116 117 } 118 | Popular Tags |