Debian Codegraph @

net/sf/saxon/tree/wrapper/SnapshotNode.java

raw symbols SCIP
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.Configuration;
11 import net.sf.saxon.event.Receiver;
12 import net.sf.saxon.expr.parser.Location;
13 import net.sf.saxon.om.AtomicSequence;
14 import net.sf.saxon.om.AxisInfo;
15 import net.sf.saxon.om.NodeInfo;
16 import net.sf.saxon.pattern.AnyNodeTest;
17 import net.sf.saxon.pattern.NodeTest;
18 import net.sf.saxon.trans.XPathException;
19 import net.sf.saxon.tree.iter.AxisIterator;
20 import net.sf.saxon.tree.iter.SingleNodeIterator;
21 import net.sf.saxon.tree.util.Navigator;
22 import net.sf.saxon.type.Type;
23 import net.sf.saxon.value.UntypedAtomicValue;
24
25 import javax.xml.transform.SourceLocator;
26
27 /**
28 * This class represents a node within a tree produced by the snapshot() function, as a virtual copy of
29 * the relevant nodes in another tree. It specializes VirtualCopy, which implements a deep copy as
30 * produced by the copy-of() function.
31 */
32
33 public class SnapshotNode extends VirtualCopy implements NodeInfo {
34
35 protected NodeInfo pivot; // a node in the source tree
36
37 /**
38 * Protected constructor: create a virtual copy of a node
39 *
40 * @param base the node in the source tree to which the new node should correspond
41 * @param pivot the pivot node is the node supplied as argument to the snapshot() function; the snapshot
42 * includes all ancestors of this node, and all descendants of this node (plus their attributes
43 * and namespaces)
44 */
45
46 protected SnapshotNode(NodeInfo base, NodeInfo pivot) {
47 super(base, pivot.getRoot());
48 this.pivot = pivot;
49 }
50
51 /**
52 * Public factory method: apply the snapshot function to a node
53 *
54 * @param original the node to be copied
55 * @return the snapshot.
56 */
57
58 public static SnapshotNode makeSnapshot(NodeInfo original) {
59 SnapshotNode vc = new SnapshotNode(original, original);
60
61 Configuration config = original.getConfiguration();
62 VirtualTreeInfo doc = new VirtualTreeInfo(config);
63 long docNr = config.getDocumentNumberAllocator().allocateDocumentNumber();
64 doc.setDocumentNumber(docNr);
65 doc.setCopyAccumulators(true);
66 vc.tree = doc;
67 doc.setRootNode(vc.getRoot());
68
69 return vc;
70 }
71
72 /**
73 * Wrap a node in a VirtualCopy.
74 *
75 * @param node the node to be wrapped
76 * @return a virtual copy of the node
77 */
78
79 /*@NotNull*/
80 protected SnapshotNode wrap(NodeInfo node) {
81 SnapshotNode vc = new SnapshotNode(node, pivot);
82 vc.tree = tree;
83 return vc;
84 }
85
86 /**
87 * Get the value of the item as a CharSequence. The string value for a node below the pivot is the same
88 * as the string value for the corresponding node in the source tree; the string value for a node above the pivot
89 * is the same as the string value of the pivot. For attributes and namespaces the string value is the same
90 * as in the original tree.
91 */
92
93 public CharSequence getStringValueCS() {
94 if (Navigator.isAncestorOrSelf(original, pivot)) {
95 return pivot.getStringValueCS();
96 } else {
97 return original.getStringValueCS();
98 }
99 }
100
101 /**
102 * Get the NodeInfo object representing the parent of this node
103 *
104 * @return the parent of this node; null if this node has no parent
105 */
106
107 /*@Nullable*/
108 public NodeInfo getParent() {
109 if (parent == null) {
110 NodeInfo basep = original.getParent();
111 if (basep == null) {
112 return null;
113 }
114 parent = wrap(basep);
115 }
116 return parent;
117 }
118
119 /**
120 * Get the root node of the tree containing this node
121 *
122 * @return the NodeInfo representing the top-level ancestor of this node.
123 * This will not necessarily be a document node
124 */
125 @Override
126 public NodeInfo getRoot() {
127 return super.getRoot();
128 }
129
130 /**
131 * Copy this node to a given outputter
132 * @param out the Receiver to which the node should be copied
133 * @param copyOptions a selection of the options defined in {@link net.sf.saxon.om.CopyOptions}
134 * @param locationId Identifies the location of the instruction
135 */
136
137 public void copy(Receiver out, int copyOptions, Location locationId) throws XPathException {
138 Navigator.copy(this, out, copyOptions, locationId);
139 }
140
141 /**
142 * Get the typed value of this node
143 * @return the typed value.
144 */
145
146 public AtomicSequence atomize() throws XPathException {
147 switch (getNodeKind()) {
148 case Type.ATTRIBUTE:
149 case Type.TEXT:
150 case Type.COMMENT:
151 case Type.PROCESSING_INSTRUCTION:
152 case Type.NAMESPACE:
153 return original.atomize();
154 default:
155 if (Navigator.isAncestorOrSelf(pivot, original)) {
156 return original.atomize();
157 } else {
158 // Ancestors of the pivot node have type xs:anyType. The typed value is therefore the
159 // string value as an instance of xs:untypedAtomic
160 return new UntypedAtomicValue(pivot.getStringValueCS());
161 }
162 }
163 }
164
165
166 /**
167 * Determine whether this node has the is-id property
168 *
169 * @return true if the node is an ID
170 */
171
172 public boolean isId() {
173 return original.isId();
174 }
175
176 /**
177 * Determine whether this node has the is-idref property
178 *
179 * @return true if the node is an IDREF or IDREFS element or attribute
180 */
181
182 public boolean isIdref() {
183 return original.isIdref();
184 }
185
186 /**
187 * Determine whether the node has the is-nilled property
188 *
189 * @return true if the node has the is-nilled property
190 */
191
192 public boolean isNilled() {
193 return original.isNilled();
194 }
195
196 /**
197 * Return the public identifier for the current document event.
198 * <p>The return value is the public identifier of the document
199 * entity or of the external parsed entity in which the markup that
200 * triggered the event appears.</p>
201 *
202 * @return A string containing the public identifier, or
203 * null if none is available.
204 * @see #getSystemId
205 */
206 /*@Nullable*/
207 public String getPublicId() {
208 return original instanceof SourceLocator ? original.getPublicId() : null;
209 }
210
211 /**
212 * Return an iteration over all the nodes reached by the given axis from this node
213 * that match a given NodeTest
214 *
215 * @param axisNumber an integer identifying the axis; one of the constants
216 * defined in class net.sf.saxon.om.Axis
217 * @param nodeTest A pattern to be matched by the returned nodes; nodes
218 * that do not match this pattern are not included in the result
219 * @return an AxisIterator that scans the nodes reached by the axis in
220 * turn.
221 * @throws UnsupportedOperationException if the namespace axis is
222 * requested and this axis is not supported for this implementation.
223 * @see net.sf.saxon.om.AxisInfo
224 */
225
226 public AxisIterator iterateAxis(byte axisNumber, NodeTest nodeTest) {
227 switch (getNodeKind()) {
228 case Type.ATTRIBUTE:
229 case Type.NAMESPACE:
230 case Type.TEXT:
231 case Type.COMMENT:
232 case Type.PROCESSING_INSTRUCTION:
233 return super.iterateAxis(axisNumber, nodeTest);
234 default:
235 if (!original.isSameNodeInfo(pivot) && Navigator.isAncestorOrSelf(original, pivot)) {
236 // We're on a node above the pivot node
237 switch (axisNumber) {
238 case AxisInfo.CHILD:
239 // return only the child that is included in the snapshot, that is, the one
240 // that is an ancestor-or-self of the pivot node
241 return SingleNodeIterator.makeIterator(getChildOfAncestorNode());
242 case AxisInfo.DESCENDANT:
243 case AxisInfo.DESCENDANT_OR_SELF:
244 // Use the child axis recursively, for efficiency
245 AxisIterator iter = new Navigator.DescendantEnumeration(
246 this, axisNumber == AxisInfo.DESCENDANT_OR_SELF, true);
247 if (!(nodeTest instanceof AnyNodeTest)) {
248 iter = new Navigator.AxisFilter(iter, nodeTest);
249 }
250 return iter;
251 default:
252 return super.iterateAxis(axisNumber, nodeTest);
253 }
254
255 } else {
256 return super.iterateAxis(axisNumber, nodeTest);
257 }
258 }
259
260 }
261
262 /**
263 * Get the child of this node assuming it is known to be above the pivot
264 * @return the child of this node; or null in the case where this node is the parent of
265 * the pivot node, and the pivot node is a namespace or attribute node
266 */
267
268 private NodeInfo getChildOfAncestorNode() {
269 int pivotKind = pivot.getNodeKind();
270 SnapshotNode p = wrap(pivot);
271 if ((pivotKind == Type.ATTRIBUTE || pivotKind == Type.NAMESPACE) && p.getParent().isSameNodeInfo(this)) {
272 return null;
273 }
274 while (true) {
275 SnapshotNode q = (SnapshotNode)p.getParent();
276 if (q == null) {
277 throw new AssertionError();
278 }
279 if (q.isSameNodeInfo(this)) {
280 return p;
281 }
282 p = q;
283 }
284
285 }
286
287
288 @Override
289 protected boolean isIncludedInCopy(NodeInfo sourceNode) {
290 switch (sourceNode.getNodeKind()) {
291 case Type.ATTRIBUTE:
292 case Type.NAMESPACE:
293 return isIncludedInCopy(sourceNode.getParent());
294 default:
295 return Navigator.isAncestorOrSelf(pivot, sourceNode) || Navigator.isAncestorOrSelf(sourceNode, pivot);
296 }
297 }
298
299
300 }
301