Debian Codegraph @

net/sf/saxon/tree/wrapper/SpaceStrippedDocument.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.lib.NamespaceConstant;
11 import net.sf.saxon.om.*;
12 import net.sf.saxon.pattern.NodeKindTest;
13 import net.sf.saxon.tree.iter.AxisIterator;
14 import net.sf.saxon.tree.tiny.TinyTree;
15 import net.sf.saxon.type.ComplexType;
16 import net.sf.saxon.type.SchemaType;
17
18 import java.util.Iterator;
19
20 /**
21 * A SpaceStrippedDocument represents a view of a real Document in which selected
22 * whitespace text nodes are treated as having been stripped.
23 */
24
25 public class SpaceStrippedDocument extends GenericTreeInfo {
26
27 private SpaceStrippingRule strippingRule;
28 private boolean preservesSpace;
29 private boolean containsAssertions;
30 private TreeInfo underlyingTree;
31
32 /**
33 * Create a space-stripped view of a document
34 *
35 * @param doc the underlying document
36 * @param strippingRule an object that contains the rules defining which whitespace
37 * text nodes are to be absent from the view
38 */
39
40 public SpaceStrippedDocument(TreeInfo doc, SpaceStrippingRule strippingRule) {
41 super(doc.getConfiguration());
42 setRootNode(wrap(doc.getRootNode()));
43 this.strippingRule = strippingRule;
44 this.underlyingTree = doc;
45 preservesSpace = findPreserveSpace(doc);
46 containsAssertions = findAssertions(doc);
47 }
48
49 /**
50 * Create a wrapped node within this document
51 */
52
53 public SpaceStrippedNode wrap(NodeInfo node) {
54 return SpaceStrippedNode.makeWrapper(node, this, null);
55 }
56
57 /**
58 * Ask whether the document contains any nodes whose type annotation is anything other than
59 * UNTYPED
60 *
61 * @return true if the document contains elements whose type is other than UNTYPED
62 */
63 public boolean isTyped() {
64 return underlyingTree.isTyped();
65 }
66
67 /**
68 * Get the document's strippingRule
69 */
70
71 public SpaceStrippingRule getStrippingRule() {
72 return strippingRule;
73 }
74
75 /**
76 * Get the element with a given ID, if any
77 *
78 * @param id the required ID value
79 * @param getParent true if we want the parent of an element with the ID value
80 * @return the element with the given ID value, or null if there is none.
81 */
82
83 /*@Nullable*/
84 public NodeInfo selectID(String id, boolean getParent) {
85 NodeInfo n = underlyingTree.selectID(id, false);
86 if (n == null) {
87 return null;
88 } else {
89 return wrap(n);
90 }
91 }
92
93 /**
94 * Get the list of unparsed entities defined in this document
95 *
96 * @return an Iterator, whose items are of type String, containing the names of all
97 * unparsed entities defined in this document. If there are no unparsed entities or if the
98 * information is not available then an empty iterator is returned
99 */
100
101 public Iterator<String> getUnparsedEntityNames() {
102 return underlyingTree.getUnparsedEntityNames();
103 }
104
105 /**
106 * Get the unparsed entity with a given name
107 *
108 * @param name the name of the entity
109 */
110
111 public String[] getUnparsedEntity(String name) {
112 return underlyingTree.getUnparsedEntity(name);
113 }
114
115 /**
116 * Determine whether the wrapped document contains any xml:space="preserve" attributes. If it
117 * does, we will look for them when stripping individual nodes. It's more efficient to scan
118 * the document in advance checking for xml:space attributes than to look for them every time
119 * we hit a whitespace text node.
120 *
121 * @param doc the wrapper of the document node
122 * @return true if any element in the document has an xml:space attribute with the value "preserve"
123 */
124
125 private static boolean findPreserveSpace(/*@NotNull*/ TreeInfo doc) {
126 if (doc instanceof TinyTree) {
127 // Optimisation - see bug 2929. Makes a vast difference especially if there are few attributes in the tree
128 return ((TinyTree) doc).hasXmlSpacePreserveAttribute();
129 } else {
130 AxisIterator iter = doc.getRootNode().iterateAxis(AxisInfo.DESCENDANT, NodeKindTest.ELEMENT);
131 NodeInfo node;
132 while ((node = iter.next()) != null) {
133 String val = node.getAttributeValue(NamespaceConstant.XML, "space");
134 if ("preserve".equals(val)) {
135 return true;
136 }
137 }
138 return false;
139 }
140 }
141
142 /**
143 * Determine whether the wrapped document contains any nodes annotated with complex types that
144 * define assertions.
145 *
146 * @param doc the wrapper of the document node
147 * @return true if any element in the document has an xml:space attribute with the value "preserve"
148 */
149
150 private static boolean findAssertions(/*@NotNull*/ TreeInfo doc) {
151 if (doc.isTyped()) {
152 AxisIterator iter = doc.getRootNode().iterateAxis(AxisInfo.DESCENDANT, NodeKindTest.ELEMENT);
153 while (true) {
154 NodeInfo node = iter.next();
155 if (node == null) {
156 return false;
157 }
158 SchemaType type = node.getSchemaType();
159 if (type.isComplexType() && ((ComplexType) type).hasAssertions()) {
160 return true;
161 }
162 }
163 } else {
164 return false;
165 }
166 }
167
168 /**
169 * Ask whether the stripped document contains any xml:space="preserve" attributes.
170 *
171 * @return true if any element in the document has an xml:space attribute with the value "preserve"
172 */
173
174 public boolean containsPreserveSpace() {
175 return preservesSpace;
176 }
177
178 /**
179 * Ask whether the stripped document contain any nodes annotated with types that carry assertions
180 *
181 * @return true if any element in the document has a type that has an assertion
182 */
183
184 public boolean containsAssertions() {
185 return containsAssertions;
186 }
187
188 }
189