| 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.wrapper; |
| 9 | |
| 10 | import net.sf.saxon.expr.parser.Location; |
| 11 | import net.sf.saxon.lib.NamespaceConstant; |
| 12 | import net.sf.saxon.om.*; |
| 13 | import net.sf.saxon.pattern.AnyNodeTest; |
| 14 | import net.sf.saxon.pattern.NodeTest; |
| 15 | import net.sf.saxon.trans.XPathException; |
| 16 | import net.sf.saxon.tree.NamespaceNode; |
| 17 | import net.sf.saxon.tree.iter.AxisIterator; |
| 18 | import net.sf.saxon.tree.iter.EmptyIterator; |
| 19 | import net.sf.saxon.tree.util.Navigator; |
| 20 | import net.sf.saxon.type.Type; |
| 21 | import net.sf.saxon.value.StringValue; |
| 22 | import net.sf.saxon.value.UntypedAtomicValue; |
| 23 | |
| 24 | /** |
| 25 | * A node in the XML parse tree representing an XML element, character content, or attribute. |
| 26 | * <p>This implementation of the NodeInfo interface contains common code used by many "wrapper" implementations |
| 27 | * for external data models.</p> |
| 28 | * |
| 29 | * @author Michael H. Kay |
| 30 | */ |
| 31 | |
| 32 | public abstract class AbstractNodeWrapper implements NodeInfo, VirtualNode { |
| 33 | |
| 34 | protected TreeInfo treeInfo; |
| 35 | |
| 36 | public TreeInfo getTreeInfo() { |
| 37 | return treeInfo; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Get the node underlying this virtual node. If this is a VirtualNode the method |
| 42 | * will automatically drill down through several layers of wrapping. |
| 43 | * |
| 44 | * @return The underlying node. |
| 45 | */ |
| 46 | |
| 47 | public final Object getRealNode() { |
| 48 | return getUnderlyingNode(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Get the name pool for this node |
| 53 | * |
| 54 | * @return the NamePool |
| 55 | */ |
| 56 | |
| 57 | public NamePool getNamePool() { |
| 58 | return getConfiguration().getNamePool(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get the typed value. |
| 63 | * |
| 64 | * @return the typed value. This will either be a single AtomicValue or a value whose items are |
| 65 | * atomic values. |
| 66 | * @since 8.5 - signature changed in 9.5 |
| 67 | */ |
| 68 | |
| 69 | public AtomicSequence atomize() throws XPathException { |
| 70 | switch (getNodeKind()) { |
| 71 | case Type.COMMENT: |
| 72 | case Type.PROCESSING_INSTRUCTION: |
| 73 | return new StringValue(getStringValueCS()); |
| 74 | default: |
| 75 | return new UntypedAtomicValue(getStringValueCS()); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * The equals() method compares nodes for identity. It is defined to give the same result |
| 81 | * as isSameNodeInfo(). |
| 82 | * |
| 83 | * @param other the node to be compared with this node |
| 84 | * @return true if this NodeInfo object and the supplied NodeInfo object represent |
| 85 | * the same node in the tree. |
| 86 | * @since 8.7 Previously, the effect of the equals() method was not defined. Callers |
| 87 | * should therefore be aware that third party implementations of the NodeInfo interface may |
| 88 | * not implement the correct semantics. It is safer to use isSameNodeInfo() for this reason. |
| 89 | * The equals() method has been defined because it is useful in contexts such as a Java Set or HashMap. |
| 90 | */ |
| 91 | |
| 92 | public boolean equals(Object other) { |
| 93 | if (!(other instanceof AbstractNodeWrapper)) { |
| 94 | return false; |
| 95 | } |
| 96 | AbstractNodeWrapper ow = (AbstractNodeWrapper) other; |
| 97 | return getUnderlyingNode().equals(ow.getUnderlyingNode()); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * The hashCode() method obeys the contract for hashCode(): that is, if two objects are equal |
| 102 | * (represent the same node) then they must have the same hashCode() |
| 103 | * |
| 104 | * @since 8.7 Previously, the effect of the equals() and hashCode() methods was not defined. Callers |
| 105 | * should therefore be aware that third party implementations of the NodeInfo interface may |
| 106 | * not implement the correct semantics. |
| 107 | */ |
| 108 | |
| 109 | public int hashCode() { |
| 110 | return getUnderlyingNode().hashCode(); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Get the System ID for the node. |
| 115 | * |
| 116 | * @return the System Identifier of the entity in the source document containing the node, |
| 117 | * or null if not known. Note this is not the same as the base URI: the base URI can be |
| 118 | * modified by xml:base, but the system ID cannot. |
| 119 | */ |
| 120 | |
| 121 | public String getSystemId() { |
| 122 | if (treeInfo instanceof GenericTreeInfo) { |
| 123 | return ((GenericTreeInfo) treeInfo).getSystemId(); |
| 124 | } else { |
| 125 | throw new UnsupportedOperationException(); |
| 126 | // must implement in subclass |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Set the system ID. Required because NodeInfo implements the JAXP Source interface |
| 132 | * @param uri the system ID. |
| 133 | */ |
| 134 | |
| 135 | public void setSystemId(String uri) { |
| 136 | if (treeInfo instanceof GenericTreeInfo) { |
| 137 | ((GenericTreeInfo) treeInfo).setSystemId(uri); |
| 138 | } else { |
| 139 | throw new UnsupportedOperationException(); |
| 140 | // must implement in subclass |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Get the Base URI for the node, that is, the URI used for resolving a relative URI contained |
| 146 | * in the node. |
| 147 | * |
| 148 | * @return the base URI of the node, taking into account xml:base attributes if present |
| 149 | */ |
| 150 | |
| 151 | public String getBaseURI() { |
| 152 | if (getNodeKind() == Type.NAMESPACE) { |
| 153 | return null; |
| 154 | } |
| 155 | NodeInfo n = this; |
| 156 | if (getNodeKind() != Type.ELEMENT) { |
| 157 | n = getParent(); |
| 158 | } |
| 159 | // Look for an xml:base attribute |
| 160 | while (n != null) { |
| 161 | String xmlbase = n.getAttributeValue(NamespaceConstant.XML, "base"); |
| 162 | if (xmlbase != null) { |
| 163 | return xmlbase; |
| 164 | } |
| 165 | n = n.getParent(); |
| 166 | } |
| 167 | // if not found, return the base URI of the document node |
| 168 | return getRoot().getSystemId(); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Get line number |
| 173 | * |
| 174 | * @return the line number of the node in its original source document; or -1 if not available. |
| 175 | * Always returns -1 in this implementation. |
| 176 | */ |
| 177 | |
| 178 | public int getLineNumber() { |
| 179 | return -1; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Get column number |
| 184 | * |
| 185 | * @return the column number of the node in its original source document; or -1 if not available |
| 186 | */ |
| 187 | |
| 188 | public int getColumnNumber() { |
| 189 | return -1; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Get an immutable copy of this Location object. By default Location objects may be mutable, so they |
| 194 | * should not be saved for later use. The result of this operation holds the same location information, |
| 195 | * but in an immutable form. |
| 196 | */ |
| 197 | public Location saveLocation() { |
| 198 | return this; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Return the string value of the node. The interpretation of this depends on the type |
| 203 | * of node. For an element it is the accumulated character content of the element, |
| 204 | * including descendant elements. |
| 205 | * |
| 206 | * @return the string value of the node |
| 207 | */ |
| 208 | |
| 209 | public String getStringValue() { |
| 210 | return getStringValueCS().toString(); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Get the display name of this node. For elements and attributes this is |
| 215 | * [prefix:]localname. For unnamed nodes, it is an empty string. |
| 216 | * |
| 217 | * @return The display name of this node. For a node with no name, return an |
| 218 | * empty string. |
| 219 | */ |
| 220 | |
| 221 | public String getDisplayName() { |
| 222 | String prefix = getPrefix(); |
| 223 | String local = getLocalPart(); |
| 224 | if (prefix.isEmpty()) { |
| 225 | return local; |
| 226 | } else { |
| 227 | return prefix + ":" + local; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Get the string value of a given attribute of this node. |
| 233 | * <p>The default implementation is suitable for nodes other than elements.</p> |
| 234 | * |
| 235 | * @param uri the namespace URI of the attribute name. Supply the empty string for an attribute |
| 236 | * that is in no namespace |
| 237 | * @param local the local part of the attribute name. |
| 238 | * @return the attribute value if it exists, or null if it does not exist. Always returns null |
| 239 | * if this node is not an element. |
| 240 | * @since 9.4 |
| 241 | */ |
| 242 | public String getAttributeValue(String uri, String local) { |
| 243 | return null; |
| 244 | } |
| 245 | |
| 246 | |
| 247 | /** |
| 248 | * Return an iteration over the nodes reached by the given axis from this node |
| 249 | * |
| 250 | * @param axisNumber the axis to be used |
| 251 | * @return a SequenceIterator that scans the nodes reached by the axis in turn. |
| 252 | */ |
| 253 | |
| 254 | public AxisIterator iterateAxis(byte axisNumber) { |
| 255 | return iterateAxis(axisNumber, AnyNodeTest.getInstance()); |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Return an iteration over the nodes reached by the given axis from this node. |
| 260 | * <p>This superclass provides implementations of the ancestor, ancestor-or-self, |
| 261 | * following, namespace, parent, preceding, self, and preceding-or-ancestor axes. |
| 262 | * The other axes are implemented by calling methods iterateAttributes(), |
| 263 | * iterateChildren(), iterateDescendants(), and iterateSiblings(), which must |
| 264 | * be provided in a subclass.</p> |
| 265 | * |
| 266 | * @param axisNumber the axis to be used |
| 267 | * @param nodeTest A pattern to be matched by the returned nodes |
| 268 | * @return a SequenceIterator that scans the nodes reached by the axis in turn. |
| 269 | */ |
| 270 | |
| 271 | public AxisIterator iterateAxis(byte axisNumber, NodeTest nodeTest) { |
| 272 | int nodeKind = getNodeKind(); |
| 273 | switch (axisNumber) { |
| 274 | case AxisInfo.ANCESTOR: |
| 275 | if (nodeKind == Type.DOCUMENT) { |
| 276 | return EmptyIterator.OfNodes.THE_INSTANCE; |
| 277 | } |
| 278 | return new Navigator.AxisFilter( |
| 279 | new Navigator.AncestorEnumeration(this, false), |
| 280 | nodeTest); |
| 281 | |
| 282 | case AxisInfo.ANCESTOR_OR_SELF: |
| 283 | if (nodeKind == Type.DOCUMENT) { |
| 284 | return Navigator.filteredSingleton(this, nodeTest); |
| 285 | } |
| 286 | return new Navigator.AxisFilter( |
| 287 | new Navigator.AncestorEnumeration(this, true), |
| 288 | nodeTest); |
| 289 | |
| 290 | case AxisInfo.ATTRIBUTE: |
| 291 | if (nodeKind != Type.ELEMENT) { |
| 292 | return EmptyIterator.OfNodes.THE_INSTANCE; |
| 293 | } |
| 294 | return iterateAttributes(nodeTest); |
| 295 | |
| 296 | case AxisInfo.CHILD: |
| 297 | if (nodeKind == Type.ELEMENT || nodeKind == Type.DOCUMENT) { |
| 298 | return iterateChildren(nodeTest); |
| 299 | } else { |
| 300 | return EmptyIterator.OfNodes.THE_INSTANCE; |
| 301 | } |
| 302 | |
| 303 | case AxisInfo.DESCENDANT: |
| 304 | if (nodeKind == Type.ELEMENT || nodeKind == Type.DOCUMENT) { |
| 305 | return iterateDescendants(nodeTest, false); |
| 306 | } else { |
| 307 | return EmptyIterator.OfNodes.THE_INSTANCE; |
| 308 | } |
| 309 | |
| 310 | case AxisInfo.DESCENDANT_OR_SELF: |
| 311 | if (nodeKind == Type.ELEMENT || nodeKind == Type.DOCUMENT) { |
| 312 | return iterateDescendants(nodeTest, true); |
| 313 | } else { |
| 314 | return Navigator.filteredSingleton(this, nodeTest); |
| 315 | } |
| 316 | |
| 317 | case AxisInfo.FOLLOWING: |
| 318 | return new Navigator.AxisFilter( |
| 319 | new Navigator.FollowingEnumeration(this), |
| 320 | nodeTest); |
| 321 | |
| 322 | case AxisInfo.FOLLOWING_SIBLING: |
| 323 | switch (nodeKind) { |
| 324 | case Type.DOCUMENT: |
| 325 | case Type.ATTRIBUTE: |
| 326 | case Type.NAMESPACE: |
| 327 | return EmptyIterator.OfNodes.THE_INSTANCE; |
| 328 | default: |
| 329 | return iterateSiblings(nodeTest, true); |
| 330 | } |
| 331 | |
| 332 | case AxisInfo.NAMESPACE: |
| 333 | if (nodeKind != Type.ELEMENT) { |
| 334 | return EmptyIterator.OfNodes.THE_INSTANCE; |
| 335 | } |
| 336 | return NamespaceNode.makeIterator(this, nodeTest); |
| 337 | |
| 338 | case AxisInfo.PARENT: |
| 339 | return Navigator.filteredSingleton(getParent(), nodeTest); |
| 340 | |
| 341 | case AxisInfo.PRECEDING: |
| 342 | return new Navigator.AxisFilter( |
| 343 | new Navigator.PrecedingEnumeration(this, false), |
| 344 | nodeTest); |
| 345 | |
| 346 | case AxisInfo.PRECEDING_SIBLING: |
| 347 | switch (nodeKind) { |
| 348 | case Type.DOCUMENT: |
| 349 | case Type.ATTRIBUTE: |
| 350 | case Type.NAMESPACE: |
| 351 | return EmptyIterator.OfNodes.THE_INSTANCE; |
| 352 | default: |
| 353 | return iterateSiblings(nodeTest, false); |
| 354 | } |
| 355 | |
| 356 | case AxisInfo.SELF: |
| 357 | return Navigator.filteredSingleton(this, nodeTest); |
| 358 | |
| 359 | case AxisInfo.PRECEDING_OR_ANCESTOR: |
| 360 | return new Navigator.AxisFilter( |
| 361 | new Navigator.PrecedingEnumeration(this, true), |
| 362 | nodeTest); |
| 363 | |
| 364 | default: |
| 365 | throw new IllegalArgumentException("Unknown axis number " + axisNumber); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Return an iterator over the attributes of this element node. |
| 371 | * This method is only called after checking that the node is an element. |
| 372 | * |
| 373 | * @param nodeTest a test that the returned attributes must satisfy |
| 374 | * @return an iterator over the attribute nodes. The order of the result, |
| 375 | * although arbitrary, must be consistent with document order. |
| 376 | */ |
| 377 | |
| 378 | protected abstract AxisIterator iterateAttributes(NodeTest nodeTest); |
| 379 | |
| 380 | /** |
| 381 | * Return an iterator over the children of this node. |
| 382 | * This method is only called after checking that the node is an element or document. |
| 383 | * |
| 384 | * @param nodeTest a test that the returned attributes must satisfy |
| 385 | * @return an iterator over the child nodes, in document order. |
| 386 | */ |
| 387 | |
| 388 | protected abstract AxisIterator iterateChildren(NodeTest nodeTest); |
| 389 | |
| 390 | /** |
| 391 | * Return an iterator over the siblings of this node. |
| 392 | * This method is only called after checking that the node is an element, text, comment, or PI node. |
| 393 | * |
| 394 | * @param nodeTest a test that the returned siblings must satisfy |
| 395 | * @param forwards true for following siblings, false for preceding siblings |
| 396 | * @return an iterator over the sibling nodes, in axis order. |
| 397 | */ |
| 398 | |
| 399 | protected abstract AxisIterator iterateSiblings(NodeTest nodeTest, boolean forwards); |
| 400 | |
| 401 | /** |
| 402 | * Return an iterator over the descendants of this node. |
| 403 | * This method is only called after checking that the node is an element or document node. |
| 404 | * |
| 405 | * @param nodeTest a test that the returned descendants must satisfy |
| 406 | * @param includeSelf true if this node is to be included in the result |
| 407 | * @return an iterator over the sibling nodes, in axis order. |
| 408 | */ |
| 409 | |
| 410 | protected AxisIterator iterateDescendants(NodeTest nodeTest, boolean includeSelf) { |
| 411 | AxisIterator iter = new Navigator.DescendantEnumeration(this, includeSelf, true); |
| 412 | if (!(nodeTest instanceof AnyNodeTest)) { |
| 413 | iter = new Navigator.AxisFilter(iter, nodeTest); |
| 414 | } |
| 415 | return iter; |
| 416 | } |
| 417 | |
| 418 | |
| 419 | |
| 420 | /** |
| 421 | * Get all namespace declarations and undeclarations defined on this element. |
| 422 | * <p>This method is intended primarily for internal use. User applications needing |
| 423 | * information about the namespace context of a node should use <code>iterateAxis(Axis.NAMESPACE)</code>. |
| 424 | * (However, not all implementations support the namespace axis, whereas all implementations are |
| 425 | * required to support this method.)</p> |
| 426 | * <p>This implementation of the method is suitable for all nodes other than elements; it returns |
| 427 | * an empty array.</p> |
| 428 | * |
| 429 | * @param buffer If this is non-null, and the result array fits in this buffer, then the result |
| 430 | * may overwrite the contents of this array, to avoid the cost of allocating a new array on the heap. |
| 431 | * @return An array of namespace binding objects representing the namespace declarations and undeclarations present on |
| 432 | * this element. For a node other than an element, return null. |
| 433 | * If the uri part is "", then this is a namespace undeclaration rather than a declaration. |
| 434 | * The XML namespace is never included in the list. If the supplied array is larger than required, |
| 435 | * then the first unused entry will be set to null. |
| 436 | * <p>For a node other than an element, the method returns null.</p> |
| 437 | */ |
| 438 | public NamespaceBinding[] getDeclaredNamespaces(NamespaceBinding[] buffer) { |
| 439 | return new NamespaceBinding[0]; |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * Get the root node - always a document node with this tree implementation |
| 444 | * |
| 445 | * @return the NodeInfo representing the containing document |
| 446 | */ |
| 447 | |
| 448 | public NodeInfo getRoot() { |
| 449 | NodeInfo p = this; |
| 450 | while (true) { |
| 451 | NodeInfo q = p.getParent(); |
| 452 | if (q==null) { |
| 453 | return p; |
| 454 | } |
| 455 | p = q; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | |
| 460 | /** |
| 461 | * Determine whether the node has any children. |
| 462 | * This implementation calls iterateAxis, so the subclass implementation of iterateAxis |
| 463 | * must avoid calling this method. |
| 464 | */ |
| 465 | |
| 466 | public boolean hasChildNodes() { |
| 467 | switch (getNodeKind()) { |
| 468 | case Type.DOCUMENT: |
| 469 | case Type.ELEMENT: |
| 470 | return iterateAxis(AxisInfo.CHILD).next() != null; |
| 471 | default: |
| 472 | return false; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * Get the fingerprint of the node |
| 478 | * |
| 479 | * @return the node's fingerprint, or -1 for an unnamed node |
| 480 | * @throws UnsupportedOperationException if this method is called for a node where |
| 481 | * hasFingerprint() returns false; |
| 482 | */ |
| 483 | @Override |
| 484 | public int getFingerprint() { |
| 485 | throw new UnsupportedOperationException(); |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * Test whether a fingerprint is available for the node name |
| 490 | */ |
| 491 | @Override |
| 492 | public boolean hasFingerprint() { |
| 493 | return false; |
| 494 | } |
| 495 | } |
| 496 |