| 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.event.Receiver; |
| 11 | import net.sf.saxon.expr.parser.Location; |
| 12 | import net.sf.saxon.om.*; |
| 13 | import net.sf.saxon.trans.XPathException; |
| 14 | import net.sf.saxon.tree.iter.AxisIterator; |
| 15 | import net.sf.saxon.type.BuiltInAtomicType; |
| 16 | import net.sf.saxon.type.SchemaType; |
| 17 | import net.sf.saxon.type.Type; |
| 18 | import net.sf.saxon.type.Untyped; |
| 19 | import net.sf.saxon.value.UntypedAtomicValue; |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | * A TypeStrippedNode is a view of a node, in a virtual tree that has type |
| 24 | * annotations stripped from it. |
| 25 | */ |
| 26 | |
| 27 | public class TypeStrippedNode extends AbstractVirtualNode implements WrappingFunction { |
| 28 | |
| 29 | protected TypeStrippedNode() { |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * This constructor is protected: nodes should be created using the makeWrapper |
| 34 | * factory method |
| 35 | * |
| 36 | * @param node The node to be wrapped |
| 37 | * @param parent The StrippedNode that wraps the parent of this node |
| 38 | */ |
| 39 | |
| 40 | protected TypeStrippedNode(NodeInfo node, TypeStrippedNode parent) { |
| 41 | this.node = node; |
| 42 | this.parent = parent; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Factory method to wrap a node with a wrapper that implements the Saxon |
| 47 | * NodeInfo interface. |
| 48 | * |
| 49 | * @param node The underlying node |
| 50 | * @param docWrapper The wrapper for the document node (must be supplied) |
| 51 | * @param parent The wrapper for the parent of the node (null if unknown) |
| 52 | * @return The new wrapper for the supplied node |
| 53 | */ |
| 54 | |
| 55 | /*@NotNull*/ |
| 56 | public static TypeStrippedNode makeWrapper(NodeInfo node, |
| 57 | TypeStrippedDocument docWrapper, |
| 58 | TypeStrippedNode parent) { |
| 59 | TypeStrippedNode wrapper = new TypeStrippedNode(node, parent); |
| 60 | wrapper.docWrapper = docWrapper; |
| 61 | return wrapper; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Factory method to wrap a node with a VirtualNode |
| 66 | * |
| 67 | * @param node The underlying node |
| 68 | * @param parent The wrapper for the parent of the node (null if unknown) |
| 69 | * @return The new wrapper for the supplied node |
| 70 | */ |
| 71 | |
| 72 | /*@NotNull*/ |
| 73 | public VirtualNode makeWrapper(NodeInfo node, VirtualNode parent) { |
| 74 | TypeStrippedNode wrapper = new TypeStrippedNode(node, (TypeStrippedNode) parent); |
| 75 | wrapper.docWrapper = this.docWrapper; |
| 76 | return wrapper; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get the typed value. |
| 81 | * |
| 82 | * @return the typed value. If requireSingleton is set to true, the result will always be an |
| 83 | * AtomicValue. In other cases it may be a Value representing a sequence whose items are atomic |
| 84 | * values. |
| 85 | * @since 8.5 |
| 86 | */ |
| 87 | |
| 88 | /*@NotNull*/ |
| 89 | public AtomicSequence atomize() throws XPathException { |
| 90 | return new UntypedAtomicValue(getStringValueCS()); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Get the type annotation |
| 95 | * |
| 96 | * @return the type annotation of the base node |
| 97 | */ |
| 98 | @Override |
| 99 | public SchemaType getSchemaType() { |
| 100 | if (getNodeKind() == Type.ELEMENT) { |
| 101 | return Untyped.getInstance(); |
| 102 | } else { |
| 103 | return BuiltInAtomicType.UNTYPED_ATOMIC; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Determine whether this is the same node as another node. |
| 109 | * <p>Note: a.isSameNode(b) if and only if generateId(a)==generateId(b)</p> |
| 110 | * |
| 111 | * @return true if this Node object and the supplied Node object represent the |
| 112 | * same node in the tree. |
| 113 | */ |
| 114 | |
| 115 | public boolean equals(Object other) { |
| 116 | if (other instanceof TypeStrippedNode) { |
| 117 | return node.equals(((TypeStrippedNode) other).node); |
| 118 | } else { |
| 119 | return node.equals(other); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Determine the relative position of this node and another node, in document order. |
| 125 | * The other node will always be in the same document. |
| 126 | * |
| 127 | * @param other The other node, whose position is to be compared with this node |
| 128 | * @return -1 if this node precedes the other node, +1 if it follows the other |
| 129 | * node, or 0 if they are the same node. (In this case, isSameNode() will always |
| 130 | * return true, and the two nodes will produce the same result for generateId()) |
| 131 | */ |
| 132 | |
| 133 | public int compareOrder(/*@NotNull*/ NodeInfo other) { |
| 134 | if (other instanceof TypeStrippedNode) { |
| 135 | return node.compareOrder(((TypeStrippedNode) other).node); |
| 136 | } else { |
| 137 | return node.compareOrder(other); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Get the NodeInfo object representing the parent of this node |
| 143 | */ |
| 144 | |
| 145 | /*@Nullable*/ |
| 146 | public NodeInfo getParent() { |
| 147 | if (parent == null) { |
| 148 | NodeInfo realParent = node.getParent(); |
| 149 | if (realParent != null) { |
| 150 | parent = makeWrapper(realParent, (TypeStrippedDocument) docWrapper, null); |
| 151 | } |
| 152 | } |
| 153 | return parent; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Return an iteration over the nodes reached by the given axis from this node |
| 158 | * |
| 159 | * @param axisNumber the axis to be used |
| 160 | * @return a SequenceIterator that scans the nodes reached by the axis in turn. |
| 161 | */ |
| 162 | |
| 163 | /*@Nullable*/ |
| 164 | public AxisIterator iterateAxis(byte axisNumber) { |
| 165 | return new WrappingIterator(node.iterateAxis(axisNumber), this, null); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | /** |
| 170 | * Copy this node to a given outputter (deep copy) |
| 171 | */ |
| 172 | |
| 173 | public void copy(Receiver out, int copyOptions, Location locationId) throws XPathException { |
| 174 | node.copy(out, copyOptions & ~CopyOptions.TYPE_ANNOTATIONS, locationId); |
| 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public boolean isNilled() { |
| 179 | return false; |
| 180 | } |
| 181 | } |
| 182 |