Debian Codegraph @

net/sf/saxon/pattern/SameNameTest.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.*;
11 import net.sf.saxon.trans.SaxonErrorCode;
12 import net.sf.saxon.trans.XPathException;
13 import net.sf.saxon.tree.tiny.NodeVectorTree;
14 import net.sf.saxon.tree.util.Navigator;
15 import net.sf.saxon.type.ItemType;
16 import net.sf.saxon.type.SchemaType;
17 import net.sf.saxon.type.Type;
18 import net.sf.saxon.type.UType;
19 import net.sf.saxon.z.IntSet;
20 import net.sf.saxon.z.IntSingletonSet;
21
22 import java.util.Optional;
23 import java.util.function.IntPredicate;
24
25 /**
26 * NodeTest is an interface that enables a test of whether a node has a particular
27 * name and type. A SameNameTest matches a node that has the same node kind and name
28 * as a supplied node.
29 *
30 * <p>Note: it's not safe to use this if the supplied node is mutable.</p>
31 *
32 * @author Michael H. Kay
33 */
34
35 public class SameNameTest extends NodeTest implements QNameTest {
36
37 private NodeInfo origin;
38 /**
39 * Create a SameNameTest to match nodes by name
40 *
41 * @param origin the node whose node kind and name must be matched
42 * @since 9.0
43 */
44
45 public SameNameTest(NodeInfo origin) {
46 this.origin = origin;
47 }
48
49 /**
50 * Get the node kind that this name test matches
51 *
52 * @return the matching node kind
53 */
54
55 public int getNodeKind() {
56 return origin.getNodeKind();
57 }
58
59 /**
60 * Get the corresponding {@link net.sf.saxon.type.UType}. A UType is a union of primitive item
61 * types.
62 *
63 * @return the smallest UType that subsumes this item type
64 */
65 public UType getUType() {
66 return UType.fromTypeCode(origin.getNodeKind());
67 }
68
69
70 /**
71 * Test whether this node test is satisfied by a given node. This method is only
72 * fully supported for a subset of NodeTests, because it doesn't provide all the information
73 * needed to evaluate all node tests. In particular (a) it can't be used to evaluate a node
74 * test of the form element(N,T) or schema-element(E) where it is necessary to know whether the
75 * node is nilled, and (b) it can't be used to evaluate a node test of the form
76 * document-node(element(X)). This in practice means that it is used (a) to evaluate the
77 * simple node tests found in the XPath 1.0 subset used in XML Schema, and (b) to evaluate
78 * node tests where the node kind is known to be an attribute.
79 *
80 * @param nodeKind The kind of node to be matched
81 * @param name identifies the expanded name of the node to be matched.
82 * The value should be null for a node with no name.
83 * @param annotation The actual content type of the node
84 */
85 @Override
86 public boolean matches(int nodeKind, NodeName name, SchemaType annotation) {
87 if (nodeKind != origin.getNodeKind()) {
88 return false;
89 }
90 if (name.hasFingerprint() && origin.hasFingerprint()) {
91 return name.getFingerprint() == origin.getFingerprint();
92 } else {
93 return name.hasURI(origin.getURI()) && name.getLocalPart().equals(origin.getLocalPart());
94 }
95 }
96
97 public IntPredicate getMatcher(final NodeVectorTree tree) {
98 final byte[] nodeKindArray = tree.getNodeKindArray();
99 final int[] nameCodeArray = tree.getNameCodeArray();
100 return nodeNr -> {
101 int k = nodeKindArray[nodeNr] & 0x0f;
102 if (k == Type.WHITESPACE_TEXT) {
103 k = Type.TEXT;
104 }
105 if (k != origin.getNodeKind()) {
106 return false;
107 } else if (origin.hasFingerprint()) {
108 return (nameCodeArray[nodeNr] & 0xfffff) == origin.getFingerprint();
109 } else {
110 return Navigator.haveSameName(tree.getNode(nodeNr), origin);
111 }
112 };
113 }
114
115 /**
116 * Test whether this node test is satisfied by a given node. This alternative
117 * method is used in the case of nodes where calculating the fingerprint is expensive,
118 * for example DOM or JDOM nodes.
119 *
120 * @param node the node to be matched
121 */
122
123 public boolean matchesNode(NodeInfo node) {
124 return node == origin ||
125 (node.getNodeKind() == origin.getNodeKind() && Navigator.haveSameName(node, origin));
126 }
127
128 /**
129 * Test whether the NameTest matches a given QName
130 *
131 * @param qname the QName to be matched
132 * @return true if the name matches
133 */
134
135 public boolean matches(StructuredQName qname) {
136 return NameOfNode.makeName(origin).getStructuredQName().equals(qname);
137 }
138
139 /**
140 * Determine the default priority of this node test when used on its own as a Pattern
141 */
142
143 public final double getDefaultPriority() {
144 return 0.0;
145 }
146
147 /**
148 * Get the fingerprint required
149 */
150
151 public int getFingerprint() {
152 if (origin.hasFingerprint()) {
153 return origin.getFingerprint();
154 } else {
155 NamePool pool = origin.getConfiguration().getNamePool();
156 return pool.allocateFingerprint(origin.getURI(), origin.getLocalPart());
157 }
158 }
159
160 /**
161 * Determine the types of nodes to which this pattern applies. Used for optimisation.
162 * For patterns that match nodes of several types, return Type.NODE
163 *
164 * @return the type of node matched by this pattern. e.g. Type.ELEMENT or Type.TEXT
165 */
166
167 public int getPrimitiveType() {
168 return origin.getNodeKind();
169 }
170
171
172 /*@NotNull*/
173 public Optional<IntSet> getRequiredNodeNames() {
174 return Optional.of(new IntSingletonSet(getFingerprint()));
175 }
176
177 /**
178 * Get the namespace URI matched by this nametest
179 *
180 * @return the namespace URI (using "" for the "null namepace")
181 */
182
183 public String getNamespaceURI() {
184 return origin.getURI();
185 }
186
187 /**
188 * Get the local name matched by this nametest
189 *
190 * @return the local name
191 */
192
193 public String getLocalPart() {
194 return origin.getLocalPart();
195 }
196
197 public String toString() {
198 switch (origin.getNodeKind()) {
199 case Type.ELEMENT:
200 return "element(" + NameOfNode.makeName(origin).getStructuredQName().getEQName() + ")";
201 case Type.ATTRIBUTE:
202 return "attribute(" + NameOfNode.makeName(origin).getStructuredQName().getEQName() + ")";
203 case Type.PROCESSING_INSTRUCTION:
204 return "processing-instruction(" + origin.getLocalPart() + ')';
205 case Type.NAMESPACE:
206 return "namespace-node(" + origin.getLocalPart() + ')';
207 case Type.COMMENT:
208 return "comment()";
209 case Type.DOCUMENT:
210 return "document-node()";
211 case Type.TEXT:
212 return "text()";
213 default:
214 return "***";
215 }
216 }
217
218 /**
219 * Returns a hash code value for the object.
220 */
221
222 public int hashCode() {
223 return origin.getNodeKind() << 20 ^ origin.getURI().hashCode() ^ origin.getLocalPart().hashCode();
224 }
225
226 /**
227 * Determines whether two NameTests are equal
228 */
229
230 public boolean equals(Object other) {
231 return other instanceof SameNameTest &&
232 matchesNode(((SameNameTest) other).origin);
233 }
234
235 /**
236 * Generate an equivalent NameTest
237 * @return a NameTest that matches the same node kind and name
238 */
239
240 public NameTest getEquivalentNameTest() {
241 return new NameTest(origin.getNodeKind(), origin.getURI(), origin.getLocalPart(), origin.getConfiguration().getNamePool());
242 }
243
244 /**
245 * Generate Javascript code to test if a name matches the test.
246 *
247 * @return JS code as a string. The generated code will be used
248 * as the body of a JS function in which the argument name "q" is an
249 * XdmQName object holding the name. The XdmQName object has properties
250 * uri and local.
251 * @param targetVersion the version of Saxon-JS being targeted
252 */
253 public String generateJavaScriptNameTest(int targetVersion) {
254 // Not applicable
255 return "false";
256 }
257
258 /**
259 * Generate Javascript code to test whether an item conforms to this item type
260 *
261 * @return a Javascript instruction or sequence of instructions, which can be used as the body
262 * of a Javascript function, and which returns a boolean indication whether the value of the
263 * variable "item" is an instance of this item type.
264 * @throws XPathException if JS code cannot be generated for this item type, for example because
265 * the test is schema-aware.
266 * @param knownToBe an item type that the value is known to conform to
267 * @param targetVersion the version of Saxon-JS being targeted
268 */
269 @Override
270 public String generateJavaScriptItemTypeTest(ItemType knownToBe, int targetVersion) throws XPathException {
271 throw new XPathException("Cannot generate JS code for a SameNameTest", SaxonErrorCode.SXJS0001);
272 }
273 }
274