Debian Codegraph @

net/sf/saxon/expr/UnionConstructorFunction.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;
9
10 import net.sf.saxon.functions.AbstractFunction;
11 import net.sf.saxon.lib.ConversionRules;
12 import net.sf.saxon.om.*;
13 import net.sf.saxon.trans.XPathException;
14 import net.sf.saxon.type.*;
15 import net.sf.saxon.value.AnyURIValue;
16 import net.sf.saxon.value.AtomicValue;
17 import net.sf.saxon.value.SequenceType;
18 import net.sf.saxon.value.StringValue;
19
20 /**
21 * Function to perform a cast to a union type
22 */
23 public class UnionConstructorFunction extends AbstractFunction {
24
25 protected UnionType targetType;
26 protected NamespaceResolver resolver;
27 protected boolean allowEmpty;
28
29 /**
30 * Create a cast expression to a union type
31 * @param targetType the union type that is the result of the cast
32 * @param allowEmpty true if an empty sequence may be supplied as input, converting to an empty sequence on output
33 */
34
35 public UnionConstructorFunction(UnionType targetType, NamespaceResolver resolver, boolean allowEmpty) {
36 this.targetType = targetType;
37 this.resolver = resolver;
38 this.allowEmpty = allowEmpty;
39 }
40
41 /**
42 * Get the usage (in terms of streamability analysis) of the single operand
43 * @return the operand usage
44 */
45
46 protected OperandRole getOperandRole() {
47 return OperandRole.SINGLE_ATOMIC;
48 }
49
50 /**
51 * Ask whether the value of the operand is allowed to be empty
52 * @return true if an empty sequence may be supplied as input, converting to an empty sequence on output
53 */
54
55 public boolean isAllowEmpty() {
56 return allowEmpty;
57 }
58
59 /**
60 * Get the union type that is the target of this cast operation
61 * @return the target type of the cast
62 */
63
64 public UnionType getTargetType() {
65 return targetType;
66 }
67
68 /**
69 * Get the namespace resolver that will be used to resolve any namespace-sensitive values (such as QNames) when casting
70 * @return the namespace resolver, or null if there is none.
71 */
72
73 public NamespaceResolver getNamespaceResolver() {
74 return resolver;
75 }
76
77 /**
78 * Get the item type of the function item
79 *
80 * @return the function item's type
81 */
82 public FunctionItemType getFunctionItemType() {
83 SequenceType resultType = targetType.getResultTypeOfCast();
84
85 SequenceType argType = allowEmpty ? SequenceType.OPTIONAL_ATOMIC : SequenceType.SINGLE_ATOMIC;
86
87 return new SpecificFunctionType(
88 new SequenceType[]{argType}, resultType);
89 }
90
91 /**
92 * Get the name of the function, or null if it is anonymous
93 *
94 * @return the function name, or null for an anonymous inline function
95 */
96 public StructuredQName getFunctionName() {
97 return targetType.getStructuredQName();
98 }
99
100 /**
101 * Get a description of this function for use in error messages. For named functions, the description
102 * is the function name (as a lexical QName). For others, it might be, for example, "inline function",
103 * or "partially-applied ends-with function".
104 *
105 * @return a description of the function for use in error messages
106 */
107 public String getDescription() {
108 return getFunctionName().getDisplayName();
109 }
110
111 /**
112 * Get the arity of the function
113 *
114 * @return the number of arguments in the function signature
115 */
116 public int getArity() {
117 return 1;
118 }
119
120
121 /**
122 * Method to perform the cast of an atomic value to a union type
123 *
124 * @param value the input value to be converted. Must not be null.
125 * @param context the evaluation context
126 * @return the result of the conversion (may be a sequence if the union includes list types in its membership)
127 * @throws net.sf.saxon.trans.XPathException if the conversion fails
128 */
129
130 public AtomicSequence cast(AtomicValue value, XPathContext context)
131 throws XPathException {
132 ConversionRules rules = context.getConfiguration().getConversionRules();
133 if (value == null) {
134 throw new NullPointerException();
135 }
136
137 // 1. If the value is a string or untypedAtomic, try casting to each of the member types
138
139 if (value instanceof StringValue && !(value instanceof AnyURIValue)) {
140 try {
141 return targetType.getTypedValue(value.getStringValueCS(), resolver, rules);
142 } catch (ValidationException e) {
143 e.setErrorCode("FORG0001");
144 throw e;
145 }
146 }
147
148 // 2. If the value is an instance of a type in the transitive membership of the union, return it unchanged
149
150 AtomicType label = value.getItemType();
151 Iterable<PlainType> memberTypes = targetType.getPlainMemberTypes();
152
153 // 2a. Is the type annotation itself a member type of the union, and of the union type itself?
154 if (targetType.isPlainType()) {
155 for (PlainType member : memberTypes) {
156 if (label.equals(member)) {
157 return value;
158 }
159 }
160 // 2b. Failing that, is some supertype of the type annotation a member type of the union?
161 for (PlainType member : memberTypes) {
162 AtomicType t = label;
163 while (t != null) {
164 if (t.equals(member)) {
165 return value;
166 } else {
167 t = t.getBaseType() instanceof AtomicType ? (AtomicType) t.getBaseType() : null;
168 }
169 }
170 }
171 }
172
173 // 3. if the value can be cast to any of the member types, return the result of that cast
174
175 for (PlainType type : memberTypes) {
176 if (type instanceof AtomicType) {
177 Converter c = rules.getConverter(value.getItemType(), (AtomicType) type);
178 if (c != null) {
179 ConversionResult result = c.convert(value);
180 if (result instanceof AtomicValue) {
181 // 3b. if the union type has constraining facets then the value must satisfy these
182 if (!targetType.isPlainType()) {
183 ValidationFailure vf = targetType.checkAgainstFacets((AtomicValue)result, rules);
184 if (vf == null) {
185 return (AtomicValue) result;
186 }
187 } else {
188 return (AtomicValue) result;
189 }
190 }
191 }
192 }
193 }
194
195 throw new XPathException("Cannot convert the supplied value to " + targetType.getDescription(), "FORG0001");
196 }
197
198 /**
199 * Invoke the function
200 *
201 * @param context the XPath dynamic evaluation context
202 * @param args the actual arguments to be supplied
203 * @return the result of invoking the function
204 * @throws net.sf.saxon.trans.XPathException if a dynamic error occurs within the function
205 */
206 public AtomicSequence call(XPathContext context, Sequence[] args) throws XPathException {
207 AtomicValue val = (AtomicValue) args[0].head();
208 if (val == null) {
209 if (allowEmpty) {
210 return EmptyAtomicSequence.getInstance();
211 } else {
212 XPathException e = new XPathException("Cast expression does not allow an empty sequence to be supplied", "XPTY0004");
213 e.setIsTypeError(true);
214 throw e;
215 }
216 }
217 return cast(val, context);
218 }
219
220 /**
221 * Static method to perform the cast of an atomic value to a union type
222 *
223 * @param value the input value to be converted. Must not be null.
224 * @param targetType the union type to which the value is to be converted
225 * @param nsResolver the namespace context, required if the type is namespace-sensitive
226 * @param rules the conversion rules
227 * @return the result of the conversion (may be a sequence if the union includes list types in its membership)
228 * @throws XPathException if the conversion fails
229 */
230
231 public static AtomicSequence cast(AtomicValue value, UnionType targetType,
232 NamespaceResolver nsResolver, ConversionRules rules)
233 throws XPathException {
234 //ConversionRules rules = context.getConfiguration().getConversionRules();
235 if (value == null) {
236 throw new NullPointerException();
237 }
238
239 // 1. If the value is a string or untypedAtomic, try casting to each of the member types
240
241 if (value instanceof StringValue && !(value instanceof AnyURIValue)) {
242 try {
243 return targetType.getTypedValue(value.getStringValueCS(), nsResolver, rules);
244 } catch (ValidationException e) {
245 e.setErrorCode("FORG0001");
246 throw e;
247 }
248 }
249
250 // 2. If the value is an instance of a type in the transitive membership of the union, return it unchanged
251
252 AtomicType label = value.getItemType();
253 Iterable<PlainType> memberTypes = targetType.getPlainMemberTypes();
254
255 // 2a. Is the type annotation itself a member type of the union?
256 for (PlainType member : memberTypes) {
257 if (label.equals(member)) {
258 return value;
259 }
260 }
261 // 2b. Failing that, is some supertype of the type annotation a member type of the union?
262 for (PlainType member : memberTypes) {
263 AtomicType t = label;
264 while (t != null) {
265 if (t.equals(member)) {
266 return value;
267 } else {
268 t = t.getBaseType() instanceof AtomicType ? (AtomicType) t.getBaseType() : null;
269 }
270 }
271 }
272
273 // 3. if the value can be cast to any of the member types, return the result of that cast
274
275 for (PlainType type : memberTypes) {
276 if (type instanceof AtomicType) {
277 Converter c = rules.getConverter(value.getItemType(), (AtomicType) type);
278 if (c != null) {
279 ConversionResult result = c.convert(value);
280 if (result instanceof AtomicValue) {
281 return (AtomicValue) result;
282 }
283 }
284 }
285 }
286
287 throw new XPathException("Cannot convert the supplied value to " + targetType.getDescription(), "FORG0001");
288 }
289
290
291 }
292
293 // Copyright (c) 2018 Saxonica Limited.
294
295