1 package com.icl.saxon.tree; 2 import com.icl.saxon.om.NodeInfo; 3 import com.icl.saxon.om.AxisEnumeration; 4 import com.icl.saxon.om.EmptyEnumeration; 5 import com.icl.saxon.om.SingletonEnumeration; 6 import com.icl.saxon.expr.NodeSetExtent; 7 import com.icl.saxon.expr.XPathException; 8 import com.icl.saxon.output.Outputter; 9 import com.icl.saxon.pattern.NodeTest; 10 import com.icl.saxon.pattern.AnyNodeTest; 11 import com.icl.saxon.sort.LocalOrderComparer; 12 13 import javax.xml.transform.TransformerException ; 14 15 import org.w3c.dom.Node ; 16 import org.w3c.dom.NodeList ; 17 18 23 24 25 abstract class ParentNodeImpl extends NodeImpl { 26 27 private Object children = null; 31 protected int sequence; 32 33 39 40 protected final long getSequenceNumber() { 41 return ((long)sequence)<<32; 42 } 43 44 47 48 public final boolean hasChildNodes() { 49 return (children!=null); 50 } 51 52 55 56 public final AxisEnumeration enumerateChildren(NodeTest test) { 57 if (children==null) { 58 return EmptyEnumeration.getInstance(); 59 } else if (children instanceof NodeImpl) { 60 NodeImpl child = (NodeImpl)children; 61 if (test.matches(child)) { 62 return new SingletonEnumeration(child); 63 } else { 64 return EmptyEnumeration.getInstance(); 65 } 66 } else { 67 if (test instanceof AnyNodeTest) { 68 return new ArrayEnumeration((NodeImpl[])children); 69 } else { 70 return new ChildEnumeration(this, test); 71 } 72 } 73 } 74 75 76 80 81 public final Node getFirstChild() { 82 if (children==null) return null; 83 if (children instanceof NodeImpl) return (NodeImpl)children; 84 return ((NodeImpl[])children)[0]; 85 } 86 87 91 92 public final Node getLastChild() { 93 if (children==null) return null; 94 if (children instanceof NodeImpl) return (NodeImpl)children; 95 NodeImpl[] n = (NodeImpl[])children; 96 return n[n.length-1]; 97 } 98 99 104 105 public final NodeList getChildNodes() { 106 if (hasChildNodes()) { 107 try { 108 return new NodeSetExtent( 109 enumerateChildren(AnyNodeTest.getInstance()), 110 LocalOrderComparer.getInstance()); 111 } catch (XPathException err) { 112 return super.getChildNodes(); 113 } 114 } else { 115 return super.getChildNodes(); 116 } 117 } 118 119 123 124 protected final NodeImpl getNthChild(int n) { 125 if (children==null) return null; 126 if (children instanceof NodeImpl) { 127 return (n==0 ? (NodeImpl)children : null); 128 } 129 NodeImpl[] nodes = (NodeImpl[])children; 130 if (n<0 || n>=nodes.length) return null; 131 return nodes[n]; 132 } 133 134 135 140 141 public String getStringValue() { 142 StringBuffer sb = null; 143 144 NodeImpl next = (NodeImpl)getFirstChild(); 145 while (next!=null) { 146 if (next instanceof TextImpl) { 147 if (sb==null) { 148 sb = new StringBuffer (); 149 } 150 sb.append(next.getStringValue()); 151 } 152 next = next.getNextInDocument(this); 153 } 154 if (sb==null) return ""; 155 return sb.toString(); 156 } 157 158 161 162 public void copyStringValue(Outputter out) throws TransformerException { 163 NodeImpl next = (NodeImpl)getFirstChild(); 164 while (next!=null) { 165 if (next.getNodeType()==NodeInfo.TEXT) { 166 next.copyStringValue(out); 167 } 168 next = next.getNextInDocument(this); 169 } 170 } 171 172 175 176 public void useChildrenArray(NodeImpl[] array) { 177 children = array; 178 } 179 180 184 185 public void addChild(NodeImpl node, int index) { 186 NodeImpl[] c; 187 if (children == null) { 188 c = new NodeImpl[10]; 189 } else if (children instanceof NodeImpl) { 190 c = new NodeImpl[10]; 191 c[0] = (NodeImpl)children; 192 } else { 193 c = (NodeImpl[])children; 194 } 195 if (index >= c.length) { 196 NodeImpl[] kids = new NodeImpl[c.length * 2]; 197 System.arraycopy(c, 0, kids, 0, c.length); 198 c = kids; 199 } 200 c[index] = node; 201 node.parent = this; 202 node.index = index; 203 children = c; 204 } 205 206 209 210 public void removeChild(int index) { 211 if (children instanceof NodeImpl) { 212 children = null; 213 } else { 214 ((NodeImpl[])children)[index] = null; 215 } 216 } 217 218 221 222 public void renumberChildren() { 223 int j = 0; 224 if (children==null) { 225 return; 226 } else if (children instanceof NodeImpl) { 227 ((NodeImpl)children).parent = this; 228 ((NodeImpl)children).index = 0; 229 } else { 230 NodeImpl[] c = (NodeImpl[])children; 231 for (int i=0; i<c.length; i++) { 232 if (c[i]!=null) { 233 c[i].parent = this; 234 c[i].index = j; 235 c[j] = c[i]; 236 j++; 237 } 238 } 239 compact(j); 240 } 241 } 242 243 248 249 public void dropChildren() { 250 NodeImpl n = getNextInDocument(this); 252 while (n!=null) { 253 if (n instanceof TextImpl) { 254 ((TextImpl)n).truncateToStart(); 255 break; 256 } 257 n = n.getNextInDocument(this); 258 } 259 children = null; 261 } 262 263 266 267 public void compact(int size) { 268 if (size==0) { 269 children = null; 270 } else if (size==1) { 271 if (children instanceof NodeImpl[]) { 272 children = ((NodeImpl[])children)[0]; 273 } 274 } else { 275 NodeImpl[] kids = new NodeImpl[size]; 276 System.arraycopy(children, 0, kids, 0, size); 277 children = kids; 278 } 279 } 280 281 284 285 public String getNodeValue() { 286 return null; 287 } 288 } 289 290 291 | Popular Tags |