| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 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 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
final Configuration config = visitor.getConfiguration(); |
| 56 |
final TypeHierarchy th = config.getTypeHierarchy(); |
| 57 |
|
| 58 |
|
| 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 |
|
| 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 |
|
| 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 |
} |