| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.expr.*; |
| 12 |
import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 13 |
import net.sf.saxon.expr.parser.ExpressionTool; |
| 14 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 15 |
import net.sf.saxon.expr.parser.TypeChecker; |
| 16 |
import net.sf.saxon.om.GroundedValue; |
| 17 |
import net.sf.saxon.om.Sequence; |
| 18 |
import net.sf.saxon.pattern.NodeTest; |
| 19 |
import net.sf.saxon.trans.XPathException; |
| 20 |
import net.sf.saxon.type.BuiltInAtomicType; |
| 21 |
import net.sf.saxon.type.TypeHierarchy; |
| 22 |
import net.sf.saxon.value.BooleanValue; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class BooleanFn extends SystemFunction { |
| 30 |
|
| 31 |
@Override |
| 32 |
public void supplyTypeInformation(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType, Expression[] arguments) throws XPathException { |
| 33 |
XPathException err = TypeChecker.ebvError(arguments[0], visitor.getConfiguration().getTypeHierarchy()); |
| 34 |
if (err != null) { |
| 35 |
throw err; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public static Expression rewriteEffectiveBooleanValue( |
| 52 |
Expression exp, ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 53 |
Configuration config = visitor.getConfiguration(); |
| 54 |
TypeHierarchy th = config.getTypeHierarchy(); |
| 55 |
boolean forStreaming = visitor.isOptimizeForStreaming(); |
| 56 |
exp = ExpressionTool.unsortedIfHomogeneous(exp, forStreaming); |
| 57 |
if (exp instanceof Literal) { |
| 58 |
GroundedValue val = ((Literal)exp).getValue(); |
| 59 |
if (val instanceof BooleanValue) { |
| 60 |
return exp; |
| 61 |
} |
| 62 |
return Literal.makeLiteral( |
| 63 |
BooleanValue.get( |
| 64 |
ExpressionTool.effectiveBooleanValue(val.iterate())), exp); |
| 65 |
} |
| 66 |
if (exp instanceof ValueComparison) { |
| 67 |
ValueComparison vc = (ValueComparison) exp; |
| 68 |
if (vc.getResultWhenEmpty() == null) { |
| 69 |
vc.setResultWhenEmpty(BooleanValue.FALSE); |
| 70 |
} |
| 71 |
return exp; |
| 72 |
} else if (exp.isCallOn(BooleanFn.class)) { |
| 73 |
return ((SystemFunctionCall) exp).getArg(0); |
| 74 |
} else if (th.isSubType(exp.getItemType(), BuiltInAtomicType.BOOLEAN) && |
| 75 |
exp.getCardinality() == StaticProperty.EXACTLY_ONE) { |
| 76 |
return exp; |
| 77 |
} else if (exp.isCallOn(Count.class)) { |
| 78 |
|
| 79 |
Expression exists = SystemFunction.makeCall("exists", exp.getRetainedStaticContext(), ((SystemFunctionCall) exp).getArg(0)); |
| 80 |
assert exists != null; |
| 81 |
ExpressionTool.copyLocationInfo(exp, exists); |
| 82 |
return exists.optimize(visitor, contextItemType); |
| 83 |
} else if (exp.getItemType() instanceof NodeTest) { |
| 84 |
|
| 85 |
Expression exists = SystemFunction.makeCall("exists", exp.getRetainedStaticContext(), exp); |
| 86 |
assert exists != null; |
| 87 |
ExpressionTool.copyLocationInfo(exp, exists); |
| 88 |
return exists.optimize(visitor, contextItemType); |
| 89 |
} else { |
| 90 |
return null; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
public BooleanValue call(XPathContext c, Sequence[] arguments) throws XPathException { |
| 95 |
boolean bValue = ExpressionTool.effectiveBooleanValue(arguments[0].iterate()); |
| 96 |
return BooleanValue.get(bValue); |
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
public Expression makeFunctionCall(Expression[] arguments) { |
| 101 |
return new SystemFunctionCall(this, arguments) { |
| 102 |
|
| 103 |
public Expression optimize( ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 104 |
Expression e = super.optimize(visitor, contextItemType); |
| 105 |
if (e == this) { |
| 106 |
Expression ebv = rewriteEffectiveBooleanValue(getArg(0), visitor, contextItemType); |
| 107 |
if (ebv != null) { |
| 108 |
ebv = ebv.optimize(visitor, contextItemType); |
| 109 |
if (ebv.getItemType() == BuiltInAtomicType.BOOLEAN && |
| 110 |
ebv.getCardinality() == StaticProperty.EXACTLY_ONE) { |
| 111 |
ebv.setParentExpression(getParentExpression()); |
| 112 |
return ebv; |
| 113 |
} else { |
| 114 |
setArg(0, ebv); |
| 115 |
adoptChildExpression(ebv); |
| 116 |
return this; |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
return e; |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public boolean effectiveBooleanValue(XPathContext c) throws XPathException { |
| 125 |
try { |
| 126 |
return getArg(0).effectiveBooleanValue(c); |
| 127 |
} catch (XPathException e) { |
| 128 |
e.maybeSetLocation(getLocation()); |
| 129 |
e.maybeSetContext(c); |
| 130 |
throw e; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
public BooleanValue evaluateItem(XPathContext context) throws XPathException { |
| 136 |
return BooleanValue.get(effectiveBooleanValue(context)); |
| 137 |
} |
| 138 |
}; |
| 139 |
} |
| 140 |
|
| 141 |
public String getCompilerName() { |
| 142 |
return "BooleanFnCompiler"; |
| 143 |
} |
| 144 |
|
| 145 |
public String getStreamerName() { |
| 146 |
return "BooleanFn"; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
} |