| 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.util; |
| 9 | |
| 10 | import net.sf.saxon.Configuration; |
| 11 | import net.sf.saxon.event.Builder; |
| 12 | import net.sf.saxon.event.ReceiverOptions; |
| 13 | import net.sf.saxon.expr.parser.Location; |
| 14 | import net.sf.saxon.om.*; |
| 15 | import net.sf.saxon.pattern.NodeTest; |
| 16 | import net.sf.saxon.trans.XPathException; |
| 17 | import net.sf.saxon.tree.iter.AxisIterator; |
| 18 | import net.sf.saxon.tree.iter.EmptyIterator; |
| 19 | import net.sf.saxon.tree.iter.SingleNodeIterator; |
| 20 | import net.sf.saxon.type.*; |
| 21 | import net.sf.saxon.value.StringValue; |
| 22 | import net.sf.saxon.value.UntypedAtomicValue; |
| 23 | |
| 24 | /** |
| 25 | * A node (implementing the NodeInfo interface) representing an attribute, text node, |
| 26 | * comment, processing instruction, or namespace that has no parent (and of course no children). |
| 27 | * Exceptionally it is also used (during whitespace stripping) to represent a standalone element. |
| 28 | * <p>In general this class does not impose constraints defined in the data model: that is the responsibility |
| 29 | * of the client. For example, the class does not prevent you from creating a comment or text node that has |
| 30 | * a name or a non-trivial type annotation.</p> |
| 31 | * |
| 32 | * @author Michael H. Kay |
| 33 | */ |
| 34 | |
| 35 | public final class Orphan implements MutableNodeInfo { |
| 36 | |
| 37 | private short kind; |
| 38 | /*@Nullable*/ private NodeName nodeName = null; |
| 39 | private CharSequence stringValue; |
| 40 | private SchemaType typeAnnotation = null; |
| 41 | private int options = 0; // Bit-settings defined in ReceiverOptions |
| 42 | private GenericTreeInfo treeInfo; |
| 43 | |
| 44 | /** |
| 45 | * Create an Orphan node |
| 46 | * |
| 47 | * @param config the Saxon configuration |
| 48 | */ |
| 49 | |
| 50 | public Orphan(Configuration config) { |
| 51 | treeInfo = new GenericTreeInfo(config); |
| 52 | treeInfo.setRootNode(this); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Get information about the tree to which this NodeInfo belongs |
| 57 | * |
| 58 | * @return the TreeInfo |
| 59 | * @since 9.7 |
| 60 | */ |
| 61 | public TreeInfo getTreeInfo() { |
| 62 | return treeInfo; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Get the System ID for the node. Note this is not the |
| 67 | * same as the base URI: the base URI can be modified by xml:base, but |
| 68 | * the system ID cannot. The base URI is used primarily for resolving |
| 69 | * relative URIs within the content of the document. The system ID is |
| 70 | * used primarily in conjunction with a line number, for identifying the |
| 71 | * location of elements within the source XML, in particular when errors |
| 72 | * are found. For a document node, the System ID represents the value of |
| 73 | * the document-uri property as defined in the XDM data model. |
| 74 | * |
| 75 | * @return the System Identifier of the entity in the source document |
| 76 | * containing the node, or null if not known or not applicable. |
| 77 | * @since 8.4 |
| 78 | */ |
| 79 | @Override |
| 80 | public String getSystemId() { |
| 81 | return treeInfo.getSystemId(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get the Public ID of the entity containing the node. |
| 86 | * |
| 87 | * @return the Public Identifier of the entity in the source document |
| 88 | * containing the node, or null if not known or not applicable |
| 89 | * @since 9.7 |
| 90 | */ |
| 91 | @Override |
| 92 | public String getPublicId() { |
| 93 | return treeInfo.getPublicId(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Set the system identifier for this Source. |
| 98 | * <p> |
| 99 | * <p>The system identifier is optional if the source does not |
| 100 | * get its data from a URL, but it may still be useful to provide one. |
| 101 | * The application can use a system identifier, for example, to resolve |
| 102 | * relative URIs and to include in error messages and warnings.</p> |
| 103 | * |
| 104 | * @param systemId The system identifier as a URL string. |
| 105 | */ |
| 106 | @Override |
| 107 | public void setSystemId(String systemId) { |
| 108 | treeInfo.setSystemId(systemId); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Get the effective boolean value of this sequence |
| 113 | * @return the effective boolean value (always true for an Orphan node) |
| 114 | */ |
| 115 | @Override |
| 116 | public boolean effectiveBooleanValue() { |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Set the node kind |
| 122 | * |
| 123 | * @param kind the kind of node, for example {@link Type#ELEMENT} or {@link Type#ATTRIBUTE} |
| 124 | */ |
| 125 | |
| 126 | public void setNodeKind(short kind) { |
| 127 | this.kind = kind; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Set the name of the node |
| 132 | * |
| 133 | * @param nodeName the name of the node. May be null for unnamed nodes such as text and comment nodes |
| 134 | */ |
| 135 | |
| 136 | public void setNodeName(/*@Nullable*/ NodeName nodeName) { |
| 137 | this.nodeName = nodeName; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Set the string value of the node |
| 142 | * |
| 143 | * @param stringValue the string value of the node |
| 144 | */ |
| 145 | |
| 146 | public void setStringValue(CharSequence stringValue) { |
| 147 | this.stringValue = stringValue; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Set the type annotation of the node |
| 152 | * |
| 153 | * @param typeAnnotation the type annotation |
| 154 | */ |
| 155 | |
| 156 | public void setTypeAnnotation(SchemaType typeAnnotation) { |
| 157 | this.typeAnnotation = typeAnnotation; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Set the isId property |
| 162 | * |
| 163 | * @param id the isId property |
| 164 | */ |
| 165 | |
| 166 | public void setIsId(boolean id) { |
| 167 | setOption(ReceiverOptions.IS_ID, id); |
| 168 | } |
| 169 | |
| 170 | private void setOption(int option, boolean on) { |
| 171 | if (on) { |
| 172 | this.options |= option; |
| 173 | } else { |
| 174 | this.options &= ~option; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | private boolean isOption(int option) { |
| 179 | return (this.options & option) != 0; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Set the isIdref property |
| 184 | * |
| 185 | * @param idref the isIdref property |
| 186 | */ |
| 187 | |
| 188 | public void setIsIdref(boolean idref) { |
| 189 | setOption(ReceiverOptions.IS_IDREF, idref); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Set the disable-output-escaping property |
| 194 | * @param doe true if the property is to be set |
| 195 | */ |
| 196 | |
| 197 | public void setDisableOutputEscaping(boolean doe) { |
| 198 | setOption(ReceiverOptions.DISABLE_ESCAPING, doe); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Return the kind of node. |
| 203 | * |
| 204 | * @return one of the values Type.ELEMENT, Type.TEXT, Type.ATTRIBUTE, etc. |
| 205 | */ |
| 206 | |
| 207 | public int getNodeKind() { |
| 208 | return kind; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Get fingerprint. The fingerprint is a coded form of the expanded name |
| 213 | * of the node: two nodes |
| 214 | * with the same name code have the same namespace URI and the same local name. |
| 215 | * The fingerprint contains no information about the namespace prefix. For a name |
| 216 | * in the null namespace, the fingerprint is the same as the name code. |
| 217 | * |
| 218 | * @return an integer fingerprint; two nodes with the same fingerprint have |
| 219 | * the same expanded QName. For unnamed nodes (text nodes, comments, document nodes, |
| 220 | * and namespace nodes for the default namespace), returns -1. |
| 221 | * @throws UnsupportedOperationException if this kind of node does not hold |
| 222 | * namepool fingerprints (specifically, if {@link #hasFingerprint()} returns false). |
| 223 | * @since 8.4 (moved into FingerprintedNode at some stage; then back into NodeInfo at 9.8). |
| 224 | */ |
| 225 | @Override |
| 226 | public int getFingerprint() { |
| 227 | throw new UnsupportedOperationException(); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Ask whether this NodeInfo implementation holds a fingerprint identifying the name of the |
| 232 | * node in the NamePool. If the answer is true, then the {@link #getFingerprint} method must |
| 233 | * return the fingerprint of the node. If the answer is false, then the {@link #getFingerprint} |
| 234 | * method should throw an {@code UnsupportedOperationException}. In the case of unnamed nodes |
| 235 | * such as text nodes, the result can be either true (in which case getFingerprint() should |
| 236 | * return -1) or false (in which case getFingerprint may throw an exception). |
| 237 | * |
| 238 | * @return true if the implementation of this node provides fingerprints. |
| 239 | * @since 9.8; previously Saxon relied on using <code>FingerprintedNode</code> as a marker interface. |
| 240 | */ |
| 241 | @Override |
| 242 | public boolean hasFingerprint() { |
| 243 | return false; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Get the typed value. |
| 248 | * @return the typed value. |
| 249 | * @since 8.5 |
| 250 | */ |
| 251 | |
| 252 | public AtomicSequence atomize() throws XPathException { |
| 253 | switch (getNodeKind()) { |
| 254 | case Type.COMMENT: |
| 255 | case Type.PROCESSING_INSTRUCTION: |
| 256 | return new StringValue(stringValue); |
| 257 | case Type.TEXT: |
| 258 | case Type.DOCUMENT: |
| 259 | case Type.NAMESPACE: |
| 260 | return new UntypedAtomicValue(stringValue); |
| 261 | default: |
| 262 | if (typeAnnotation == null || typeAnnotation == Untyped.getInstance() || |
| 263 | typeAnnotation == BuiltInAtomicType.UNTYPED_ATOMIC) { |
| 264 | return new UntypedAtomicValue(stringValue); |
| 265 | } else { |
| 266 | return typeAnnotation.atomize(this); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Get the type annotation of this node, if any. The type annotation is represented as |
| 273 | * SchemaType object. |
| 274 | * <p>Types derived from a DTD are not reflected in the result of this method.</p> |
| 275 | * |
| 276 | * @return For element and attribute nodes: the type annotation derived from schema |
| 277 | * validation (defaulting to xs:untyped and xs:untypedAtomic in the absence of schema |
| 278 | * validation). For comments, text nodes, processing instructions, and namespaces: null. |
| 279 | * For document nodes, either xs:untyped if the document has not been validated, or |
| 280 | * xs:anyType if it has. |
| 281 | * @since 9.4 |
| 282 | */ |
| 283 | public SchemaType getSchemaType() { |
| 284 | if (typeAnnotation == null) { |
| 285 | if (kind == Type.ELEMENT) { |
| 286 | return Untyped.getInstance(); |
| 287 | } else if (kind == Type.ATTRIBUTE) { |
| 288 | return BuiltInAtomicType.UNTYPED_ATOMIC; |
| 289 | } |
| 290 | } |
| 291 | return typeAnnotation; |
| 292 | } |
| 293 | |
| 294 | /** |
| 295 | * Determine whether this is the same node as another node. |
| 296 | * <p>Note: a.equals(b) if and only if generateId(a)==generateId(b)</p> |
| 297 | * |
| 298 | * @return true if this Node object and the supplied Node object represent the |
| 299 | * same node in the tree. |
| 300 | */ |
| 301 | |
| 302 | public boolean equals(NodeInfo other) { |
| 303 | return this == other; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * The hashCode() method obeys the contract for hashCode(): that is, if two objects are equal |
| 308 | * (represent the same node) then they must have the same hashCode() |
| 309 | * |
| 310 | * @since 8.7 Previously, the effect of the equals() and hashCode() methods was not defined. Callers |
| 311 | * should therefore be aware that third party implementations of the NodeInfo interface may |
| 312 | * not implement the correct semantics. |
| 313 | */ |
| 314 | |
| 315 | public int hashCode() { |
| 316 | return super.hashCode(); |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Get the Base URI for the node, that is, the URI used for resolving a relative URI contained |
| 321 | * in the node. This will be the same as the System ID unless xml:base has been used. |
| 322 | */ |
| 323 | |
| 324 | /*@Nullable*/ |
| 325 | public String getBaseURI() { |
| 326 | if (kind == Type.PROCESSING_INSTRUCTION) { |
| 327 | return getSystemId(); |
| 328 | } else { |
| 329 | return null; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Get an immutable copy of this Location object. By default Location objects may be mutable, so they |
| 335 | * should not be saved for later use. The result of this operation holds the same location information, |
| 336 | * but in an immutable form. |
| 337 | */ |
| 338 | public Location saveLocation() { |
| 339 | return this; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Determine the relative position of this node and another node, in document order. |
| 344 | * The other node will always be in the same document. |
| 345 | * |
| 346 | * @param other The other node, whose position is to be compared with this node |
| 347 | * @return -1 if this node precedes the other node, +1 if it follows the other |
| 348 | * node, or 0 if they are the same node. (In this case, isSameNode() will always |
| 349 | * return true, and the two nodes will produce the same result for generateId()) |
| 350 | */ |
| 351 | |
| 352 | public int compareOrder(/*@NotNull*/ NodeInfo other) { |
| 353 | |
| 354 | // are they the same node? |
| 355 | if (this.equals(other)) { |
| 356 | return 0; |
| 357 | } |
| 358 | return this.hashCode() < other.hashCode() ? -1 : +1; |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Return the string value of the node. |
| 363 | * |
| 364 | * @return the string value of the node |
| 365 | */ |
| 366 | |
| 367 | public String getStringValue() { |
| 368 | return stringValue.toString(); |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Get the value of the item as a CharSequence. This is in some cases more efficient than |
| 373 | * the version of the method that returns a String. |
| 374 | */ |
| 375 | |
| 376 | public CharSequence getStringValueCS() { |
| 377 | return stringValue; |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Get the local part of the name of this node. This is the name after the ":" if any. |
| 382 | * |
| 383 | * @return the local part of the name. For an unnamed node, returns "". |
| 384 | */ |
| 385 | |
| 386 | public String getLocalPart() { |
| 387 | if (nodeName == null) { |
| 388 | return ""; |
| 389 | } else { |
| 390 | return nodeName.getLocalPart(); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Get the URI part of the name of this node. This is the URI corresponding to the |
| 396 | * prefix, or the URI of the default namespace if appropriate. |
| 397 | * |
| 398 | * @return The URI of the namespace of this node. For an unnamed node, return null. |
| 399 | * For a node with an empty prefix, return an empty string. |
| 400 | */ |
| 401 | |
| 402 | public String getURI() { |
| 403 | if (nodeName == null) { |
| 404 | return ""; |
| 405 | } else { |
| 406 | return nodeName.getURI(); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | /** |
| 411 | * Get the prefix of the name of the node. This is defined only for elements and attributes. |
| 412 | * If the node has no prefix, or for other kinds of node, return a zero-length string. |
| 413 | * |
| 414 | * @return The prefix of the name of the node. |
| 415 | */ |
| 416 | |
| 417 | public String getPrefix() { |
| 418 | if (nodeName == null) { |
| 419 | return ""; |
| 420 | } else { |
| 421 | return nodeName.getPrefix(); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Get the display name of this node. For elements and attributes this is [prefix:]localname. |
| 427 | * For unnamed nodes, it is an empty string. |
| 428 | * |
| 429 | * @return The display name of this node. |
| 430 | * For a node with no name, return an empty string. |
| 431 | */ |
| 432 | |
| 433 | public String getDisplayName() { |
| 434 | if (nodeName == null) { |
| 435 | return ""; |
| 436 | } else { |
| 437 | return nodeName.getDisplayName(); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * Get the NodeInfo object representing the parent of this node |
| 443 | * |
| 444 | * @return null - an Orphan has no parent. |
| 445 | */ |
| 446 | |
| 447 | /*@Nullable*/ |
| 448 | public NodeInfo getParent() { |
| 449 | return null; |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * Return an iteration over the nodes reached by the given axis from this node |
| 454 | * |
| 455 | * @param axisNumber the axis to be searched, e.g. Axis.CHILD or Axis.ANCESTOR |
| 456 | * @return a SequenceIterator that scans the nodes reached by the axis in turn. |
| 457 | */ |
| 458 | |
| 459 | /*@NotNull*/ |
| 460 | public AxisIterator iterateAxis(byte axisNumber) { |
| 461 | switch (axisNumber) { |
| 462 | case AxisInfo.ANCESTOR_OR_SELF: |
| 463 | case AxisInfo.DESCENDANT_OR_SELF: |
| 464 | case AxisInfo.SELF: |
| 465 | return SingleNodeIterator.makeIterator(this); |
| 466 | case AxisInfo.ANCESTOR: |
| 467 | case AxisInfo.ATTRIBUTE: |
| 468 | case AxisInfo.CHILD: |
| 469 | case AxisInfo.DESCENDANT: |
| 470 | case AxisInfo.FOLLOWING: |
| 471 | case AxisInfo.FOLLOWING_SIBLING: |
| 472 | case AxisInfo.NAMESPACE: |
| 473 | case AxisInfo.PARENT: |
| 474 | case AxisInfo.PRECEDING: |
| 475 | case AxisInfo.PRECEDING_SIBLING: |
| 476 | case AxisInfo.PRECEDING_OR_ANCESTOR: |
| 477 | return EmptyIterator.OfNodes.THE_INSTANCE; |
| 478 | default: |
| 479 | throw new IllegalArgumentException("Unknown axis number " + axisNumber); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | |
| 484 | /** |
| 485 | * Return an iteration over the nodes reached by the given axis from this node |
| 486 | * |
| 487 | * @param axisNumber the axis to be searched, e.g. Axis.CHILD or Axis.ANCESTOR |
| 488 | * @param nodeTest A pattern to be matched by the returned nodes |
| 489 | * @return a SequenceIterator that scans the nodes reached by the axis in turn. |
| 490 | */ |
| 491 | |
| 492 | /*@NotNull*/ |
| 493 | public AxisIterator iterateAxis(byte axisNumber, /*@NotNull*/ NodeTest nodeTest) { |
| 494 | switch (axisNumber) { |
| 495 | case AxisInfo.ANCESTOR_OR_SELF: |
| 496 | case AxisInfo.DESCENDANT_OR_SELF: |
| 497 | case AxisInfo.SELF: |
| 498 | return Navigator.filteredSingleton(this, nodeTest); |
| 499 | case AxisInfo.ANCESTOR: |
| 500 | case AxisInfo.ATTRIBUTE: |
| 501 | case AxisInfo.CHILD: |
| 502 | case AxisInfo.DESCENDANT: |
| 503 | case AxisInfo.FOLLOWING: |
| 504 | case AxisInfo.FOLLOWING_SIBLING: |
| 505 | case AxisInfo.NAMESPACE: |
| 506 | case AxisInfo.PARENT: |
| 507 | case AxisInfo.PRECEDING: |
| 508 | case AxisInfo.PRECEDING_SIBLING: |
| 509 | case AxisInfo.PRECEDING_OR_ANCESTOR: |
| 510 | return EmptyIterator.OfNodes.THE_INSTANCE; |
| 511 | default: |
| 512 | throw new IllegalArgumentException("Unknown axis number " + axisNumber); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | /** |
| 517 | * Get the string value of a given attribute of this node |
| 518 | * |
| 519 | * @param uri the namespace URI of the attribute name. Supply the empty string for an attribute |
| 520 | * that is in no namespace |
| 521 | * @param local the local part of the attribute name. |
| 522 | * @return the attribute value if it exists, or null if it does not exist. Always returns null |
| 523 | * if this node is not an element. |
| 524 | * @since 9.4 |
| 525 | */ |
| 526 | public String getAttributeValue(/*@NotNull*/ String uri, /*@NotNull*/ String local) { |
| 527 | return null; |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Get the root node of this tree (not necessarily a document node). |
| 532 | * Always returns this node in the case of an Orphan node. |
| 533 | */ |
| 534 | |
| 535 | /*@NotNull*/ |
| 536 | public NodeInfo getRoot() { |
| 537 | return this; |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * Determine whether the node has any children. |
| 542 | * |
| 543 | * @return false - an orphan node never has any children |
| 544 | */ |
| 545 | |
| 546 | public boolean hasChildNodes() { |
| 547 | return false; |
| 548 | } |
| 549 | |
| 550 | /** |
| 551 | * Get a character string that uniquely identifies this node. |
| 552 | * Note: a.isSameNode(b) if and only if generateId(a)==generateId(b) |
| 553 | * |
| 554 | * @param buffer a buffer, into which will be placed |
| 555 | * a string that uniquely identifies this node, within this |
| 556 | * document. The calling code prepends information to make the result |
| 557 | * unique across all documents. |
| 558 | */ |
| 559 | |
| 560 | public void generateId(/*@NotNull*/ FastStringBuffer buffer) { |
| 561 | buffer.append('Q'); |
| 562 | buffer.append(Integer.toString(hashCode())); |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * Get all namespace undeclarations and undeclarations defined on this element. |
| 567 | * |
| 568 | * @param buffer If this is non-null, and the result array fits in this buffer, then the result |
| 569 | * may overwrite the contents of this array, to avoid the cost of allocating a new array on the heap. |
| 570 | * @return An array of integers representing the namespace declarations and undeclarations present on |
| 571 | * this element. For a node other than an element, return null. Otherwise, the returned array is a |
| 572 | * sequence of namespace codes, whose meaning may be interpreted by reference to the name pool. The |
| 573 | * top half word of each namespace code represents the prefix, the bottom half represents the URI. |
| 574 | * If the bottom half is zero, then this is a namespace undeclaration rather than a declaration. |
| 575 | * The XML namespace is never included in the list. If the supplied array is larger than required, |
| 576 | * then the first unused entry will be set to -1. |
| 577 | * <p>For a node other than an element, the method returns null.</p> |
| 578 | */ |
| 579 | |
| 580 | /*@Nullable*/ |
| 581 | public NamespaceBinding[] getDeclaredNamespaces(NamespaceBinding[] buffer) { |
| 582 | return null; |
| 583 | } |
| 584 | |
| 585 | /** |
| 586 | * Ask whether this node has the is-id property |
| 587 | * |
| 588 | * @return true if the node is an ID |
| 589 | */ |
| 590 | |
| 591 | public boolean isId() { |
| 592 | return isOption(ReceiverOptions.IS_ID) || (kind == Type.ATTRIBUTE && nodeName.equals(StandardNames.XML_ID_NAME)); |
| 593 | } |
| 594 | |
| 595 | /** |
| 596 | * Ask whether this node has the is-idref property |
| 597 | * |
| 598 | * @return true if the node is an IDREF or IDREFS element or attribute |
| 599 | */ |
| 600 | |
| 601 | public boolean isIdref() { |
| 602 | return isOption(ReceiverOptions.IS_IDREF); |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * Ask whether the node has the disable-output-escaping property |
| 607 | * @return true if the node has the disable-output-escaping property |
| 608 | */ |
| 609 | |
| 610 | public boolean isDisableOutputEscaping() { |
| 611 | return isOption(ReceiverOptions.DISABLE_ESCAPING); |
| 612 | } |
| 613 | |
| 614 | /** |
| 615 | * Insert copies of a sequence of nodes as children of this node. |
| 616 | * <p>This method takes no action unless the target node is a document node or element node. It also |
| 617 | * takes no action in respect of any supplied nodes that are not elements, text nodes, comments, or |
| 618 | * processing instructions.</p> |
| 619 | * <p>The supplied nodes will be copied to form the new children. Adjacent text nodes will be merged, and |
| 620 | * zero-length text nodes removed.</p> |
| 621 | * |
| 622 | * @param source the nodes to be inserted |
| 623 | * @param atStart true if the new nodes are to be inserted before existing children; false if they are |
| 624 | * @param inherit true if the insert nodes are to inherit the namespaces of their new parent; false |
| 625 | * if such namespaces are to be undeclared |
| 626 | */ |
| 627 | |
| 628 | public void insertChildren(NodeInfo[] source, boolean atStart, boolean inherit) { |
| 629 | // no action: node is not a document or element node |
| 630 | } |
| 631 | |
| 632 | /** |
| 633 | * Insert copies of a sequence of nodes as siblings of this node. |
| 634 | * <p>This method takes no action unless the target node is an element, text node, comment, or |
| 635 | * processing instruction, and one that has a parent node. It also |
| 636 | * takes no action in respect of any supplied nodes that are not elements, text nodes, comments, or |
| 637 | * processing instructions.</p> |
| 638 | * <p>The supplied nodes must use the same data model implementation as the tree into which they |
| 639 | * will be inserted.</p> |
| 640 | * |
| 641 | * @param source the nodes to be inserted |
| 642 | * @param before true if the new nodes are to be inserted before the target node; false if they are |
| 643 | * @param inherit true if the insert nodes are to inherit the namespaces of their new parent; false |
| 644 | * if such namespaces are to be undeclared |
| 645 | */ |
| 646 | |
| 647 | public void insertSiblings(NodeInfo[] source, boolean before, boolean inherit) { |
| 648 | // no action: node has no parent |
| 649 | } |
| 650 | |
| 651 | /** |
| 652 | * Remove an attribute from this element node |
| 653 | * <p>If this node is not an element, or if it has no attribute with the specified name, |
| 654 | * this method takes no action.</p> |
| 655 | * <p>The attribute node itself is not modified in any way.</p> |
| 656 | * |
| 657 | * @param attribute the attribute node to be removed |
| 658 | */ |
| 659 | |
| 660 | public void removeAttribute(NodeInfo attribute) { |
| 661 | // no action: node is not an element |
| 662 | } |
| 663 | |
| 664 | /** |
| 665 | * Add an attribute to this element node. |
| 666 | * <p>If this node is not an element, or if the supplied node is not an attribute, the method |
| 667 | * takes no action. If the element already has an attribute with this name, the existing attribute |
| 668 | * is replaced.</p> |
| 669 | * |
| 670 | * @param nameCode the name of the new attribute |
| 671 | * @param attType the type annotation of the new attribute |
| 672 | * @param value the string value of the new attribute |
| 673 | * @param properties properties including IS_ID and IS_IDREF properties |
| 674 | */ |
| 675 | |
| 676 | public void addAttribute(NodeName nameCode, SimpleType attType, CharSequence value, int properties) { |
| 677 | // no action: node is not an element |
| 678 | } |
| 679 | |
| 680 | /** |
| 681 | * Delete this node (that is, detach it from its parent). |
| 682 | * <p>If this node has preceding and following siblings that are both text nodes, |
| 683 | * the two text nodes will be joined into a single text node (the identity of this node |
| 684 | * with respect to its predecessors is undefined).</p> |
| 685 | */ |
| 686 | |
| 687 | public void delete() { |
| 688 | // no action other than to mark it deleted: node has no parent from which it can be detached |
| 689 | kind = -1; |
| 690 | } |
| 691 | |
| 692 | /** |
| 693 | * Test whether this MutableNodeInfo object represents a node that has been deleted. |
| 694 | * Generally, such a node is unusable, and any attempt to use it will result in an exception |
| 695 | * being thrown |
| 696 | * |
| 697 | * @return true if this node has been deleted |
| 698 | */ |
| 699 | |
| 700 | public boolean isDeleted() { |
| 701 | return kind == -1; |
| 702 | } |
| 703 | |
| 704 | /** |
| 705 | * Replace this node with a given sequence of nodes |
| 706 | * |
| 707 | * @param replacement the replacement nodes |
| 708 | * @param inherit true if the replacement nodes are to inherit the namespaces of their new parent; false |
| 709 | * if such namespaces are to be undeclared |
| 710 | * @throws IllegalArgumentException if any of the replacement nodes is of the wrong kind. When replacing |
| 711 | * a child node, the replacement nodes must all be elements, text, comment, or PI nodes; when replacing |
| 712 | * an attribute, the replacement nodes must all be attributes. |
| 713 | * @throws IllegalStateException if this node is deleted or if it has no parent node. |
| 714 | */ |
| 715 | |
| 716 | public void replace(NodeInfo[] replacement, boolean inherit) { |
| 717 | throw new IllegalStateException("Cannot replace a parentless node"); |
| 718 | } |
| 719 | |
| 720 | /** |
| 721 | * Replace the string-value of this node. If applied to an element or document node, this |
| 722 | * causes all existing children to be deleted, and replaced with a new text node |
| 723 | * whose string value is the value supplied. The caller is responsible for checking |
| 724 | * that the value is valid, for example that comments do not contain a double hyphen; the |
| 725 | * implementation is not required to check for such conditions. |
| 726 | * |
| 727 | * @param stringValue the new string value |
| 728 | */ |
| 729 | |
| 730 | public void replaceStringValue(CharSequence stringValue) { |
| 731 | this.stringValue = stringValue; |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * Rename this node. |
| 736 | * <p>This call has no effect if applied to a nameless node, such as a text node or comment.</p> |
| 737 | * <p>If necessary, a new namespace binding will be added to the target element, or to the element |
| 738 | * parent of the target attribute</p> |
| 739 | * |
| 740 | * @param newNameCode the namecode of the new name in the name pool |
| 741 | * @throws IllegalArgumentException if the new name code is not present in the name pool, or if |
| 742 | * it has a (prefix, uri) pair in which the |
| 743 | * prefix is the same as that of an existing in-scope namespace binding and the uri is different from that |
| 744 | * namespace binding. |
| 745 | */ |
| 746 | |
| 747 | public void rename(NodeName newNameCode) { |
| 748 | if (kind == Type.ATTRIBUTE || kind == Type.PROCESSING_INSTRUCTION) { |
| 749 | nodeName = newNameCode; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | /** |
| 754 | * Add a namespace binding (that is, a namespace node) to this element. This call has no effect if applied |
| 755 | * to a node other than an element. |
| 756 | * |
| 757 | * @param nscode The namespace code representing the (prefix, uri) pair of the namespace binding to be |
| 758 | * added. If the target element already has a namespace binding with this (prefix, uri) pair, the call has |
| 759 | * no effect. If the target element currently has a namespace binding with this prefix and a different URI, an |
| 760 | * exception is raised. |
| 761 | * @param unused |
| 762 | * @throws IllegalArgumentException if the namespace code is not present in the namepool, or if the target |
| 763 | * element already has a namespace binding for this prefix |
| 764 | */ |
| 765 | |
| 766 | public void addNamespace(NamespaceBinding nscode, boolean unused) { |
| 767 | // no action: node is not an element |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * Remove type information from this node (and its ancestors, recursively). |
| 772 | * This method implements the upd:removeType() primitive defined in the XQuery Update specification. |
| 773 | * (Note: the caller is responsible for updating the set of nodes marked for revalidation) |
| 774 | */ |
| 775 | |
| 776 | public void removeTypeAnnotation() { |
| 777 | typeAnnotation = BuiltInAtomicType.UNTYPED_ATOMIC; |
| 778 | } |
| 779 | |
| 780 | /** |
| 781 | * Get a Builder suitable for building nodes that can be attached to this document. |
| 782 | * This implementation always throws an exception: the method should only be called on a document or element |
| 783 | * node when creating new children. |
| 784 | */ |
| 785 | |
| 786 | /*@NotNull*/ |
| 787 | public Builder newBuilder() { |
| 788 | throw new UnsupportedOperationException("Cannot create children for an Orphan node"); |
| 789 | } |
| 790 | } |
| 791 |