Debian Codegraph @

net/sf/saxon/pattern/NodeKindTest.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.pattern;
9
10 import net.sf.saxon.om.Item;
11 import net.sf.saxon.om.NodeInfo;
12 import net.sf.saxon.om.NodeName;
13 import net.sf.saxon.tree.tiny.NodeVectorTree;
14 import net.sf.saxon.type.*;
15
16 import java.util.Optional;
17 import java.util.function.IntPredicate;
18
19 /**
20 * NodeTest is an interface that enables a test of whether a node has a particular
21 * name and kind. A NodeKindTest matches the node kind only.
22 *
23 * @author Michael H. Kay
24 */
25
26 public class NodeKindTest extends NodeTest {
27
28 public static final NodeKindTest DOCUMENT = new NodeKindTest(Type.DOCUMENT);
29 public static final NodeKindTest ELEMENT = new NodeKindTest(Type.ELEMENT);
30 public static final NodeKindTest ATTRIBUTE = new NodeKindTest(Type.ATTRIBUTE);
31 public static final NodeKindTest TEXT = new NodeKindTest(Type.TEXT);
32 public static final NodeKindTest COMMENT = new NodeKindTest(Type.COMMENT);
33 public static final NodeKindTest PROCESSING_INSTRUCTION = new NodeKindTest(Type.PROCESSING_INSTRUCTION);
34 public static final NodeKindTest NAMESPACE = new NodeKindTest(Type.NAMESPACE);
35
36
37 private int kind;
38 private UType uType;
39
40 private NodeKindTest(int nodeKind) {
41 kind = nodeKind;
42 uType = UType.fromTypeCode(nodeKind);
43 }
44
45 /**
46 * Get the node kind matched by this test
47 *
48 * @return the matching node kind
49 */
50
51 public int getNodeKind() {
52 return kind;
53 }
54
55 /**
56 * Get the corresponding {@link net.sf.saxon.type.UType}. A UType is a union of primitive item
57 * types.
58 *
59 * @return the smallest UType that subsumes this item type
60 */
61 public UType getUType() {
62 return uType;
63 }
64
65 /**
66 * Make a test for a given kind of node
67 */
68
69 public static NodeTest makeNodeKindTest(int kind) {
70 switch (kind) {
71 case Type.DOCUMENT:
72 return DOCUMENT;
73 case Type.ELEMENT:
74 return ELEMENT;
75 case Type.ATTRIBUTE:
76 return ATTRIBUTE;
77 case Type.COMMENT:
78 return COMMENT;
79 case Type.TEXT:
80 return TEXT;
81 case Type.PROCESSING_INSTRUCTION:
82 return PROCESSING_INSTRUCTION;
83 case Type.NAMESPACE:
84 return NAMESPACE;
85 case Type.NODE:
86 return AnyNodeTest.getInstance();
87 default:
88 throw new IllegalArgumentException("Unknown node kind " + kind + " in NodeKindTest");
89 }
90 }
91
92 public boolean matches(Item item, /*@NotNull*/TypeHierarchy th) {
93 return item instanceof NodeInfo && kind == ((NodeInfo) item).getNodeKind();
94 }
95
96 /**
97 * Test whether this node test is satisfied by a given node. This method is only
98 * fully supported for a subset of NodeTests, because it doesn't provide all the information
99 * needed to evaluate all node tests. In particular (a) it can't be used to evaluate a node
100 * test of the form element(N,T) or schema-element(E) where it is necessary to know whether the
101 * node is nilled, and (b) it can't be used to evaluate a node test of the form
102 * document-node(element(X)). This in practice means that it is used (a) to evaluate the
103 * simple node tests found in the XPath 1.0 subset used in XML Schema, and (b) to evaluate
104 * node tests where the node kind is known to be an attribute.
105 *
106 * @param nodeKind The kind of node to be matched
107 * @param name identifies the expanded name of the node to be matched.
108 * The value should be null for a node with no name.
109 * @param annotation The actual content type of the node
110 */
111 @Override
112 public boolean matches(int nodeKind, NodeName name, SchemaType annotation) {
113 return kind == nodeKind;
114 }
115
116 public IntPredicate getMatcher(final NodeVectorTree tree) {
117 final byte[] nodeKindArray = tree.getNodeKindArray();
118 if (kind == Type.TEXT) {
119 return nodeNr -> {
120 int k = nodeKindArray[nodeNr];
121 return k == Type.TEXT || k == Type.WHITESPACE_TEXT;
122 };
123 } else {
124 return nodeNr -> (nodeKindArray[nodeNr] & 0x0f) == kind;
125 }
126 }
127
128 /**
129 * Test whether this node test is satisfied by a given node. This alternative
130 * method is used in the case of nodes where calculating the fingerprint is expensive,
131 * for example DOM or JDOM nodes.
132 *
133 * @param node the node to be matched
134 */
135
136 public boolean matchesNode(NodeInfo node) {
137 return node.getNodeKind() == kind;
138 }
139
140
141 /**
142 * Determine the default priority of this node test when used on its own as a Pattern
143 */
144
145 public final double getDefaultPriority() {
146 return -0.5;
147 }
148
149 /**
150 * Determine the types of nodes to which this pattern applies. Used for optimisation.
151 *
152 * @return the type of node matched by this pattern. e.g. Type.ELEMENT or Type.TEXT
153 */
154
155 public int getPrimitiveType() {
156 return kind;
157 }
158
159 /**
160 * Get the content type allowed by this NodeTest (that is, the type of content allowed).
161 * Return AnyType if there are no restrictions.
162 */
163
164 public SchemaType getContentType() {
165 switch (kind) {
166 case Type.DOCUMENT:
167 return AnyType.getInstance();
168 case Type.ELEMENT:
169 return AnyType.getInstance();
170 case Type.ATTRIBUTE:
171 return AnySimpleType.getInstance();
172 case Type.COMMENT:
173 return BuiltInAtomicType.STRING;
174 case Type.TEXT:
175 return BuiltInAtomicType.UNTYPED_ATOMIC;
176 case Type.PROCESSING_INSTRUCTION:
177 return BuiltInAtomicType.STRING;
178 case Type.NAMESPACE:
179 return BuiltInAtomicType.STRING;
180 default:
181 throw new AssertionError("Unknown node kind");
182 }
183 }
184
185 /**
186 * Get the content type allowed by this NodeTest (that is, the type annotation).
187 * Return AnyType if there are no restrictions. The default implementation returns AnyType.
188 */
189
190 /*@NotNull*/
191 public AtomicType getAtomizedItemType() {
192 switch (kind) {
193 case Type.DOCUMENT:
194 return BuiltInAtomicType.UNTYPED_ATOMIC;
195 case Type.ELEMENT:
196 return BuiltInAtomicType.ANY_ATOMIC;
197 case Type.ATTRIBUTE:
198 return BuiltInAtomicType.ANY_ATOMIC;
199 case Type.COMMENT:
200 return BuiltInAtomicType.STRING;
201 case Type.TEXT:
202 return BuiltInAtomicType.UNTYPED_ATOMIC;
203 case Type.PROCESSING_INSTRUCTION:
204 return BuiltInAtomicType.STRING;
205 case Type.NAMESPACE:
206 return BuiltInAtomicType.STRING;
207 default:
208 throw new AssertionError("Unknown node kind");
209 }
210 }
211
212 /*@NotNull*/
213 public String toString() {
214 return toString(kind);
215 }
216
217 public static String toString(int kind) {
218 switch (kind) {
219 case Type.DOCUMENT:
220 return "document-node()";
221 case Type.ELEMENT:
222 return "element()";
223 case Type.ATTRIBUTE:
224 return "attribute()";
225 case Type.COMMENT:
226 return "comment()";
227 case Type.TEXT:
228 return "text()";
229 case Type.PROCESSING_INSTRUCTION:
230 return "processing-instruction()";
231 case Type.NAMESPACE:
232 return "namespace-node()";
233 default:
234 return "** error **";
235 }
236 }
237
238 /**
239 * Get the name of a node kind
240 *
241 * @param kind the node kind, for example Type.ELEMENT or Type.ATTRIBUTE
242 * @return the name of the node kind, for example "element" or "attribute"
243 */
244
245 public static String nodeKindName(int kind) {
246 switch (kind) {
247 case Type.DOCUMENT:
248 return "document";
249 case Type.ELEMENT:
250 return "element";
251 case Type.ATTRIBUTE:
252 return "attribute";
253 case Type.COMMENT:
254 return "comment";
255 case Type.TEXT:
256 return "text";
257 case Type.PROCESSING_INSTRUCTION:
258 return "processing-instruction";
259 case Type.NAMESPACE:
260 return "namespace";
261 default:
262 return "** error **";
263 }
264 }
265
266
267 /**
268 * Returns a hash code value for the object.
269 */
270
271 public int hashCode() {
272 return kind;
273 }
274
275 /**
276 * Indicates whether some other object is "equal to" this one.
277 */
278 public boolean equals(Object other) {
279 return other instanceof NodeKindTest &&
280 ((NodeKindTest) other).kind == kind;
281 }
282
283 /**
284 * Generate Javascript code to test whether an item conforms to this item type
285 *
286 * @return a Javascript instruction or sequence of instructions, which can be used as the body
287 * of a Javascript function, and which returns a boolean indication whether the value of the
288 * variable "item" is an instance of this item type.
289 * @param knownToBe An item type that the supplied item is known to conform to; the generated code
290 * can assume that the item is an instance of this type.
291 * @param targetVersion The version of Saxon-JS for which code is being generated. Currently either 1 or 2.
292
293 */
294 @Override
295 public String generateJavaScriptItemTypeTest(ItemType knownToBe, int targetVersion) {
296 String instNode = knownToBe instanceof NodeTest ? " " : " SaxonJS.U.isNode(item) && ";
297 switch (getNodeKind()) {
298 case Type.DOCUMENT:
299 return "return" + instNode + "(item.nodeType===9||item.nodeType===11);";
300 case Type.ELEMENT:
301 return "return" + instNode + "item.nodeType===1;";
302 case Type.TEXT:
303 return "return" + instNode + "item.nodeType===3;";
304 case Type.COMMENT:
305 return "return" + instNode + "item.nodeType===8;";
306 case Type.PROCESSING_INSTRUCTION:
307 return "return" + instNode + "item.nodeType===7&&item.target!=='xml';";
308 case Type.ATTRIBUTE:
309 return "return SaxonJS.U.isAttr(item)";
310 case Type.NAMESPACE:
311 return "return SaxonJS.U.isNamespaceNode(item)";
312 default:
313 return "return false;";
314 }
315 }
316
317 /**
318 * Get extra diagnostic information about why a supplied item does not conform to this
319 * item type, if available. If extra information is returned, it should be in the form of a complete
320 * sentence, minus the closing full stop. No information should be returned for obvious cases.
321 *
322 * @param item the item that doesn't match this type
323 * @param th the type hierarchy cache
324 * @return optionally, a message explaining why the item does not match the type
325 */
326 @Override
327 public Optional<String> explainMismatch(Item item, TypeHierarchy th) {
328 Optional<String> explanation = super.explainMismatch(item, th);
329 if (explanation.isPresent()) {
330 return explanation;
331 }
332 if (item instanceof NodeInfo) {
333 UType actualKind = UType.getUType(item);
334 if (!getUType().overlaps(actualKind)) {
335 return Optional.of("The supplied value is " + actualKind.toStringWithIndefiniteArticle());
336 } else {
337 return Optional.empty();
338 }
339 } else {
340 return Optional.of("The supplied value is " + item.getGenre().getDescription());
341 }
342 }
343
344 public String toShortString() {
345 switch (getNodeKind()) {
346 case Type.ELEMENT:
347 return "*";
348 case Type.ATTRIBUTE:
349 return "@*";
350 case Type.DOCUMENT:
351 return "/";
352 default:
353 return toString();
354 }
355 }
356
357 }
358