Debian Codegraph @

net/sf/saxon/pattern/AnyChildNodeTest.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.NodeInfo;
11 import net.sf.saxon.om.NodeName;
12 import net.sf.saxon.tree.tiny.NodeVectorTree;
13 import net.sf.saxon.type.ItemType;
14 import net.sf.saxon.type.SchemaType;
15 import net.sf.saxon.type.Type;
16 import net.sf.saxon.type.UType;
17 import java.util.function.IntPredicate;
18
19 /**
20 * An AnyChildNodePattern is the pattern node(), which matches any node except a root node,
21 * an attribute node, or a namespace node: in other words, any node that is potentially the child of another
22 * node. But it matches the node whether or not it actually has a parent.
23 */
24
25 public final class AnyChildNodeTest extends NodeTest {
26
27 private final static AnyChildNodeTest THE_INSTANCE = new AnyChildNodeTest();
28
29 /**
30 * Get the singular instance of this class
31 *
32 * @return the singular instance
33 */
34
35 public static AnyChildNodeTest getInstance() {
36 return THE_INSTANCE;
37 }
38
39 private AnyChildNodeTest() {
40 }
41
42 /**
43 * Get the corresponding {@link net.sf.saxon.type.UType}. A UType is a union of primitive item
44 * types.
45 *
46 * @return the smallest UType that subsumes this item type
47 */
48 public UType getUType() {
49 return UType.CHILD_NODE_KINDS;
50 }
51
52 /**
53 * Test whether this node test is satisfied by a given node. This method is only
54 * fully supported for a subset of NodeTests, because it doesn't provide all the information
55 * needed to evaluate all node tests. In particular (a) it can't be used to evaluate a node
56 * test of the form element(N,T) or schema-element(E) where it is necessary to know whether the
57 * node is nilled, and (b) it can't be used to evaluate a node test of the form
58 * document-node(element(X)). This in practice means that it is used (a) to evaluate the
59 * simple node tests found in the XPath 1.0 subset used in XML Schema, and (b) to evaluate
60 * node tests where the node kind is known to be an attribute.
61 *
62 * @param nodeKind The kind of node to be matched
63 * @param name identifies the expanded name of the node to be matched.
64 * The value should be null for a node with no name.
65 * @param annotation The actual content type of the node
66 */
67 @Override
68 public boolean matches(int nodeKind, NodeName name, SchemaType annotation) {
69 return nodeKind == Type.ELEMENT ||
70 nodeKind == Type.TEXT ||
71 nodeKind == Type.COMMENT ||
72 nodeKind == Type.PROCESSING_INSTRUCTION;
73 }
74
75 public IntPredicate getMatcher(final NodeVectorTree tree) {
76 final byte[] nodeKindArray = tree.getNodeKindArray();
77 return nodeNr -> {
78 int nodeKind = nodeKindArray[nodeNr] & 0x0f;
79 return nodeKind == Type.ELEMENT ||
80 nodeKind == Type.TEXT ||
81 nodeKind == Type.WHITESPACE_TEXT ||
82 nodeKind == Type.COMMENT ||
83 nodeKind == Type.PROCESSING_INSTRUCTION;
84 };
85 }
86
87 /**
88 * Test whether this node test is satisfied by a given node. This alternative
89 * method is used in the case of nodes where calculating the fingerprint is expensive,
90 * for example DOM or JDOM nodes.
91 *
92 * @param node the node to be matched
93 */
94
95 public boolean matchesNode(NodeInfo node) {
96 int nodeKind = node.getNodeKind();
97 return nodeKind == Type.ELEMENT ||
98 nodeKind == Type.TEXT ||
99 nodeKind == Type.COMMENT ||
100 nodeKind == Type.PROCESSING_INSTRUCTION;
101 }
102
103
104 /**
105 * Determine the default priority to use if this pattern appears as a match pattern
106 * for a template with no explicit priority attribute.
107 */
108
109 public double getDefaultPriority() {
110 return -0.5;
111 }
112
113 /*@NotNull*/
114 public String toString() {
115 return "( element() | text() | comment() | processing-instruction() )";
116 }
117
118
119 /**
120 * Generate Javascript code to test whether an item conforms to this item type
121 *
122 * @return a Javascript instruction or sequence of instructions, which can be used as the body
123 * of a Javascript function, and which returns a boolean indication whether the value of the
124 * variable "item" is an instance of this item type.
125 * @param knownToBe An item type that the supplied item is known to conform to; the generated code
126 * can assume that the item is an instance of this type.
127 * @param targetVersion The version of Saxon-JS for which code is being generated. Currently either 1 or 2.
128
129 */
130 @Override
131 public String generateJavaScriptItemTypeTest(ItemType knownToBe, int targetVersion) {
132 return "return SaxonJS.U.isNode(item) && (item.nodeType===1 || item.nodeType===3 || item.nodeType===7 || item.nodeType===8);";
133 }
134
135
136 }
137