| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.instruct.Choose; |
| 11 |
import net.sf.saxon.expr.parser.*; |
| 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.TypeHierarchy; |
| 16 |
import net.sf.saxon.value.BooleanValue; |
| 17 |
|
| 18 |
import java.util.Collection; |
| 19 |
|
| 20 |
public class AndExpression extends BooleanExpression { |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public AndExpression(Expression p1, Expression p2) { |
| 30 |
super(p1, Token.AND, p2); |
| 31 |
} |
| 32 |
|
| 33 |
@Override |
| 34 |
protected Expression preEvaluate() { |
| 35 |
|
| 36 |
|
| 37 |
if (Literal.isConstantBoolean(getLhsExpression(), false) || Literal.isConstantBoolean(getRhsExpression(), false)) { |
| 38 |
|
| 39 |
|
| 40 |
return Literal.makeLiteral(BooleanValue.FALSE, this); |
| 41 |
} else if (Literal.hasEffectiveBooleanValue(getLhsExpression(), true)) { |
| 42 |
|
| 43 |
return forceToBoolean(getRhsExpression()); |
| 44 |
} else if (Literal.hasEffectiveBooleanValue(getRhsExpression(), true)) { |
| 45 |
|
| 46 |
return forceToBoolean(getLhsExpression()); |
| 47 |
} else { |
| 48 |
return this; |
| 49 |
} |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 70 |
|
| 71 |
Expression t = super.optimize(visitor, contextInfo); |
| 72 |
if (t != this) { |
| 73 |
return t; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy(); |
| 82 |
if ( getRhsExpression() instanceof UserFunctionCall && |
| 83 |
th.isSubType(getRhsExpression().getItemType(), BuiltInAtomicType.BOOLEAN) && |
| 84 |
!ExpressionTool.isLoopingSubexpression(this, null)) { |
| 85 |
Expression cond = Choose.makeConditional( |
| 86 |
getLhsExpression(), getRhsExpression(), Literal.makeLiteral(BooleanValue.FALSE, this)); |
| 87 |
ExpressionTool.copyLocationInfo(this, cond); |
| 88 |
return cond; |
| 89 |
} |
| 90 |
return this; |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public int getCost() { |
| 95 |
|
| 96 |
return getLhsExpression().getCost() + getRhsExpression().getCost() / 2; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public Expression copy(RebindingMap rebindings) { |
| 108 |
AndExpression a2 = new AndExpression(getLhsExpression().copy(rebindings), getRhsExpression().copy(rebindings)); |
| 109 |
ExpressionTool.copyLocationInfo(this, a2); |
| 110 |
return a2; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public Expression negate() { |
| 122 |
|
| 123 |
|
| 124 |
Expression not0 = SystemFunction.makeCall("not", getRetainedStaticContext(), getLhsExpression()); |
| 125 |
Expression not1 = SystemFunction.makeCall("not", getRetainedStaticContext(), getRhsExpression()); |
| 126 |
return new OrExpression(not0, not1); |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
@Override |
| 136 |
protected String tag() { |
| 137 |
return "and"; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public boolean effectiveBooleanValue(XPathContext c) throws XPathException { |
| 145 |
return getLhsExpression().effectiveBooleanValue(c) && getRhsExpression().effectiveBooleanValue(c); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public static Expression distribute(Collection<Expression> exprs) { |
| 154 |
Expression result = null; |
| 155 |
if(exprs != null) { |
| 156 |
boolean first = true; |
| 157 |
for(Expression e: exprs) { |
| 158 |
if(first){ |
| 159 |
first = false; |
| 160 |
result = e; |
| 161 |
} else { |
| 162 |
result = new AndExpression(result,e); |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
return result; |
| 167 |
} |
| 168 |
} |
| 169 |
|