1 package net.sf.saxon.tree; 2 import net.sf.saxon.om.*; 3 import net.sf.saxon.pattern.AnyNodeTest; 4 import net.sf.saxon.pattern.NodeTest; 5 6 11 12 13 abstract class ParentNodeImpl extends NodeImpl { 14 15 private Object children = null; 19 protected int sequence; 20 21 27 28 protected final long getSequenceNumber() { 29 return ((long)sequence)<<32; 30 } 31 32 35 36 public final boolean hasChildNodes() { 37 return (children!=null); 38 } 39 40 45 46 public final AxisIterator enumerateChildren(NodeTest test) { 47 if (children==null) { 48 return EmptyIterator.getInstance(); 49 } else if (children instanceof NodeImpl) { 50 NodeImpl child = (NodeImpl)children; 51 if (test==null || test.matches(child)) { 52 return SingletonIterator.makeIterator(child); 53 } else { 54 return EmptyIterator.getInstance(); 55 } 56 } else { 57 if (test==null || test instanceof AnyNodeTest) { 58 return new ArrayIterator((NodeImpl[])children); 59 } else { 60 return new ChildEnumeration(this, test); 61 } 62 } 63 } 64 65 66 70 71 public final NodeInfo getFirstChild() { 72 if (children==null) return null; 73 if (children instanceof NodeImpl) return (NodeImpl)children; 74 return ((NodeImpl[])children)[0]; 75 } 76 77 81 82 public final NodeInfo getLastChild() { 83 if (children==null) return null; 84 if (children instanceof NodeImpl) return (NodeImpl)children; 85 NodeImpl[] n = (NodeImpl[])children; 86 return n[n.length-1]; 87 } 88 89 93 94 protected final NodeImpl getNthChild(int n) { 95 if (children==null) return null; 96 if (children instanceof NodeImpl) { 97 return (n==0 ? (NodeImpl)children : null); 98 } 99 NodeImpl[] nodes = (NodeImpl[])children; 100 if (n<0 || n>=nodes.length) return null; 101 return nodes[n]; 102 } 103 104 105 110 111 public String getStringValue() { 112 return getStringValueCS().toString(); 113 } 114 115 116 public CharSequence getStringValueCS() { 117 FastStringBuffer sb = null; 118 119 NodeImpl next = (NodeImpl)getFirstChild(); 120 while (next!=null) { 121 if (next instanceof TextImpl) { 122 if (sb==null) { 123 sb = new FastStringBuffer(1024); 124 } 125 sb.append(next.getStringValueCS()); 126 } 127 next = next.getNextInDocument(this); 128 } 129 if (sb==null) return ""; 130 return sb.condense(); 131 } 132 133 136 147 150 151 public void useChildrenArray(NodeImpl[] array) { 152 children = array; 153 } 154 155 159 160 public void addChild(NodeImpl node, int index) { 161 NodeImpl[] c; 162 if (children == null) { 163 c = new NodeImpl[10]; 164 } else if (children instanceof NodeImpl) { 165 c = new NodeImpl[10]; 166 c[0] = (NodeImpl)children; 167 } else { 168 c = (NodeImpl[])children; 169 } 170 if (index >= c.length) { 171 NodeImpl[] kids = new NodeImpl[c.length * 2]; 172 System.arraycopy(c, 0, kids, 0, c.length); 173 c = kids; 174 } 175 c[index] = node; 176 node.parent = this; 177 node.index = index; 178 children = c; 179 } 180 181 182 185 186 public void compact(int size) { 187 if (size==0) { 188 children = null; 189 } else if (size==1) { 190 if (children instanceof NodeImpl[]) { 191 children = ((NodeImpl[])children)[0]; 192 } 193 } else { 194 NodeImpl[] kids = new NodeImpl[size]; 195 System.arraycopy(children, 0, kids, 0, size); 196 children = kids; 197 } 198 } 199 200 } 201 202 203 | Popular Tags |