Debian Codegraph @

net/sf/saxon/pattern/MultipleNodeKindTest.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2013 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.tree.util.FastStringBuffer;
14 import net.sf.saxon.type.*;
15
16 import java.util.LinkedList;
17 import java.util.function.Function;
18 import java.util.function.IntPredicate;
19
20 /**
21 * An MultipleNodeKindTest is a nodetest that matches nodes belonging to any subset of possible
22 * node kinds, for example element and document nodes, or attribute and namespace nodes
23 */
24
25 public final class MultipleNodeKindTest extends NodeTest {
26
27 public final static MultipleNodeKindTest PARENT_NODE =
28 new MultipleNodeKindTest(UType.DOCUMENT.union(UType.ELEMENT));
29
30 public final static MultipleNodeKindTest DOC_ELEM_ATTR =
31 new MultipleNodeKindTest(UType.DOCUMENT.union(UType.ELEMENT).union(UType.ATTRIBUTE));
32
33 public final static MultipleNodeKindTest LEAF =
34 new MultipleNodeKindTest(UType.TEXT.union(UType.COMMENT).union(UType.PI).union(UType.NAMESPACE).union(UType.ATTRIBUTE));
35
36
37 UType uType;
38 int nodeKindMask;
39
40 public MultipleNodeKindTest(UType u) {
41 this.uType = u;
42 if (UType.DOCUMENT.overlaps(u)) {
43 nodeKindMask |= 1 << Type.DOCUMENT;
44 }
45 if (UType.ELEMENT.overlaps(u)) {
46 nodeKindMask |= 1 << Type.ELEMENT;
47 }
48 if (UType.ATTRIBUTE.overlaps(u)) {
49 nodeKindMask |= 1 << Type.ATTRIBUTE;
50 }
51 if (UType.TEXT.overlaps(u)) {
52 nodeKindMask |= 1 << Type.TEXT;
53 }
54 if (UType.COMMENT.overlaps(u)) {
55 nodeKindMask |= 1 << Type.COMMENT;
56 }
57 if (UType.PI.overlaps(u)) {
58 nodeKindMask |= 1 << Type.PROCESSING_INSTRUCTION;
59 }
60 if (UType.NAMESPACE.overlaps(u)) {
61 nodeKindMask |= 1 << Type.NAMESPACE;
62 }
63 }
64
65 /**
66 * Get the corresponding {@link net.sf.saxon.type.UType}. A UType is a union of primitive item
67 * types.
68 *
69 * @return the smallest UType that subsumes this item type
70 */
71 public UType getUType() {
72 return uType;
73 }
74
75 /**
76 * Test whether this node test is satisfied by a given node. This method is only
77 * fully supported for a subset of NodeTests, because it doesn't provide all the information
78 * needed to evaluate all node tests. In particular (a) it can't be used to evaluate a node
79 * test of the form element(N,T) or schema-element(E) where it is necessary to know whether the
80 * node is nilled, and (b) it can't be used to evaluate a node test of the form
81 * document-node(element(X)). This in practice means that it is used (a) to evaluate the
82 * simple node tests found in the XPath 1.0 subset used in XML Schema, and (b) to evaluate
83 * node tests where the node kind is known to be an attribute.
84 *
85 * @param nodeKind The kind of node to be matched
86 * @param name identifies the expanded name of the node to be matched.
87 * The value should be null for a node with no name.
88 * @param annotation The actual content type of the node
89 */
90 @Override
91 public boolean matches(int nodeKind, NodeName name, SchemaType annotation) {
92 return (nodeKindMask & (1<<nodeKind)) != 0;
93 }
94
95 public IntPredicate getMatcher(final NodeVectorTree tree) {
96 final byte[] nodeKindArray = tree.getNodeKindArray();
97 return nodeNr -> {
98 int nodeKind = nodeKindArray[nodeNr] & 0x0f;
99 if (nodeKind == Type.WHITESPACE_TEXT) {
100 nodeKind = Type.TEXT;
101 }
102 return (nodeKindMask & (1 << nodeKind)) != 0;
103 };
104 }
105
106 /**
107 * Test whether this node test is satisfied by a given node. This alternative
108 * method is used in the case of nodes where calculating the fingerprint is expensive,
109 * for example DOM or JDOM nodes.
110 *
111 * @param node the node to be matched
112 */
113
114 public boolean matchesNode(NodeInfo node) {
115 int nodeKind = node.getNodeKind();
116 return (nodeKindMask & (1<<nodeKind)) != 0;
117 }
118
119
120 /**
121 * Determine the default priority to use if this pattern appears as a match pattern
122 * for a template with no explicit priority attribute.
123 */
124
125 public double getDefaultPriority() {
126 return -0.5;
127 }
128
129 /*@NotNull*/
130 public String toString() {
131 FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C64);
132 LinkedList<PrimitiveUType> types = new LinkedList<>(uType.decompose());
133 format(types, fsb, ItemType::toString);
134 return fsb.toString();
135 }
136
137 public String toExportString() {
138 FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C64);
139 LinkedList<PrimitiveUType> types = new LinkedList<>(uType.decompose());
140 format(types, fsb, ItemType::toExportString);
141 return fsb.toString();
142 }
143
144 private void format(LinkedList<PrimitiveUType> list, FastStringBuffer fsb, Function<ItemType, String> show) {
145 if (list.size() == 1) {
146 fsb.append(list.get(0).toItemType().toString());
147 } else {
148 fsb.append('(');
149 fsb.append(list.removeFirst().toItemType().toString());
150 fsb.append('|');
151 format(list, fsb, show);
152 fsb.append(')');
153 }
154 }
155
156 /**
157 * Returns a hash code value for the object.
158 */
159
160 public int hashCode() {
161 return uType.hashCode();
162 }
163
164 @Override
165 public boolean equals(Object obj) {
166 return obj instanceof MultipleNodeKindTest && uType.equals(((MultipleNodeKindTest)obj).uType);
167 }
168
169 /**
170 * Generate Javascript code to test whether an item conforms to this item type
171 *
172 * @return a Javascript instruction or sequence of instructions, which can be used as the body
173 * of a Javascript function, and which returns a boolean indication whether the value of the
174 * variable "item" is an instance of this item type.
175 * @param knownToBe An item type that the supplied item is known to conform to; the generated code
176 * can assume that the item is an instance of this type.
177 * @param targetVersion The version of Saxon-JS for which code is being generated. Currently either 1 or 2.
178
179 */
180 @Override
181 public String generateJavaScriptItemTypeTest(ItemType knownToBe, int targetVersion) {
182 FastStringBuffer fsb = new FastStringBuffer(256);
183 fsb.append("return SaxonJS.U.isNode(item) && (");
184 String or = "";
185 UType u = uType;
186 if (UType.DOCUMENT.overlaps(u)) {
187 fsb.append(or + "item.nodeType===9||item.nodeType===11");
188 or = "||";
189 }
190 if (UType.ELEMENT.overlaps(u)) {
191 fsb.append(or + "item.nodeType===1");
192 or = "||";
193 }
194 if (UType.ATTRIBUTE.overlaps(u)) {
195 fsb.append(or + "item.nodeType===2");
196 or = "||";
197 }
198 if (UType.TEXT.overlaps(u)) {
199 fsb.append(or + "item.nodeType===3");
200 or = "||";
201 }
202 if (UType.COMMENT.overlaps(u)) {
203 fsb.append(or + "item.nodeType===8");
204 or = "||";
205 }
206 if (UType.PI.overlaps(u)) {
207 fsb.append(or + "item.nodeType===7");
208 or = "||";
209 }
210 if (UType.NAMESPACE.overlaps(u)) {
211 fsb.append(or + "item.nodeType===13");
212 or = "||";
213 }
214 fsb.append(");");
215 return fsb.toString().replace("false||", "");
216 }
217
218
219 }
220