Debian Codegraph @

net/sf/saxon/pattern/NamespaceTest.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.trace.ExpressionPresenter;
12 import net.sf.saxon.tree.tiny.NodeVectorTree;
13 import net.sf.saxon.type.*;
14 import net.sf.saxon.z.IntSet;
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 type. A NamespaceTest matches the node type and the namespace URI.
22 *
23 * @author Michael H. Kay
24 */
25
26 public final class NamespaceTest extends NodeTest implements QNameTest {
27
28 private NamePool namePool;
29 private int nodeKind;
30 private UType uType;
31 private String uri;
32
33 public NamespaceTest(NamePool pool, int nodeKind, String uri) {
34 namePool = pool;
35 this.nodeKind = nodeKind;
36 this.uri = uri;
37 this.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 && name.hasURI(uri);
91 }
92
93 public IntPredicate getMatcher(final NodeVectorTree tree) {
94 final byte[] nodeKindArray = tree.getNodeKindArray();
95 final int[] nameCodeArray = tree.getNameCodeArray();
96 return nodeNr -> {
97 int fp = nameCodeArray[nodeNr] & 0xfffff;
98 return fp != -1 &&
99 (nodeKindArray[nodeNr]&0x0f) == nodeKind &&
100 uri.equals(namePool.getURI(fp));
101 };
102 }
103
104 /**
105 * Test whether this node test is satisfied by a given node. This alternative
106 * method is used in the case of nodes where calculating the fingerprint is expensive,
107 * for example DOM or JDOM nodes.
108 *
109 * @param node the node to be matched
110 */
111
112 public boolean matchesNode(NodeInfo node) {
113 return node.getNodeKind() == nodeKind && node.getURI().equals(uri);
114 }
115
116 /**
117 * Test whether this NamespaceTest matches a given QName
118 *
119 * @param qname the QName to be matched
120 * @return true if the name matches, false if not
121 */
122
123 public boolean matches(StructuredQName qname) {
124 return qname.hasURI(uri);
125 }
126
127 /**
128 * Determine the default priority of this node test when used on its own as a Pattern
129 */
130
131 public final double getDefaultPriority() {
132 return -0.25;
133 }
134
135 /**
136 * Determine the types of nodes to which this pattern applies. Used for optimisation.
137 * For patterns that match nodes of several types, return Type.NODE
138 *
139 * @return the type of node matched by this pattern. e.g. Type.ELEMENT or Type.TEXT
140 */
141
142 public int getPrimitiveType() {
143 return nodeKind;
144 }
145
146 /**
147 * Get the namespace URI matched by this NamespaceTest
148 *
149 * @return the namespace URI matched by this NamespaceTest
150 */
151
152 public String getNamespaceURI() {
153 return uri;
154 }
155
156 public String toString() {
157 switch (nodeKind) {
158 case Type.ELEMENT:
159 return "Q{" + uri + "}*";
160 case Type.ATTRIBUTE:
161 return "@Q{" + uri + "}*";
162 default:
163 // should not happen
164 return "(*" + nodeKind + "*)Q{" + uri + "}*";
165 }
166 }
167
168 /**
169 * Returns a hash code value for the object.
170 */
171
172 public int hashCode() {
173 return uri.hashCode() << 5 + nodeKind;
174 }
175
176 /**
177 * Indicates whether some other object is "equal to" this one.
178 */
179 public boolean equals(Object other) {
180 return other instanceof NamespaceTest &&
181 ((NamespaceTest) other).namePool == namePool &&
182 ((NamespaceTest) other).nodeKind == nodeKind &&
183 ((NamespaceTest) other).uri.equals(uri);
184 }
185
186 /**
187 * Generate Javascript code to test if a name matches the test.
188 *
189 * @return JS code as a string. The generated code will be used
190 * as the body of a JS function in which the argument name "q" is an
191 * XdmQName object holding the name. The XdmQName object has properties
192 * uri and local.
193 * @param targetVersion the version of Saxon-JS being targeted
194 */
195
196 public String generateJavaScriptNameTest(int targetVersion) {
197 return "q.uri==='" + ExpressionPresenter.jsEscape(uri) + "'";
198 }
199
200 /**
201 * Generate Javascript code to test whether an item conforms to this item type
202 *
203 * @return a Javascript instruction or sequence of instructions, which can be used as the body
204 * of a Javascript function, and which returns a boolean indication whether the value of the
205 * variable "item" is an instance of this item type.
206 * @param knownToBe An item type that the supplied item is known to conform to; the generated code
207 * can assume that the item is an instance of this type.
208 * @param targetVersion The version of Saxon-JS for which code is being generated. Currently either 1 or 2.
209
210 */
211 @Override
212 public String generateJavaScriptItemTypeTest(ItemType knownToBe, int targetVersion) {
213 if (targetVersion == 1) {
214 return "var q=SaxonJS.U.nameOfNode(item); return SaxonJS.U.isNode(item) && item.nodeType===" + nodeKind + "&&" + generateJavaScriptNameTest(targetVersion);
215 } else {
216 return "return SaxonJS.U.isNode(item) && item.nodeType===" + nodeKind + " && SaxonJS.U.hasURI(item, '" +
217 ExpressionPresenter.jsEscape(uri) + "');";
218 }
219 }
220
221 /**
222 * Get extra diagnostic information about why a supplied item does not conform to this
223 * item type, if available. If extra information is returned, it should be in the form of a complete
224 * sentence, minus the closing full stop. No information should be returned for obvious cases.
225 *
226 * @param item the item that doesn't match this type
227 * @param th the type hierarchy cache
228 * @return optionally, a message explaining why the item does not match the type
229 */
230 @Override
231 public Optional<String> explainMismatch(Item item, TypeHierarchy th) {
232 Optional<String> explanation = super.explainMismatch(item, th);
233 if (explanation.isPresent()) {
234 return explanation;
235 }
236 return Optional.of("The node is in the wrong namespace");
237 }
238
239 }