Debian Codegraph @

net/sf/saxon/expr/parser/RetainedStaticContext.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.expr.parser;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.Version;
12 import net.sf.saxon.expr.PackageData;
13 import net.sf.saxon.expr.StaticContext;
14 import net.sf.saxon.lib.NamespaceConstant;
15 import net.sf.saxon.om.NamespaceResolver;
16 import net.sf.saxon.trans.DecimalFormatManager;
17 import net.sf.saxon.trans.XPathException;
18
19 import java.net.URI;
20 import java.net.URISyntaxException;
21 import java.util.*;
22
23 /**
24 * This class contains the part of the static context of expressions that (a) can change from one expression
25 * to another within a query or stylesheet, and (b) are potentially needed at run-time.
26 * <p>From 9.6, the retained static context is available to every expression in the expression tree (previously,
27 * expressions only retained that part of the static context needed by the particular expression). For economy,
28 * a new RetainedStaticContext object is only created when the context changes: which is fairly rare (for
29 * example, it never happens within an XPath expression).</p>
30 */
31 public class RetainedStaticContext implements NamespaceResolver {
32
33 private Configuration config;
34 private PackageData packageData;
35 private URI staticBaseUri;
36 private String staticBaseUriString;
37 private String defaultCollationName;
38 private Map<String, String> namespaces;
39 private String defaultFunctionNamespace = NamespaceConstant.FN;
40 private String defaultElementNamespace;
41 private DecimalFormatManager decimalFormatManager;
42 private boolean backwardsCompatibility;
43
44
45 public RetainedStaticContext(Configuration config) {
46 this.config = config;
47 packageData = new PackageData(config);
48 namespaces = new HashMap<>();
49 defaultCollationName = NamespaceConstant.CODEPOINT_COLLATION_URI;
50 }
51
52 // This is used by copyDefaultURI()
53 private RetainedStaticContext(Map<String, String> namespaces,
54 String defaultElementNamespace) {
55 this.namespaces = namespaces;
56 this.defaultElementNamespace = defaultElementNamespace;
57 this.defaultCollationName = NamespaceConstant.CODEPOINT_COLLATION_URI;
58 }
59
60 public RetainedStaticContext(StaticContext sc) {
61 this.config = sc.getConfiguration();
62 this.packageData = sc.getPackageData();
63 if (sc.getStaticBaseURI() != null) {
64 staticBaseUriString = sc.getStaticBaseURI();
65 try {
66 this.staticBaseUri = ExpressionTool.getBaseURI(sc, null, true);
67 } catch (XPathException e) {
68 staticBaseUri = null;
69 }
70 }
71 this.defaultCollationName = sc.getDefaultCollationName();
72 this.decimalFormatManager = sc.getDecimalFormatManager();
73 this.defaultElementNamespace = sc.getDefaultElementNamespace();
74 defaultFunctionNamespace = sc.getDefaultFunctionNamespace();
75 backwardsCompatibility = sc.isInBackwardsCompatibleMode();
76 if (Version.platform.JAXPStaticContextCheck(this, sc)) {
77 //updated in method
78 } else {
79 this.namespaces = new HashMap<>(4);
80 NamespaceResolver resolver = sc.getNamespaceResolver();
81 for (Iterator<String> it = resolver.iteratePrefixes(); it.hasNext(); ) {
82 String prefix = it.next();
83 if (!prefix.equals("xml")) {
84 namespaces.put(prefix, resolver.getURIForPrefix(prefix, true));
85 }
86 }
87 }
88
89 }
90
91
92 /**
93 * Get the Configuration
94 *
95 * @return the Saxon Configuration object
96 */
97
98 public Configuration getConfiguration() {
99 return config;
100 }
101
102 /**
103 * Set information about the package (unit of compilation
104 *
105 * @param packageData the package data
106 */
107
108 public void setPackageData(PackageData packageData) {
109 this.packageData = packageData;
110 }
111
112 /**
113 * Get information about the package (unit of compilation)
114 *
115 * @return the package data
116 */
117
118 public PackageData getPackageData() {
119 return packageData;
120 }
121
122 /**
123 * Set the static base URI as a string
124 *
125 * @param baseUri the base URI as a string
126 */
127
128 public void setStaticBaseUriString(String baseUri) {
129 if (baseUri != null) {
130 staticBaseUriString = baseUri;
131 try {
132 this.staticBaseUri = new URI(baseUri);
133 } catch (URISyntaxException e) {
134 staticBaseUri = null;
135 }
136 }
137 }
138
139 /**
140 * Get the static base URI as a URI.
141 *
142 * @return the static base URI as a URI if it is known and valid. Return null if the base URI is
143 * unknown.
144 * @throws XPathException if the static base URI is not a valid URI.
145 */
146
147 public URI getStaticBaseUri() throws XPathException {
148 if (staticBaseUri == null) {
149 if (staticBaseUriString == null) {
150 return null;
151 } else {
152 throw new XPathException("Supplied static base URI " + staticBaseUriString + " is not a valid URI");
153 }
154 }
155 return staticBaseUri;
156 }
157
158 /**
159 * Get the static base URI as a string.
160 *
161 * @return the static base URI as a string, or null if unknown
162 */
163
164 public String getStaticBaseUriString() {
165 return staticBaseUriString;
166 }
167
168 /**
169 * Get the name of the default collation for this static context
170 *
171 * @return the default collation URI
172 */
173
174 public String getDefaultCollationName() {
175 return defaultCollationName;
176 }
177
178 /**
179 * Set the name of the default collation for this static context
180 *
181 * @param defaultCollationName the default collation URI
182 */
183
184 public void setDefaultCollationName(String defaultCollationName) {
185 this.defaultCollationName = defaultCollationName;
186 }
187
188 /**
189 * Get the default namespace for functions
190 *
191 * @return the default namespace for functions
192 */
193
194 public String getDefaultFunctionNamespace() {
195 return defaultFunctionNamespace;
196 }
197
198 /**
199 * Set the default namespace for functions
200 *
201 * @param defaultFunctionNamespace the default namespace for functions
202 */
203
204 public void setDefaultFunctionNamespace(String defaultFunctionNamespace) {
205 this.defaultFunctionNamespace = defaultFunctionNamespace;
206 }
207
208 /**
209 * Get the default namespace for elements and types
210 *
211 * @return the default namespace for elements and types. Return "" if the default is "no namespace"
212 */
213
214 public String getDefaultElementNamespace() {
215 return defaultElementNamespace == null ? "" : defaultElementNamespace;
216 }
217
218 /**
219 * Set the default namespace for elements and type
220 *
221 * @param ns the default namespace for elements and types.
222 */
223
224 public void setDefaultElementNamespace(String ns) {
225 defaultElementNamespace = ns;
226 }
227
228 /**
229 * Get the decimal format manager
230 *
231 * @return the decimal format manager
232 */
233
234 public DecimalFormatManager getDecimalFormatManager() {
235 return decimalFormatManager;
236 }
237
238 /**
239 * Set the decimal format manager
240 *
241 * @param decimalFormatManager the decimal format manager
242 */
243 public void setDecimalFormatManager(DecimalFormatManager decimalFormatManager) {
244 this.decimalFormatManager = decimalFormatManager;
245 }
246
247 public boolean isBackwardsCompatibility() {
248 return backwardsCompatibility;
249 }
250
251 public void setBackwardsCompatibility(boolean backwardsCompatibility) {
252 this.backwardsCompatibility = backwardsCompatibility;
253 }
254
255
256 /**
257 * Add a namespace binding to the static namespace context ("in-scope namespaces")
258 *
259 * @param prefix the namespace prefix
260 * @param uri the namespace URI
261 */
262
263 public void declareNamespace(String prefix, String uri) {
264 namespaces.put(prefix, uri);
265 }
266
267 /**
268 * Get the namespace URI corresponding to a given prefix. Return null
269 * if the prefix is not in scope.
270 *
271 * @param prefix the namespace prefix. May be the zero-length string, indicating
272 * that there is no prefix. This indicates either the default namespace or the
273 * null namespace, depending on the value of useDefault.
274 * @param useDefault true if the default namespace is to be used when the
275 * prefix is "". If set to false, and the prefix is "", then the
276 * value "" is returned regardless of the default namespace in force.
277 * The "default namespace" here means the one bound to the prefix "".
278 * @return the uri for the namespace, or null if the prefix is not in scope.
279 * The "null namespace" is represented by the pseudo-URI "".
280 */
281 public String getURIForPrefix(String prefix, boolean useDefault) {
282 if (prefix.isEmpty()) {
283 if (useDefault) {
284 String u = namespaces.get(prefix);
285 return u == null ? "" : u;
286 } else {
287 return "";
288 }
289 }
290 if (prefix.equals("xml")) {
291 return NamespaceConstant.XML;
292 }
293 return namespaces.get(prefix);
294 }
295
296 /**
297 * Get an iterator over all the prefixes declared in this namespace context. This will include
298 * the default namespace (prefix="") and the XML namespace where appropriate
299 *
300 * @return an iterator over all the prefixes for which a namespace binding exists, including
301 * the zero-length string to represent the null/absent prefix if it is bound
302 */
303 public Iterator<String> iteratePrefixes() {
304 return namespaces.keySet().iterator();
305 }
306
307 /**
308 * Test whether this static context declares the same namespaces as another static context
309 * @return true if the namespace bindings (prefix:uri pairs) are the same
310 */
311
312 public boolean declaresSameNamespaces(RetainedStaticContext other) {
313 return namespaces.equals(other.namespaces);
314 }
315
316 public int hashCode() {
317 int h = 0x8457cbce;
318 if (staticBaseUriString != null) {
319 h ^= staticBaseUriString.hashCode();
320 }
321 h ^= defaultCollationName.hashCode();
322 h ^= defaultFunctionNamespace.hashCode();
323 h ^= namespaces.hashCode();
324 return h;
325 }
326
327
328 public boolean equals(Object other) {
329 if (!(other instanceof RetainedStaticContext)) {
330 return false;
331 }
332 RetainedStaticContext r = (RetainedStaticContext) other;
333 return ExpressionTool.equalOrNull(staticBaseUriString, r.staticBaseUriString)
334 && defaultCollationName.equals(r.defaultCollationName)
335 && defaultFunctionNamespace.equals(r.defaultFunctionNamespace)
336 && namespaces.equals(r.namespaces);
337 }
338
339
340 public void setNamespaces(Map<String,String> namespaces) {
341 this.namespaces = namespaces;
342 }
343 }
344