Debian Codegraph @

net/sf/saxon/sxpath/AbstractStaticContext.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.sxpath;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.expr.EarlyEvaluationContext;
12 import net.sf.saxon.expr.PackageData;
13 import net.sf.saxon.expr.StaticContext;
14 import net.sf.saxon.expr.XPathContext;
15 import net.sf.saxon.expr.parser.ExplicitLocation;
16 import net.sf.saxon.expr.parser.Location;
17 import net.sf.saxon.expr.parser.RetainedStaticContext;
18 import net.sf.saxon.functions.FunctionLibrary;
19 import net.sf.saxon.functions.FunctionLibraryList;
20 import net.sf.saxon.functions.registry.ConstructorFunctionLibrary;
21 import net.sf.saxon.functions.registry.XPath20FunctionSet;
22 import net.sf.saxon.lib.NamespaceConstant;
23 import net.sf.saxon.lib.StringCollator;
24 import net.sf.saxon.om.StructuredQName;
25 import net.sf.saxon.trans.DecimalFormatManager;
26 import net.sf.saxon.trans.KeyManager;
27 import net.sf.saxon.trans.XPathException;
28 import net.sf.saxon.type.AnyItemType;
29 import net.sf.saxon.type.ItemType;
30
31 import java.util.HashMap;
32 import java.util.Map;
33
34 /**
35 * An abstract and configurable implementation of the StaticContext interface,
36 * which defines the static context of an XPath expression.
37 * <p>This class implements those parts of the functionality of a static context
38 * that tend to be common to most implementations: simple-valued properties such
39 * as base URI and default element namespace; availability of the standard
40 * function library; and support for collations.</p>
41 */
42
43 public abstract class AbstractStaticContext implements StaticContext {
44
45 private String baseURI = null;
46 private Configuration config;
47 private PackageData packageData;
48 private Location containingLocation = ExplicitLocation.UNKNOWN_LOCATION;
49 private String defaultCollationName;
50 private FunctionLibraryList libraryList = new FunctionLibraryList();
51 private String defaultFunctionNamespace = NamespaceConstant.FN;
52 private String defaultElementNamespace = NamespaceConstant.NULL;
53 private boolean backwardsCompatible = false;
54 private int xpathLanguageLevel = 31;
55 protected boolean usingDefaultFunctionLibrary;
56 private Map<StructuredQName, ItemType> typeAliases = new HashMap<>();
57
58 /**
59 * Set the Configuration. This is protected so it can be used only by subclasses;
60 * the configuration will normally be set at construction time
61 *
62 * @param config the configuration
63 */
64
65 protected void setConfiguration(Configuration config) {
66 this.config = config;
67 this.defaultCollationName = config.getDefaultCollationName();
68 }
69
70 /**
71 * Get the system configuration
72 */
73
74 public Configuration getConfiguration() {
75 return config;
76 }
77
78 /**
79 * Set data about the unit of compilation (XQuery module, XSLT package)
80 *
81 * @param packageData the package data
82 */
83 public void setPackageData(PackageData packageData) {
84 this.packageData = packageData;
85 }
86
87 /**
88 * Get data about the unit of compilation (XQuery module, XSLT package) to which this
89 * container belongs
90 */
91 public PackageData getPackageData() {
92 return packageData;
93 }
94
95 /**
96 * Say whether this static context is schema-aware
97 *
98 * @param aware true if this static context is schema-aware
99 */
100
101 public void setSchemaAware(boolean aware) {
102 getPackageData().setSchemaAware(aware);
103 }
104
105 /**
106 * Construct a RetainedStaticContext, which extracts information from this StaticContext
107 * to provide the subset of static context information that is potentially needed
108 * during expression evaluation
109 *
110 * @return a RetainedStaticContext object: either a newly created one, or one that is
111 * reused from a previous invocation.
112 */
113 public RetainedStaticContext makeRetainedStaticContext() {
114 return new RetainedStaticContext(this);
115 }
116
117
118 /**
119 * Initialize the default function library for XPath.
120 * This can be overridden using setFunctionLibrary().
121 */
122
123 protected final void setDefaultFunctionLibrary() {
124 FunctionLibraryList lib = new FunctionLibraryList();
125 lib.addFunctionLibrary(config.getXPath31FunctionSet());
126 lib.addFunctionLibrary(getConfiguration().getBuiltInExtensionLibraryList());
127 lib.addFunctionLibrary(new ConstructorFunctionLibrary(getConfiguration()));
128 lib.addFunctionLibrary(config.getIntegratedFunctionLibrary());
129 config.addExtensionBinders(lib);
130 setFunctionLibrary(lib);
131 }
132
133 public final void setDefaultFunctionLibrary(int version) {
134 FunctionLibraryList lib = new FunctionLibraryList();
135 switch (version) {
136 case 20:
137 default:
138 lib.addFunctionLibrary(XPath20FunctionSet.getInstance());
139 break;
140 case 30:
141 case 305:
142 lib.addFunctionLibrary(config.getXPath30FunctionSet());
143 break;
144 case 31:
145 lib.addFunctionLibrary(config.getXPath31FunctionSet());
146 break;
147 }
148 lib.addFunctionLibrary(getConfiguration().getBuiltInExtensionLibraryList());
149 lib.addFunctionLibrary(new ConstructorFunctionLibrary(getConfiguration()));
150 lib.addFunctionLibrary(config.getIntegratedFunctionLibrary());
151 config.addExtensionBinders(lib);
152 setFunctionLibrary(lib);
153 }
154 /**
155 * Add a function library to the list of function libraries
156 *
157 * @param library the function library to be added
158 */
159
160 protected final void addFunctionLibrary(FunctionLibrary library) {
161 libraryList.addFunctionLibrary(library);
162 }
163
164 /**
165 * Get the host language (XSLT, XQuery, XPath) used to implement the code in this container
166 *
167 * @return the value {@link net.sf.saxon.Configuration#XPATH}
168 */
169
170 public int getHostLanguage() {
171 return Configuration.XPATH;
172 }
173
174 /**
175 * Construct a dynamic context for early evaluation of constant subexpressions
176 */
177
178 public XPathContext makeEarlyEvaluationContext() {
179 return new EarlyEvaluationContext(getConfiguration());
180 }
181
182
183 public Location getContainingLocation() {
184 return containingLocation;
185 }
186
187 /**
188 * Set the containing location, which represents the location of the outermost expression using this
189 * static context (typically, subexpressions will have a nested location that refers to this outer
190 * containing location)
191 *
192 * @param location the location map to be used
193 */
194
195 public void setContainingLocation(Location location) {
196 containingLocation = location;
197 }
198
199 /**
200 * Set the base URI in the static context
201 *
202 * @param baseURI the base URI of the expression; the value null is allowed to indicate that the base URI is not available.
203 */
204
205 public void setBaseURI(String baseURI) {
206 this.baseURI = baseURI;
207 }
208
209 /**
210 * Get the Base URI, for resolving any relative URI's used
211 * in the expression. Used by the document() function, resolve-uri(), etc.
212 *
213 * @return "" if no base URI has been set
214 */
215
216 public String getStaticBaseURI() {
217 return baseURI == null ? "" : baseURI;
218 }
219
220 /**
221 * Get the function library containing all the in-scope functions available in this static
222 * context. This method is called by the XPath parser when binding a function call in the
223 * XPath expression to an implementation of the function.
224 */
225
226 public FunctionLibrary getFunctionLibrary() {
227 return libraryList;
228 }
229
230 /**
231 * Set the function library to be used
232 *
233 * @param lib the function library
234 */
235
236 public void setFunctionLibrary(FunctionLibraryList lib) {
237 libraryList = lib;
238 usingDefaultFunctionLibrary = false;
239 }
240
241 /**
242 * Declare a named collation
243 *
244 * @param name The name of the collation (technically, a URI)
245 * @param comparator The StringCollator used to implement the collating sequence
246 * @param isDefault True if this is to be used as the default collation
247 * @deprecated since 9.6. All collations are now registered at the level of the
248 * Configuration. If this method is called, the effect is (a) the supplied collation
249 * is registered with the configuration, and (b) if isDefault=true, the collation
250 * becomes the default collation for this static context
251 */
252
253 public void declareCollation(String name, StringCollator comparator, boolean isDefault) {
254 getConfiguration().registerCollation(name, comparator);
255 if (isDefault) {
256 defaultCollationName = name;
257 }
258 }
259
260 /**
261 * Set the name of the default collation for this static context.
262 * @param collationName the name of the default collation
263 */
264
265 public void setDefaultCollationName(String collationName) {
266 defaultCollationName = collationName;
267 }
268
269 /**
270 * Get the name of the default collation.
271 *
272 * @return the name of the default collation; or the name of the codepoint collation
273 * if no default collation has been defined
274 */
275
276 public String getDefaultCollationName() {
277 return defaultCollationName;
278 }
279
280 /**
281 * Issue a compile-time warning. This method is used during XPath expression compilation to
282 * output warning conditions. The default implementation writes the message to the
283 * error listener registered with the Configuration.
284 */
285
286 public void issueWarning(String s, Location locator) {
287 XPathException ex = new XPathException(s);
288 ex.setLocation(locator);
289 config.getErrorListener().warning(ex);
290 }
291
292 /**
293 * Get the system ID of the container of the expression. Used to construct error messages.
294 *
295 * @return "" always
296 */
297
298 public String getSystemId() {
299 return "";
300 }
301
302
303 /**
304 * Get the default namespace URI for elements and types
305 * Return NamespaceConstant.NULL (that is, the zero-length string) for the non-namespace
306 *
307 * @return the default namespace for elements and type
308 */
309
310 public String getDefaultElementNamespace() {
311 return defaultElementNamespace;
312 }
313
314 /**
315 * Set the default namespace for elements and types
316 *
317 * @param uri the namespace to be used for unprefixed element and type names.
318 * The value "" (or NamespaceConstant.NULL) represents the non-namespace
319 */
320
321 public void setDefaultElementNamespace(String uri) {
322 defaultElementNamespace = uri;
323 }
324
325 /**
326 * Set the default function namespace
327 *
328 * @param uri the namespace to be used for unprefixed function names.
329 * The value "" (or NamespaceConstant.NULL) represents the non-namespace
330 */
331
332 public void setDefaultFunctionNamespace(String uri) {
333 defaultFunctionNamespace = uri;
334 }
335
336 /**
337 * Get the default function namespace.
338 * The value "" (or NamespaceConstant.NULL) represents the non-namespace
339 *
340 * @return the default namesapce for functions
341 */
342
343 public String getDefaultFunctionNamespace() {
344 return defaultFunctionNamespace;
345 }
346
347 /**
348 * Set the XPath language level supported.
349 * The current levels supported are 20 (=2.0) and 31 (=3.1). The default is 3.1.
350 *
351 * @param level the XPath language level
352 * @since 9.3. From 9.8 this only affects the XPath syntax that is accepted;
353 * it does not affect the function library that is available, which must be
354 * set separately using {@link #setFunctionLibrary(FunctionLibraryList)}
355 */
356
357 public void setXPathLanguageLevel(int level) {
358 xpathLanguageLevel = level;
359 }
360
361 /**
362 * Get the XPath language level supported, as an integer (being the actual version
363 * number times ten). In Saxon 9.9 the possible values are 20 (XPath 2.0), 30 (XPath 3.0),
364 * 31 (XPath 3.1), and 305 (XPath 3.0 plus the extensions defined in XSLT 3.0).
365 *
366 * @return the XPath language level; the return value will be either 20, 30, 305, or 31
367 * @since 9.7
368 */
369
370 public int getXPathVersion() {
371 return xpathLanguageLevel;
372 }
373
374 /**
375 * Set XPath 1.0 backwards compatibility mode on or off
376 *
377 * @param option true if XPath 1.0 compatibility mode is to be set to true;
378 * otherwise false
379 */
380
381 public void setBackwardsCompatibilityMode(boolean option) {
382 backwardsCompatible = option;
383 }
384
385 /**
386 * Determine whether Backwards Compatible Mode is used
387 *
388 * @return true if XPath 1.0 compatibility mode is to be set to true;
389 * otherwise false
390 */
391
392 public boolean isInBackwardsCompatibleMode() {
393 return backwardsCompatible;
394 }
395
396 /**
397 * Set the DecimalFormatManager used to resolve the names of decimal formats used in calls
398 * to the format-number() function.
399 *
400 * @param manager the decimal format manager for this static context, or null if no named decimal
401 * formats are available in this environment.
402 */
403
404 public void setDecimalFormatManager(DecimalFormatManager manager) {
405 getPackageData().setDecimalFormatManager(manager);
406 }
407
408 /**
409 * Get the required type of the context item. If no type has been explicitly declared for the context
410 * item, an instance of AnyItemType (representing the type item()) is returned.
411 *
412 * @return the required type of the context item
413 * @since 9.3
414 */
415
416 public ItemType getRequiredContextItemType() {
417 return AnyItemType.getInstance();
418 }
419
420 /**
421 * Get a DecimalFormatManager to resolve the names of decimal formats used in calls
422 * to the format-number() function.
423 *
424 * @return the decimal format manager for this static context; a newly created empty
425 * DecimalFormatManager if none has been supplied
426 * @since 9.2
427 */
428
429 public DecimalFormatManager getDecimalFormatManager() {
430 DecimalFormatManager manager = getPackageData().getDecimalFormatManager();
431 if (manager == null) {
432 manager = new DecimalFormatManager(Configuration.XPATH, xpathLanguageLevel);
433 getPackageData().setDecimalFormatManager(manager);
434 }
435 return manager;
436 }
437
438 /**
439 * Get the KeyManager, containing definitions of keys available for use.
440 *
441 * @return the KeyManager. This is used to resolve key names, both explicit calls
442 * on key() used in XSLT, and system-generated calls on key() which may
443 * also appear in XQuery and XPath
444 */
445 public KeyManager getKeyManager() {
446 return getPackageData().getKeyManager();
447 }
448
449 /**
450 * Register an alias for an ItemType. This is a Saxon extension. If {@code typename}
451 * has been registered as an alias for, say, map{xs:string, xs:integer*}, then
452 * the syntax {@code ~typename} is accepted anywhere this ItemType would
453 * be accepted.
454 */
455
456 public void setTypeAlias(StructuredQName name, ItemType type) {
457 typeAliases.put(name, type);
458 }
459
460 /**
461 * Get type alias. This is a Saxon extension. A type alias is a QName which can
462 * be used as a shorthand for an itemtype, using the syntax ~typename anywhere that
463 * an item type is permitted.
464 *
465 * @param typeName the name of the type alias
466 * @return the corresponding item type, if the name is recognised; otherwise null.
467 */
468
469 @Override
470 public ItemType resolveTypeAlias(StructuredQName typeName) {
471 return typeAliases.get(typeName);
472 }
473 }
474