| 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.om.NodeInfo; |
| 11 | import net.sf.saxon.tree.iter.AxisIterator; |
| 12 | |
| 13 | import java.util.function.Function; |
| 14 | |
| 15 | |
| 16 | /** |
| 17 | * A RebasedNode is a view of a node, in a virtual tree that maps the base URI and/or |
| 18 | * system ID to new values |
| 19 | */ |
| 20 | |
| 21 | public class RebasedNode extends AbstractVirtualNode implements WrappingFunction { |
| 22 | |
| 23 | protected RebasedNode() { |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * This constructor is protected: nodes should be created using the makeWrapper |
| 28 | * factory method |
| 29 | * |
| 30 | * @param node The node to be wrapped |
| 31 | * @param parent The RebasedNode that wraps the parent of this node |
| 32 | */ |
| 33 | |
| 34 | protected RebasedNode(NodeInfo node, RebasedNode parent) { |
| 35 | this.node = node; |
| 36 | this.parent = parent; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Factory method to wrap a node with a wrapper that implements the Saxon |
| 41 | * NodeInfo interface. |
| 42 | * |
| 43 | * @param node The underlying node |
| 44 | * @param docWrapper The wrapper for the document node (must be supplied) |
| 45 | * @param parent The wrapper for the parent of the node (null if unknown) |
| 46 | * @return The new wrapper for the supplied node |
| 47 | */ |
| 48 | |
| 49 | /*@NotNull*/ |
| 50 | public static RebasedNode makeWrapper(NodeInfo node, |
| 51 | RebasedDocument docWrapper, |
| 52 | RebasedNode parent) { |
| 53 | RebasedNode wrapper = new RebasedNode(node, parent); |
| 54 | wrapper.docWrapper = docWrapper; |
| 55 | return wrapper; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Factory method to wrap a node with a VirtualNode |
| 60 | * |
| 61 | * @param node The underlying node |
| 62 | * @param parent The wrapper for the parent of the node (null if unknown) |
| 63 | * @return The new wrapper for the supplied node |
| 64 | */ |
| 65 | |
| 66 | /*@NotNull*/ |
| 67 | public RebasedNode makeWrapper(NodeInfo node, VirtualNode parent) { |
| 68 | RebasedNode wrapper = new RebasedNode(node, (RebasedNode) parent); |
| 69 | wrapper.docWrapper = this.docWrapper; |
| 70 | return wrapper; |
| 71 | } |
| 72 | |
| 73 | private Function<NodeInfo, String> getBaseUriMappingFunction() { |
| 74 | return ((RebasedDocument)docWrapper).getBaseUriMapper(); |
| 75 | } |
| 76 | |
| 77 | private Function<NodeInfo, String> getSystemIdMappingFunction() { |
| 78 | return ((RebasedDocument) docWrapper).getSystemIdMapper(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get the Base URI for the node, that is, the URI used for resolving a relative URI contained |
| 83 | * in the node. |
| 84 | */ |
| 85 | @Override |
| 86 | public String getBaseURI() { |
| 87 | return getBaseUriMappingFunction().apply(node); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Get the System ID for the node. |
| 92 | * |
| 93 | * @return the System Identifier of the entity in the source document containing the node, |
| 94 | * or null if not known. Note this is not the same as the base URI: the base URI can be |
| 95 | * modified by xml:base, but the system ID cannot. |
| 96 | */ |
| 97 | @Override |
| 98 | public String getSystemId() { |
| 99 | return getSystemIdMappingFunction().apply(node); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Determine whether this is the same node as another node. |
| 104 | * <p>Note: a.isSameNode(b) if and only if generateId(a)==generateId(b)</p> |
| 105 | * |
| 106 | * @return true if this Node object and the supplied Node object represent the |
| 107 | * same node in the tree. |
| 108 | */ |
| 109 | |
| 110 | public boolean equals(Object other) { |
| 111 | return other instanceof RebasedNode && node.equals(((RebasedNode) other).node); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Determine the relative position of this node and another node, in document order. |
| 116 | * The other node will always be in the same document. |
| 117 | * |
| 118 | * @param other The other node, whose position is to be compared with this node |
| 119 | * @return -1 if this node precedes the other node, +1 if it follows the other |
| 120 | * node, or 0 if they are the same node. (In this case, isSameNode() will always |
| 121 | * return true, and the two nodes will produce the same result for generateId()) |
| 122 | */ |
| 123 | |
| 124 | public int compareOrder(/*@NotNull*/ NodeInfo other) { |
| 125 | if (other instanceof RebasedNode) { |
| 126 | return node.compareOrder(((RebasedNode) other).node); |
| 127 | } else { |
| 128 | return node.compareOrder(other); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Get the NodeInfo object representing the parent of this node |
| 134 | */ |
| 135 | |
| 136 | /*@Nullable*/ |
| 137 | public NodeInfo getParent() { |
| 138 | if (parent == null) { |
| 139 | NodeInfo realParent = node.getParent(); |
| 140 | if (realParent != null) { |
| 141 | parent = makeWrapper(realParent, (RebasedDocument) docWrapper, null); |
| 142 | } |
| 143 | } |
| 144 | return parent; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Return an iteration over the nodes reached by the given axis from this node |
| 149 | * |
| 150 | * @param axisNumber the axis to be used |
| 151 | * @return a SequenceIterator that scans the nodes reached by the axis in turn. |
| 152 | */ |
| 153 | |
| 154 | /*@Nullable*/ |
| 155 | public AxisIterator iterateAxis(byte axisNumber) { |
| 156 | return new WrappingIterator(node.iterateAxis(axisNumber), this, null); |
| 157 | } |
| 158 | |
| 159 | } |
| 160 |