| 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.expr.sort.GlobalOrderComparer; |
| 12 |
import net.sf.saxon.om.NodeInfo; |
| 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.UType; |
| 17 |
import net.sf.saxon.value.BooleanValue; |
| 18 |
import net.sf.saxon.value.Cardinality; |
| 19 |
import net.sf.saxon.value.SequenceType; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
public final class IdentityComparison extends BinaryExpression { |
| 29 |
|
| 30 |
private boolean generateIdEmulation = false; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public IdentityComparison(Expression p1, int op, Expression p2) { |
| 44 |
super(p1, op, p2); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public void setGenerateIdEmulation(boolean flag) { |
| 58 |
generateIdEmulation = flag; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public boolean isGenerateIdEmulation() { |
| 69 |
return generateIdEmulation; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 78 |
|
| 79 |
getLhs().typeCheck(visitor, contextInfo); |
| 80 |
getRhs().typeCheck(visitor, contextInfo); |
| 81 |
|
| 82 |
if (!generateIdEmulation) { |
| 83 |
if (Literal.isEmptySequence(getLhsExpression()) || Literal.isEmptySequence(getRhsExpression())) { |
| 84 |
return Literal.makeEmptySequence(); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
RoleDiagnostic role0 = new RoleDiagnostic(RoleDiagnostic.BINARY_EXPR, Token.tokens[operator], 0); |
| 89 |
TypeChecker tc = visitor.getConfiguration().getTypeChecker(false); |
| 90 |
|
| 91 |
setLhsExpression(tc.staticTypeCheck(getLhsExpression(), SequenceType.OPTIONAL_NODE, role0, visitor)); |
| 92 |
|
| 93 |
RoleDiagnostic role1 = new RoleDiagnostic(RoleDiagnostic.BINARY_EXPR, Token.tokens[operator], 1); |
| 94 |
|
| 95 |
setRhsExpression(tc.staticTypeCheck(getRhsExpression(), SequenceType.OPTIONAL_NODE, role1, visitor)); |
| 96 |
|
| 97 |
if (!Cardinality.allowsZero(getLhsExpression().getCardinality()) && !Cardinality.allowsZero(getRhsExpression().getCardinality())) { |
| 98 |
generateIdEmulation = false; |
| 99 |
|
| 100 |
} |
| 101 |
return this; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException { |
| 121 |
Expression r = super.optimize(visitor, contextItemType); |
| 122 |
if (r != this) { |
| 123 |
if (!generateIdEmulation) { |
| 124 |
if (Literal.isEmptySequence(getLhsExpression()) || Literal.isEmptySequence(getRhsExpression())) { |
| 125 |
return Literal.makeEmptySequence(); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
return r; |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
protected OperandRole getOperandRole(int arg) { |
| 134 |
return OperandRole.INSPECT; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public Expression copy(RebindingMap rebindings) { |
| 147 |
IdentityComparison ic = new IdentityComparison(getLhsExpression().copy(rebindings), operator, getRhsExpression().copy(rebindings)); |
| 148 |
ExpressionTool.copyLocationInfo(this, ic); |
| 149 |
ic.generateIdEmulation = generateIdEmulation; |
| 150 |
return ic; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
@Override |
| 159 |
protected String tag() { |
| 160 |
switch (operator) { |
| 161 |
case Token.IS: return "is"; |
| 162 |
case Token.PRECEDES: return "precedes"; |
| 163 |
case Token.FOLLOWS: return "follows"; |
| 164 |
default: return "?"; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public BooleanValue evaluateItem(XPathContext context) throws XPathException { |
| 174 |
NodeInfo node0 = getNode(getLhsExpression(), context); |
| 175 |
if (node0 == null) { |
| 176 |
if (generateIdEmulation) { |
| 177 |
return BooleanValue.get(getNode(getRhsExpression(), context) == null); |
| 178 |
} |
| 179 |
return null; |
| 180 |
} |
| 181 |
|
| 182 |
NodeInfo node1 = getNode(getRhsExpression(), context); |
| 183 |
if (node1 == null) { |
| 184 |
if (generateIdEmulation) { |
| 185 |
return BooleanValue.FALSE; |
| 186 |
} |
| 187 |
return null; |
| 188 |
} |
| 189 |
|
| 190 |
return BooleanValue.get(compareIdentity(node0, node1)); |
| 191 |
} |
| 192 |
|
| 193 |
public boolean effectiveBooleanValue(XPathContext context) throws XPathException { |
| 194 |
NodeInfo node0 = getNode(getLhsExpression(), context); |
| 195 |
if (node0 == null) { |
| 196 |
return generateIdEmulation && getNode(getRhsExpression(), context) == null; |
| 197 |
} |
| 198 |
|
| 199 |
NodeInfo node1 = getNode(getRhsExpression(), context); |
| 200 |
return node1 != null && compareIdentity(node0, node1); |
| 201 |
|
| 202 |
} |
| 203 |
|
| 204 |
private boolean compareIdentity(NodeInfo node0, NodeInfo node1) { |
| 205 |
|
| 206 |
switch (operator) { |
| 207 |
case Token.IS: |
| 208 |
return node0.equals(node1); |
| 209 |
case Token.PRECEDES: |
| 210 |
return GlobalOrderComparer.getInstance().compare(node0, node1) < 0; |
| 211 |
case Token.FOLLOWS: |
| 212 |
return GlobalOrderComparer.getInstance().compare(node0, node1) > 0; |
| 213 |
default: |
| 214 |
throw new UnsupportedOperationException("Unknown node identity test"); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
private static NodeInfo getNode(Expression exp, XPathContext c) throws XPathException { |
| 219 |
return (NodeInfo) exp.evaluateItem(c); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public ItemType getItemType() { |
| 231 |
return BuiltInAtomicType.BOOLEAN; |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
@Override |
| 242 |
public UType getStaticUType(UType contextItemType) { |
| 243 |
return UType.BOOLEAN; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
@Override |
| 254 |
public String getExpressionName() { |
| 255 |
return "nodeComparison"; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
} |
| 260 |
|