| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr; |
| 9 |
|
| 10 |
import net.sf.saxon.event.Receiver; |
| 11 |
import net.sf.saxon.event.TypeCheckingFilter; |
| 12 |
import net.sf.saxon.expr.instruct.Block; |
| 13 |
import net.sf.saxon.expr.parser.*; |
| 14 |
import net.sf.saxon.om.Item; |
| 15 |
import net.sf.saxon.om.SequenceIterator; |
| 16 |
import net.sf.saxon.pattern.CombinedNodeTest; |
| 17 |
import net.sf.saxon.pattern.DocumentNodeTest; |
| 18 |
import net.sf.saxon.pattern.NodeTest; |
| 19 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 20 |
import net.sf.saxon.trans.XPathException; |
| 21 |
import net.sf.saxon.type.*; |
| 22 |
import net.sf.saxon.value.Cardinality; |
| 23 |
import net.sf.saxon.value.IntegerValue; |
| 24 |
import net.sf.saxon.value.SequenceType; |
| 25 |
|
| 26 |
import java.util.ArrayList; |
| 27 |
import java.util.List; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public final class ItemChecker extends UnaryExpression { |
| 36 |
|
| 37 |
private ItemType requiredItemType; |
| 38 |
private RoleDiagnostic role; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public ItemChecker(Expression sequence, ItemType itemType, RoleDiagnostic role) { |
| 49 |
super(sequence); |
| 50 |
requiredItemType = itemType; |
| 51 |
this.role = role; |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public ItemType getRequiredType() { |
| 62 |
return requiredItemType; |
| 63 |
} |
| 64 |
|
| 65 |
protected OperandRole getOperandRole() { |
| 66 |
return OperandRole.SAME_FOCUS_ACTION; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public RoleDiagnostic getRoleLocator() { |
| 76 |
return role; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public Expression simplify() throws XPathException { |
| 86 |
Expression operand = getBaseExpression().simplify(); |
| 87 |
if (requiredItemType instanceof AnyItemType) { |
| 88 |
return operand; |
| 89 |
} |
| 90 |
setBaseExpression(operand); |
| 91 |
return this; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 100 |
getOperand().typeCheck(visitor, contextInfo); |
| 101 |
Expression operand = getBaseExpression(); |
| 102 |
|
| 103 |
if (operand instanceof Block) { |
| 104 |
|
| 105 |
|
| 106 |
Block block = (Block)operand; |
| 107 |
List<Expression> checkedOperands = new ArrayList<>(); |
| 108 |
for (Operand o : block.operands()) { |
| 109 |
ItemChecker checkedOp = new ItemChecker(o.getChildExpression(), requiredItemType, role); |
| 110 |
checkedOperands.add(checkedOp); |
| 111 |
} |
| 112 |
Block newBlock = new Block(checkedOperands.toArray(new Expression[0])); |
| 113 |
ExpressionTool.copyLocationInfo(this, newBlock); |
| 114 |
return newBlock.typeCheck(visitor, contextInfo); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
final TypeHierarchy th = getConfiguration().getTypeHierarchy(); |
| 119 |
int card = operand.getCardinality(); |
| 120 |
if (card == StaticProperty.EMPTY) { |
| 121 |
|
| 122 |
return operand; |
| 123 |
} |
| 124 |
ItemType supplied = operand.getItemType(); |
| 125 |
int relation = th.relationship(requiredItemType, supplied); |
| 126 |
if (relation == TypeHierarchy.SAME_TYPE || relation == TypeHierarchy.SUBSUMES) { |
| 127 |
return operand; |
| 128 |
} else if (relation == TypeHierarchy.DISJOINT) { |
| 129 |
if (requiredItemType.equals(BuiltInAtomicType.STRING) && th.isSubType(supplied, BuiltInAtomicType.ANY_URI)) { |
| 130 |
|
| 131 |
return operand; |
| 132 |
} else if (Cardinality.allowsZero(card)) { |
| 133 |
if (!(operand instanceof Literal)) { |
| 134 |
String message = role.composeErrorMessage( |
| 135 |
requiredItemType, operand, th); |
| 136 |
visitor.getStaticContext().issueWarning( |
| 137 |
"The only value that can pass type-checking is an empty sequence. " + |
| 138 |
message, getLocation()); |
| 139 |
} |
| 140 |
} else { |
| 141 |
String message = role.composeErrorMessage(requiredItemType, operand, th); |
| 142 |
XPathException err = new XPathException(message); |
| 143 |
err.setErrorCode(role.getErrorCode()); |
| 144 |
err.setLocation(this.getLocation()); |
| 145 |
err.setIsTypeError(role.isTypeError()); |
| 146 |
throw err; |
| 147 |
} |
| 148 |
} |
| 149 |
return this; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
@Override |
| 167 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 168 |
getOperand().optimize(visitor, contextInfo); |
| 169 |
TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy(); |
| 170 |
int rel = th.relationship(requiredItemType, getBaseExpression().getItemType()); |
| 171 |
if (rel == TypeHierarchy.SAME_TYPE || rel == TypeHierarchy.SUBSUMES) { |
| 172 |
return getBaseExpression(); |
| 173 |
} |
| 174 |
return this; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public int getImplementationMethod() { |
| 184 |
int m = ITERATE_METHOD | PROCESS_METHOD | ITEM_FEED_METHOD; |
| 185 |
if (!Cardinality.allowsMany(getCardinality())) { |
| 186 |
m |= EVALUATE_METHOD; |
| 187 |
} |
| 188 |
return m; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
@Override |
| 198 |
public String getStreamerName() { |
| 199 |
return "ItemChecker"; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
@Override |
| 217 |
public IntegerValue[] getIntegerBounds() { |
| 218 |
return getBaseExpression().getIntegerBounds(); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public SequenceIterator<?> iterate(XPathContext context) throws XPathException { |
| 227 |
SequenceIterator<?> base = getBaseExpression().iterate(context); |
| 228 |
return new ItemMappingIterator<>(base, getMappingFunction(context), true); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
public ItemMappingFunction<Item<?>, Item<?>> getMappingFunction(XPathContext context) { |
| 241 |
return new ItemTypeCheckingFunction(requiredItemType, role, getBaseExpression(), context.getConfiguration()); |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public Item evaluateItem(XPathContext context) throws XPathException { |
| 249 |
final TypeHierarchy th = context.getConfiguration().getTypeHierarchy(); |
| 250 |
Item item = getBaseExpression().evaluateItem(context); |
| 251 |
if (item == null) { |
| 252 |
return null; |
| 253 |
} |
| 254 |
if (requiredItemType.matches(item, th)) { |
| 255 |
return item; |
| 256 |
} else if (requiredItemType.getUType().subsumes(UType.STRING) && BuiltInAtomicType.ANY_URI.matches(item, th)) { |
| 257 |
return item; |
| 258 |
} else { |
| 259 |
String message = role.composeErrorMessage(requiredItemType, item, th); |
| 260 |
String errorCode = role.getErrorCode(); |
| 261 |
if ("XPDY0050".equals(errorCode)) { |
| 262 |
|
| 263 |
dynamicError(message, errorCode, context); |
| 264 |
} else { |
| 265 |
typeError(message, errorCode, context); |
| 266 |
} |
| 267 |
return null; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
public void process(XPathContext context) throws XPathException { |
| 279 |
Expression next = getBaseExpression(); |
| 280 |
int card = StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 281 |
if (next instanceof CardinalityChecker) { |
| 282 |
card = ((CardinalityChecker) next).getRequiredCardinality(); |
| 283 |
next = ((CardinalityChecker) next).getBaseExpression(); |
| 284 |
} |
| 285 |
if ((next.getImplementationMethod() & PROCESS_METHOD) != 0 && !(requiredItemType instanceof DocumentNodeTest)) { |
| 286 |
Receiver out = context.getReceiver(); |
| 287 |
TypeCheckingFilter filter = new TypeCheckingFilter(out); |
| 288 |
filter.setRequiredType(requiredItemType, card, role, getLocation()); |
| 289 |
context.setReceiver(filter); |
| 290 |
next.process(context); |
| 291 |
filter.close(); |
| 292 |
context.setReceiver(out); |
| 293 |
} else { |
| 294 |
|
| 295 |
super.process(context); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
public Expression copy(RebindingMap rebindings) { |
| 308 |
ItemChecker exp = new ItemChecker(getBaseExpression().copy(rebindings), requiredItemType, role); |
| 309 |
ExpressionTool.copyLocationInfo(this, exp); |
| 310 |
return exp; |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
public ItemType getItemType() { |
| 320 |
ItemType operandType = getBaseExpression().getItemType(); |
| 321 |
TypeHierarchy th = getConfiguration().getTypeHierarchy(); |
| 322 |
int relationship = th.relationship(requiredItemType, operandType); |
| 323 |
switch (relationship) { |
| 324 |
case TypeHierarchy.OVERLAPS: |
| 325 |
if (requiredItemType instanceof NodeTest && operandType instanceof NodeTest) { |
| 326 |
return new CombinedNodeTest((NodeTest) requiredItemType, Token.INTERSECT, (NodeTest) operandType); |
| 327 |
} else { |
| 328 |
|
| 329 |
return requiredItemType; |
| 330 |
} |
| 331 |
|
| 332 |
case TypeHierarchy.SUBSUMES: |
| 333 |
case TypeHierarchy.SAME_TYPE: |
| 334 |
|
| 335 |
return operandType; |
| 336 |
case TypeHierarchy.SUBSUMED_BY: |
| 337 |
default: |
| 338 |
return requiredItemType; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
@Override |
| 350 |
public UType getStaticUType(UType contextItemType) { |
| 351 |
return UType.fromTypeCode(requiredItemType.getPrimitiveType()); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
public boolean equals(Object other) { |
| 359 |
return super.equals(other) && |
| 360 |
requiredItemType == ((ItemChecker) other).requiredItemType; |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
@Override |
| 369 |
public int computeHashCode() { |
| 370 |
return super.computeHashCode() ^ requiredItemType.hashCode(); |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
public void export(ExpressionPresenter out) throws XPathException { |
| 380 |
out.startElement("treat", this); |
| 381 |
out.emitAttribute("as", requiredItemType.toExportString()); |
| 382 |
ExpressionPresenter.ExportOptions options = (ExpressionPresenter.ExportOptions) out.getOptions(); |
| 383 |
if ("JS".equals(options.target)) { |
| 384 |
int targetVersion = options.targetVersion; |
| 385 |
out.emitAttribute("jsTest", requiredItemType.generateJavaScriptItemTypeTest(getBaseExpression().getItemType(), targetVersion)); |
| 386 |
if (targetVersion == 2) { |
| 387 |
SequenceType st = SequenceType.makeSequenceType(requiredItemType, StaticProperty.EXACTLY_ONE); |
| 388 |
out.emitAttribute("asJ", st.toExportString2()); |
| 389 |
} |
| 390 |
} |
| 391 |
out.emitAttribute("diag", role.save()); |
| 392 |
getBaseExpression().export(out); |
| 393 |
out.endElement(); |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
public String getExpressionName() { |
| 405 |
return "treatAs"; |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
public String toString() { |
| 415 |
String typeDesc = requiredItemType.toString(); |
| 416 |
return "(" + getBaseExpression() + ") treat as " + typeDesc; |
| 417 |
} |
| 418 |
|
| 419 |
@Override |
| 420 |
public String toShortString() { |
| 421 |
return getBaseExpression().toShortString(); |
| 422 |
} |
| 423 |
|
| 424 |
} |
| 425 |
|
| 426 |
|