| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.tree.linked; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.event.Builder; |
| 12 |
import net.sf.saxon.event.Receiver; |
| 13 |
import net.sf.saxon.expr.parser.Location; |
| 14 |
import net.sf.saxon.om.*; |
| 15 |
import net.sf.saxon.trans.XPathException; |
| 16 |
import net.sf.saxon.tree.iter.AxisIterator; |
| 17 |
import net.sf.saxon.tree.iter.ListIterator; |
| 18 |
import net.sf.saxon.tree.util.AttributeCollectionImpl; |
| 19 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 20 |
import net.sf.saxon.type.AnyType; |
| 21 |
import net.sf.saxon.type.SchemaType; |
| 22 |
import net.sf.saxon.type.Type; |
| 23 |
import net.sf.saxon.type.Untyped; |
| 24 |
import net.sf.saxon.value.Whitespace; |
| 25 |
import net.sf.saxon.z.IntHashMap; |
| 26 |
|
| 27 |
import java.util.*; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public final class DocumentImpl extends ParentNodeImpl implements TreeInfo, MutableDocumentInfo { |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
private ElementImpl documentElement; |
| 44 |
|
| 45 |
|
| 46 |
private HashMap<String, NodeInfo> idTable; |
| 47 |
private long documentNumber; |
| 48 |
private String baseURI; |
| 49 |
private HashMap<String, String[]> entityTable; |
| 50 |
private Set<ElementImpl> nilledElements; |
| 51 |
private Set<ElementImpl> topWithinEntityElements; |
| 52 |
private IntHashMap<List<NodeInfo>> elementList; |
| 53 |
private HashMap<String, Object> userData; |
| 54 |
private Configuration config; |
| 55 |
private LineNumberMap lineNumberMap; |
| 56 |
private SystemIdMap systemIdMap = new SystemIdMap(); |
| 57 |
private boolean imaginary; |
| 58 |
private SpaceStrippingRule spaceStrippingRule = NoElementsSpaceStrippingRule.getInstance(); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public DocumentImpl() { |
| 66 |
setRawParent(null); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public NodeInfo getRootNode() { |
| 76 |
return this; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public void setConfiguration( Configuration config) { |
| 86 |
this.config = config; |
| 87 |
documentNumber = config.getDocumentNumberAllocator().allocateDocumentNumber(); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public Configuration getConfiguration() { |
| 97 |
return config; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public NamePool getNamePool() { |
| 105 |
return config.getNamePool(); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public Builder newBuilder() { |
| 116 |
LinkedTreeBuilder builder = new LinkedTreeBuilder(config.makePipelineConfiguration()); |
| 117 |
builder.setAllocateSequenceNumbers(false); |
| 118 |
return builder; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public void setImaginary(boolean imaginary) { |
| 128 |
this.imaginary = imaginary; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public boolean isImaginary() { |
| 138 |
return imaginary; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public boolean isTyped() { |
| 148 |
return documentElement != null && documentElement.getSchemaType() != Untyped.getInstance(); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public long getDocumentNumber() { |
| 156 |
return documentNumber; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public void setDocumentElement(ElementImpl e) { |
| 168 |
documentElement = e; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public void graftLocationMap( DocumentImpl original) { |
| 180 |
systemIdMap = original.systemIdMap; |
| 181 |
lineNumberMap = original.lineNumberMap; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public void setSystemId(String uri) { |
| 189 |
if (uri == null) { |
| 190 |
uri = ""; |
| 191 |
} |
| 192 |
systemIdMap.setSystemId(getRawSequenceNumber(), uri); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public String getSystemId() { |
| 200 |
return systemIdMap.getSystemId(getRawSequenceNumber()); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
public void setBaseURI(String uri) { |
| 210 |
baseURI = uri; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public String getBaseURI() { |
| 220 |
if (baseURI != null) { |
| 221 |
return baseURI; |
| 222 |
} |
| 223 |
return getSystemId(); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
void setSystemId(int seq, String uri) { |
| 235 |
if (uri == null) { |
| 236 |
uri = ""; |
| 237 |
} |
| 238 |
systemIdMap.setSystemId(seq, uri); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
String getSystemId(int seq) { |
| 250 |
return systemIdMap.getSystemId(seq); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
public void setLineNumbering() { |
| 259 |
lineNumberMap = new LineNumberMap(); |
| 260 |
lineNumberMap.setLineAndColumn(getRawSequenceNumber(), 0, -1); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
void setLineAndColumn(int sequence, int line, int column) { |
| 272 |
if (lineNumberMap != null && sequence >= 0) { |
| 273 |
lineNumberMap.setLineAndColumn(sequence, line, column); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
int getLineNumber(int sequence) { |
| 286 |
if (lineNumberMap != null && sequence >= 0) { |
| 287 |
return lineNumberMap.getLineNumber(sequence); |
| 288 |
} |
| 289 |
return -1; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
int getColumnNumber(int sequence) { |
| 301 |
if (lineNumberMap != null && sequence >= 0) { |
| 302 |
return lineNumberMap.getColumnNumber(sequence); |
| 303 |
} |
| 304 |
return -1; |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
public void addNilledElement(ElementImpl element) { |
| 314 |
if (nilledElements == null) { |
| 315 |
nilledElements = new HashSet<>(); |
| 316 |
} |
| 317 |
nilledElements.add(element); |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
boolean isNilledElement(ElementImpl element) { |
| 327 |
return nilledElements != null && nilledElements.contains(element); |
| 328 |
} |
| 329 |
|
| 330 |
public void markTopWithinEntity(ElementImpl element) { |
| 331 |
if (topWithinEntityElements == null) { |
| 332 |
topWithinEntityElements = new HashSet<>(); |
| 333 |
} |
| 334 |
topWithinEntityElements.add(element); |
| 335 |
} |
| 336 |
|
| 337 |
public boolean isTopWithinEntity(ElementImpl element) { |
| 338 |
return topWithinEntityElements != null && topWithinEntityElements.contains(element); |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
public int getLineNumber() { |
| 348 |
return 0; |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
public final int getNodeKind() { |
| 358 |
return Type.DOCUMENT; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
public final NodeImpl getNextSibling() { |
| 369 |
return null; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
public final NodeImpl getPreviousSibling() { |
| 380 |
return null; |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
public ElementImpl getDocumentElement() { |
| 391 |
return documentElement; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
public NodeInfo getRoot() { |
| 402 |
return this; |
| 403 |
} |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
public DocumentImpl getPhysicalRoot() { |
| 414 |
return this; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
public void generateId( FastStringBuffer buffer) { |
| 424 |
buffer.append('d'); |
| 425 |
buffer.append(Long.toString(documentNumber)); |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
AxisIterator getAllElements(int fingerprint) { |
| 437 |
if (elementList == null) { |
| 438 |
elementList = new IntHashMap<>(500); |
| 439 |
} |
| 440 |
IntHashMap<List<NodeInfo>> eList = elementList; |
| 441 |
List<NodeInfo> list = eList.get(fingerprint); |
| 442 |
if (list == null) { |
| 443 |
list = new ArrayList<>(500); |
| 444 |
NodeImpl next = getNextInDocument(this); |
| 445 |
while (next != null) { |
| 446 |
if (next.getNodeKind() == Type.ELEMENT && |
| 447 |
next.getFingerprint() == fingerprint) { |
| 448 |
list.add(next); |
| 449 |
} |
| 450 |
next = next.getNextInDocument(this); |
| 451 |
} |
| 452 |
eList.put(fingerprint, list); |
| 453 |
} |
| 454 |
return new ListIterator.OfNodes(list); |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
public void deIndex( NodeImpl node) { |
| 464 |
if (node instanceof ElementImpl) { |
| 465 |
IntHashMap<List<NodeInfo>> eList = elementList; |
| 466 |
if (eList != null) { |
| 467 |
List<NodeInfo> list = eList.get(node.getFingerprint()); |
| 468 |
if (list == null) { |
| 469 |
return; |
| 470 |
} |
| 471 |
list.remove(node); |
| 472 |
} |
| 473 |
if (node.isId()) { |
| 474 |
deregisterID(node.getStringValue()); |
| 475 |
} |
| 476 |
} else if (node instanceof AttributeImpl) { |
| 477 |
if (node.isId()) { |
| 478 |
deregisterID(node.getStringValue()); |
| 479 |
} |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
private void indexIDs() { |
| 489 |
if (idTable != null) { |
| 490 |
return; |
| 491 |
} |
| 492 |
idTable = new HashMap<>(256); |
| 493 |
|
| 494 |
NodeImpl curr = this; |
| 495 |
NodeImpl root = curr; |
| 496 |
while (curr != null) { |
| 497 |
if (curr.getNodeKind() == Type.ELEMENT) { |
| 498 |
|
| 499 |
ElementImpl e = (ElementImpl) curr; |
| 500 |
if (e.isId()) { |
| 501 |
registerID(e, Whitespace.trim(e.getStringValueCS())); |
| 502 |
} |
| 503 |
AttributeCollectionImpl atts = (AttributeCollectionImpl) e.getAttributeList(); |
| 504 |
for (int i = 0; i < atts.getLength(); i++) { |
| 505 |
if (!atts.isDeleted(i) && atts.isId(i) && NameChecker.isValidNCName(Whitespace.trim(atts.getValue(i)))) { |
| 506 |
|
| 507 |
registerID(e, Whitespace.trim(atts.getValue(i))); |
| 508 |
} |
| 509 |
} |
| 510 |
} |
| 511 |
curr = curr.getNextInDocument(root); |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
protected void registerID(NodeInfo e, String id) { |
| 523 |
|
| 524 |
if (idTable == null) { |
| 525 |
idTable = new HashMap<>(256); |
| 526 |
} |
| 527 |
HashMap<String, NodeInfo> table = idTable; |
| 528 |
table.putIfAbsent(id, e); |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
public NodeInfo selectID(String id, boolean getParent) { |
| 542 |
if (idTable == null) { |
| 543 |
indexIDs(); |
| 544 |
} |
| 545 |
assert idTable != null; |
| 546 |
NodeInfo node = idTable.get(id); |
| 547 |
if (node != null && getParent && node.isId() && node.getStringValue().equals(id)) { |
| 548 |
node = node.getParent(); |
| 549 |
} |
| 550 |
return node; |
| 551 |
} |
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
protected void deregisterID(String id) { |
| 561 |
id = Whitespace.trim(id); |
| 562 |
if (idTable != null) { |
| 563 |
idTable.remove(id); |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
public void setUnparsedEntity(String name, String uri, String publicId) { |
| 577 |
|
| 578 |
if (entityTable == null) { |
| 579 |
entityTable = new HashMap<>(10); |
| 580 |
} |
| 581 |
String[] ids = new String[2]; |
| 582 |
ids[0] = uri; |
| 583 |
ids[1] = publicId; |
| 584 |
entityTable.put(name, ids); |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
public Iterator<String> getUnparsedEntityNames() { |
| 596 |
if (entityTable == null) { |
| 597 |
List<String> ls = Collections.emptyList(); |
| 598 |
return ls.iterator(); |
| 599 |
} else { |
| 600 |
return entityTable.keySet().iterator(); |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
public String[] getUnparsedEntity(String name) { |
| 615 |
if (entityTable == null) { |
| 616 |
return null; |
| 617 |
} |
| 618 |
return entityTable.get(name); |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
public SchemaType getSchemaType() { |
| 628 |
if (documentElement == null || documentElement.getSchemaType() == Untyped.getInstance()) { |
| 629 |
return Untyped.getInstance(); |
| 630 |
} else { |
| 631 |
return AnyType.getInstance(); |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
public void copy( Receiver out, int copyOptions, Location locationId) throws XPathException { |
| 640 |
out.startDocument(CopyOptions.getStartDocumentProperties(copyOptions)); |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
for (Iterator<String> names = getUnparsedEntityNames(); names.hasNext(); ) { |
| 645 |
String name = names.next(); |
| 646 |
String[] details = getUnparsedEntity(name); |
| 647 |
assert details != null; |
| 648 |
out.setUnparsedEntity(name, details[0], details[1]); |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
NodeImpl next = getFirstChild(); |
| 654 |
while (next != null) { |
| 655 |
next.copy(out, copyOptions, locationId); |
| 656 |
next = next.getNextSibling(); |
| 657 |
} |
| 658 |
|
| 659 |
out.endDocument(); |
| 660 |
} |
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
public void replaceStringValue(CharSequence stringValue) { |
| 670 |
throw new UnsupportedOperationException("Cannot replace the value of a document node"); |
| 671 |
} |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
public void resetIndexes() { |
| 679 |
idTable = null; |
| 680 |
elementList = null; |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
public void setSpaceStrippingRule(SpaceStrippingRule rule) { |
| 693 |
this.spaceStrippingRule = rule; |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
public SpaceStrippingRule getSpaceStrippingRule() { |
| 707 |
return spaceStrippingRule; |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
public void setUserData(String key, Object value) { |
| 722 |
|
| 723 |
if (userData == null) { |
| 724 |
userData = new HashMap<>(4); |
| 725 |
} |
| 726 |
if (value == null) { |
| 727 |
userData.remove(key); |
| 728 |
} else { |
| 729 |
userData.put(key, value); |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
public Object getUserData(String key) { |
| 743 |
if (userData == null) { |
| 744 |
return null; |
| 745 |
} else { |
| 746 |
return userData.get(key); |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
|
| 751 |
} |
| 752 |
|