Debian Codegraph @

net/sf/saxon/expr/instruct/GlobalParameterSet.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.instruct;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.expr.XPathContext;
12 import net.sf.saxon.expr.parser.ExplicitLocation;
13 import net.sf.saxon.expr.parser.RoleDiagnostic;
14 import net.sf.saxon.expr.parser.TypeChecker;
15 import net.sf.saxon.om.*;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.value.SequenceType;
18
19 import java.util.Collection;
20 import java.util.HashMap;
21
22
23 /**
24 * A GlobalParameterSet is a set of parameters supplied when invoking a stylesheet or query.
25 * It is a collection of name-value pairs, the names being represented by StructuredQName objects.
26 * The values are objects, as supplied by the caller: conversion of the object
27 * to a required type takes place when the parameter is actually used.
28 */
29
30 public class GlobalParameterSet {
31 private HashMap<StructuredQName, GroundedValue<?>> params =
32 new HashMap<>(10);
33
34 /**
35 * Create an empty parameter set
36 */
37
38 public GlobalParameterSet() {
39 }
40
41 /**
42 * Create a parameter set as a copy of an existing parameter set
43 * @param parameterSet the parameter set to be copied
44 */
45
46 public GlobalParameterSet(GlobalParameterSet parameterSet) {
47 this.params = new HashMap<>(parameterSet.params);
48 }
49
50 /**
51 * Add a parameter to the ParameterSet
52 *
53 * @param qName The fingerprint of the parameter name.
54 * @param value The value of the parameter, or null if the parameter is to be removed
55 */
56
57 public void put(StructuredQName qName, GroundedValue<?> value) {
58 if (value == null) {
59 params.remove(qName);
60 } else {
61 params.put(qName, value);
62 }
63 }
64
65 /**
66 * Get a parameter
67 *
68 * @param qName The parameter name.
69 * @return The value of the parameter, or null if not defined
70 */
71
72 public GroundedValue<?> get(StructuredQName qName) {
73 return params.get(qName);
74 }
75
76 public boolean containsKey(StructuredQName qName) {
77 return params.containsKey(qName);
78 }
79
80 /**
81 * Get the value of a parameter, converted to the required type, or checked against the
82 * required type if no type conversion is in force
83 *
84 * @param qName the name of the parameter
85 * @param requiredType the required type of the parameter
86 * @param convert set to true if function conversion rules are to be applied, or to false if the value
87 * is simply to be checked against the required type
88 * @param context dynamic evaluation context
89 * @return the value after conversion and type checking; or null if there is no value for this parameter
90 * @throws XPathException if the value is of the wrong type
91 */
92
93 public GroundedValue<?> convertParameterValue(
94 StructuredQName qName, SequenceType requiredType, boolean convert, XPathContext context)
95 throws XPathException {
96 Sequence<?> val = get(qName);
97 if (val == null) {
98 return null;
99 }
100
101 if (requiredType != null) {
102 if (convert) {
103 RoleDiagnostic role = new RoleDiagnostic(RoleDiagnostic.VARIABLE, qName.getDisplayName(), -1);
104 Configuration config = context.getConfiguration();
105 val = config.getTypeHierarchy().applyFunctionConversionRules(
106 val, requiredType, role, ExplicitLocation.UNKNOWN_LOCATION);
107 } else {
108 XPathException err = TypeChecker.testConformance(val, requiredType, context);
109 if (err != null) {
110 throw err;
111 }
112 }
113 }
114 return val.materialize();
115 }
116
117
118 /**
119 * Clear all values
120 */
121
122 public void clear() {
123 params.clear();
124 }
125
126 /**
127 * Get all the keys that have been allocated
128 *
129 * @return the names of the parameter keys (QNames)
130 */
131
132 public Collection<StructuredQName> getKeys() {
133 return params.keySet();
134 }
135
136 /**
137 * Get the number of entries in the result of getKeys() that are significant
138 *
139 * @return the number of entries
140 */
141
142 public int getNumberOfKeys() {
143 return params.size();
144 }
145
146 }