Debian Codegraph @

net/sf/saxon/expr/ErrorExpression.java

raw symbols SCIP
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.expr;
9
10 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
11 import net.sf.saxon.expr.parser.ExpressionTool;
12 import net.sf.saxon.expr.parser.ExpressionVisitor;
13 import net.sf.saxon.expr.parser.RebindingMap;
14 import net.sf.saxon.om.Item;
15 import net.sf.saxon.om.SequenceIterator;
16 import net.sf.saxon.trace.ExpressionPresenter;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.type.AnyItemType;
19 import net.sf.saxon.type.ItemType;
20
21
22 /**
23 * Error expression: this expression is generated when the supplied expression cannot be
24 * parsed, and the containing element enables forwards-compatible processing. It defers
25 * the generation of an error message until an attempt is made to evaluate the expression
26 */
27
28 public class ErrorExpression extends Expression {
29
30 private XPathException exception;
31 private String message;
32 private String errorCode;
33 private boolean isTypeError;
34 private Expression original;
35
36 /**
37 * This constructor is never executed, but it is used in the expression parser
38 * as a dummy so that the Java compiler recognizes parsing methods as always returning
39 * a non-null result.
40 */
41 public ErrorExpression() {
42 this("Unspecified error", "XXXX9999", false);
43 }
44
45 /**
46 * Create an ErrorExpression, which if evaluated, generates a dynamic error
47 * @param message the error message
48 * @param errorCode the error code
49 * @param isTypeError true if this is a type error
50 */
51
52 public ErrorExpression(String message, String errorCode, boolean isTypeError) {
53 this.message = message;
54 this.errorCode = errorCode;
55 this.isTypeError = isTypeError;
56 }
57
58 /**
59 * Constructor taking an exception. Creating exceptions is expensive, so this
60 * constructor should be used only if the exception object already exists.
61 *
62 * @param exception the error to be thrown when this expression is evaluated
63 */
64
65 public ErrorExpression(XPathException exception) {
66 this.exception = exception;
67 this.message = exception.getMessage();
68 this.errorCode = exception.getErrorCodeLocalPart();
69 }
70
71 /**
72 * Get the wrapped exception
73 *
74 * @return the exception to be thrown when the expression is evaluated
75 */
76
77 public XPathException getException() {
78 return exception;
79 }
80
81 public boolean isTypeError() {
82 return exception == null ? isTypeError : exception.isTypeError();
83 }
84
85 public String getMessage() {
86 return exception == null ? message : exception.getMessage();
87 }
88
89 public String getErrorCodeLocalPart() {
90 return exception == null ? errorCode : exception.getErrorCodeLocalPart();
91 }
92
93 /**
94 * Set the original expression (for diagnostics)
95 *
96 * @param original the expression that this error expression replaces
97 */
98
99 public void setOriginalExpression(Expression original) {
100 this.original = original;
101 }
102
103 /**
104 * Type-check the expression.
105 */
106
107 /*@NotNull*/
108 public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
109 return this;
110 }
111
112 /*@NotNull*/
113 public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType) throws XPathException {
114 return this;
115 }
116
117 /**
118 * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
119 * This method indicates which of these methods is provided directly. The other methods will always be available
120 * indirectly, using an implementation that relies on one of the other methods.
121 *
122 * @return the implementation method, for example {@link #ITERATE_METHOD} or {@link #EVALUATE_METHOD} or
123 * {@link #PROCESS_METHOD}
124 */
125 @Override
126 public int getImplementationMethod() {
127 return EVALUATE_METHOD | ITERATE_METHOD;
128 }
129
130 /**
131 * Evaluate the expression. This always throws the exception registered when the expression
132 * was first parsed.
133 */
134
135 public Item evaluateItem(XPathContext context) throws XPathException {
136 if (exception != null) {
137 // copy the exception for thread-safety, because we want to add context information
138 XPathException err = new XPathException(exception);
139 err.setLocation(exception.getLocator());
140 err.maybeSetLocation(getLocation());
141 err.setErrorCodeQName(exception.getErrorCodeQName());
142 err.setXPathContext(exception.getXPathContext());
143 err.maybeSetContext(context);
144 err.setIsTypeError(exception.isTypeError());
145 err.setIsStaticError(exception.isStaticError());
146 err.setIsGlobalError(exception.isGlobalError());
147 throw err;
148 } else {
149 XPathException err = new XPathException(message);
150 err.setLocation(getLocation());
151 err.setErrorCode(errorCode);
152 err.setXPathContext(context);
153 err.setIsTypeError(isTypeError);
154 throw err;
155 }
156 }
157
158 /**
159 * Iterate over the expression. This always throws the exception registered when the expression
160 * was first parsed.
161 */
162
163 /*@NotNull*/
164 public SequenceIterator<?> iterate(XPathContext context) throws XPathException {
165 evaluateItem(context);
166 return null; // to fool the compiler
167 }
168
169 /**
170 * Determine the data type of the expression, if possible
171 *
172 * @return Type.ITEM (meaning not known in advance)
173 */
174
175 /*@NotNull*/
176 public ItemType getItemType() {
177 return AnyItemType.getInstance();
178 }
179
180 /**
181 * Determine the static cardinality
182 */
183
184 public int computeCardinality() {
185 return StaticProperty.ALLOWS_ZERO_OR_MORE;
186 // we return a liberal value, so that we never get a type error reported
187 // statically
188 }
189
190 @Override
191 public Expression copy(RebindingMap rebindings) {
192 if (exception != null) {
193 ErrorExpression e2 = new ErrorExpression(exception);
194 e2.setOriginalExpression(original);
195 ExpressionTool.copyLocationInfo(this, e2);
196 return e2;
197 } else {
198 ErrorExpression e2 = new ErrorExpression(message, errorCode, isTypeError);
199 e2.setOriginalExpression(original);
200 ExpressionTool.copyLocationInfo(this, e2);
201 return e2;
202 }
203 }
204
205 /**
206 * Get a name identifying the kind of expression, in terms meaningful to a user.
207 *
208 * @return a name identifying the kind of expression, in terms meaningful to a user.
209 * The name will always be in the form of a lexical XML QName, and should match the name used
210 * in export() output displaying the expression.
211 */
212 @Override
213 public String getExpressionName() {
214 return "errorExpr";
215 }
216
217 @Override
218 public String toString() {
219 if (original != null) {
220 return original.toString();
221 } else {
222 return "error(\"" + message + "\")";
223 }
224 }
225
226 @Override
227 public String toShortString() {
228 if (original != null) {
229 return original.toShortString();
230 } else {
231 return "error(\"" + message + "\")";
232 }
233 }
234
235
236 /**
237 * Diagnostic print of expression structure. The abstract expression tree
238 * is written to the supplied output destination.
239 */
240
241 public void export(ExpressionPresenter destination) throws XPathException {
242 destination.startElement("error", this);
243 destination.emitAttribute("message", exception == null ? message : exception.getMessage());
244 destination.emitAttribute("code", exception == null ? errorCode : exception.getErrorCodeLocalPart());
245 destination.emitAttribute("isTypeErr", isTypeError?"0":"1");
246 destination.endElement();
247 }
248
249 }