| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.tree.tiny; |
| 9 | |
| 10 | import net.sf.saxon.Configuration; |
| 11 | import net.sf.saxon.expr.parser.Location; |
| 12 | import net.sf.saxon.om.*; |
| 13 | import net.sf.saxon.pattern.AnyNodeTest; |
| 14 | import net.sf.saxon.pattern.NameTest; |
| 15 | import net.sf.saxon.pattern.NodeTest; |
| 16 | import net.sf.saxon.tree.NamespaceNode; |
| 17 | import net.sf.saxon.tree.iter.*; |
| 18 | import net.sf.saxon.tree.util.FastStringBuffer; |
| 19 | import net.sf.saxon.tree.util.Navigator; |
| 20 | import net.sf.saxon.type.SchemaType; |
| 21 | import net.sf.saxon.type.Type; |
| 22 | import net.sf.saxon.type.UType; |
| 23 | |
| 24 | |
| 25 | /** |
| 26 | * A node in a TinyTree representing an XML element, character content, or attribute. |
| 27 | * <p>This is the top-level class in the implementation class hierarchy; it essentially contains |
| 28 | * all those methods that can be defined using other primitive methods, without direct access |
| 29 | * to data.</p> |
| 30 | * |
| 31 | * @author Michael H. Kay |
| 32 | */ |
| 33 | |
| 34 | public abstract class TinyNodeImpl implements NodeInfo { |
| 35 | |
| 36 | protected TinyTree tree; |
| 37 | protected int nodeNr; |
| 38 | /*@Nullable*/ protected TinyNodeImpl parent = null; |
| 39 | |
| 40 | /** |
| 41 | * Get the genre of this item |
| 42 | * |
| 43 | * @return the genre |
| 44 | */ |
| 45 | @Override |
| 46 | public Genre getGenre() { |
| 47 | return Genre.NODE; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Get information about the tree to which this NodeInfo belongs |
| 52 | * |
| 53 | * @return the TreeInfo |
| 54 | * @since 9.7 |
| 55 | */ |
| 56 | public TreeInfo getTreeInfo() { |
| 57 | return tree; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Characteristic letters to identify each type of node, indexed using the node type |
| 62 | * values. These are used as the initial letter of the result of generate-id() |
| 63 | */ |
| 64 | |
| 65 | /*@NotNull*/ public static final char[] NODE_LETTER = |
| 66 | {'x', 'e', 'a', 't', 'x', 'x', 'x', 'p', 'c', 'r', 'x', 'x', 'x', 'n'}; |
| 67 | |
| 68 | /** |
| 69 | * To implement {@link Sequence}, this method returns the item itself |
| 70 | * |
| 71 | * @return this item |
| 72 | */ |
| 73 | |
| 74 | public NodeInfo head() { |
| 75 | return this; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * To implement {@link Sequence}, this method returns a singleton iterator |
| 80 | * that delivers this item in the form of a sequence |
| 81 | * |
| 82 | * @return a singleton iterator that returns this item |
| 83 | */ |
| 84 | |
| 85 | public UnfailingIterator<NodeInfo> iterate() { |
| 86 | return SingletonIterator.makeIterator(this); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Get the value of the item as a CharSequence. This is in some cases more efficient than |
| 91 | * the version of the method that returns a String. |
| 92 | */ |
| 93 | |
| 94 | public CharSequence getStringValueCS() { |
| 95 | return getStringValue(); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Get the type annotation of this node, if any. The type annotation is represented as |
| 100 | * SchemaType object. |
| 101 | * <p>Types derived from a DTD are not reflected in the result of this method.</p> |
| 102 | * |
| 103 | * @return For element and attribute nodes: the type annotation derived from schema |
| 104 | * validation (defaulting to xs:untyped and xs:untypedAtomic in the absence of schema |
| 105 | * validation). For comments, text nodes, processing instructions, and namespaces: null. |
| 106 | * For document nodes, either xs:untyped if the document has not been validated, or |
| 107 | * xs:anyType if it has. |
| 108 | * @since 9.4 |
| 109 | */ |
| 110 | public SchemaType getSchemaType() { |
| 111 | return null; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Get the column number of the node. |
| 116 | * The default implementation returns -1, meaning unknown |
| 117 | */ |
| 118 | |
| 119 | public int getColumnNumber() { |
| 120 | return tree.getColumnNumber(nodeNr); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Set the system id of this node. |
| 125 | * <p>This method is present to ensure that |
| 126 | * the class implements the javax.xml.transform.Source interface, so a node can |
| 127 | * be used as the source of a transformation.</p> |
| 128 | */ |
| 129 | |
| 130 | public void setSystemId(String uri) { |
| 131 | tree.setSystemId(nodeNr, uri); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Set the parent of this node. Providing this information is useful, |
| 136 | * if it is known, because otherwise getParent() has to search backwards |
| 137 | * through the document. |
| 138 | * |
| 139 | * @param parent the parent of this node |
| 140 | */ |
| 141 | |
| 142 | protected void setParentNode(TinyNodeImpl parent) { |
| 143 | this.parent = parent; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Determine whether this is the same node as another node |
| 148 | * |
| 149 | * @return true if this Node object and the supplied Node object represent the |
| 150 | * same node in the tree. |
| 151 | */ |
| 152 | |
| 153 | public boolean isSameNodeInfo(/*@NotNull*/ NodeInfo other) { |
| 154 | return this == other || |
| 155 | (other instanceof TinyNodeImpl && |
| 156 | tree == ((TinyNodeImpl) other).tree && |
| 157 | nodeNr == ((TinyNodeImpl) other).nodeNr && |
| 158 | getNodeKind() == other.getNodeKind()); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * The equals() method compares nodes for identity. It is defined to give the same result |
| 163 | * as isSameNodeInfo(). |
| 164 | * |
| 165 | * @param other the node to be compared with this node |
| 166 | * @return true if this NodeInfo object and the supplied NodeInfo object represent |
| 167 | * the same node in the tree. |
| 168 | * @since 8.7 Previously, the effect of the equals() method was not defined. Callers |
| 169 | * should therefore be aware that third party implementations of the NodeInfo interface may |
| 170 | * not implement the correct semantics. It is safer to use isSameNodeInfo() for this reason. |
| 171 | * The equals() method has been defined because it is useful in contexts such as a Java Set or HashMap. |
| 172 | */ |
| 173 | |
| 174 | public boolean equals(Object other) { |
| 175 | return other instanceof NodeInfo && isSameNodeInfo((NodeInfo) other); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * The hashCode() method obeys the contract for hashCode(): that is, if two objects are equal |
| 180 | * (represent the same node) then they must have the same hashCode() |
| 181 | * |
| 182 | * @since 8.7 Previously, the effect of the equals() and hashCode() methods was not defined. Callers |
| 183 | * should therefore be aware that third party implementations of the NodeInfo interface may |
| 184 | * not implement the correct semantics. |
| 185 | */ |
| 186 | |
| 187 | public int hashCode() { |
| 188 | return ((int) (tree.getDocumentNumber() & 0x3ff) << 20) ^ nodeNr ^ (getNodeKind() << 14); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Get the system ID for the entity containing the node. |
| 193 | */ |
| 194 | |
| 195 | public String getSystemId() { |
| 196 | return tree.getSystemId(nodeNr); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Get the base URI for the node. Default implementation for child nodes gets |
| 201 | * the base URI of the parent node. |
| 202 | */ |
| 203 | |
| 204 | public String getBaseURI() { |
| 205 | return getParent().getBaseURI(); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Get the line number of the node within its source document entity |
| 210 | */ |
| 211 | |
| 212 | public int getLineNumber() { |
| 213 | return tree.getLineNumber(nodeNr); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Get an immutable copy of this Location object. By default Location objects may be mutable, so they |
| 218 | * should not be saved for later use. The result of this operation holds the same location information, |
| 219 | * but in an immutable form. |
| 220 | */ |
| 221 | public Location saveLocation() { |
| 222 | return this; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Get the node sequence number (in document order). Sequence numbers are monotonic but not |
| 227 | * consecutive. The sequence number must be unique within the document (not, as in |
| 228 | * previous releases, within the whole document collection). |
| 229 | * For document nodes, elements, text nodes, comment nodes, and PIs, the sequence number |
| 230 | * is a long with the sequential node number in the top half and zero in the bottom half. |
| 231 | * The bottom half is used only for attributes and namespace. |
| 232 | * |
| 233 | * @return the sequence number |
| 234 | */ |
| 235 | |
| 236 | protected long getSequenceNumber() { |
| 237 | return (long) nodeNr << 32; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Determine the relative position of this node and another node, in document order. |
| 242 | * The other node will always be in the same document. |
| 243 | * |
| 244 | * @param other The other node, whose position is to be compared with this node |
| 245 | * @return -1 if this node precedes the other node, +1 if it follows the other |
| 246 | * node, or 0 if they are the same node. (In this case, isSameNode() will always |
| 247 | * return true, and the two nodes will produce the same result for generateId()) |
| 248 | */ |
| 249 | |
| 250 | public final int compareOrder(/*@NotNull*/ NodeInfo other) { |
| 251 | long a = getSequenceNumber(); |
| 252 | if (other instanceof TinyNodeImpl) { |
| 253 | long b = ((TinyNodeImpl) other).getSequenceNumber(); |
| 254 | return Long.compare(a, b); |
| 255 | } else { |
| 256 | // it must be a namespace node, or a TinyTextualElementText node |
| 257 | return 0 - other.compareOrder(this); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Ask whether this NodeInfo implementation holds a fingerprint identifying the name of the |
| 263 | * node in the NamePool. If the answer is true, then the {@link #getFingerprint} method must |
| 264 | * return the fingerprint of the node. If the answer is false, then the {@link #getFingerprint} |
| 265 | * method should throw an {@code UnsupportedOperationException}. In the case of unnamed nodes |
| 266 | * such as text nodes, the result can be either true (in which case getFingerprint() should |
| 267 | * return -1) or false (in which case getFingerprint may throw an exception). |
| 268 | * |
| 269 | * @return true if the implementation of this node provides fingerprints. |
| 270 | * @since 9.8; previously Saxon relied on using <code>FingerprintedNode</code> as a marker interface. |
| 271 | */ |
| 272 | @Override |
| 273 | public final boolean hasFingerprint() { |
| 274 | return true; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Get the fingerprint of the node, used for matching names |
| 279 | */ |
| 280 | |
| 281 | public int getFingerprint() { |
| 282 | int nc = tree.nameCode[nodeNr]; |
| 283 | if (nc == -1) { |
| 284 | return -1; |
| 285 | } |
| 286 | return nc & NamePool.FP_MASK; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Get the prefix part of the name of this node. This is the name before the ":" if any. |
| 291 | * |
| 292 | * @return the prefix part of the name. For an unnamed node, return "". |
| 293 | */ |
| 294 | |
| 295 | public String getPrefix() { |
| 296 | int code = tree.nameCode[nodeNr]; |
| 297 | if (code < 0) { |
| 298 | return ""; |
| 299 | } |
| 300 | if (!NamePool.isPrefixed(code)) { |
| 301 | return ""; |
| 302 | } |
| 303 | return tree.prefixPool.getPrefix(code >> 20); |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Get the URI part of the name of this node. This is the URI corresponding to the |
| 308 | * prefix, or the URI of the default namespace if appropriate. |
| 309 | * |
| 310 | * @return The URI of the namespace of this node. For an unnamed node, or for |
| 311 | * an element or attribute in the default namespace, return an empty string. |
| 312 | */ |
| 313 | |
| 314 | public String getURI() { |
| 315 | int code = tree.nameCode[nodeNr]; |
| 316 | if (code < 0) { |
| 317 | return ""; |
| 318 | } |
| 319 | return tree.getNamePool().getURI(code & NamePool.FP_MASK); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Get the display name of this node (a lexical QName). For elements and attributes this is [prefix:]localname. |
| 324 | * The original prefix is retained. For unnamed nodes, the result is an empty string. |
| 325 | * |
| 326 | * @return The display name of this node. |
| 327 | * For a node with no name, return an empty string. |
| 328 | */ |
| 329 | |
| 330 | public String getDisplayName() { |
| 331 | int code = tree.nameCode[nodeNr]; |
| 332 | if (code < 0) { |
| 333 | return ""; |
| 334 | } |
| 335 | if (NamePool.isPrefixed(code)) { |
| 336 | return getPrefix() + ":" + getLocalPart(); |
| 337 | } else { |
| 338 | return getLocalPart(); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Get the local part of the name of this node. |
| 344 | * |
| 345 | * @return The local name of this node. |
| 346 | * For a node with no name, return "". |
| 347 | */ |
| 348 | |
| 349 | public String getLocalPart() { |
| 350 | int code = tree.nameCode[nodeNr]; |
| 351 | if (code < 0) { |
| 352 | return ""; |
| 353 | } |
| 354 | return tree.getNamePool().getLocalName(code); |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Return an iterator over all the nodes reached by the given axis from this node |
| 359 | * |
| 360 | * @param axisNumber Identifies the required axis, eg. Axis.CHILD or Axis.PARENT |
| 361 | * @return a AxisIteratorImpl that scans the nodes reached by the axis in turn. |
| 362 | */ |
| 363 | |
| 364 | public AxisIterator iterateAxis(byte axisNumber) { |
| 365 | // fast path for child axis |
| 366 | if (axisNumber == AxisInfo.CHILD) { |
| 367 | if (hasChildNodes()) { |
| 368 | return new SiblingIterator(tree, this, null, true); |
| 369 | } else { |
| 370 | return EmptyIterator.OfNodes.THE_INSTANCE; |
| 371 | } |
| 372 | } else { |
| 373 | return iterateAxis(axisNumber, AnyNodeTest.getInstance()); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * Return an iterator over the nodes reached by the given axis from this node |
| 379 | * |
| 380 | * @param axisNumber Identifies the required axis, eg. Axis.CHILD or Axis.PARENT |
| 381 | * @param nodeTest A pattern to be matched by the returned nodes. |
| 382 | * @return a AxisIteratorImpl that scans the nodes reached by the axis in turn. |
| 383 | */ |
| 384 | |
| 385 | public AxisIterator iterateAxis(byte axisNumber, /*@NotNull*/ NodeTest nodeTest) { |
| 386 | |
| 387 | int type = getNodeKind(); |
| 388 | switch (axisNumber) { |
| 389 | case AxisInfo.ANCESTOR: |
| 390 | return new AncestorIterator(this, nodeTest); |
| 391 | |
| 392 | case AxisInfo.ANCESTOR_OR_SELF: |
| 393 | AxisIterator ancestors = new AncestorIterator(this, nodeTest); |
| 394 | if (nodeTest.matchesNode(this)) { |
| 395 | return new PrependAxisIterator(this, ancestors); |
| 396 | } else { |
| 397 | return ancestors; |
| 398 | } |
| 399 | |
| 400 | case AxisInfo.ATTRIBUTE: |
| 401 | if (type != Type.ELEMENT) { |
| 402 | return EmptyIterator.OfNodes.THE_INSTANCE; |
| 403 | } |
| 404 | if (tree.alpha[nodeNr] < 0) { |
| 405 | return EmptyIterator.OfNodes.THE_INSTANCE; |
| 406 | } |
| 407 | return new AttributeIterator(tree, nodeNr, nodeTest); |
| 408 | |
| 409 | case AxisInfo.CHILD: |
| 410 | if (hasChildNodes()) { |
| 411 | if (nodeTest instanceof NameTest && ((NameTest) nodeTest).getNodeKind() == Type.ELEMENT) { |
| 412 | // fast path for common case |
| 413 | return new NamedChildIterator(tree, this, nodeTest.getFingerprint()); |
| 414 | } else { |
| 415 | return new SiblingIterator(tree, this, nodeTest, true); |
| 416 | } |
| 417 | } else { |
| 418 | return EmptyIterator.OfNodes.THE_INSTANCE; |
| 419 | } |
| 420 | |
| 421 | case AxisInfo.DESCENDANT: |
| 422 | if (type == Type.DOCUMENT && |
| 423 | nodeTest instanceof NameTest && |
| 424 | nodeTest.getPrimitiveType() == Type.ELEMENT) { |
| 425 | return ((TinyDocumentImpl) this).getAllElements(nodeTest.getFingerprint()); |
| 426 | } else if (hasChildNodes()) { |
| 427 | if (tree.externalNodes != null) { |
| 428 | return new GraftingDescendantIterator(tree, this, nodeTest); |
| 429 | } else if (nodeTest.getUType().overlaps(UType.TEXT)) { |
| 430 | return new DescendantIterator(tree, this, nodeTest); |
| 431 | } else { |
| 432 | return new DescendantIteratorSansText(tree, this, nodeTest); |
| 433 | } |
| 434 | } else { |
| 435 | return EmptyIterator.OfNodes.THE_INSTANCE; |
| 436 | } |
| 437 | |
| 438 | case AxisInfo.DESCENDANT_OR_SELF: |
| 439 | AxisIterator descendants = iterateAxis(AxisInfo.DESCENDANT, nodeTest); |
| 440 | if (nodeTest.matchesNode(this)) { |
| 441 | return new PrependAxisIterator(this, descendants); |
| 442 | } else { |
| 443 | return descendants; |
| 444 | } |
| 445 | |
| 446 | case AxisInfo.FOLLOWING: |
| 447 | if (type == Type.ATTRIBUTE || type == Type.NAMESPACE) { |
| 448 | return new FollowingIterator(tree, getParent(), nodeTest, true); |
| 449 | } else if (tree.depth[nodeNr] == 0) { |
| 450 | return EmptyIterator.OfNodes.THE_INSTANCE; |
| 451 | } else { |
| 452 | return new FollowingIterator(tree, this, nodeTest, false); |
| 453 | } |
| 454 | |
| 455 | case AxisInfo.FOLLOWING_SIBLING: |
| 456 | if (type == Type.ATTRIBUTE || type == Type.NAMESPACE || tree.depth[nodeNr] == 0) { |
| 457 | return EmptyIterator.OfNodes.THE_INSTANCE; |
| 458 | } else { |
| 459 | return new SiblingIterator(tree, this, nodeTest, false); |
| 460 | } |
| 461 | |
| 462 | case AxisInfo.NAMESPACE: |
| 463 | if (type != Type.ELEMENT) { |
| 464 | return EmptyIterator.OfNodes.THE_INSTANCE; |
| 465 | } |
| 466 | return NamespaceNode.makeIterator(this, nodeTest); |
| 467 | |
| 468 | case AxisInfo.PARENT: |
| 469 | NodeInfo parent = getParent(); |
| 470 | return Navigator.filteredSingleton(parent, nodeTest); |
| 471 | |
| 472 | case AxisInfo.PRECEDING: |
| 473 | if (type == Type.ATTRIBUTE || type == Type.NAMESPACE) { |
| 474 | return new PrecedingIterator(tree, getParent(), nodeTest, false); |
| 475 | } else if (tree.depth[nodeNr] == 0) { |
| 476 | return EmptyIterator.OfNodes.THE_INSTANCE; |
| 477 | } else { |
| 478 | return new PrecedingIterator(tree, this, nodeTest, false); |
| 479 | } |
| 480 | |
| 481 | case AxisInfo.PRECEDING_SIBLING: |
| 482 | if (type == Type.ATTRIBUTE || type == Type.NAMESPACE || tree.depth[nodeNr] == 0) { |
| 483 | return EmptyIterator.OfNodes.THE_INSTANCE; |
| 484 | } else { |
| 485 | return new PrecedingSiblingIterator(tree, this, nodeTest); |
| 486 | } |
| 487 | |
| 488 | case AxisInfo.SELF: |
| 489 | return Navigator.filteredSingleton(this, nodeTest); |
| 490 | |
| 491 | case AxisInfo.PRECEDING_OR_ANCESTOR: |
| 492 | if (type == Type.DOCUMENT) { |
| 493 | return EmptyIterator.OfNodes.THE_INSTANCE; |
| 494 | } else if (type == Type.ATTRIBUTE || type == Type.NAMESPACE) { |
| 495 | // See test numb32. |
| 496 | TinyNodeImpl el = getParent(); |
| 497 | return new PrependAxisIterator(el, new PrecedingIterator(tree, el, nodeTest, true)); |
| 498 | } else { |
| 499 | return new PrecedingIterator(tree, this, nodeTest, true); |
| 500 | } |
| 501 | |
| 502 | default: |
| 503 | throw new IllegalArgumentException("Unknown axis number " + axisNumber); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * Find the parent node of this node. |
| 509 | * |
| 510 | * @return The Node object describing the containing element or root node. |
| 511 | */ |
| 512 | |
| 513 | /*@Nullable*/ |
| 514 | public TinyNodeImpl getParent() { |
| 515 | if (parent != null) { |
| 516 | return parent; |
| 517 | } |
| 518 | int p = getParentNodeNr(tree, nodeNr); |
| 519 | if (p == -1) { |
| 520 | return null; |
| 521 | } else { |
| 522 | return parent = tree.getNode(p); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * Static method to get the parent of a given node, without instantiating the node as an object. |
| 528 | * The starting node is any node other than an attribute or namespace node. |
| 529 | * |
| 530 | * @param tree the tree containing the starting node |
| 531 | * @param nodeNr the node number of the starting node within the tree |
| 532 | * @return the node number of the parent node, or -1 if there is no parent. |
| 533 | */ |
| 534 | |
| 535 | static int getParentNodeNr(/*@NotNull*/ TinyTree tree, int nodeNr) { |
| 536 | |
| 537 | if (tree.depth[nodeNr] == 0) { |
| 538 | return -1; |
| 539 | } |
| 540 | |
| 541 | // follow the next-sibling pointers until we reach either a next sibling pointer that |
| 542 | // points backwards, or a parent-pointer pseudo-node |
| 543 | int p = tree.next[nodeNr]; |
| 544 | while (p > nodeNr) { |
| 545 | if (tree.nodeKind[p] == Type.PARENT_POINTER) { |
| 546 | return tree.alpha[p]; |
| 547 | } |
| 548 | p = tree.next[p]; |
| 549 | } |
| 550 | return p; |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * Determine whether the node has any children. |
| 555 | * |
| 556 | * @return <code>true</code> if this node has any attributes, |
| 557 | * <code>false</code> otherwise. |
| 558 | */ |
| 559 | |
| 560 | public boolean hasChildNodes() { |
| 561 | // overridden in TinyParentNodeImpl |
| 562 | return false; |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * Get the string value of a given attribute of this node |
| 567 | * |
| 568 | * @param uri the namespace URI of the attribute name. Supply the empty string for an attribute |
| 569 | * that is in no namespace |
| 570 | * @param local the local part of the attribute name. |
| 571 | * @return the attribute value if it exists, or null if it does not exist. Always returns null |
| 572 | * if this node is not an element. |
| 573 | * @since 9.4 |
| 574 | */ |
| 575 | |
| 576 | public String getAttributeValue(/*@NotNull*/ String uri, /*@NotNull*/ String local) { |
| 577 | return null; |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * Get the root node of the tree (not necessarily a document node) |
| 582 | * |
| 583 | * @return the NodeInfo representing the root of this tree |
| 584 | */ |
| 585 | |
| 586 | public NodeInfo getRoot() { |
| 587 | return nodeNr==0 ? this : tree.getRootNode(); |
| 588 | // if (tree.depth[nodeNr] == 0) { |
| 589 | // return this; |
| 590 | // } |
| 591 | // if (parent != null) { |
| 592 | // return parent.getRoot(); |
| 593 | // } |
| 594 | // return tree.getNode(tree.getRootNode(nodeNr)); |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * Get the configuration |
| 599 | */ |
| 600 | |
| 601 | public Configuration getConfiguration() { |
| 602 | return tree.getConfiguration(); |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * Get the NamePool for the tree containing this node |
| 607 | * |
| 608 | * @return the NamePool |
| 609 | */ |
| 610 | |
| 611 | public NamePool getNamePool() { |
| 612 | return tree.getNamePool(); |
| 613 | } |
| 614 | |
| 615 | /** |
| 616 | * Get all namespace undeclarations and undeclarations defined on this element. |
| 617 | * |
| 618 | * @param buffer If this is non-null, and the result array fits in this buffer, then the result |
| 619 | * may overwrite the contents of this array, to avoid the cost of allocating a new array on the heap. |
| 620 | * @return An array of objects representing the namespace declarations and undeclarations present on |
| 621 | * this element. For a node other than an element, return null. Otherwise, the returned array is a |
| 622 | * sequence of namespace binding objects (essentially prefix/uri pairs) |
| 623 | * If the URI is "", then this is a namespace undeclaration rather than a declaration. |
| 624 | * The XML namespace is never included in the list. If the supplied array is larger than required, |
| 625 | * then the first unused entry will be set to null. |
| 626 | */ |
| 627 | |
| 628 | /*@Nullable*/ |
| 629 | public NamespaceBinding[] getDeclaredNamespaces(NamespaceBinding[] buffer) { |
| 630 | return null; |
| 631 | } |
| 632 | |
| 633 | /** |
| 634 | * Get a character string that uniquely identifies this node |
| 635 | * |
| 636 | * @param buffer buffer, which on return will contain |
| 637 | * a character string that uniquely identifies this node. |
| 638 | */ |
| 639 | |
| 640 | public void generateId(/*@NotNull*/ FastStringBuffer buffer) { |
| 641 | buffer.append("d"); |
| 642 | buffer.append(Long.toString(tree.getDocumentNumber())); |
| 643 | buffer.append(NODE_LETTER[getNodeKind()]); |
| 644 | buffer.append(Integer.toString(nodeNr)); |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * Test if this node is an ancestor-or-self of another |
| 649 | * |
| 650 | * @param d the putative descendant-or-self node |
| 651 | * @return true if this node is an ancestor-or-self of d |
| 652 | */ |
| 653 | |
| 654 | public boolean isAncestorOrSelf(/*@NotNull*/ TinyNodeImpl d) { |
| 655 | // If it's a different tree, return false |
| 656 | if (tree != d.tree) { |
| 657 | return false; |
| 658 | } |
| 659 | int dn = d.nodeNr; |
| 660 | // If d is an attribute, then either "this" must be the same attribute, or "this" must |
| 661 | // be an ancestor-or-self of the parent of d. |
| 662 | if (d instanceof TinyAttributeImpl) { |
| 663 | if (this instanceof TinyAttributeImpl) { |
| 664 | return nodeNr == dn; |
| 665 | } else { |
| 666 | dn = tree.attParent[dn]; |
| 667 | } |
| 668 | } |
| 669 | // If this is an attribute, return false (we've already handled the case where it's the same attribute) |
| 670 | if (this instanceof TinyAttributeImpl) { |
| 671 | return false; |
| 672 | } |
| 673 | |
| 674 | // From now on, we know that both "this" and "dn" are nodes in the primary array |
| 675 | |
| 676 | // If this node is later in document order, return false |
| 677 | if (nodeNr > dn) { |
| 678 | return false; |
| 679 | } |
| 680 | |
| 681 | // If it's the same node, return true |
| 682 | if (nodeNr == dn) { |
| 683 | return true; |
| 684 | } |
| 685 | |
| 686 | // We've dealt with the "self" case: to be an ancestor, it must be an element or document node |
| 687 | if (!(this instanceof TinyParentNodeImpl)) { |
| 688 | return false; |
| 689 | } |
| 690 | |
| 691 | // If this node is deeper than the target node then it can't be an ancestor |
| 692 | if (tree.depth[nodeNr] >= tree.depth[dn]) { |
| 693 | return false; |
| 694 | } |
| 695 | |
| 696 | // The following code will exit as soon as we find an ancestor that has a following-sibling: |
| 697 | // when that happens, we know it's an ancestor iff its following-sibling is beyond the node we're |
| 698 | // looking for. If the ancestor has no following sibling, we go up a level. |
| 699 | |
| 700 | // The algorithm depends on the following assertion: if A is before D in document order, then |
| 701 | // either A is an ancestor of D, or some ancestor-or-self of A has a following-sibling that |
| 702 | // is before-or-equal to D in document order. |
| 703 | |
| 704 | int n = nodeNr; |
| 705 | while (true) { |
| 706 | int nextSib = tree.next[n]; |
| 707 | if (nextSib < 0 || nextSib > dn) { |
| 708 | return true; |
| 709 | } else if (tree.depth[nextSib] == 0) { |
| 710 | return true; |
| 711 | } else if (nextSib < n) { |
| 712 | n = nextSib; |
| 713 | // continue |
| 714 | } else { |
| 715 | return false; |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | /** |
| 721 | * Determine whether this node has the is-id property |
| 722 | * |
| 723 | * @return true if the node is an ID |
| 724 | */ |
| 725 | |
| 726 | public boolean isId() { |
| 727 | return false; // overridden for element and attribute nodes |
| 728 | } |
| 729 | |
| 730 | /** |
| 731 | * Determine whether this node has the is-idref property |
| 732 | * |
| 733 | * @return true if the node is an IDREF or IDREFS element or attribute |
| 734 | */ |
| 735 | |
| 736 | public boolean isIdref() { |
| 737 | return false; // overridden for element and attribute nodes |
| 738 | } |
| 739 | |
| 740 | /** |
| 741 | * Determine whether the node has the is-nilled property |
| 742 | * |
| 743 | * @return true if the node has the is-nilled property |
| 744 | */ |
| 745 | |
| 746 | public boolean isNilled() { |
| 747 | return tree.isNilled(nodeNr); |
| 748 | } |
| 749 | |
| 750 | /** |
| 751 | * Ask whether this is a node in a streamed document |
| 752 | * |
| 753 | * @return true if the node is in a document being processed using streaming |
| 754 | */ |
| 755 | @Override |
| 756 | public boolean isStreamed() { |
| 757 | return false; |
| 758 | } |
| 759 | |
| 760 | /** |
| 761 | * Get the TinyTree object containing this node |
| 762 | * |
| 763 | * @return the TinyTree. Note that this may also contain other unrelated trees |
| 764 | */ |
| 765 | |
| 766 | public TinyTree getTree() { |
| 767 | return tree; |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * Get the node number of this node within the TinyTree. This method is intended for internal use. |
| 772 | * |
| 773 | * @return the internal node number |
| 774 | */ |
| 775 | |
| 776 | public int getNodeNumber() { |
| 777 | return nodeNr; |
| 778 | } |
| 779 | |
| 780 | } |
| 781 |