| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr; |
| 9 |
|
| 10 |
import net.sf.saxon.event.ReceiverOptions; |
| 11 |
import net.sf.saxon.expr.instruct.Block; |
| 12 |
import net.sf.saxon.expr.instruct.Choose; |
| 13 |
import net.sf.saxon.expr.instruct.ValueOf; |
| 14 |
import net.sf.saxon.expr.parser.*; |
| 15 |
import net.sf.saxon.lib.NamespaceConstant; |
| 16 |
import net.sf.saxon.ma.arrays.ArrayItemType; |
| 17 |
import net.sf.saxon.ma.map.MapType; |
| 18 |
import net.sf.saxon.om.*; |
| 19 |
import net.sf.saxon.pattern.NameTest; |
| 20 |
import net.sf.saxon.pattern.NodeKindTest; |
| 21 |
import net.sf.saxon.pattern.NodeTest; |
| 22 |
import net.sf.saxon.trans.XPathException; |
| 23 |
import net.sf.saxon.tree.iter.AtomizingIterator; |
| 24 |
import net.sf.saxon.tree.iter.EmptyIterator; |
| 25 |
import net.sf.saxon.tree.iter.UntypedAtomizingIterator; |
| 26 |
import net.sf.saxon.type.*; |
| 27 |
import net.sf.saxon.value.AtomicValue; |
| 28 |
import net.sf.saxon.value.Cardinality; |
| 29 |
import net.sf.saxon.value.EmptySequence; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public final class Atomizer extends UnaryExpression { |
| 38 |
|
| 39 |
private boolean untyped = false; |
| 40 |
private boolean singleValued = false; |
| 41 |
private ItemType operandItemType = null; |
| 42 |
private RoleDiagnostic roleDiagnostic = null; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public Atomizer(Expression sequence, RoleDiagnostic role) { |
| 52 |
super(sequence); |
| 53 |
this.roleDiagnostic = role; |
| 54 |
sequence.setFlattened(true); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public static Expression makeAtomizer(Expression sequence, RoleDiagnostic role) { |
| 66 |
if (sequence instanceof Literal && ((Literal) sequence).getValue() instanceof AtomicSequence) { |
| 67 |
return sequence; |
| 68 |
} else { |
| 69 |
return new Atomizer(sequence, role); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
protected OperandRole getOperandRole() { |
| 74 |
return OperandRole.ATOMIC_SEQUENCE; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public int getImplementationMethod() { |
| 87 |
return ITERATE_METHOD | WATCH_METHOD; |
| 88 |
} |
| 89 |
|
| 90 |
public ItemType getOperandItemType() { |
| 91 |
if (operandItemType == null) { |
| 92 |
operandItemType = getBaseExpression().getItemType(); |
| 93 |
} |
| 94 |
return operandItemType; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public Expression simplify() throws XPathException { |
| 104 |
|
| 105 |
untyped = !getPackageData().isSchemaAware(); |
| 106 |
computeSingleValued(getConfiguration().getTypeHierarchy()); |
| 107 |
Expression operand = getBaseExpression().simplify(); |
| 108 |
if (operand instanceof Literal) { |
| 109 |
GroundedValue val = ((Literal) operand).getValue(); |
| 110 |
|
| 111 |
if (val instanceof AtomicValue) { |
| 112 |
return operand; |
| 113 |
} |
| 114 |
SequenceIterator iter = val.iterate(); |
| 115 |
Item i; |
| 116 |
while ((i = iter.next()) != null) { |
| 117 |
if (i instanceof NodeInfo) { |
| 118 |
return this; |
| 119 |
} |
| 120 |
if (i instanceof Function) { |
| 121 |
if (((Function)i).isArray()) { |
| 122 |
return this; |
| 123 |
} else if (((Function)i).isMap()) { |
| 124 |
XPathException err = new XPathException( |
| 125 |
expandMessage("Cannot atomize a map (" + i.toShortString() + ")"), "FOTY0013"); |
| 126 |
err.setIsTypeError(true); |
| 127 |
err.setLocation(getLocation()); |
| 128 |
throw err; |
| 129 |
} else { |
| 130 |
XPathException err = new XPathException( |
| 131 |
expandMessage("Cannot atomize a function item"), "FOTY0013"); |
| 132 |
err.setIsTypeError(true); |
| 133 |
err.setLocation(getLocation()); |
| 134 |
throw err; |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
return operand; |
| 141 |
} else if (operand instanceof ValueOf && (((ValueOf) operand).getOptions() & ReceiverOptions.DISABLE_ESCAPING) == 0) { |
| 142 |
|
| 143 |
return ((ValueOf) operand).convertToCastAsString(); |
| 144 |
} |
| 145 |
setBaseExpression(operand); |
| 146 |
return this; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 155 |
getOperand().typeCheck(visitor, contextInfo); |
| 156 |
untyped = untyped | !visitor.getStaticContext().getPackageData().isSchemaAware(); |
| 157 |
|
| 158 |
|
| 159 |
final TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy(); |
| 160 |
computeSingleValued(th); |
| 161 |
resetLocalStaticProperties(); |
| 162 |
ItemType operandType = getOperandItemType(); |
| 163 |
if (th.isSubType(operandType, BuiltInAtomicType.ANY_ATOMIC)) { |
| 164 |
return getBaseExpression(); |
| 165 |
} |
| 166 |
if (!operandType.isAtomizable(th)) { |
| 167 |
XPathException err; |
| 168 |
if (operandType instanceof FunctionItemType) { |
| 169 |
String thing = operandType instanceof MapType ? "map" : "function item"; |
| 170 |
err = new XPathException( |
| 171 |
expandMessage("Cannot atomize a " + thing), "FOTY0013"); |
| 172 |
} else { |
| 173 |
err = new XPathException( |
| 174 |
expandMessage("Cannot atomize an element that is defined in the schema to have element-only content"), "FOTY0012"); |
| 175 |
} |
| 176 |
err.setIsTypeError(true); |
| 177 |
err.setLocation(getLocation()); |
| 178 |
throw err; |
| 179 |
} |
| 180 |
getBaseExpression().setFlattened(true); |
| 181 |
return this; |
| 182 |
} |
| 183 |
|
| 184 |
private void computeSingleValued(TypeHierarchy th) { |
| 185 |
ItemType operandType = getOperandItemType(); |
| 186 |
if (th.relationship(operandType, ArrayItemType.ANY_ARRAY_TYPE) != TypeHierarchy.DISJOINT) { |
| 187 |
singleValued = false; |
| 188 |
} else { |
| 189 |
singleValued = untyped; |
| 190 |
if (!singleValued) { |
| 191 |
ItemType nodeType = getBaseExpression().getItemType(); |
| 192 |
if (nodeType instanceof NodeTest) { |
| 193 |
SchemaType st = ((NodeTest) nodeType).getContentType(); |
| 194 |
if (st == Untyped.getInstance() || st.isAtomicType() || (st.isComplexType() && st != AnyType.getInstance())) { |
| 195 |
singleValued = true; |
| 196 |
} |
| 197 |
if (!nodeType.getUType().overlaps(UType.ELEMENT.union(UType.ATTRIBUTE))) { |
| 198 |
singleValued = true; |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
private String expandMessage(String message) { |
| 211 |
if (roleDiagnostic == null) { |
| 212 |
return message; |
| 213 |
} else { |
| 214 |
return message + ". Found while atomizing the " + roleDiagnostic.getMessage(); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 237 |
Expression exp = super.optimize(visitor, contextInfo); |
| 238 |
if (exp == this) { |
| 239 |
final TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy(); |
| 240 |
Expression operand = getBaseExpression(); |
| 241 |
if (th.isSubType(operand.getItemType(), BuiltInAtomicType.ANY_ATOMIC)) { |
| 242 |
return operand; |
| 243 |
} |
| 244 |
if (operand instanceof ValueOf && (((ValueOf) operand).getOptions() & ReceiverOptions.DISABLE_ESCAPING) == 0) { |
| 245 |
|
| 246 |
Expression cast = ((ValueOf) operand).convertToCastAsString(); |
| 247 |
return cast.optimize(visitor, contextInfo); |
| 248 |
} |
| 249 |
if (operand instanceof LetExpression || operand instanceof ForExpression) { |
| 250 |
|
| 251 |
Expression action = ((Assignation) operand).getAction(); |
| 252 |
((Assignation) operand).setAction(new Atomizer(action, roleDiagnostic)); |
| 253 |
return operand.optimize(visitor, contextInfo); |
| 254 |
} |
| 255 |
if (operand instanceof Choose) { |
| 256 |
|
| 257 |
((Choose)operand).atomizeActions(); |
| 258 |
return operand.optimize(visitor, contextInfo); |
| 259 |
} |
| 260 |
if (operand instanceof Block) { |
| 261 |
|
| 262 |
|
| 263 |
Operand[] children = ((Block) operand).getOperanda(); |
| 264 |
Expression[] atomizedChildren = new Expression[children.length]; |
| 265 |
for (int i = 0; i < children.length; i++) { |
| 266 |
atomizedChildren[i] = new Atomizer(children[i].getChildExpression(), roleDiagnostic); |
| 267 |
} |
| 268 |
Block newBlock = new Block(atomizedChildren); |
| 269 |
return newBlock.typeCheck(visitor, contextInfo).optimize(visitor, contextInfo); |
| 270 |
} |
| 271 |
if (untyped && operand instanceof AxisExpression && |
| 272 |
((AxisExpression)operand).getAxis() == AxisInfo.ATTRIBUTE && |
| 273 |
((AxisExpression) operand).getNodeTest() instanceof NameTest && |
| 274 |
!((AxisExpression) operand).isContextPossiblyUndefined()) { |
| 275 |
StructuredQName name = ((AxisExpression) operand).getNodeTest().getMatchingNodeName(); |
| 276 |
FingerprintedQName qName = new FingerprintedQName(name, visitor.getConfiguration().getNamePool()); |
| 277 |
AttributeGetter ag = new AttributeGetter(qName); |
| 278 |
int checks = 0; |
| 279 |
if (!(((AxisExpression) operand).getContextItemType() instanceof NodeTest)) { |
| 280 |
checks = AttributeGetter.CHECK_CONTEXT_ITEM_IS_NODE; |
| 281 |
} |
| 282 |
ag.setRequiredChecks(checks); |
| 283 |
ExpressionTool.copyLocationInfo(this, ag); |
| 284 |
return ag; |
| 285 |
} |
| 286 |
} |
| 287 |
return exp; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
public boolean isUntyped() { |
| 296 |
return untyped; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
public int computeSpecialProperties() { |
| 307 |
int p = super.computeSpecialProperties(); |
| 308 |
p &= ~StaticProperty.NODESET_PROPERTIES; |
| 309 |
return p | StaticProperty.NO_NODES_NEWLY_CREATED; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
@Override |
| 317 |
public void resetLocalStaticProperties() { |
| 318 |
super.resetLocalStaticProperties(); |
| 319 |
operandItemType = null; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
public Expression copy(RebindingMap rebindings) { |
| 331 |
Atomizer copy = new Atomizer(getBaseExpression().copy(rebindings), roleDiagnostic); |
| 332 |
copy.untyped = untyped; |
| 333 |
copy.singleValued = singleValued; |
| 334 |
ExpressionTool.copyLocationInfo(this, copy); |
| 335 |
return copy; |
| 336 |
} |
| 337 |
|
| 338 |
@Override |
| 339 |
public String getStreamerName() { |
| 340 |
return "Atomizer"; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
public SequenceIterator<?> iterate(XPathContext context) throws XPathException { |
| 349 |
try { |
| 350 |
SequenceIterator base = getBaseExpression().iterate(context); |
| 351 |
return getAtomizingIterator(base, untyped && operandItemType instanceof NodeTest); |
| 352 |
} catch (XPathException e) { |
| 353 |
if (roleDiagnostic == null || !e.getErrorCodeQName().hasURI(NamespaceConstant.ERR)) { |
| 354 |
throw e; |
| 355 |
} else { |
| 356 |
String message = expandMessage(e.getMessage()); |
| 357 |
XPathException e2 = new XPathException(message, e.getErrorCodeLocalPart(), e.getLocator()); |
| 358 |
e2.setXPathContext(context); |
| 359 |
e2.maybeSetLocation(getLocation()); |
| 360 |
throw e2; |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
public AtomicValue evaluateItem(XPathContext context) throws XPathException { |
| 371 |
Item i = getBaseExpression().evaluateItem(context); |
| 372 |
if (i == null) { |
| 373 |
return null; |
| 374 |
} else { |
| 375 |
return i.atomize().head(); |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
public ItemType getItemType() { |
| 388 |
operandItemType = getBaseExpression().getItemType(); |
| 389 |
TypeHierarchy th = getConfiguration().getTypeHierarchy(); |
| 390 |
return getAtomizedItemType(getBaseExpression(), untyped, th); |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
public static ItemType getAtomizedItemType(Expression operand, boolean alwaysUntyped, TypeHierarchy th) { |
| 404 |
ItemType in = operand.getItemType(); |
| 405 |
if (in.isPlainType()) { |
| 406 |
return in; |
| 407 |
} else if (in instanceof NodeTest) { |
| 408 |
UType kinds = in.getUType(); |
| 409 |
if (alwaysUntyped) { |
| 410 |
|
| 411 |
|
| 412 |
if (STRING_KINDS.subsumes(kinds)) { |
| 413 |
return BuiltInAtomicType.STRING; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
if (UNTYPED_IF_UNTYPED_KINDS.subsumes(kinds)) { |
| 419 |
return BuiltInAtomicType.UNTYPED_ATOMIC; |
| 420 |
} |
| 421 |
} else { |
| 422 |
if (UNTYPED_KINDS.subsumes(kinds)) { |
| 423 |
return BuiltInAtomicType.UNTYPED_ATOMIC; |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
return in.getAtomizedItemType(); |
| 428 |
} else if (in instanceof JavaExternalObjectType) { |
| 429 |
return in.getAtomizedItemType(); |
| 430 |
} else if (in instanceof ArrayItemType) { |
| 431 |
PlainType ait = ((ArrayItemType)in).getMemberType().getPrimaryType().getAtomizedItemType(); |
| 432 |
return ait == null ? ErrorType.getInstance() : ait; |
| 433 |
} else if (in instanceof FunctionItemType) { |
| 434 |
return ErrorType.getInstance(); |
| 435 |
} |
| 436 |
return BuiltInAtomicType.ANY_ATOMIC; |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
public static final UType STRING_KINDS = |
| 443 |
UType.NAMESPACE.union(UType.COMMENT).union(UType.PI); |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
public static final UType UNTYPED_KINDS = |
| 450 |
UType.TEXT.union(UType.DOCUMENT); |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
public static final UType UNTYPED_IF_UNTYPED_KINDS = |
| 457 |
UType.TEXT.union(UType.ELEMENT).union(UType.DOCUMENT).union(UType.ATTRIBUTE); |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
public int computeCardinality() { |
| 464 |
ItemType in = getOperandItemType(); |
| 465 |
Expression operand = getBaseExpression(); |
| 466 |
if (singleValued) { |
| 467 |
return operand.getCardinality(); |
| 468 |
} else if (untyped && in instanceof NodeTest) { |
| 469 |
return operand.getCardinality(); |
| 470 |
} else if (Cardinality.allowsMany(operand.getCardinality())) { |
| 471 |
return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 472 |
} else if (in.isPlainType()) { |
| 473 |
return operand.getCardinality(); |
| 474 |
} else if (in instanceof NodeTest) { |
| 475 |
SchemaType schemaType = ((NodeTest) in).getContentType(); |
| 476 |
if (schemaType.isAtomicType()) { |
| 477 |
|
| 478 |
return operand.getCardinality(); |
| 479 |
} |
| 480 |
} |
| 481 |
return StaticProperty.ALLOWS_ZERO_OR_MORE; |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
public PathMap.PathMapNodeSet addToPathMap(PathMap pathMap, PathMap.PathMapNodeSet pathMapNodeSet) { |
| 506 |
PathMap.PathMapNodeSet result = getBaseExpression().addToPathMap(pathMap, pathMapNodeSet); |
| 507 |
if (result != null) { |
| 508 |
TypeHierarchy th = getConfiguration().getTypeHierarchy(); |
| 509 |
ItemType operandItemType = getBaseExpression().getItemType(); |
| 510 |
if (th.relationship(NodeKindTest.ELEMENT, operandItemType) != TypeHierarchy.DISJOINT || |
| 511 |
th.relationship(NodeKindTest.DOCUMENT, operandItemType) != TypeHierarchy.DISJOINT) { |
| 512 |
result.setAtomized(); |
| 513 |
} |
| 514 |
} |
| 515 |
return null; |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
public static SequenceIterator<? extends AtomicValue> getAtomizingIterator(SequenceIterator base, boolean oneToOne) throws XPathException { |
| 532 |
int properties = base.getProperties(); |
| 533 |
if ((properties & SequenceIterator.LAST_POSITION_FINDER) != 0) { |
| 534 |
int count = ((LastPositionFinder) base).getLength(); |
| 535 |
if (count == 0) { |
| 536 |
return EmptyIterator.emptyIterator(); |
| 537 |
} else if (count == 1) { |
| 538 |
Item first = base.next(); |
| 539 |
return first.atomize().iterate(); |
| 540 |
} |
| 541 |
} else if ((properties & SequenceIterator.ATOMIZING) != 0) { |
| 542 |
return new AxisAtomizingIterator((AtomizedValueIterator)base); |
| 543 |
} |
| 544 |
if (oneToOne) { |
| 545 |
return new UntypedAtomizingIterator(base); |
| 546 |
} else { |
| 547 |
return new AtomizingIterator(base); |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
public static AtomicSequence atomize(Sequence<?> sequence) throws XPathException { |
| 552 |
if (sequence instanceof AtomicSequence) { |
| 553 |
return (AtomicSequence)sequence; |
| 554 |
} else if (sequence instanceof EmptySequence) { |
| 555 |
return EmptyAtomicSequence.getInstance(); |
| 556 |
} else { |
| 557 |
SequenceIterator<? extends AtomicValue> iter = getAtomizingIterator(sequence.iterate(), false); |
| 558 |
return new AtomicArray(iter); |
| 559 |
} |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
public String getExpressionName() { |
| 568 |
return "data"; |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
@Override |
| 577 |
public String toString() { |
| 578 |
return "data(" + getBaseExpression().toString() + ")"; |
| 579 |
} |
| 580 |
|
| 581 |
@Override |
| 582 |
public String toShortString() { |
| 583 |
return getBaseExpression().toShortString(); |
| 584 |
} |
| 585 |
|
| 586 |
|
| 587 |
} |
| 588 |
|