| 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.type; |
| 9 | |
| 10 | import net.sf.saxon.expr.parser.Location; |
| 11 | import net.sf.saxon.om.AbsolutePath; |
| 12 | import net.sf.saxon.om.NodeInfo; |
| 13 | import net.sf.saxon.trans.XPathException; |
| 14 | import net.sf.saxon.tree.util.Navigator; |
| 15 | |
| 16 | /** |
| 17 | * This exception indicates a failure when validating an instance against a type |
| 18 | * defined in a schema. It may also be used when validating against a built-in type. |
| 19 | */ |
| 20 | |
| 21 | public class ValidationException extends XPathException { |
| 22 | |
| 23 | private ValidationFailure failure; |
| 24 | |
| 25 | /** |
| 26 | * Creates a new ValidationException with the given nested |
| 27 | * exception. |
| 28 | * |
| 29 | * @param exception the nested exception |
| 30 | */ |
| 31 | public ValidationException(Exception exception) { |
| 32 | super(exception); |
| 33 | /*setIsTypeError(true);*/ |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Creates a new ValidationException with the given message |
| 38 | * and nested exception. |
| 39 | * |
| 40 | * @param message the detail message for this exception |
| 41 | * @param exception the nested exception |
| 42 | */ |
| 43 | public ValidationException(String message, Exception exception) { |
| 44 | super(message, exception); |
| 45 | /*setIsTypeError(true);*/ |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Create a new ValidationException from a message and a Locator. |
| 50 | * |
| 51 | * @param message The error or warning message. |
| 52 | * @param locator The locator object for the error or warning. |
| 53 | */ |
| 54 | public ValidationException(String message, Location locator) { |
| 55 | super(message, null, locator); |
| 56 | /*setIsTypeError(true);*/ |
| 57 | // With Xerces, it's enough to store the locator as part of the exception. But with Crimson, |
| 58 | // the locator is destroyed when the parse terminates, which means the location information |
| 59 | // will not be available in the final error message. So we copy the location information now, |
| 60 | // as part of the exception object itself. |
| 61 | //setSourceLocator(locator); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Create a new ValidationException that wraps a ValidationFailure |
| 66 | * @param failure the ValidationFailure to be wrapped |
| 67 | */ |
| 68 | |
| 69 | public ValidationException(ValidationFailure failure) { |
| 70 | super(failure.getMessage(), failure.getErrorCode(), failure.getLocator()); |
| 71 | this.failure = failure; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns the detail message string of this throwable. |
| 76 | * |
| 77 | * @return the detail message string of this {@code Throwable} instance |
| 78 | * (which may be {@code null}). |
| 79 | */ |
| 80 | @Override |
| 81 | public String getMessage() { |
| 82 | // The message held in the ValidationFailure is sometimes updated, so we use that one in preference. |
| 83 | // The message in the exception can't be updated, it can only be set from the constructor. |
| 84 | if (failure != null) { |
| 85 | return failure.getMessage(); |
| 86 | } else { |
| 87 | return super.getMessage(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Get a ValidationFailure object containing information from this ValidationException |
| 93 | * @return a ValidationFailure object |
| 94 | */ |
| 95 | |
| 96 | public ValidationFailure getValidationFailure() { |
| 97 | if (failure != null) { |
| 98 | return failure; |
| 99 | } else { |
| 100 | ValidationFailure failure = new ValidationFailure(getMessage()); |
| 101 | failure.setErrorCodeQName(getErrorCodeQName()); |
| 102 | failure.setLocator(getLocator()); |
| 103 | return failure; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Returns the String representation of this Exception |
| 109 | * |
| 110 | * @return the String representation of this Exception |
| 111 | */ |
| 112 | public String toString() { |
| 113 | StringBuilder sb = new StringBuilder("ValidationException: "); |
| 114 | String message = getMessage(); |
| 115 | if (message != null) { |
| 116 | sb.append(message); |
| 117 | } |
| 118 | return sb.toString(); |
| 119 | } |
| 120 | |
| 121 | /*@Nullable*/ |
| 122 | public NodeInfo getNode() { |
| 123 | if (failure != null) { |
| 124 | return failure.getInvalidNode(); |
| 125 | } else { |
| 126 | return null; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Get the location of the error in terms of a path expressed as a string |
| 132 | * |
| 133 | * @return the location, as a path. The result format is similar to that of the fn:path() function |
| 134 | */ |
| 135 | |
| 136 | /*@Nullable*/ |
| 137 | public String getPath() { |
| 138 | AbsolutePath ap = getAbsolutePath(); |
| 139 | if (ap == null) { |
| 140 | NodeInfo node = getNode(); |
| 141 | if (node != null) { |
| 142 | return Navigator.getPath(node); |
| 143 | } else { |
| 144 | return null; |
| 145 | } |
| 146 | } else { |
| 147 | return ap.getPathUsingAbbreviatedUris(); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Get the location of the error as a structured path object |
| 153 | * |
| 154 | * @return the location, as a structured path object indicating the position of the error within the containing document |
| 155 | */ |
| 156 | |
| 157 | public AbsolutePath getAbsolutePath() { |
| 158 | if (failure != null) { |
| 159 | return failure.getPath(); |
| 160 | } else { |
| 161 | return null; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | } |
| 166 |