Debian Codegraph @

net/sf/saxon/style/UseWhenStaticContext.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.style;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.expr.Expression;
12 import net.sf.saxon.expr.Literal;
13 import net.sf.saxon.expr.parser.Location;
14 import net.sf.saxon.expr.parser.RetainedStaticContext;
15 import net.sf.saxon.functions.FunctionLibrary;
16 import net.sf.saxon.functions.FunctionLibraryList;
17 import net.sf.saxon.functions.registry.ConstructorFunctionLibrary;
18 import net.sf.saxon.lib.NamespaceConstant;
19 import net.sf.saxon.om.*;
20 import net.sf.saxon.sxpath.AbstractStaticContext;
21 import net.sf.saxon.trans.DecimalFormatManager;
22 import net.sf.saxon.trans.XPathException;
23 import net.sf.saxon.type.ItemType;
24
25 import java.util.Collections;
26 import java.util.Set;
27
28 /**
29 * This class implements the static context used for evaluating use-when and other
30 * static expressions in XSLT 3.0
31 * A new instance of this class is created for each use-when expression encountered; there are
32 * therefore no issues with reusability. The class provides a Container for the expression as well
33 * as the static context information; the Executable contains the single XPath expression only, and
34 * is created for the purpose.
35 */
36
37 public class UseWhenStaticContext extends AbstractStaticContext implements XSLTStaticContext {
38
39 private NamespaceResolver namespaceContext;
40 private FunctionLibrary functionLibrary;
41 private Compilation compilation;
42
43 /**
44 * Create a static context for evaluating use-when expressions
45 *
46 * @param compilation the package compilation episode
47 * @param namespaceContext the namespace context in which the static expression appears
48 */
49
50 public UseWhenStaticContext(Compilation compilation, NamespaceResolver namespaceContext) {
51 Configuration config = compilation.getConfiguration();
52 setConfiguration(config);
53 this.compilation = compilation;
54 setPackageData(compilation.getPackageData());
55 this.namespaceContext = namespaceContext;
56 setXPathLanguageLevel(31);
57
58 FunctionLibraryList lib = new FunctionLibraryList();
59 lib.addFunctionLibrary(config.getUseWhenFunctionSet());
60 lib.addFunctionLibrary(getConfiguration().getBuiltInExtensionLibraryList());
61 lib.addFunctionLibrary(new ConstructorFunctionLibrary(getConfiguration()));
62 lib.addFunctionLibrary(config.getIntegratedFunctionLibrary());
63 config.addExtensionBinders(lib);
64 functionLibrary = lib;
65 }
66
67 /**
68 * Construct a RetainedStaticContext, which extracts information from this StaticContext
69 * to provide the subset of static context information that is potentially needed
70 * during expression evaluation
71 *
72 * @return a RetainedStaticContext object: either a newly created one, or one that is
73 * reused from a previous invocation.
74 */
75 public RetainedStaticContext makeRetainedStaticContext() {
76 return new RetainedStaticContext(this);
77 }
78
79
80 public Compilation getCompilation() {
81 return compilation;
82 }
83
84 /**
85 * Issue a compile-time warning
86 */
87
88 public void issueWarning(String s, Location locator) {
89 XPathException err = new XPathException(s);
90 err.setLocator(locator);
91 getConfiguration().getErrorListener().warning(err);
92 }
93
94 /**
95 * Get the System ID of the container of the expression. This is the containing
96 * entity (file) and is therefore useful for diagnostics. Use getBaseURI() to get
97 * the base URI, which may be different.
98 */
99
100 public String getSystemId() {
101 return getStaticBaseURI();
102 }
103
104 /**
105 * Bind a variable used in this element to its declaration
106 *
107 * @param qName the name of the variable
108 * @return an expression representing the variable reference, This will often be
109 * a {@link net.sf.saxon.expr.VariableReference}, suitably initialized to refer to the corresponding variable declaration,
110 * but in general it can be any expression which returns the variable's value when evaluated. In this version of the method,
111 * the value of the variable is known statically, so the returned expression is a literal containing the variable's value.
112 */
113
114 public Expression bindVariable(StructuredQName qName) throws XPathException {
115 GroundedValue<?> val = compilation.getStaticVariable(qName);
116 if (val != null) {
117 return Literal.makeLiteral(val);
118 } else {
119 XPathException err = new XPathException
120 ("Variables (other than XSLT 3.0 static variables) cannot be used in a static expression: " +
121 qName.getDisplayName());
122 err.setErrorCode("XPST0008");
123 err.setIsStaticError(true);
124 throw err;
125 }
126 }
127
128 /**
129 * Get the function library containing all the in-scope functions available in this static
130 * context
131 */
132
133 public FunctionLibrary getFunctionLibrary() {
134 return functionLibrary;
135 }
136
137 /**
138 * Get the name of the default collation.
139 *
140 * @return the name of the default collation; or the name of the codepoint collation
141 * if no default collation has been defined
142 */
143
144 public String getDefaultCollationName() {
145 return NamespaceConstant.CODEPOINT_COLLATION_URI;
146 }
147
148 /**
149 * Get the default function namespace
150 */
151
152 public String getDefaultFunctionNamespace() {
153 return NamespaceConstant.FN;
154 }
155
156 /**
157 * Determine whether Backwards Compatible Mode is used
158 */
159
160 public boolean isInBackwardsCompatibleMode() {
161 return false;
162 }
163
164 /**
165 * Determine whether a Schema for a given target namespace has been imported. Note that the
166 * in-scope element declarations, attribute declarations and schema types are the types registered
167 * with the (schema-aware) configuration, provided that their namespace URI is registered
168 * in the static context as being an imported schema namespace. (A consequence of this is that
169 * within a Configuration, there can only be one schema for any given namespace, including the
170 * null namespace).
171 */
172
173 public boolean isImportedSchema(String namespace) {
174 return false;
175 }
176
177 /**
178 * Get the set of imported schemas
179 *
180 * @return a Set, the set of URIs representing the names of imported schemas
181 */
182
183 public Set<String> getImportedSchemaNamespaces() {
184 return Collections.emptySet();
185 }
186
187 /**
188 * Get a namespace resolver to resolve the namespaces declared in this static context.
189 *
190 * @return a namespace resolver.
191 */
192
193 public NamespaceResolver getNamespaceResolver() {
194 return namespaceContext;
195 }
196
197 /**
198 * Get a DecimalFormatManager to resolve the names of decimal formats used in calls
199 * to the format-number() function.
200 *
201 * @return the decimal format manager for this static context, or null if named decimal
202 * formats are not supported in this environment.
203 */
204
205 public DecimalFormatManager getDecimalFormatManager() {
206 return null;
207 }
208
209 /**
210 * Determine if an extension element is available
211 *
212 * @throws net.sf.saxon.trans.XPathException
213 * if the name is invalid or the prefix is not declared
214 */
215
216 public boolean isElementAvailable(String qname) throws XPathException {
217 try {
218 String[] parts = NameChecker.getQNameParts(qname);
219 String uri;
220 if (parts[0].isEmpty()) {
221 uri = getDefaultElementNamespace();
222 } else {
223 uri = namespaceContext.getURIForPrefix(parts[0], false);
224 }
225 StyleNodeFactory factory = getConfiguration().makeStyleNodeFactory(compilation);
226 return factory.isElementAvailable(uri, parts[1], true);
227 } catch (QNameException e) {
228 XPathException err = new XPathException("Invalid element name. " + e.getMessage());
229 err.setErrorCode("XTDE1440");
230 throw err;
231 }
232 }
233
234 public int getColumnNumber() {
235 return 0;
236 }
237
238 public String getPublicId() {
239 return null;
240 }
241
242 public int getLineNumber() {
243 return -1;
244 }
245
246 /**
247 * Get type alias. This is a Saxon extension. A type alias is a QName which can
248 * be used as a shorthand for an itemtype, using the syntax ~typename anywhere that
249 * an item type is permitted.
250 *
251 * @param typeName the name of the type alias
252 * @return the corresponding item type, if the name is recognised; otherwise null.
253 * This implementation always returns null (type aliases cannot be used in XSLT static
254 * expressions).
255 */
256 @Override
257 public ItemType resolveTypeAlias(StructuredQName typeName) {
258 return null;
259 }
260
261 }
262