Debian Codegraph @

net/sf/saxon/pattern/LocalNameTest.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.tree.tiny.NodeVectorTree;
12 import net.sf.saxon.type.*;
13 import net.sf.saxon.z.IntSet;
14
15 import java.util.Optional;
16 import java.util.function.IntPredicate;
17
18 /**
19 * NodeTest is an interface that enables a test of whether a node has a particular
20 * name and type. A LocalNameTest matches the node type and the local name,
21 * it represents an XPath 2.0 test of the form *:name.
22 *
23 * @author Michael H. Kay
24 */
25
26 public final class LocalNameTest extends NodeTest implements QNameTest {
27
28 private NamePool namePool;
29 private int nodeKind;
30 private String localName;
31 private UType uType;
32
33 public LocalNameTest(NamePool pool, int nodeKind, String localName) {
34 this.namePool = pool;
35 this.nodeKind = nodeKind;
36 this.localName = localName;
37 uType = UType.fromTypeCode(nodeKind);
38 }
39
40 /**
41 * Get the node kind matched by this test
42 *
43 * @return the matching node kind
44 */
45
46 public int getNodeKind() {
47 return nodeKind;
48 }
49
50 /**
51 * Get the corresponding {@link net.sf.saxon.type.UType}. A UType is a union of primitive item
52 * types.
53 *
54 * @return the smallest UType that subsumes this item type
55 */
56 public UType getUType() {
57 return uType;
58 }
59
60 /**
61 * Get the set of node names allowed by this NodeTest. Return no result, because the
62 * set of names cannot be represented.
63 *
64 * @return the set of integer fingerprints of the node names that this node test can match; or absent
65 * if the set of names cannot be represented (for example, with the name tests *:xxx or xxx:*)
66 */
67 @Override
68 public Optional<IntSet> getRequiredNodeNames() {
69 // See bug 3713
70 return Optional.empty();
71 }
72
73 /**
74 * Test whether this node test is satisfied by a given node. This method is only
75 * fully supported for a subset of NodeTests, because it doesn't provide all the information
76 * needed to evaluate all node tests. In particular (a) it can't be used to evaluate a node
77 * test of the form element(N,T) or schema-element(E) where it is necessary to know whether the
78 * node is nilled, and (b) it can't be used to evaluate a node test of the form
79 * document-node(element(X)). This in practice means that it is used (a) to evaluate the
80 * simple node tests found in the XPath 1.0 subset used in XML Schema, and (b) to evaluate
81 * node tests where the node kind is known to be an attribute.
82 *
83 * @param nodeKind The kind of node to be matched
84 * @param name identifies the expanded name of the node to be matched.
85 * The value should be null for a node with no name.
86 * @param annotation The actual content type of the node
87 */
88 @Override
89 public boolean matches(int nodeKind, /*@Nullable*/ NodeName name, SchemaType annotation) {
90 return name != null && nodeKind == this.nodeKind && localName.equals(name.getLocalPart());
91 }
92
93 public IntPredicate getMatcher(final NodeVectorTree tree) {
94 final byte[] nodeKindArray = tree.getNodeKindArray();
95 final int[] nameCodeArray = tree.getNameCodeArray();
96 return nodeNr -> (nodeKindArray[nodeNr] & 0x0f) == nodeKind &&
97 localName.equals(namePool.getLocalName(nameCodeArray[nodeNr] & 0xfffff));
98 }
99
100 /**
101 * Test whether this node test is satisfied by a given node. This alternative
102 * method is used in the case of nodes where calculating the fingerprint is expensive,
103 * for example DOM or JDOM nodes.
104 *
105 * @param node the node to be matched
106 */
107
108 public boolean matchesNode(NodeInfo node) {
109 return localName.equals(node.getLocalPart()) && nodeKind == node.getNodeKind();
110 }
111
112 /**
113 * Test whether this QNameTest matches a given QName
114 *
115 * @param qname the QName to be matched
116 * @return true if the name matches, false if not
117 */
118
119 public boolean matches(StructuredQName qname) {
120 return localName.equals(qname.getLocalPart());
121 }
122
123 /**
124 * Determine the default priority of this node test when used on its own as a Pattern
125 */
126
127 public final double getDefaultPriority() {
128 return -0.25;
129 }
130
131 /**
132 * Get the local name used in this LocalNameTest
133 *
134 * @return the local name matched by the test
135 */
136
137 public String getLocalName() {
138 return localName;
139 }
140
141 /**
142 * Determine the types of nodes to which this pattern applies. Used for optimisation.
143 * For patterns that match nodes of several types, return Type.NODE
144 *
145 * @return the type of node matched by this pattern. e.g. Type.ELEMENT or Type.TEXT
146 */
147
148 public int getPrimitiveType() {
149 return nodeKind;
150 }
151
152 public String toString() {
153 switch (nodeKind) {
154 case Type.ELEMENT:
155 return "*:" + localName;
156 case Type.ATTRIBUTE:
157 return "@*:" + localName;
158 default:
159 return "(*" + nodeKind + "*):" + localName; // should not be used
160 }
161 }
162
163 /**
164 * Returns a hash code value for the object.
165 */
166
167 public int hashCode() {
168 return nodeKind << 20 ^ localName.hashCode();
169 }
170
171 /**
172 * Indicates whether some other object is "equal to" this one.
173 */
174 public boolean equals(Object other) {
175 return other instanceof LocalNameTest &&
176 ((LocalNameTest) other).nodeKind == nodeKind &&
177 ((LocalNameTest) other).localName.equals(localName);
178 }
179
180 public NamePool getNamePool() {return namePool;}
181
182 /**
183 * Generate Javascript code to test if a name matches the test.
184 *
185 * @return JS code as a string. The generated code will be used
186 * as the body of a JS function in which the argument name "q" is an
187 * XdmQName object holding the name. The XdmQName object has properties
188 * uri and local.
189 * @param targetVersion the version of Saxon-JS being targeted
190 */
191 public String generateJavaScriptNameTest(int targetVersion) {
192 return "q.local==='" + localName + "'";
193 }
194
195 /**
196 * Generate Javascript code to test whether an item conforms to this item type
197 *
198 * @return a Javascript instruction or sequence of instructions, which can be used as the body
199 * of a Javascript function, and which returns a boolean indication whether the value of the
200 * variable "item" is an instance of this item type.
201 * @param knownToBe An item type that the supplied item is known to conform to; the generated code
202 * can assume that the item is an instance of this type.
203 * @param targetVersion The version of Saxon-JS for which code is being generated. Currently either 1 or 2.
204 */
205 @Override
206 public String generateJavaScriptItemTypeTest(ItemType knownToBe, int targetVersion) {
207 if (targetVersion == 1) {
208 return "var q=SaxonJS.U.nameOfNode(item); return SaxonJS.U.isNode(item) && item.nodeType===" +
209 nodeKind + "&&" + generateJavaScriptNameTest(targetVersion);
210 } else {
211 return "return SaxonJS.U.isNode(item) && item.nodeType===" + nodeKind + " && SaxonJS.U.hasLocalName(item, '" + localName + "')";
212 }
213 }
214
215 /**
216 * Get extra diagnostic information about why a supplied item does not conform to this
217 * item type, if available. If extra information is returned, it should be in the form of a complete
218 * sentence, minus the closing full stop. No information should be returned for obvious cases.
219 *
220 * @param item the item that doesn't match this type
221 * @param th the type hierarchy cache
222 * @return optionally, a message explaining why the item does not match the type
223 */
224 @Override
225 public Optional<String> explainMismatch(Item item, TypeHierarchy th) {
226 Optional<String> explanation = super.explainMismatch(item, th);
227 if (explanation.isPresent()) {
228 return explanation;
229 }
230 return Optional.of("The node has the wrong local name");
231 }
232
233 }
234