Debian Codegraph @

net/sf/saxon/tree/wrapper/SpaceStrippedNode.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.event.Receiver;
11 import net.sf.saxon.event.Stripper;
12 import net.sf.saxon.expr.parser.Location;
13 import net.sf.saxon.lib.NamespaceConstant;
14 import net.sf.saxon.om.AtomicSequence;
15 import net.sf.saxon.om.AxisInfo;
16 import net.sf.saxon.om.NameOfNode;
17 import net.sf.saxon.om.NodeInfo;
18 import net.sf.saxon.pattern.NodeKindTest;
19 import net.sf.saxon.pattern.NodeTest;
20 import net.sf.saxon.trans.XPathException;
21 import net.sf.saxon.tree.iter.AxisIterator;
22 import net.sf.saxon.tree.iter.EmptyIterator;
23 import net.sf.saxon.tree.util.FastStringBuffer;
24 import net.sf.saxon.type.ComplexType;
25 import net.sf.saxon.type.SchemaType;
26 import net.sf.saxon.type.Type;
27 import net.sf.saxon.type.UType;
28 import net.sf.saxon.value.Whitespace;
29
30
31 /**
32 * A StrippedNode is a view of a node, in a virtual tree that has whitespace
33 * text nodes stripped from it. All operations on the node produce the same result
34 * as operations on the real underlying node, except that iterations over the axes
35 * take care to skip whitespace-only text nodes that are supposed to be stripped.
36 * Note that this class is only used in cases where a pre-built tree is supplied as
37 * the input to a transformation, and where the stylesheet does whitespace stripping;
38 * if a SAXSource or StreamSource is supplied, whitespace is stripped as the tree
39 * is built.
40 */
41
42 public class SpaceStrippedNode extends AbstractVirtualNode implements WrappingFunction {
43
44 protected SpaceStrippedNode() {
45 }
46
47 /**
48 * This constructor is protected: nodes should be created using the makeWrapper
49 * factory method
50 *
51 * @param node The node to be wrapped
52 * @param parent The StrippedNode that wraps the parent of this node
53 */
54
55 protected SpaceStrippedNode(NodeInfo node, SpaceStrippedNode parent) {
56 this.node = node;
57 this.parent = parent;
58 }
59
60 /**
61 * Factory method to wrap a node with a wrapper that implements the Saxon
62 * NodeInfo interface.
63 *
64 * @param node The underlying node
65 * @param docWrapper The wrapper for the document node (must be supplied)
66 * @param parent The wrapper for the parent of the node (null if unknown)
67 * @return The new wrapper for the supplied node
68 */
69
70 /*@NotNull*/
71 protected static SpaceStrippedNode makeWrapper(NodeInfo node,
72 SpaceStrippedDocument docWrapper,
73 SpaceStrippedNode parent) {
74 SpaceStrippedNode wrapper = new SpaceStrippedNode(node, parent);
75 wrapper.docWrapper = docWrapper;
76 return wrapper;
77 }
78
79 /**
80 * Factory method to wrap a node within the same document as this node with a VirtualNode
81 *
82 * @param node The underlying node
83 * @param parent The wrapper for the parent of the node (null if unknown)
84 * @return The new wrapper for the supplied node
85 */
86
87 /*@NotNull*/
88 public VirtualNode makeWrapper(NodeInfo node, VirtualNode parent) {
89 SpaceStrippedNode wrapper = new SpaceStrippedNode(node, (SpaceStrippedNode) parent);
90 wrapper.docWrapper = this.docWrapper;
91 return wrapper;
92 }
93
94 /**
95 * Ask whether a node is preserved after whitespace stripping
96 * @param node the node in question
97 * @param docWrapper the root of the space-stripped virtual tree
98 * @param actualParent the (real) parent of the node in question
99 * @return true if the node survives whitespace-stripping
100 */
101
102 public static boolean isPreservedNode(NodeInfo node, SpaceStrippedDocument docWrapper, NodeInfo actualParent) {
103
104 // Non-text nodes, non-whitespace nodes, and parentless nodes are preserved
105 if (node.getNodeKind() != Type.TEXT || actualParent == null || !Whitespace.isWhite(node.getStringValueCS())) {
106 return true;
107 }
108
109 // if the node has a simple type annotation, it is preserved
110 SchemaType type = actualParent.getSchemaType();
111 if (type.isSimpleType() || ((ComplexType) type).isSimpleContent()) {
112 return true;
113 }
114
115 // if there is an ancestor with xml:space="preserve", it is preserved
116 if (docWrapper.containsPreserveSpace()) {
117 NodeInfo p = actualParent;
118 // the document contains one or more xml:space="preserve" attributes, so we need to see
119 // if one of them is on an ancestor of this node
120 while (p.getNodeKind() == Type.ELEMENT) {
121 String val = p.getAttributeValue(NamespaceConstant.XML, "space");
122 if (val != null) {
123 if ("preserve".equals(val)) {
124 return true;
125 } else if ("default".equals(val)) {
126 break;
127 }
128 }
129 p = p.getParent();
130 }
131 }
132
133 // if there is an ancestor whose type has an assertion, it is preserved
134 if (docWrapper.containsAssertions()) {
135 NodeInfo p = actualParent;
136 // the document contains one or more xml:space="preserve" attributes, so we need to see
137 // if one of them is on an ancestor of this node
138 while (p.getNodeKind() == Type.ELEMENT) {
139 SchemaType t = p.getSchemaType();
140 if (t instanceof ComplexType && ((ComplexType) t).hasAssertions()) {
141 return true;
142 }
143 p = p.getParent();
144 }
145 }
146
147 // otherwise it depends on xsl:strip-space
148 try {
149 int preserve = docWrapper.getStrippingRule().isSpacePreserving(NameOfNode.makeName(actualParent), null);
150 return preserve == Stripper.ALWAYS_PRESERVE;
151 } catch (XPathException e) {
152 // Ambiguity between strip-space and preserve-space. Take the recovery action.
153 return true;
154 }
155
156 }
157
158 /**
159 * Get the typed value.
160 *
161 * @return the typed value.
162 * @since 8.5
163 */
164
165 public AtomicSequence atomize() throws XPathException {
166 if (getNodeKind() == Type.ELEMENT) {
167 return getSchemaType().atomize(this);
168 } else {
169 return node.atomize();
170 }
171 }
172
173 /**
174 * Determine whether this is the same node as another node.
175 * <p>Note: a.isSameNode(b) if and only if generateId(a)==generateId(b)</p>
176 *
177 * @return true if this Node object and the supplied Node object represent the
178 * same node in the tree.
179 */
180
181 public boolean equals(Object other) {
182 if (other instanceof SpaceStrippedNode) {
183 return node.equals(((SpaceStrippedNode) other).node);
184 } else {
185 return node.equals(other);
186 }
187 }
188
189
190 /**
191 * Determine the relative position of this node and another node, in document order.
192 * The other node will always be in the same document.
193 *
194 * @param other The other node, whose position is to be compared with this node
195 * @return -1 if this node precedes the other node, +1 if it follows the other
196 * node, or 0 if they are the same node. (In this case, isSameNode() will always
197 * return true, and the two nodes will produce the same result for generateId())
198 */
199
200 public int compareOrder(/*@NotNull*/ NodeInfo other) {
201 if (other instanceof SpaceStrippedNode) {
202 return node.compareOrder(((SpaceStrippedNode) other).node);
203 } else {
204 return node.compareOrder(other);
205 }
206 }
207
208 /**
209 * Get the value of the item as a CharSequence. This is in some cases more efficient than
210 * the version of the method that returns a String.
211 */
212
213 public CharSequence getStringValueCS() {
214 // Might not be the same as the string value of the underlying node because of space stripping
215 switch (getNodeKind()) {
216 case Type.DOCUMENT:
217 case Type.ELEMENT:
218 AxisIterator iter = iterateAxis(AxisInfo.DESCENDANT, NodeKindTest.makeNodeKindTest(Type.TEXT));
219 FastStringBuffer sb = new FastStringBuffer(FastStringBuffer.C64);
220 while (true) {
221 NodeInfo it = iter.next();
222 if (it == null) {
223 break;
224 }
225 sb.append(it.getStringValueCS());
226 }
227 return sb.condense();
228 default:
229 return node.getStringValueCS();
230 }
231 }
232
233 /**
234 * Get the NodeInfo object representing the parent of this node
235 */
236
237 /*@Nullable*/
238 public NodeInfo getParent() {
239 if (parent == null) {
240 NodeInfo realParent = node.getParent();
241 if (realParent != null) {
242 parent = makeWrapper(realParent, (SpaceStrippedDocument) docWrapper, null);
243 }
244 }
245 return parent;
246 }
247
248 @Override
249 public AxisIterator iterateAxis(byte axisNumber, NodeTest nodeTest) {
250 if (nodeTest.getUType().intersection(UType.TEXT) == UType.VOID ||
251 axisNumber == AxisInfo.ATTRIBUTE || axisNumber == AxisInfo.NAMESPACE) {
252 // iteration does not include text nodes, so no stripping needed
253 return new WrappingIterator(node.iterateAxis(axisNumber, nodeTest), this, getParentForAxis(axisNumber));
254 } else {
255 return new StrippingIterator(node.iterateAxis(axisNumber, nodeTest), getParentForAxis(axisNumber));
256 }
257 }
258
259 /**
260 * Return an iteration over the nodes reached by the given axis from this node
261 *
262 * @param axisNumber the axis to be used
263 * @return a SequenceIterator that scans the nodes reached by the axis in turn.
264 */
265
266 /*@Nullable*/
267 public AxisIterator iterateAxis(byte axisNumber) {
268 switch (axisNumber) {
269 case AxisInfo.ATTRIBUTE:
270 case AxisInfo.NAMESPACE:
271 return new WrappingIterator(node.iterateAxis(axisNumber), this, this);
272 case AxisInfo.CHILD:
273 return new StrippingIterator(node.iterateAxis(axisNumber), this);
274 case AxisInfo.FOLLOWING_SIBLING:
275 case AxisInfo.PRECEDING_SIBLING:
276 SpaceStrippedNode parent = (SpaceStrippedNode) getParent();
277 if (parent == null) {
278 return EmptyIterator.OfNodes.THE_INSTANCE;
279 } else {
280 return new StrippingIterator(node.iterateAxis(axisNumber), parent);
281 }
282 default:
283 return new StrippingIterator(node.iterateAxis(axisNumber), null);
284 }
285 }
286
287 private SpaceStrippedNode getParentForAxis(byte axisNumber) {
288 switch (axisNumber) {
289 case AxisInfo.CHILD:
290 case AxisInfo.ATTRIBUTE:
291 case AxisInfo.NAMESPACE:
292 return this;
293 case AxisInfo.FOLLOWING_SIBLING:
294 case AxisInfo.PRECEDING_SIBLING:
295 return (SpaceStrippedNode)getParent();
296 default:
297 return null;
298 }
299 }
300
301 /**
302 * Copy this node to a given outputter (deep copy)
303 */
304
305 public void copy(Receiver out, int copyOptions, Location locationId) throws XPathException {
306 // The underlying code does not do whitespace stripping. So we need to interpose
307 // a stripper.
308 Stripper stripper = new Stripper(((SpaceStrippedDocument) docWrapper).getStrippingRule(), out);
309 node.copy(stripper, copyOptions, locationId);
310 }
311
312
313 /**
314 * A StrippingIterator delivers wrappers for the nodes delivered
315 * by its underlying iterator. It is used when whitespace stripping
316 * may be needed, e.g. for the child axis. It examines all text nodes
317 * encountered to see if they need to be stripped, and if so, it
318 * skips them.
319 */
320
321 private final class StrippingIterator implements AxisIterator {
322
323 AxisIterator base;
324 SpaceStrippedNode parent;
325 NodeInfo currentVirtualNode;
326 int position;
327
328 /**
329 * Create a StrippingIterator
330 *
331 * @param base The underlying iterator
332 * @param parent If all the nodes to be wrapped have the same parent,
333 * it can be specified here. Otherwise specify null.
334 */
335
336 public StrippingIterator(AxisIterator base, SpaceStrippedNode parent) {
337 this.base = base;
338 this.parent = parent;
339 position = 0;
340 }
341
342
343 /*@Nullable*/
344 public NodeInfo next() {
345 NodeInfo nextRealNode;
346 while (true) {
347 nextRealNode = base.next();
348 if (nextRealNode == null) {
349 return null;
350 }
351 if (isPreserved(nextRealNode)) {
352 break;
353 }
354 // otherwise skip this whitespace text node
355 }
356
357 currentVirtualNode = makeWrapper(nextRealNode, (SpaceStrippedDocument) docWrapper, parent);
358 position++;
359 return currentVirtualNode;
360 }
361
362 private boolean isPreserved(NodeInfo nextRealNode) {
363 if (nextRealNode.getNodeKind() != Type.TEXT) {
364 return true;
365 }
366 NodeInfo actualParent =
367 parent == null ? nextRealNode.getParent() : parent.node;
368 return isPreservedNode(nextRealNode, (SpaceStrippedDocument)docWrapper, actualParent);
369 }
370
371 public void close() {
372 base.close();
373 }
374
375 /**
376 * Get properties of this iterator, as a bit-significant integer.
377 *
378 * @return the properties of this iterator. This will be some combination of
379 * properties such as {@link #GROUNDED}, {@link #LAST_POSITION_FINDER},
380 * and {@link #LOOKAHEAD}. It is always
381 * acceptable to return the value zero, indicating that there are no known special properties.
382 * It is acceptable for the properties of the iterator to change depending on its state.
383 */
384
385 public int getProperties() {
386 return 0;
387 }
388
389 } // end of class StrippingIterator
390
391
392 }
393