| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.expr; |
| 9 |
|
| 10 |
import net.sf.saxon.event.EventBuffer; |
| 11 |
import net.sf.saxon.event.EventMonitor; |
| 12 |
import net.sf.saxon.expr.instruct.BreakInstr; |
| 13 |
import net.sf.saxon.expr.parser.ContextItemStaticInfo; |
| 14 |
import net.sf.saxon.expr.parser.ExpressionTool; |
| 15 |
import net.sf.saxon.expr.parser.ExpressionVisitor; |
| 16 |
import net.sf.saxon.expr.parser.RebindingMap; |
| 17 |
import net.sf.saxon.lib.NamespaceConstant; |
| 18 |
import net.sf.saxon.om.*; |
| 19 |
import net.sf.saxon.pattern.QNameTest; |
| 20 |
import net.sf.saxon.trace.ExpressionPresenter; |
| 21 |
import net.sf.saxon.trans.XPathException; |
| 22 |
import net.sf.saxon.type.ItemType; |
| 23 |
import net.sf.saxon.type.Type; |
| 24 |
import net.sf.saxon.value.Cardinality; |
| 25 |
|
| 26 |
import javax.xml.transform.ErrorListener; |
| 27 |
import javax.xml.transform.TransformerException; |
| 28 |
import java.util.ArrayList; |
| 29 |
import java.util.List; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class TryCatch extends Expression { |
| 40 |
|
| 41 |
private Operand tryOp; |
| 42 |
private List<CatchClause> catchClauses = new ArrayList<>(); |
| 43 |
private boolean rollbackOutput; |
| 44 |
|
| 45 |
public TryCatch(Expression tryExpr) { |
| 46 |
this.tryOp = new Operand(this, tryExpr, OperandRole.SAME_FOCUS_ACTION); |
| 47 |
} |
| 48 |
|
| 49 |
public void addCatchExpression(QNameTest test, Expression catchExpr) { |
| 50 |
CatchClause clause = new CatchClause(); |
| 51 |
clause.catchOp = new Operand(this, catchExpr, OperandRole.SAME_FOCUS_ACTION); |
| 52 |
clause.nameTest = test; |
| 53 |
catchClauses.add(clause); |
| 54 |
} |
| 55 |
|
| 56 |
public void setRollbackOutput(boolean rollback) { |
| 57 |
this.rollbackOutput = rollback; |
| 58 |
} |
| 59 |
|
| 60 |
public boolean isRollbackOutput() { |
| 61 |
return this.rollbackOutput; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public Operand getTryOperand() { |
| 70 |
return tryOp; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public Expression getTryExpr() { |
| 79 |
return tryOp.getChildExpression(); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public List<CatchClause> getCatchClauses() { |
| 89 |
return catchClauses; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
@Override |
| 100 |
public boolean isInstruction() { |
| 101 |
return true; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
@Override |
| 113 |
public boolean allowExtractingCommonSubexpressions() { |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public int computeCardinality() { |
| 122 |
int card = getTryExpr().getCardinality(); |
| 123 |
for (CatchClause catchClause : catchClauses) { |
| 124 |
card = Cardinality.union(card, catchClause.catchOp.getChildExpression().getCardinality()); |
| 125 |
} |
| 126 |
return card; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public ItemType getItemType() { |
| 135 |
ItemType type = getTryExpr().getItemType(); |
| 136 |
for (CatchClause catchClause : catchClauses) { |
| 137 |
type = Type.getCommonSuperType(type, catchClause.catchOp.getChildExpression().getItemType()); |
| 138 |
} |
| 139 |
return type; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
@Override |
| 151 |
public Iterable<Operand> operands() { |
| 152 |
List<Operand> list = new ArrayList<>(); |
| 153 |
list.add(tryOp); |
| 154 |
for (CatchClause cc : catchClauses) { |
| 155 |
list.add(cc.catchOp); |
| 156 |
} |
| 157 |
return list; |
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException { |
| 162 |
optimizeChildren(visitor, contextInfo); |
| 163 |
Expression e = getParentExpression(); |
| 164 |
while (e != null) { |
| 165 |
if (e instanceof LetExpression && ExpressionTool.dependsOnVariable(getTryExpr(), new Binding[]{(LetExpression)e})) { |
| 166 |
((LetExpression)e).setNeedsEagerEvaluation(true); |
| 167 |
} |
| 168 |
e = e.getParentExpression(); |
| 169 |
} |
| 170 |
return this; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
@Override |
| 182 |
public int getImplementationMethod() { |
| 183 |
return ITERATE_METHOD; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
@Override |
| 193 |
public boolean equals(Object other) { |
| 194 |
return other instanceof TryCatch && ((TryCatch)other).tryOp.getChildExpression().isEqual(tryOp.getChildExpression()) |
| 195 |
&& ((TryCatch)other).catchClauses.equals(catchClauses); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public int computeHashCode() { |
| 203 |
int h = 0x836b12a0; |
| 204 |
for (int i = 0; i < catchClauses.size(); i++) { |
| 205 |
h ^= catchClauses.get(i).hashCode()<<i; |
| 206 |
} |
| 207 |
return h + tryOp.getChildExpression().hashCode(); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public Expression copy(RebindingMap rebindings) { |
| 219 |
TryCatch t2 = new TryCatch(tryOp.getChildExpression().copy(rebindings)); |
| 220 |
for (CatchClause clause : catchClauses) { |
| 221 |
t2.addCatchExpression(clause.nameTest, clause.catchOp.getChildExpression().copy(rebindings)); |
| 222 |
} |
| 223 |
t2.setRollbackOutput(rollbackOutput); |
| 224 |
ExpressionTool.copyLocationInfo(this, t2); |
| 225 |
return t2; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
public Item evaluateItem(XPathContext c) throws XPathException { |
| 235 |
XPathContext c1 = c.newMinorContext(); |
| 236 |
try { |
| 237 |
return ExpressionTool.eagerEvaluate(tryOp.getChildExpression(), c1).head(); |
| 238 |
} catch (XPathException err) { |
| 239 |
if (err.isGlobalError()) { |
| 240 |
err.setIsGlobalError(false); |
| 241 |
} else { |
| 242 |
StructuredQName code = err.getErrorCodeQName(); |
| 243 |
if (code == null) { |
| 244 |
code = new StructuredQName("saxon", NamespaceConstant.SAXON, "SXWN9000"); |
| 245 |
} |
| 246 |
for (CatchClause clause : catchClauses) { |
| 247 |
if (clause.nameTest.matches(code)) { |
| 248 |
Expression caught = clause.catchOp.getChildExpression(); |
| 249 |
XPathContextMajor c2 = c.newContext(); |
| 250 |
c2.setCurrentException(err); |
| 251 |
return caught.evaluateItem(c2); |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
err.setHasBeenReported(false); |
| 256 |
throw err; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public SequenceIterator<?> iterate(XPathContext c) throws XPathException { |
| 266 |
XPathContextMajor c1 = c.newContext(); |
| 267 |
c1.createThreadManager(); |
| 268 |
c1.setErrorListener(new FilteringErrorListener(c.getErrorListener())); |
| 269 |
try { |
| 270 |
|
| 271 |
Sequence<?> v = ExpressionTool.eagerEvaluate(tryOp.getChildExpression(), c1); |
| 272 |
c1.waitForChildThreads(); |
| 273 |
|
| 274 |
TailCallLoop.TailCallInfo tci = c1.getTailCallInfo(); |
| 275 |
if (tci instanceof BreakInstr) { |
| 276 |
((BreakInstr)tci).markContext(c); |
| 277 |
} |
| 278 |
return v.iterate(); |
| 279 |
} catch (XPathException err) { |
| 280 |
if (err.isGlobalError()) { |
| 281 |
err.setIsGlobalError(false); |
| 282 |
} else { |
| 283 |
StructuredQName code = err.getErrorCodeQName(); |
| 284 |
for (CatchClause clause : catchClauses) { |
| 285 |
if (code == null) { |
| 286 |
code = new StructuredQName("saxon", NamespaceConstant.SAXON, "SXWN9000"); |
| 287 |
} |
| 288 |
if (clause.nameTest.matches(code)) { |
| 289 |
Expression caught = clause.catchOp.getChildExpression(); |
| 290 |
XPathContextMajor c2 = c.newContext(); |
| 291 |
c2.setCurrentException(err); |
| 292 |
|
| 293 |
Sequence<?> v = ExpressionTool.eagerEvaluate(caught, c2); |
| 294 |
TailCallLoop.TailCallInfo tci = c2.getTailCallInfo(); |
| 295 |
if (tci instanceof BreakInstr) { |
| 296 |
((BreakInstr) tci).markContext(c); |
| 297 |
} |
| 298 |
return v.iterate(); |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
err.setHasBeenReported(false); |
| 303 |
throw err; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
@Override |
| 308 |
public void process(XPathContext context) throws XPathException { |
| 309 |
XPathContextMajor c1 = context.newContext(); |
| 310 |
c1.createThreadManager(); |
| 311 |
c1.setErrorListener(new FilteringErrorListener(context.getErrorListener())); |
| 312 |
EventBuffer buffer = null; |
| 313 |
EventMonitor monitor = null; |
| 314 |
if (rollbackOutput) { |
| 315 |
buffer = new EventBuffer(context.getReceiver().getPipelineConfiguration()); |
| 316 |
c1.setReceiver(buffer); |
| 317 |
} else { |
| 318 |
monitor = new EventMonitor(context.getReceiver()); |
| 319 |
c1.setReceiver(monitor); |
| 320 |
} |
| 321 |
try { |
| 322 |
tryOp.getChildExpression().process(c1); |
| 323 |
c1.waitForChildThreads(); |
| 324 |
|
| 325 |
TailCallLoop.TailCallInfo tci = c1.getTailCallInfo(); |
| 326 |
if (tci instanceof BreakInstr) { |
| 327 |
((BreakInstr) tci).markContext(context); |
| 328 |
} |
| 329 |
if (rollbackOutput) { |
| 330 |
buffer.replay(context.getReceiver()); |
| 331 |
} |
| 332 |
} catch (XPathException err) { |
| 333 |
if (err.isGlobalError()) { |
| 334 |
err.setIsGlobalError(false); |
| 335 |
} else { |
| 336 |
StructuredQName code = err.getErrorCodeQName(); |
| 337 |
if (code == null) { |
| 338 |
code = new StructuredQName("saxon", NamespaceConstant.SAXON, "SXWN9000"); |
| 339 |
} |
| 340 |
for (CatchClause clause : catchClauses) { |
| 341 |
if (clause.nameTest.matches(code)) { |
| 342 |
if (monitor != null && monitor.hasBeenWrittenTo()) { |
| 343 |
|
| 344 |
String message = err.getMessage() + |
| 345 |
". The error could not be caught, because rollback-output=no was specified, and output was already written to the result tree"; |
| 346 |
XPathException xe = new XPathException(message, "XTDE3530"); |
| 347 |
xe.setLocation(err.getLocator()); |
| 348 |
xe.setXPathContext(context); |
| 349 |
throw xe; |
| 350 |
} |
| 351 |
Expression caught = clause.catchOp.getChildExpression(); |
| 352 |
XPathContextMajor c2 = context.newContext(); |
| 353 |
c2.setCurrentException(err); |
| 354 |
|
| 355 |
Sequence<?> v = ExpressionTool.eagerEvaluate(caught, c2); |
| 356 |
TailCallLoop.TailCallInfo tci = c2.getTailCallInfo(); |
| 357 |
if (tci instanceof BreakInstr) { |
| 358 |
((BreakInstr) tci).markContext(context); |
| 359 |
} |
| 360 |
((GroundedValue<?>) v).iterate().forEachOrFail( |
| 361 |
item -> c2.getReceiver().append(item) |
| 362 |
); |
| 363 |
return; |
| 364 |
} |
| 365 |
} |
| 366 |
} |
| 367 |
err.setHasBeenReported(false); |
| 368 |
throw err; |
| 369 |
|
| 370 |
} |
| 371 |
|
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
@Override |
| 382 |
public String getExpressionName() { |
| 383 |
return "tryCatch"; |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
public void export(ExpressionPresenter out) throws XPathException { |
| 394 |
out.startElement("try", this); |
| 395 |
if (rollbackOutput) { |
| 396 |
out.emitAttribute("flags", "r"); |
| 397 |
} |
| 398 |
tryOp.getChildExpression().export(out); |
| 399 |
for (CatchClause clause : catchClauses) { |
| 400 |
out.startElement("catch"); |
| 401 |
out.emitAttribute("err", clause.nameTest.toString()); |
| 402 |
if ("JS".equals(((ExpressionPresenter.ExportOptions) out.getOptions()).target)) { |
| 403 |
int targetVersion = ((ExpressionPresenter.ExportOptions) out.getOptions()).targetVersion; |
| 404 |
out.emitAttribute("test", clause.nameTest.generateJavaScriptNameTest(targetVersion)); |
| 405 |
} |
| 406 |
clause.catchOp.getChildExpression().export(out); |
| 407 |
out.endElement(); |
| 408 |
} |
| 409 |
out.endElement(); |
| 410 |
} |
| 411 |
|
| 412 |
public static class CatchClause { |
| 413 |
public int slotNumber = -1; |
| 414 |
public Operand catchOp; |
| 415 |
public QNameTest nameTest; |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
private class FilteringErrorListener implements ErrorListener { |
| 423 |
|
| 424 |
private ErrorListener base; |
| 425 |
|
| 426 |
FilteringErrorListener(ErrorListener base) { |
| 427 |
this.base = base; |
| 428 |
} |
| 429 |
|
| 430 |
private boolean isCaught(TransformerException err) { |
| 431 |
if (err instanceof XPathException) { |
| 432 |
StructuredQName code = ((XPathException) err).getErrorCodeQName(); |
| 433 |
for (CatchClause clause : catchClauses) { |
| 434 |
if (clause.nameTest.matches(code)) { |
| 435 |
return true; |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
return false; |
| 440 |
} |
| 441 |
|
| 442 |
@Override |
| 443 |
public void warning(TransformerException exception) throws TransformerException { |
| 444 |
base.warning(exception); |
| 445 |
} |
| 446 |
|
| 447 |
@Override |
| 448 |
public void error(TransformerException exception) throws TransformerException { |
| 449 |
if (!isCaught(exception)) { |
| 450 |
base.error(exception); |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
@Override |
| 455 |
public void fatalError(TransformerException exception) throws TransformerException { |
| 456 |
if (!isCaught(exception)) { |
| 457 |
base.fatalError(exception); |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
@Override |
| 469 |
public String getStreamerName() { |
| 470 |
return "TryCatch"; |
| 471 |
} |
| 472 |
} |
| 473 |
|