Debian Codegraph @

net/sf/saxon/expr/compat/TypeChecker10.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.compat;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.expr.AtomicSequenceConverter;
12 import net.sf.saxon.expr.Atomizer;
13 import net.sf.saxon.expr.Expression;
14 import net.sf.saxon.expr.FirstItemExpression;
15 import net.sf.saxon.expr.parser.ExpressionVisitor;
16 import net.sf.saxon.expr.parser.RetainedStaticContext;
17 import net.sf.saxon.expr.parser.RoleDiagnostic;
18 import net.sf.saxon.expr.parser.TypeChecker;
19 import net.sf.saxon.functions.SystemFunction;
20 import net.sf.saxon.trans.XPathException;
21 import net.sf.saxon.type.BuiltInAtomicType;
22 import net.sf.saxon.type.ItemType;
23 import net.sf.saxon.type.NumericType;
24 import net.sf.saxon.type.TypeHierarchy;
25 import net.sf.saxon.value.Cardinality;
26 import net.sf.saxon.value.SequenceType;
27
28 /**
29 * This class provides type checking capability with XPath 1.0 backwards compatibility enabled.
30 */
31
32 public class TypeChecker10 extends TypeChecker {
33
34 public TypeChecker10() {
35 }
36
37 public Expression staticTypeCheck(Expression supplied,
38 SequenceType req,
39 RoleDiagnostic role,
40 final ExpressionVisitor visitor) throws XPathException {
41
42 if (supplied.implementsStaticTypeCheck()) {
43 return supplied.staticTypeCheck(req, true, role, visitor);
44 }
45
46 // In a static function call, if XPath 1.0 compatibility mode is true and an argument of a static function is
47 // not of the expected type, then the following conversions are applied sequentially to the argument value V:
48 // (1) If the expected type calls for a single item or optional single item(examples:xs:
49 // string, xs:string ?, xs:untypedAtomic, xs:untypedAtomic ?, node(), node() ?, item(), item() ?),then the value V
50 // is effectively replaced by V[1].
51 // (2) If the expected type is xs:string or xs:string?, then the value V is effectively replaced by fn:string(V).
52 // (3) If the expected type is xs:double or xs:double?,then the value V is effectively replaced by fn:number(V).
53 // We interpret this as including xs:numeric so that the intended effect is achieved with functions such as fn:floor().
54
55 final Configuration config = visitor.getConfiguration();
56 final TypeHierarchy th = config.getTypeHierarchy();
57
58 // rule 1
59 if (!Cardinality.allowsMany(req.getCardinality()) && Cardinality.allowsMany(supplied.getCardinality())) {
60 Expression cexp = FirstItemExpression.makeFirstItemExpression(supplied);
61 cexp.adoptChildExpression(supplied);
62 supplied = cexp;
63 }
64
65 // rule 2
66 ItemType reqItemType = req.getPrimaryType();
67 if (req.getPrimaryType().equals(BuiltInAtomicType.STRING) &&
68 !Cardinality.allowsMany(req.getCardinality()) &&
69 !th.isSubType(supplied.getItemType(), BuiltInAtomicType.STRING)) {
70 final RetainedStaticContext rsc = new RetainedStaticContext(config);
71 Expression fn = SystemFunction.makeCall("string", rsc, supplied);
72 try {
73 return fn.typeCheck(visitor, config.getDefaultContextItemStaticInfo());
74 } catch (XPathException err) {
75 err.maybeSetLocation(supplied.getLocation());
76 err.setIsStaticError(true);
77 throw err;
78 }
79 }
80
81 // rule 3
82 if (reqItemType.equals(NumericType.getInstance()) || reqItemType.equals(BuiltInAtomicType.DOUBLE) &&
83 !Cardinality.allowsMany(req.getCardinality()) &&
84 !th.isSubType(supplied.getItemType(), BuiltInAtomicType.DOUBLE)) {
85 final RetainedStaticContext rsc = new RetainedStaticContext(config);
86 Expression fn = SystemFunction.makeCall("number", rsc, supplied);
87 try {
88 return fn.typeCheck(visitor, config.getDefaultContextItemStaticInfo());
89 } catch (XPathException err) {
90 err.maybeSetLocation(supplied.getLocation());
91 err.setIsStaticError(true);
92 throw err;
93 }
94 }
95 return super.staticTypeCheck(supplied, req, role, visitor);
96
97 }
98
99 public Expression makeArithmeticExpression(Expression lhs, int operator, Expression rhs) {
100 return new ArithmeticExpression10(lhs, operator, rhs);
101 }
102
103 public Expression makeGeneralComparison(Expression lhs, int operator, Expression rhs) {
104 return new GeneralComparison10(lhs, operator, rhs);
105 }
106
107 public Expression processValueOf(Expression select, Configuration config) {
108 TypeHierarchy th = config.getTypeHierarchy();
109 if (!select.getItemType().isPlainType()) {
110 select = Atomizer.makeAtomizer(select, null);
111 }
112 if (Cardinality.allowsMany(select.getCardinality())) {
113 select = FirstItemExpression.makeFirstItemExpression(select);
114 }
115 if (!th.isSubType(select.getItemType(), BuiltInAtomicType.STRING)) {
116 select = new AtomicSequenceConverter(select, BuiltInAtomicType.STRING);
117 ((AtomicSequenceConverter) select).allocateConverterStatically(config, false);
118 }
119 return select;
120 }
121
122
123 }