| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.*; |
| 11 |
import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 12 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 13 |
import net.sf.saxon.expr.parser.Token; |
| 14 |
import net.sf.saxon.om.Sequence; |
| 15 |
import net.sf.saxon.om.SequenceIterator; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.tree.iter.LookaheadIterator; |
| 18 |
import net.sf.saxon.value.BooleanValue; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public class Empty extends Aggregate { |
| 25 |
|
| 26 |
public Expression makeOptimizedFunctionCall( |
| 27 |
ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, final Expression... arguments) throws XPathException { |
| 28 |
|
| 29 |
|
| 30 |
int c = arguments[0].getCardinality(); |
| 31 |
if (c == StaticProperty.ALLOWS_ONE_OR_MORE) { |
| 32 |
return Literal.makeLiteral(BooleanValue.FALSE, arguments[0]); |
| 33 |
} else if (c == StaticProperty.ALLOWS_ZERO) { |
| 34 |
return Literal.makeLiteral(BooleanValue.TRUE, arguments[0]); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
if (arguments[0] instanceof VennExpression && !visitor.isOptimizeForStreaming()) { |
| 40 |
VennExpression v = (VennExpression) arguments[0]; |
| 41 |
if (v.getOperator() == Token.UNION) { |
| 42 |
Expression e0 = SystemFunction.makeCall("empty", getRetainedStaticContext(), v.getLhsExpression()); |
| 43 |
Expression e1 = SystemFunction.makeCall("empty", getRetainedStaticContext(), v.getRhsExpression()); |
| 44 |
return new AndExpression(e0, e1).optimize(visitor, contextInfo); |
| 45 |
} |
| 46 |
} |
| 47 |
return null; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public BooleanValue call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 61 |
return BooleanValue.get(empty(arguments[0].iterate())); |
| 62 |
} |
| 63 |
|
| 64 |
private static boolean empty(SequenceIterator iter) throws XPathException { |
| 65 |
boolean result; |
| 66 |
if ((iter.getProperties() & SequenceIterator.LOOKAHEAD) != 0) { |
| 67 |
result = !((LookaheadIterator) iter).hasNext(); |
| 68 |
} else { |
| 69 |
result = iter.next() == null; |
| 70 |
} |
| 71 |
iter.close(); |
| 72 |
return result; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
@Override |
| 77 |
public Expression makeFunctionCall(Expression[] arguments) { |
| 78 |
return new SystemFunctionCall(this, arguments) { |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public Expression optimize( ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 97 |
Expression e2 = super.optimize(visitor, contextInfo); |
| 98 |
if (e2 != this) { |
| 99 |
return e2; |
| 100 |
} |
| 101 |
|
| 102 |
int c = getArg(0).getCardinality(); |
| 103 |
if (c == StaticProperty.ALLOWS_ONE_OR_MORE) { |
| 104 |
return Literal.makeLiteral(BooleanValue.FALSE, e2); |
| 105 |
} else if (c == StaticProperty.ALLOWS_ZERO) { |
| 106 |
return Literal.makeLiteral(BooleanValue.TRUE, e2); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
setArg(0, getArg(0).unordered(false, visitor.isOptimizeForStreaming())); |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
if (getArg(0) instanceof VennExpression) { |
| 115 |
VennExpression v = (VennExpression) getArg(0); |
| 116 |
if (v.getOperator() == Token.UNION && !visitor.isOptimizeForStreaming()) { |
| 117 |
Expression e0 = SystemFunction.makeCall("empty", getRetainedStaticContext(), v.getLhsExpression()); |
| 118 |
Expression e1 = SystemFunction.makeCall("empty", getRetainedStaticContext(), v.getRhsExpression()); |
| 119 |
return new AndExpression(e0, e1).optimize(visitor, contextInfo); |
| 120 |
} |
| 121 |
} |
| 122 |
return this; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public BooleanValue evaluateItem(XPathContext context) throws XPathException { |
| 130 |
return BooleanValue.get(effectiveBooleanValue(context)); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public boolean effectiveBooleanValue(XPathContext c) throws XPathException { |
| 138 |
SequenceIterator iter = getArg(0).iterate(c); |
| 139 |
boolean result; |
| 140 |
if ((iter.getProperties() & SequenceIterator.LOOKAHEAD) != 0) { |
| 141 |
result = !((LookaheadIterator) iter).hasNext(); |
| 142 |
} else { |
| 143 |
result = iter.next() == null; |
| 144 |
} |
| 145 |
iter.close(); |
| 146 |
return result; |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public int getNetCost() { |
| 151 |
return 0; |
| 152 |
} |
| 153 |
}; |
| 154 |
} |
| 155 |
|
| 156 |
public String getCompilerName() { |
| 157 |
return "EmptyCompiler"; |
| 158 |
} |
| 159 |
|
| 160 |
public String getStreamerName() { |
| 161 |
return "Empty"; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
} |
| 166 |
|