| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.tree.linked; |
| 9 |
|
| 10 |
import net.sf.saxon.om.NodeInfo; |
| 11 |
import net.sf.saxon.pattern.AnyNodeTest; |
| 12 |
import net.sf.saxon.pattern.NodeTest; |
| 13 |
import net.sf.saxon.tree.iter.ArrayIterator; |
| 14 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 15 |
import net.sf.saxon.tree.iter.EmptyIterator; |
| 16 |
import net.sf.saxon.tree.iter.SingleNodeIterator; |
| 17 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 18 |
import net.sf.saxon.tree.util.Navigator; |
| 19 |
import net.sf.saxon.type.Type; |
| 20 |
|
| 21 |
import java.util.Arrays; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
abstract class ParentNodeImpl extends NodeImpl { |
| 32 |
|
| 33 |
private Object children = null; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
private int sequence; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
protected final long getSequenceNumber() { |
| 51 |
return getRawSequenceNumber() == -1 ? -1L : (long) getRawSequenceNumber() << 32; |
| 52 |
} |
| 53 |
|
| 54 |
protected final int getRawSequenceNumber() { |
| 55 |
return sequence; |
| 56 |
} |
| 57 |
|
| 58 |
protected final void setRawSequenceNumber(int seq) { |
| 59 |
sequence = seq; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
protected final void setChildren(Object children) { |
| 70 |
this.children = children; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public final boolean hasChildNodes() { |
| 78 |
return children != null; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public final int getNumberOfChildren() { |
| 88 |
if (children == null) { |
| 89 |
return 0; |
| 90 |
} else if (children instanceof NodeImpl) { |
| 91 |
return 1; |
| 92 |
} else { |
| 93 |
return ((NodeInfo[]) children).length; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
protected final AxisIterator iterateChildren( NodeTest test) { |
| 106 |
if (children == null) { |
| 107 |
return EmptyIterator.OfNodes.THE_INSTANCE; |
| 108 |
} else if (children instanceof NodeImpl) { |
| 109 |
NodeImpl child = (NodeImpl) children; |
| 110 |
if (test == null || test == AnyNodeTest.getInstance()) { |
| 111 |
return SingleNodeIterator.makeIterator(child); |
| 112 |
} else { |
| 113 |
return Navigator.filteredSingleton(child, test); |
| 114 |
} |
| 115 |
} else { |
| 116 |
if (test == null || test == AnyNodeTest.getInstance()) { |
| 117 |
return new ArrayIterator.OfNodes((NodeImpl[]) children); |
| 118 |
} else { |
| 119 |
return new ChildEnumeration(this, test); |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public final NodeImpl getFirstChild() { |
| 133 |
if (children == null) { |
| 134 |
return null; |
| 135 |
} else if (children instanceof NodeImpl) { |
| 136 |
return (NodeImpl) children; |
| 137 |
} else { |
| 138 |
return ((NodeImpl[]) children)[0]; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public final NodeImpl getLastChild() { |
| 150 |
if (children == null) { |
| 151 |
return null; |
| 152 |
} |
| 153 |
if (children instanceof NodeImpl) { |
| 154 |
return (NodeImpl) children; |
| 155 |
} |
| 156 |
NodeImpl[] n = (NodeImpl[]) children; |
| 157 |
return n[n.length - 1]; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
protected final NodeImpl getNthChild(int n) { |
| 169 |
if (children == null) { |
| 170 |
return null; |
| 171 |
} |
| 172 |
if (children instanceof NodeImpl) { |
| 173 |
return n == 0 ? (NodeImpl) children : null; |
| 174 |
} |
| 175 |
NodeImpl[] nodes = (NodeImpl[]) children; |
| 176 |
if (n < 0 || n >= nodes.length) { |
| 177 |
return null; |
| 178 |
} |
| 179 |
return nodes[n]; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
protected void removeChild(NodeImpl child) { |
| 189 |
if (children == null) { |
| 190 |
return; |
| 191 |
} |
| 192 |
if (children == child) { |
| 193 |
children = null; |
| 194 |
return; |
| 195 |
} |
| 196 |
NodeImpl[] nodes = (NodeImpl[]) children; |
| 197 |
for (int i = 0; i < nodes.length; i++) { |
| 198 |
if (nodes[i] == child) { |
| 199 |
if (nodes.length == 2) { |
| 200 |
children = nodes[1 - i]; |
| 201 |
} else { |
| 202 |
NodeImpl[] n2 = new NodeImpl[nodes.length - 1]; |
| 203 |
if (i > 0) { |
| 204 |
System.arraycopy(nodes, 0, n2, 0, i); |
| 205 |
} |
| 206 |
if (i < nodes.length - 1) { |
| 207 |
System.arraycopy(nodes, i + 1, n2, i, nodes.length - i - 1); |
| 208 |
} |
| 209 |
children = cleanUpChildren(n2); |
| 210 |
} |
| 211 |
break; |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
private NodeImpl[] cleanUpChildren( NodeImpl[] children) { |
| 226 |
boolean prevText = false; |
| 227 |
int j = 0; |
| 228 |
NodeImpl[] c2 = new NodeImpl[children.length]; |
| 229 |
for (NodeImpl node : children) { |
| 230 |
if (node instanceof TextImpl) { |
| 231 |
if (prevText) { |
| 232 |
TextImpl prev = (TextImpl) c2[j - 1]; |
| 233 |
prev.replaceStringValue(prev.getStringValue() + node.getStringValue()); |
| 234 |
} else if (!node.getStringValue().isEmpty()) { |
| 235 |
prevText = true; |
| 236 |
node.setSiblingPosition(j); |
| 237 |
c2[j++] = node; |
| 238 |
} |
| 239 |
} else { |
| 240 |
node.setSiblingPosition(j); |
| 241 |
c2[j++] = node; |
| 242 |
prevText = false; |
| 243 |
} |
| 244 |
} |
| 245 |
if (j == c2.length) { |
| 246 |
return c2; |
| 247 |
} else { |
| 248 |
return Arrays.copyOf(c2, j); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
public String getStringValue() { |
| 261 |
return getStringValueCS().toString(); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
public CharSequence getStringValueCS() { |
| 266 |
FastStringBuffer sb = null; |
| 267 |
|
| 268 |
NodeImpl next = getFirstChild(); |
| 269 |
while (next != null) { |
| 270 |
if (next instanceof TextImpl) { |
| 271 |
if (sb == null) { |
| 272 |
sb = new FastStringBuffer(FastStringBuffer.C64); |
| 273 |
} |
| 274 |
sb.append(next.getStringValueCS()); |
| 275 |
} |
| 276 |
next = next.getNextInDocument(this); |
| 277 |
} |
| 278 |
if (sb == null) { |
| 279 |
return ""; |
| 280 |
} |
| 281 |
return sb.condense(); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
protected synchronized void addChild( NodeImpl node, int index) { |
| 295 |
NodeImpl[] c; |
| 296 |
if (children == null) { |
| 297 |
c = new NodeImpl[10]; |
| 298 |
} else if (children instanceof NodeImpl) { |
| 299 |
c = new NodeImpl[10]; |
| 300 |
c[0] = (NodeImpl) children; |
| 301 |
} else { |
| 302 |
c = (NodeImpl[]) children; |
| 303 |
} |
| 304 |
if (index >= c.length) { |
| 305 |
c = Arrays.copyOf(c, c.length*2); |
| 306 |
} |
| 307 |
c[index] = node; |
| 308 |
node.setRawParent(this); |
| 309 |
node.setSiblingPosition(index); |
| 310 |
children = c; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
public void insertChildren( NodeInfo[] source, boolean atStart, boolean inherit) { |
| 340 |
if (atStart) { |
| 341 |
insertChildrenAt(source, 0, inherit); |
| 342 |
} else { |
| 343 |
insertChildrenAt(source, getNumberOfChildren(), inherit); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
protected synchronized void insertChildrenAt( NodeInfo[] source, int index, boolean inherit) { |
| 359 |
if (source.length == 0) { |
| 360 |
return; |
| 361 |
} |
| 362 |
NodeImpl[] source2 = adjustSuppliedNodeArray(source, inherit); |
| 363 |
if (children == null) { |
| 364 |
if (source2.length == 1) { |
| 365 |
children = source2[0]; |
| 366 |
((NodeImpl) children).setSiblingPosition(0); |
| 367 |
} else { |
| 368 |
children = cleanUpChildren(source2); |
| 369 |
} |
| 370 |
} else if (children instanceof NodeImpl) { |
| 371 |
int adjacent = index == 0 ? source2.length - 1 : 0; |
| 372 |
if (children instanceof TextImpl && source2[adjacent] instanceof TextImpl) { |
| 373 |
if (index == 0) { |
| 374 |
source2[adjacent].replaceStringValue( |
| 375 |
source2[adjacent].getStringValue() + ((TextImpl) children).getStringValue()); |
| 376 |
} else { |
| 377 |
source2[adjacent].replaceStringValue( |
| 378 |
((TextImpl) children).getStringValue() + source2[adjacent].getStringValue()); |
| 379 |
} |
| 380 |
children = cleanUpChildren(source2); |
| 381 |
} else { |
| 382 |
NodeImpl[] n2 = new NodeImpl[source2.length + 1]; |
| 383 |
if (index == 0) { |
| 384 |
System.arraycopy(source2, 0, n2, 0, source2.length); |
| 385 |
n2[source2.length] = (NodeImpl) children; |
| 386 |
} else { |
| 387 |
n2[0] = (NodeImpl) children; |
| 388 |
System.arraycopy(source2, 0, n2, 1, source2.length); |
| 389 |
} |
| 390 |
children = cleanUpChildren(n2); |
| 391 |
} |
| 392 |
} else { |
| 393 |
NodeImpl[] n0 = (NodeImpl[]) children; |
| 394 |
NodeImpl[] n2 = new NodeImpl[n0.length + source2.length]; |
| 395 |
System.arraycopy(n0, 0, n2, 0, index); |
| 396 |
System.arraycopy(source2, 0, n2, index, source2.length); |
| 397 |
System.arraycopy(n0, index, n2, index + source2.length, n0.length - index); |
| 398 |
children = cleanUpChildren(n2); |
| 399 |
} |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
private NodeImpl convertForeignNode( NodeInfo source) { |
| 405 |
if (!(source instanceof NodeImpl)) { |
| 406 |
int kind = source.getNodeKind(); |
| 407 |
switch (kind) { |
| 408 |
case Type.TEXT: |
| 409 |
return new TextImpl(source.getStringValue()); |
| 410 |
case Type.COMMENT: |
| 411 |
return new CommentImpl(source.getStringValue()); |
| 412 |
case Type.PROCESSING_INSTRUCTION: |
| 413 |
return new ProcInstImpl(source.getLocalPart(), source.getStringValue()); |
| 414 |
case Type.ELEMENT: |
| 415 |
throw new IllegalArgumentException( |
| 416 |
"Cannot insert an element node unless it is an instance of net.sf.saxon.om.tree.ElementImpl"); |
| 417 |
default: |
| 418 |
throw new IllegalArgumentException( |
| 419 |
"Cannot insert a node unless it is an element, comment, text node, or processing instruction"); |
| 420 |
} |
| 421 |
} |
| 422 |
return (NodeImpl) source; |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
protected synchronized void replaceChildrenAt( NodeInfo[] source, int index, boolean inherit) { |
| 439 |
if (children == null) { |
| 440 |
return; |
| 441 |
} |
| 442 |
NodeImpl[] source2 = adjustSuppliedNodeArray(source, inherit); |
| 443 |
if (children instanceof NodeImpl) { |
| 444 |
if (source2.length == 0) { |
| 445 |
children = null; |
| 446 |
} else if (source2.length == 1) { |
| 447 |
children = source2[0]; |
| 448 |
} else { |
| 449 |
NodeImpl[] n2 = new NodeImpl[source2.length]; |
| 450 |
System.arraycopy(source2, 0, n2, 0, source.length); |
| 451 |
children = cleanUpChildren(n2); |
| 452 |
} |
| 453 |
} else { |
| 454 |
NodeImpl[] n0 = (NodeImpl[]) children; |
| 455 |
NodeImpl[] n2 = new NodeImpl[n0.length + source2.length - 1]; |
| 456 |
System.arraycopy(n0, 0, n2, 0, index); |
| 457 |
System.arraycopy(source2, 0, n2, index, source2.length); |
| 458 |
System.arraycopy(n0, index + 1, n2, index + source2.length, n0.length - index - 1); |
| 459 |
children = cleanUpChildren(n2); |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
private NodeImpl[] adjustSuppliedNodeArray(NodeInfo[] source, boolean inherit) { |
| 464 |
NodeImpl[] source2 = new NodeImpl[source.length]; |
| 465 |
for (int i = 0; i < source.length; i++) { |
| 466 |
source2[i] = convertForeignNode(source[i]); |
| 467 |
NodeImpl child = source2[i]; |
| 468 |
child.setRawParent(this); |
| 469 |
if (child instanceof ElementImpl) { |
| 470 |
|
| 471 |
|
| 472 |
((ElementImpl) child).fixupInsertedNamespaces(inherit); |
| 473 |
} |
| 474 |
} |
| 475 |
return source2; |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
public synchronized void compact(int size) { |
| 486 |
if (size == 0) { |
| 487 |
children = null; |
| 488 |
} else if (size == 1) { |
| 489 |
if (children instanceof NodeImpl[]) { |
| 490 |
children = ((NodeImpl[]) children)[0]; |
| 491 |
} |
| 492 |
} else { |
| 493 |
children = Arrays.copyOf((NodeImpl[])children, size); |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
} |
| 499 |
|