| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.trans; |
| 9 | |
| 10 | import net.sf.saxon.expr.Expression; |
| 11 | import net.sf.saxon.expr.XPathContext; |
| 12 | import net.sf.saxon.expr.parser.ExplicitLocation; |
| 13 | import net.sf.saxon.expr.parser.Location; |
| 14 | import net.sf.saxon.lib.NamespaceConstant; |
| 15 | import net.sf.saxon.om.Sequence; |
| 16 | import net.sf.saxon.om.StructuredQName; |
| 17 | |
| 18 | import javax.xml.transform.SourceLocator; |
| 19 | import javax.xml.transform.TransformerException; |
| 20 | |
| 21 | /** |
| 22 | * XPathException is used to indicate an error (static or dynamic) in an XPath expression, |
| 23 | * or in a query or stylesheet. |
| 24 | */ |
| 25 | |
| 26 | public class XPathException extends TransformerException { |
| 27 | |
| 28 | private boolean isTypeError = false; |
| 29 | private boolean isSyntaxError = false; |
| 30 | private boolean isStaticError = false; |
| 31 | private boolean isGlobalError = false; |
| 32 | private String hostLanguage = null; |
| 33 | private StructuredQName errorCode; |
| 34 | private Sequence errorObject; |
| 35 | private Expression failingExpression; |
| 36 | private boolean hasBeenReported = false; |
| 37 | transient XPathContext context; |
| 38 | // declared transient because a compiled stylesheet might contain a "deferred action" dynamic error |
| 39 | // and the EarlyEvaluationContext links back to the source stylesheet. |
| 40 | |
| 41 | /** |
| 42 | * Create an XPathException with an error message |
| 43 | * |
| 44 | * @param message the message explaining what is wrong. This should not include location information. |
| 45 | */ |
| 46 | |
| 47 | public XPathException(String message) { |
| 48 | super(message); |
| 49 | breakPoint(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Create an XPathException that wraps another exception |
| 54 | * |
| 55 | * @param err the wrapped error or exception |
| 56 | */ |
| 57 | |
| 58 | public XPathException(Throwable err) { |
| 59 | super(err); |
| 60 | breakPoint(); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Create an XPathException that supplies an error message and wraps an underlying exception |
| 65 | * |
| 66 | * @param message the error message (which should generally explain what Saxon was doing when the |
| 67 | * underlying exception occurred) |
| 68 | * @param err the underlying exception (the cause of this exception) |
| 69 | */ |
| 70 | |
| 71 | public XPathException(String message, Throwable err) { |
| 72 | super(message, err); |
| 73 | breakPoint(); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Create an XPathException that supplies an error message and supplies location information |
| 78 | * |
| 79 | * @param message the error message |
| 80 | * @param loc indicates where in the user-written query or stylesheet (or sometimes in a source |
| 81 | * document) the error occurred |
| 82 | */ |
| 83 | |
| 84 | public XPathException(String message, String errorCode, Location loc) { |
| 85 | this(message, errorCode); |
| 86 | setLocator(loc); |
| 87 | breakPoint(); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Create an XPathException that supplies an error message and wraps an underlying exception |
| 92 | * and supplies location information |
| 93 | * |
| 94 | * @param message the error message (which should generally explain what Saxon was doing when the |
| 95 | * underlying exception occurred) |
| 96 | * @param loc indicates where in the user-written query or stylesheet (or sometimes in a source |
| 97 | * document) the error occurred |
| 98 | * @param err the underlying exception (the cause of this exception) |
| 99 | */ |
| 100 | |
| 101 | public XPathException(String message, Location loc, Throwable err) { |
| 102 | super(message, loc, err); |
| 103 | breakPoint(); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Create an XPathException that supplies an error message and an error code |
| 108 | * |
| 109 | * @param message the error message |
| 110 | * @param errorCode the error code - an eight-character code, which is taken to be in the standard |
| 111 | * system error code namespace |
| 112 | */ |
| 113 | |
| 114 | public XPathException(String message, String errorCode) { |
| 115 | super(message); |
| 116 | setErrorCode(errorCode); |
| 117 | breakPoint(); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Create an XPathException that supplies an error message and an error code and provides the |
| 122 | * dynamic context |
| 123 | * |
| 124 | * @param message the error message |
| 125 | * @param errorCode the error code - an eight-character code, which is taken to be in the standard |
| 126 | * system error code namespace |
| 127 | * @param context the dynamic evaluation context |
| 128 | */ |
| 129 | |
| 130 | public XPathException(String message, String errorCode, XPathContext context) { |
| 131 | super(message); |
| 132 | setErrorCode(errorCode); |
| 133 | setXPathContext(context); |
| 134 | breakPoint(); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Breakpoint for debugging when we need to catch an error. Change to "assert false" to get |
| 139 | * a stack trace when an exception is created. |
| 140 | */ |
| 141 | |
| 142 | private static void breakPoint() { |
| 143 | //System.err.println("Error!!"); |
| 144 | assert true; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Create an XPathException from an exception of another kind. If the supplied Exception is an XPathException; |
| 149 | * if its cause is an XPathException, that XPathException is returned unchanged; otherwise the |
| 150 | * supplied Exception is wrapped. |
| 151 | * |
| 152 | * @param err the supplied Exception |
| 153 | * @return an XPathException obtained from the supplied TransformerException |
| 154 | */ |
| 155 | |
| 156 | /*@NotNull*/ |
| 157 | public static XPathException makeXPathException(Exception err) { |
| 158 | if (err instanceof XPathException) { |
| 159 | return (XPathException) err; |
| 160 | } else if (err.getCause() instanceof XPathException) { |
| 161 | return (XPathException) err.getCause(); |
| 162 | } else if (err instanceof TransformerException) { |
| 163 | XPathException xe = new XPathException(err.getMessage(), err); |
| 164 | xe.setLocator(((TransformerException) err).getLocator()); |
| 165 | return xe; |
| 166 | } else { |
| 167 | return new XPathException(err); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Set dynamic context information in the exception object |
| 173 | * |
| 174 | * @param context the dynamic context at the time the exception occurred |
| 175 | */ |
| 176 | |
| 177 | public void setXPathContext(XPathContext context) { |
| 178 | this.context = context; |
| 179 | } |
| 180 | |
| 181 | public void setLocation(Location loc) { |
| 182 | if (loc != null) { |
| 183 | setLocator(loc.saveLocation()); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | |
| 188 | public Expression getFailingExpression() { |
| 189 | return failingExpression; |
| 190 | } |
| 191 | |
| 192 | public void setFailingExpression(Expression failingExpression) { |
| 193 | this.failingExpression = failingExpression; |
| 194 | } |
| 195 | |
| 196 | public void maybeSetFailingExpression(Expression failingExpression) { |
| 197 | if (this.failingExpression == null) { |
| 198 | this.failingExpression = failingExpression; |
| 199 | } |
| 200 | maybeSetLocation(failingExpression.getLocation()); |
| 201 | } |
| 202 | |
| 203 | |
| 204 | /** |
| 205 | * Method getLocator retrieves an instance of a SourceLocator |
| 206 | * object that specifies where an error occured. |
| 207 | * |
| 208 | * @return A SourceLocator object, or null if none was specified. |
| 209 | */ |
| 210 | @Override |
| 211 | public Location getLocator() { |
| 212 | SourceLocator locator = super.getLocator(); |
| 213 | if (locator == null) { |
| 214 | return null; |
| 215 | } else if (locator instanceof Location) { |
| 216 | return (Location)locator; |
| 217 | } else { |
| 218 | return new ExplicitLocation(locator); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Get the dynamic context at the time the exception occurred |
| 224 | * |
| 225 | * @return the dynamic context if known; otherwise null |
| 226 | */ |
| 227 | |
| 228 | public XPathContext getXPathContext() { |
| 229 | return context; |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Mark this exception to indicate that it represents (or does not represent) a static error |
| 234 | * |
| 235 | * @param is true if this exception is a static error |
| 236 | */ |
| 237 | |
| 238 | public void setIsStaticError(boolean is) { |
| 239 | isStaticError = is; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Ask whether this exception represents a static error |
| 244 | * |
| 245 | * @return true if this exception is a static error |
| 246 | */ |
| 247 | |
| 248 | public boolean isStaticError() { |
| 249 | return isStaticError; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Mark this exception to indicate that it represents (or does not represent) a syntax error |
| 254 | * |
| 255 | * @param is true if this exception is a syntax error |
| 256 | */ |
| 257 | |
| 258 | public void setIsSyntaxError(boolean is) { |
| 259 | if (is) { |
| 260 | isStaticError = true; |
| 261 | } |
| 262 | isSyntaxError = is; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Ask whether this exception represents a syntax error |
| 267 | * |
| 268 | * @return true if this exception is a syntax error |
| 269 | */ |
| 270 | |
| 271 | public boolean isSyntaxError() { |
| 272 | return isSyntaxError; |
| 273 | } |
| 274 | |
| 275 | |
| 276 | /** |
| 277 | * Mark this exception to indicate that it represents (or does not represent) a type error |
| 278 | * |
| 279 | * @param is true if this exception is a type error |
| 280 | */ |
| 281 | |
| 282 | public void setIsTypeError(boolean is) { |
| 283 | isTypeError = is; |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Ask whether this exception represents a type error |
| 288 | * |
| 289 | * @return true if this exception is a type error |
| 290 | */ |
| 291 | |
| 292 | public boolean isTypeError() { |
| 293 | return isTypeError; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Mark this exception to indicate that it originated while evaluating a global |
| 298 | * variable reference, and is therefore to be reported regardless of the try/catch |
| 299 | * context surrounding the variable reference |
| 300 | * |
| 301 | * @param is true if this exception is a global variable error |
| 302 | */ |
| 303 | |
| 304 | public void setIsGlobalError(boolean is) { |
| 305 | isGlobalError = is; |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Ask whether this exception originated while evaluating a global |
| 310 | * variable reference, and is therefore to be reported regardless of the try/catch |
| 311 | * context surrounding the variable reference |
| 312 | * |
| 313 | * @return true if this exception is a global variable error |
| 314 | */ |
| 315 | |
| 316 | public boolean isGlobalError() { |
| 317 | return isGlobalError; |
| 318 | } |
| 319 | |
| 320 | |
| 321 | |
| 322 | /** |
| 323 | * Set the host language code |
| 324 | * |
| 325 | * @param language a value such as "XPath", "XQuery", "XSLT Pattern" |
| 326 | */ |
| 327 | |
| 328 | public void setHostLanguage(String language) { |
| 329 | this.hostLanguage = language; |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Get the host language code |
| 334 | * |
| 335 | * @return a value such as "XPath", "XQuery", "XSLT Pattern", or null |
| 336 | */ |
| 337 | |
| 338 | public String getHostLanguage() { |
| 339 | return hostLanguage; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Set the error code. The error code is a QName; this method sets the local part of the name, |
| 344 | * setting the namespace of the error code to the standard system namespace {@link net.sf.saxon.lib.NamespaceConstant#ERR} |
| 345 | * |
| 346 | * @param code The local part of the name of the error code |
| 347 | */ |
| 348 | |
| 349 | public void setErrorCode(/*@Nullable*/ String code) { |
| 350 | if (code != null) { |
| 351 | errorCode = new StructuredQName("err", NamespaceConstant.ERR, code); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Set the error code, provided it has not already been set. |
| 357 | * The error code is a QName; this method sets the local part of the name, |
| 358 | * setting the namespace of the error code to the standard system namespace {@link NamespaceConstant#ERR} |
| 359 | * |
| 360 | * @param code The local part of the name of the error code |
| 361 | */ |
| 362 | |
| 363 | public void maybeSetErrorCode(/*@Nullable*/ String code) { |
| 364 | if (errorCode == null && code != null) { |
| 365 | errorCode = new StructuredQName("err", NamespaceConstant.ERR, code); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * Set the error code. The error code is a QName; this method sets both parts of the name. |
| 371 | * |
| 372 | * @param code The error code as a QName |
| 373 | */ |
| 374 | |
| 375 | public void setErrorCodeQName(/*@Nullable*/ StructuredQName code) { |
| 376 | errorCode = code; |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Get the error code as a QName |
| 381 | * |
| 382 | * @return the error code as a QName |
| 383 | */ |
| 384 | |
| 385 | /*@Nullable*/ |
| 386 | public StructuredQName getErrorCodeQName() { |
| 387 | return errorCode; |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Get the local part of the name of the error code |
| 392 | * |
| 393 | * @return the local part of the name of the error code |
| 394 | */ |
| 395 | |
| 396 | /*@Nullable*/ |
| 397 | public String getErrorCodeLocalPart() { |
| 398 | return errorCode == null ? null : errorCode.getLocalPart(); |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Get the namespace URI part of the name of the error code |
| 403 | * |
| 404 | * @return the namespace URI part of the name of the error code |
| 405 | */ |
| 406 | |
| 407 | /*@Nullable*/ |
| 408 | public String getErrorCodeNamespace() { |
| 409 | return errorCode == null ? null : errorCode.getURI(); |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Set the error object associated with this error. This is used by the standard XPath fn:error() function |
| 414 | * |
| 415 | * @param value the error object, as supplied to the fn:error() function |
| 416 | */ |
| 417 | |
| 418 | public void setErrorObject(Sequence<?> value) { |
| 419 | errorObject = value; |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Get the error object associated with this error. This is used by the standard XPath fn:error() function |
| 424 | * |
| 425 | * @return the error object, as supplied to the fn:error() function |
| 426 | */ |
| 427 | |
| 428 | public Sequence<?> getErrorObject() { |
| 429 | return errorObject; |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * Mark this error to indicate that it has already been reported to the error listener, and should not be |
| 434 | * reported again |
| 435 | * |
| 436 | * @param reported true if the error has been reported to the error listener |
| 437 | */ |
| 438 | |
| 439 | public void setHasBeenReported(boolean reported) { |
| 440 | hasBeenReported = reported; |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Ask whether this error is marked to indicate that it has already been reported to the error listener, |
| 445 | * and should not be reported again |
| 446 | * |
| 447 | * @return true if this error has already been reported |
| 448 | */ |
| 449 | |
| 450 | public boolean hasBeenReported() { |
| 451 | return hasBeenReported; |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Set the location of a message, only if it is not already set |
| 456 | * |
| 457 | * @param here the current location (or null) |
| 458 | */ |
| 459 | |
| 460 | public void maybeSetLocation(Location here) { |
| 461 | if (here != null) { |
| 462 | if (getLocator() == null) { |
| 463 | setLocator(here.saveLocation()); |
| 464 | } else if (getLocator().getLineNumber() == -1 |
| 465 | && !(getLocator().getSystemId() != null && here.getSystemId() != null && !getLocator().getSystemId().equals(here.getSystemId()))) { |
| 466 | setLocator(here.saveLocation()); |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Set the context of a message, only if it is not already set |
| 473 | * |
| 474 | * @param context the current XPath context (or null) |
| 475 | */ |
| 476 | |
| 477 | public void maybeSetContext(XPathContext context) { |
| 478 | if (getXPathContext() == null) { |
| 479 | setXPathContext(context); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | /** |
| 484 | * Tests whether this is a dynamic error that may be reported statically if it is detected statically |
| 485 | * |
| 486 | * @return true if the error can be reported statically |
| 487 | */ |
| 488 | |
| 489 | public boolean isReportableStatically() { |
| 490 | if (isStaticError() || isTypeError()) { |
| 491 | return true; |
| 492 | } |
| 493 | StructuredQName err = errorCode; |
| 494 | if (err != null && err.hasURI(NamespaceConstant.ERR)) { |
| 495 | String local = err.getLocalPart(); |
| 496 | return local.equals("XTDE1260") || |
| 497 | local.equals("XTDE1280") || |
| 498 | local.equals("XTDE1390") || |
| 499 | local.equals("XTDE1400") || |
| 500 | local.equals("XTDE1428") || |
| 501 | local.equals("XTDE1440") || |
| 502 | local.equals("XTDE1460"); |
| 503 | } |
| 504 | return false; |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * Subclass of XPathException used to report circularities |
| 509 | */ |
| 510 | |
| 511 | public static class Circularity extends XPathException { |
| 512 | |
| 513 | /** |
| 514 | * Create an exception indicating that a circularity was detected |
| 515 | * |
| 516 | * @param message the error message |
| 517 | */ |
| 518 | public Circularity(String message) { |
| 519 | super(message); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | } |
| 524 |