| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.parser.*; |
| 11 |
import net.sf.saxon.functions.BooleanFn; |
| 12 |
import net.sf.saxon.functions.SystemFunction; |
| 13 |
import net.sf.saxon.trans.XPathException; |
| 14 |
import net.sf.saxon.type.BuiltInAtomicType; |
| 15 |
import net.sf.saxon.type.ItemType; |
| 16 |
import net.sf.saxon.type.TypeHierarchy; |
| 17 |
import net.sf.saxon.type.UType; |
| 18 |
import net.sf.saxon.value.BooleanValue; |
| 19 |
|
| 20 |
import java.util.List; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public abstract class BooleanExpression extends BinaryExpression implements Negatable { |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public BooleanExpression(Expression p1, int operator, Expression p2) { |
| 38 |
super(p1, operator, p2); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
@Override |
| 50 |
public String getExpressionName() { |
| 51 |
return Token.tokens[getOperator()] + "-expression"; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 56 |
|
| 57 |
getLhs().typeCheck(visitor, contextInfo); |
| 58 |
getRhs().typeCheck(visitor, contextInfo); |
| 59 |
|
| 60 |
TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy(); |
| 61 |
XPathException err0 = TypeChecker.ebvError(getLhsExpression(), th); |
| 62 |
if (err0 != null) { |
| 63 |
err0.setLocator(getLocation()); |
| 64 |
throw err0; |
| 65 |
} |
| 66 |
XPathException err1 = TypeChecker.ebvError(getRhsExpression(), th); |
| 67 |
if (err1 != null) { |
| 68 |
err1.setLocator(getLocation()); |
| 69 |
throw err1; |
| 70 |
} |
| 71 |
|
| 72 |
if (getLhsExpression() instanceof Literal && !(((Literal) getLhsExpression()).getValue() instanceof BooleanValue)) { |
| 73 |
setLhsExpression(Literal.makeLiteral( |
| 74 |
BooleanValue.get(getLhsExpression().effectiveBooleanValue(visitor.makeDynamicContext())), this)); |
| 75 |
} |
| 76 |
if (getRhsExpression() instanceof Literal && !(((Literal) getRhsExpression()).getValue() instanceof BooleanValue)) { |
| 77 |
setRhsExpression(Literal.makeLiteral( |
| 78 |
BooleanValue.get(getRhsExpression().effectiveBooleanValue(visitor.makeDynamicContext())), this)); |
| 79 |
} |
| 80 |
return preEvaluate(); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public int computeCardinality() { |
| 88 |
return StaticProperty.EXACTLY_ONE; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 108 |
|
| 109 |
optimizeChildren(visitor, contextItemType); |
| 110 |
|
| 111 |
boolean forStreaming = visitor.isOptimizeForStreaming(); |
| 112 |
setLhsExpression(ExpressionTool.unsortedIfHomogeneous(getLhsExpression(), forStreaming)); |
| 113 |
setRhsExpression(ExpressionTool.unsortedIfHomogeneous(getRhsExpression(), forStreaming)); |
| 114 |
|
| 115 |
Expression op0 = BooleanFn.rewriteEffectiveBooleanValue(getLhsExpression(), visitor, contextItemType); |
| 116 |
if (op0 != null) { |
| 117 |
setLhsExpression(op0); |
| 118 |
} |
| 119 |
Expression op1 = BooleanFn.rewriteEffectiveBooleanValue(getRhsExpression(), visitor, contextItemType); |
| 120 |
if (op1 != null) { |
| 121 |
setRhsExpression(op1); |
| 122 |
} |
| 123 |
return preEvaluate(); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
protected abstract Expression preEvaluate(); |
| 134 |
|
| 135 |
protected Expression forceToBoolean(Expression in) { |
| 136 |
if (in.getItemType() == BuiltInAtomicType.BOOLEAN && in.getCardinality() == StaticProperty.ALLOWS_ONE) { |
| 137 |
return in; |
| 138 |
} else { |
| 139 |
return SystemFunction.makeCall("boolean", getRetainedStaticContext(), in); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public boolean isNegatable(TypeHierarchy th) { |
| 151 |
return true; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public abstract Expression negate(); |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public BooleanValue evaluateItem(XPathContext context) throws XPathException { |
| 168 |
return BooleanValue.get(effectiveBooleanValue(context)); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public abstract boolean effectiveBooleanValue(XPathContext c) throws XPathException; |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public ItemType getItemType() { |
| 185 |
return BuiltInAtomicType.BOOLEAN; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
@Override |
| 196 |
public UType getStaticUType(UType contextItemType) { |
| 197 |
return UType.BOOLEAN; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
public static void listAndComponents(Expression exp, List<Expression> list) { |
| 209 |
if (exp instanceof BooleanExpression && ((BooleanExpression) exp).getOperator() == Token.AND) { |
| 210 |
for (Operand o : exp.operands()) { |
| 211 |
listAndComponents(o.getChildExpression(), list); |
| 212 |
} |
| 213 |
} else { |
| 214 |
list.add(exp); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
@Override |
| 219 |
protected OperandRole getOperandRole(int arg) { |
| 220 |
return OperandRole.INSPECT; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
} |
| 225 |
|