| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.pattern; |
| 9 |
|
| 10 |
import com.saxonica.ee.stream.Streamability; |
| 11 |
import com.saxonica.ee.stream.Sweep; |
| 12 |
import com.saxonica.ee.trans.ContextItemStaticInfoEE; |
| 13 |
import net.sf.saxon.expr.*; |
| 14 |
import net.sf.saxon.expr.instruct.SlotManager; |
| 15 |
import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 16 |
import net.sf.saxon.expr.parser.ExpressionTool; |
| 17 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 18 |
import net.sf.saxon.expr.parser.RebindingMap; |
| 19 |
import net.sf.saxon.om.Item; |
| 20 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 21 |
import net.sf.saxon.trans.XPathException; |
| 22 |
import net.sf.saxon.tree.iter.ManualIterator; |
| 23 |
import net.sf.saxon.type.AnyItemType; |
| 24 |
import net.sf.saxon.type.ItemType; |
| 25 |
import net.sf.saxon.type.UType; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class BooleanExpressionPattern extends Pattern implements PatternWithPredicate { |
| 36 |
|
| 37 |
private Operand expressionOp; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public BooleanExpressionPattern(Expression expression) { |
| 47 |
this.expressionOp = new Operand(this, expression, OperandRole.SINGLE_ATOMIC); |
| 48 |
setPriority(1); |
| 49 |
} |
| 50 |
|
| 51 |
@Override |
| 52 |
public Expression getPredicate() { |
| 53 |
return expressionOp.getChildExpression(); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
@Override |
| 67 |
public Iterable<Operand> operands() { |
| 68 |
return expressionOp; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
@Override |
| 77 |
public UType getUType() { |
| 78 |
if (getPredicate() instanceof InstanceOfExpression) { |
| 79 |
return ((InstanceOfExpression) getPredicate()).getRequiredItemType().getUType(); |
| 80 |
} else { |
| 81 |
return UType.ANY; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public int allocateSlots(SlotManager slotManager, int nextFree) { |
| 93 |
return ExpressionTool.allocateSlots(getPredicate(), nextFree, slotManager); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
@Override |
| 105 |
public Pattern typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 106 |
ContextItemStaticInfo cit = visitor.getConfiguration().getDefaultContextItemStaticInfo(); |
| 107 |
expressionOp.setChildExpression(getPredicate().typeCheck(visitor, cit)); |
| 108 |
return this; |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public Pattern optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 113 |
ContextItemStaticInfo cit = visitor.getConfiguration().getDefaultContextItemStaticInfo(); |
| 114 |
expressionOp.setChildExpression(getPredicate().optimize(visitor, cit)); |
| 115 |
return this; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
@Override |
| 120 |
public boolean isMotionless() { |
| 121 |
ContextItemStaticInfoEE cio = (ContextItemStaticInfoEE)getConfiguration().makeContextItemStaticInfo(getItemType(), false); |
| 122 |
cio.setContextPostureStriding(); |
| 123 |
Streamability.getStreamability(getPredicate(), cio, null); |
| 124 |
return Streamability.getSweep(getPredicate()) == Sweep.MOTIONLESS; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public boolean matches(Item<?> item, XPathContext context) { |
| 139 |
XPathContext c2 = context.newMinorContext(); |
| 140 |
ManualIterator<Item<?>> iter = new ManualIterator<>(item); |
| 141 |
c2.setCurrentIterator(iter); |
| 142 |
c2.setCurrentOutputUri(null); |
| 143 |
try { |
| 144 |
return getPredicate().effectiveBooleanValue(c2); |
| 145 |
} catch (XPathException e) { |
| 146 |
return false; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public ItemType getItemType() { |
| 155 |
if (getPredicate() instanceof InstanceOfExpression) { |
| 156 |
InstanceOfExpression ioe = (InstanceOfExpression)getPredicate(); |
| 157 |
if (ioe.getBaseExpression() instanceof ContextItemExpression) { |
| 158 |
return ioe.getRequiredItemType(); |
| 159 |
} |
| 160 |
} |
| 161 |
return AnyItemType.getInstance(); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public int getFingerprint() { |
| 172 |
return -1; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public String toString() { |
| 180 |
return ".[" + getPredicate() + "]"; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public boolean equals( Object other) { |
| 190 |
return (other instanceof BooleanExpressionPattern) && |
| 191 |
((BooleanExpressionPattern) other).getPredicate().isEqual(getPredicate()); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public int computeHashCode() { |
| 199 |
return 0x7aeffea9 ^ getPredicate().hashCode(); |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public Pattern copy(RebindingMap rebindings) { |
| 211 |
BooleanExpressionPattern n = new BooleanExpressionPattern(getPredicate().copy(rebindings)); |
| 212 |
ExpressionTool.copyLocationInfo(this, n); |
| 213 |
return n; |
| 214 |
} |
| 215 |
|
| 216 |
public void export(ExpressionPresenter presenter) throws XPathException { |
| 217 |
presenter.startElement("p.booleanExp"); |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
getPredicate().export(presenter); |
| 222 |
|
| 223 |
presenter.endElement(); |
| 224 |
} |
| 225 |
|
| 226 |
} |
| 227 |
|