Debian Codegraph @

net/sf/saxon/expr/UntypedSequenceConverter.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.Configuration;
11 import net.sf.saxon.expr.parser.*;
12 import net.sf.saxon.lib.ConversionRules;
13 import net.sf.saxon.trace.ExpressionPresenter;
14 import net.sf.saxon.trans.XPathException;
15 import net.sf.saxon.type.*;
16 import net.sf.saxon.value.AtomicValue;
17 import net.sf.saxon.value.UntypedAtomicValue;
18
19 /**
20 * An UntypedSequenceConverter is an expression that performs a cast on each member of
21 * a supplied sequence that is an untypedAtomic value, while leaving other items unchanged
22 */
23
24 public final class UntypedSequenceConverter extends AtomicSequenceConverter {
25
26 /**
27 * Constructor
28 *
29 * @param sequence this must be a sequence of atomic values. This is not checked; a ClassCastException
30 * will occur if the precondition is not satisfied.
31 * @param requiredItemType the item type to which all items in the sequence should be converted,
32 * using the rules for "cast as".
33 */
34
35 public UntypedSequenceConverter(Expression sequence, PlainType requiredItemType) {
36 super(sequence, requiredItemType);
37 }
38
39 /**
40 * Create an AtomicSequenceConverter that converts all untypedAtomic values in the input sequence to
41 * a specified target type, while leaving items other than untypedAtomic unchanged
42 *
43 * @param config the Saxon configuration
44 * @param operand the expression that delivers the input sequence
45 * @param requiredItemType the type to which untypedAtomic values should be cast, which must either be an
46 * atomic type or a "plain" union type
47 * @return an AtomicSequenceConverter that performs the required conversion
48 * @throws net.sf.saxon.trans.XPathException
49 * if an error occurs, for example if the target type is namespace-sensitive
50 */
51
52
53 public static UntypedSequenceConverter makeUntypedSequenceConverter(Configuration config, Expression operand, PlainType requiredItemType)
54 throws XPathException {
55 TypeHierarchy th = config.getTypeHierarchy();
56 UntypedSequenceConverter atomicSeqConverter =
57 new UntypedSequenceConverter(operand, requiredItemType);
58 final ConversionRules rules = config.getConversionRules();
59 final Converter untypedConverter;
60 if (((SimpleType) requiredItemType).isNamespaceSensitive()) {
61 throw new XPathException("Cannot convert untyped atomic values to a namespace-sensitive type", "XPTY0117");
62 }
63 if (requiredItemType.isAtomicType()) {
64 untypedConverter = rules.getConverter(BuiltInAtomicType.UNTYPED_ATOMIC, (AtomicType) requiredItemType);
65 } else {
66 untypedConverter = new StringConverter.StringToUnionConverter(requiredItemType, rules);
67 }
68 // source type not known statically; create a converter that decides at run-time
69 Converter converter = new UntypedConverter(rules, untypedConverter);
70 atomicSeqConverter.setConverter(converter);
71 return atomicSeqConverter;
72 }
73
74 /**
75 * A Converter that converts untyped atomic values to the required type, while
76 * leaving other values unchanged
77 */
78
79 public static class UntypedConverter extends Converter {
80 Converter untypedConverter = null;
81
82 /**
83 * Create an UntypedConverter
84 *
85 * @param rules the conversion rules
86 * @param converter the converter to be used in the case where the supplied
87 * value is untypedAtomic
88 */
89
90 public UntypedConverter(ConversionRules rules, Converter converter) {
91 super(rules);
92 untypedConverter = converter;
93 //untypedConverter.setConversionRules(rules);
94
95 }
96
97 /*@NotNull*/
98 @Override
99 public ConversionResult convert(/*@NotNull*/ AtomicValue input) {
100 if (input instanceof UntypedAtomicValue) {
101 return untypedConverter.convert(input);
102 } else {
103 return input;
104 }
105 }
106 }
107
108 public static UntypedSequenceConverter makeUntypedSequenceRejector(Configuration config, final Expression operand, final PlainType requiredItemType) {
109 UntypedSequenceConverter atomicSeqConverter = new UntypedSequenceConverter(operand, requiredItemType);
110 final ConversionRules rules = config.getConversionRules();
111 final Converter untypedConverter = new Converter() {
112 // called when an untyped atomic value is encountered
113 public ConversionResult convert(/*@NotNull*/ AtomicValue input) {
114 ValidationFailure vf = new ValidationFailure(
115 "Implicit conversion of untypedAtomic value to " + requiredItemType.toString() + " is not allowed");
116 vf.setErrorCode("XPTY0117");
117 vf.setLocator(operand.getLocation());
118 return vf;
119 }
120 };
121
122 // source type not known statically; create a converter that decides at run-time
123 Converter converter = new UntypedConverter(rules, untypedConverter);
124 atomicSeqConverter.setConverter(converter);
125 return atomicSeqConverter;
126 }
127
128 @Override
129 public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
130 Expression e2 = super.typeCheck(visitor, contextInfo);
131 if (e2 != this) {
132 return e2;
133 }
134 TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy();
135 Expression base = getBaseExpression();
136 if (th.relationship(base.getItemType(), BuiltInAtomicType.UNTYPED_ATOMIC) == TypeHierarchy.DISJOINT ||
137 base.hasSpecialProperty(StaticProperty.NOT_UNTYPED_ATOMIC)) {
138 // operand cannot return untyped atomic values, so there's nothing to convert
139 return getBaseExpression();
140 }
141 return this;
142 }
143
144 /**
145 * Determine the special properties of this expression
146 *
147 * @return {@link net.sf.saxon.expr.StaticProperty#NO_NODES_NEWLY_CREATED}.
148 */
149
150 public int computeSpecialProperties() {
151 int p = super.computeSpecialProperties();
152 return p | StaticProperty.NO_NODES_NEWLY_CREATED | StaticProperty.NOT_UNTYPED_ATOMIC;
153 }
154
155 /**
156 * Copy an expression. This makes a deep copy.
157 *
158 * @return the copy of the original expression
159 * @param rebindings
160 */
161
162 /*@NotNull*/
163 public Expression copy(RebindingMap rebindings) {
164 UntypedSequenceConverter atomicConverter = new UntypedSequenceConverter(getBaseExpression().copy(rebindings), getRequiredItemType());
165 ExpressionTool.copyLocationInfo(this, atomicConverter);
166 atomicConverter.setConverter(converter);
167 atomicConverter.setRoleDiagnostic(getRoleDiagnostic());
168 return atomicConverter;
169 }
170
171
172 /**
173 * Determine the data type of the items returned by the expression, if possible
174 *
175 * @return a value such as Type.STRING, Type.BOOLEAN, Type.NUMBER, Type.NODE,
176 * or Type.ITEM (meaning not known in advance)
177 */
178
179 /*@NotNull*/
180 public ItemType getItemType() {
181 if (getBaseExpression().getItemType() == BuiltInAtomicType.UNTYPED_ATOMIC) {
182 return getRequiredItemType();
183 } else {
184 TypeHierarchy th = getConfiguration().getTypeHierarchy();
185 return Type.getCommonSuperType(getRequiredItemType(), getBaseExpression().getItemType(), th);
186 }
187 }
188
189 /**
190 * Determine the static cardinality of the expression
191 */
192
193 public int computeCardinality() {
194 return getBaseExpression().getCardinality();
195 }
196
197 /**
198 * Is this expression the same as another expression?
199 */
200
201 public boolean equals(Object other) {
202 return other instanceof UntypedSequenceConverter &&
203 getBaseExpression().isEqual(((UntypedSequenceConverter)other).getBaseExpression());
204 }
205
206 /**
207 * get HashCode for comparing two expressions.
208 */
209
210 @Override
211 public int computeHashCode() {
212 return super.computeHashCode();
213 }
214
215 @Override
216 protected String displayOperator(Configuration config) {
217 return "convertUntyped";
218 }
219
220 /**
221 * Get a name identifying the kind of expression, in terms meaningful to a user.
222 *
223 * @return a name identifying the kind of expression, in terms meaningful to a user.
224 * The name will always be in the form of a lexical XML QName, and should match the name used
225 * in explain() output displaying the expression.
226 */
227 @Override
228 public String getExpressionName() {
229 return "convertUntyped";
230 }
231
232 @Override
233 public String toShortString() {
234 return getBaseExpression().toShortString();
235 }
236
237 /**
238 * Diagnostic print of expression structure. The abstract expression tree
239 * is written to the supplied output destination.
240 */
241
242 public void export(ExpressionPresenter destination) throws XPathException {
243 destination.startElement("cvUntyped", this);
244 destination.emitAttribute("to", getRequiredItemType().toExportString());
245 if (getRoleDiagnostic() != null) {
246 destination.emitAttribute("diag", getRoleDiagnostic().save());
247 }
248 getBaseExpression().export(destination);
249 destination.endElement();
250 }
251
252 }
253