Debian Codegraph @

net/sf/saxon/trans/KeyDefinition.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.trans;
9
10 import net.sf.saxon.expr.Component;
11 import net.sf.saxon.expr.ContextOriginator;
12 import net.sf.saxon.expr.Expression;
13 import net.sf.saxon.expr.instruct.Actor;
14 import net.sf.saxon.expr.instruct.SlotManager;
15 import net.sf.saxon.lib.NamespaceConstant;
16 import net.sf.saxon.lib.StringCollator;
17 import net.sf.saxon.om.StandardNames;
18 import net.sf.saxon.om.StructuredQName;
19 import net.sf.saxon.pattern.Pattern;
20 import net.sf.saxon.style.StylesheetPackage;
21 import net.sf.saxon.trace.ExpressionPresenter;
22 import net.sf.saxon.trace.LocationKind;
23 import net.sf.saxon.type.BuiltInAtomicType;
24 import net.sf.saxon.type.UType;
25
26 import java.util.Map;
27
28 /**
29 * Corresponds to a single xsl:key declaration.
30 *
31 * @author Michael H. Kay
32 */
33
34 public class KeyDefinition extends Actor implements ContextOriginator {
35
36 private SymbolicName symbolicName;
37 private Pattern match; // the match pattern
38 private BuiltInAtomicType useType; // the type of the values returned by the atomized use expression
39 private StringCollator collation; // the collating sequence, when type=string
40 private String collationName; // the collation URI
41 private boolean backwardsCompatible = false;
42 private boolean strictComparison = false;
43 private boolean convertUntypedToOther = false;
44 private boolean rangeKey = false;
45 private boolean composite = false;
46
47 /**
48 * Constructor to create a key definition
49 *
50 * @param match the pattern in the xsl:key match attribute
51 * @param use the expression in the xsl:key use attribute, or the expression that results from compiling
52 * the xsl:key contained instructions.
53 * @param collationName the name of the collation being used
54 * @param collation the actual collation. This must be one that supports generation of collation keys.
55 */
56
57 public KeyDefinition(SymbolicName symbolicName, Pattern match, Expression use, String collationName, StringCollator collation) {
58 this.symbolicName = symbolicName;
59 this.match = match;
60 setBody(use);
61 this.collation = collation;
62 this.collationName = collationName;
63 }
64
65 /**
66 * Get the symbolic name of the component
67 *
68 * @return the symbolic name
69 */
70 @Override
71 public SymbolicName getSymbolicName() {
72 return symbolicName;
73 }
74
75 /**
76 * Say whether this key is a range key, that is, a key capable of returning
77 * (a) the values in a selected range, and (b) the keys in order
78 *
79 * @param rangeKey true if this is a range key; false if not
80 */
81
82 public void setRangeKey(boolean rangeKey) {
83 this.rangeKey = rangeKey;
84 }
85
86 /**
87 * Ask whether this key is a range key, that is, a key capable of returning
88 * (a) the values in a selected range, and (b) the keys in order
89 *
90 * @return true if this is a range key; false if not
91 */
92
93 public boolean isRangeKey() {
94 return rangeKey;
95 }
96
97 /**
98 * Say whether this is a composite key. (If so, a sequence of atomic values in the use expression
99 * is taken as a single key value, rather than as a set of independent key values)
100 * @param composite true if this is a composite key
101 */
102
103 public void setComposite(boolean composite) {
104 this.composite = composite;
105 }
106
107 /**
108 * Ask whether this is a composite key
109 * @return true if the key is composite
110 */
111
112 public boolean isComposite() {
113 return composite;
114 }
115
116 /**
117 * Set the primitive item type of the values returned by the use expression
118 *
119 * @param itemType the primitive type of the indexed values
120 */
121
122 public void setIndexedItemType(BuiltInAtomicType itemType) {
123 useType = itemType;
124 }
125
126 /**
127 * Get the primitive item type of the values returned by the use expression
128 *
129 * @return the primitive item type of the indexed values
130 */
131
132 public BuiltInAtomicType getIndexedItemType() {
133 if (useType == null) {
134 return BuiltInAtomicType.ANY_ATOMIC;
135 } else {
136 return useType;
137 }
138 }
139
140 /**
141 * Set backwards compatibility mode. The key definition is backwards compatible if ANY of the xsl:key
142 * declarations has version="1.0" in scope.
143 *
144 * @param bc set to true if running in XSLT 2.0 backwards compatibility mode
145 */
146
147 public void setBackwardsCompatible(boolean bc) {
148 backwardsCompatible = bc;
149 }
150
151 /**
152 * Test backwards compatibility mode
153 *
154 * @return true if running in XSLT backwards compatibility mode
155 */
156
157 public boolean isBackwardsCompatible() {
158 return backwardsCompatible;
159 }
160
161 /**
162 * Set whether strict comparison is needed. Strict comparison treats non-comparable values as an
163 * error rather than a no-match. This is used for internal keys that support value comparisons in
164 * Saxon-EE, it is not used for user-defined XSLT keys.
165 *
166 * @param strict true if strict comparison is needed
167 */
168
169 public void setStrictComparison(boolean strict) {
170 strictComparison = strict;
171 }
172
173 /**
174 * Ask whether strict comparison is needed. Strict comparison treats non-comparable values as an
175 * error rather than a no-match. This is used for internal keys that support value comparisons in
176 * Saxon-EE, it is not used for user-defined XSLT keys.
177 *
178 * @return true if strict comparison is needed.
179 */
180
181 public boolean isStrictComparison() {
182 return strictComparison;
183 }
184
185 /**
186 * Indicate that untypedAtomic values should be converted to the type of the other operand,
187 * rather than to strings. This is used for indexes constructed internally by Saxon-EE to
188 * support filter expressions that use the "=" operator, as distinct from "eq".
189 *
190 * @param convertToOther true if comparisons follow the semantics of the "=" operator rather than
191 * the "eq" operator
192 */
193
194 public void setConvertUntypedToOther(boolean convertToOther) {
195 convertUntypedToOther = convertToOther;
196 }
197
198 /**
199 * Determine whether untypedAtomic values are converted to the type of the other operand.
200 *
201 * @return true if comparisons follow the semantics of the "=" operator rather than
202 * the "eq" operator
203 */
204
205 public boolean isConvertUntypedToOther() {
206 return convertUntypedToOther;
207 }
208
209 /**
210 * Set the map of local variables needed while evaluating the "use" expression
211 */
212
213 public void setStackFrameMap(SlotManager map) {
214 if (map != null && map.getNumberOfVariables() > 0) {
215 super.setStackFrameMap(map);
216 }
217 }
218
219 @Override
220 public void allocateAllBindingSlots(StylesheetPackage pack) {
221 super.allocateAllBindingSlots(pack);
222 allocateBindingSlotsRecursive(pack, this, match, getDeclaringComponent().getComponentBindings());
223 }
224
225 @Override
226 public int getComponentKind() {
227 return StandardNames.XSL_KEY;
228 }
229
230 /**
231 * Set the system Id and line number of the source xsl:key definition
232 *
233 * @param systemId the URI of the module containing the key definition
234 * @param lineNumber the line number of the key definition
235 */
236
237 public void setLocation(String systemId, int lineNumber) {
238 setSystemId(systemId);
239 setLineNumber(lineNumber);
240 }
241
242 /**
243 * Get the match pattern for the key definition
244 *
245 * @return the pattern specified in the "match" attribute of the xsl:key declaration
246 */
247
248 public Pattern getMatch() {
249 return match;
250 }
251
252 /**
253 * Get the use expression for the key definition
254 *
255 * @return the expression specified in the "use" attribute of the xsl:key declaration
256 */
257
258 public Expression getUse() {
259 return getBody();
260 }
261
262 /**
263 * Get the collation name for this key definition.
264 *
265 * @return the collation name (the collation URI)
266 */
267
268 public String getCollationName() {
269 return collationName;
270 }
271
272 /**
273 * Get the collation.
274 *
275 * @return the collation
276 */
277
278 public StringCollator getCollation() {
279 return collation;
280 }
281
282 /**
283 * Get the type of construct. This will either be the fingerprint of a standard XSLT instruction name
284 * (values in {@link net.sf.saxon.om.StandardNames}: all less than 1024)
285 * or it will be a constant in class {@link LocationKind}.
286 */
287
288 public int getConstructType() {
289 return StandardNames.XSL_KEY;
290 }
291
292 /**
293 * Get a name identifying the object of the expression, for example a function name, template name,
294 * variable name, key name, element name, etc. This is used only where the name is known statically.
295 */
296
297 /*@Nullable*/
298 public StructuredQName getObjectName() {
299 return symbolicName.getComponentName();
300 }
301
302 /**
303 * Diagnostic print of expression structure. The abstract expression tree
304 * is written to the supplied outputstream.
305 *
306 * @param out the expression presenter used to display the structure
307 */
308
309 public void export(ExpressionPresenter out, boolean reusable, Map<Component, Integer> componentIdMap) throws XPathException {
310 out.startElement("key");
311 out.emitAttribute("name", getObjectName());
312 if (!NamespaceConstant.CODEPOINT_COLLATION_URI.equals(collationName)) {
313 out.emitAttribute("collation", collationName);
314 }
315 out.emitAttribute("line", getLineNumber() + "");
316 out.emitAttribute("module", getSystemId());
317 if (getStackFrameMap() != null && getStackFrameMap().getNumberOfVariables() != 0) {
318 out.emitAttribute("slots", getStackFrameMap().getNumberOfVariables() + "");
319 }
320 if (componentIdMap != null) {
321 out.emitAttribute("binds", "" + getDeclaringComponent().listComponentReferences(componentIdMap));
322 }
323 String flags = "";
324 if (backwardsCompatible) {
325 flags += "b";
326 }
327 if (isRangeKey()) {
328 flags += "r";
329 out.emitAttribute("range", "1");
330 }
331 if (match.getUType().overlaps(UType.ATTRIBUTE)) {
332 flags += "a";
333 }
334 if (match.getUType().overlaps(UType.NAMESPACE)) {
335 flags += "n";
336 }
337 if (composite) {
338 flags += "c";
339 }
340 if (reusable) {
341 flags += "u";
342 }
343 if (!"".equals(flags)) {
344 out.emitAttribute("flags", flags);
345 }
346 getMatch().export(out);
347 getBody().export(out);
348 out.endElement();
349 }
350
351 @Override
352 public void export(ExpressionPresenter presenter) throws XPathException {
353 throw new UnsupportedOperationException();
354 }
355 }
356