Debian Codegraph @

net/sf/saxon/type/ValidationFailure.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.type;
9
10 import net.sf.saxon.expr.parser.ExplicitLocation;
11 import net.sf.saxon.expr.parser.Location;
12 import net.sf.saxon.lib.Invalidity;
13 import net.sf.saxon.lib.NamespaceConstant;
14 import net.sf.saxon.om.AbsolutePath;
15 import net.sf.saxon.om.NodeInfo;
16 import net.sf.saxon.om.StructuredQName;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.tree.util.FastStringBuffer;
19 import net.sf.saxon.value.AtomicValue;
20
21 import javax.xml.transform.SourceLocator;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25
26 /**
27 * This exception indicates a failure when validating an instance against a type
28 * defined in a schema.
29 * <p>This class holds the same information as a ValidationException, except that it is not an exception,
30 * and does not carry system overheads such as a stack trace. It is used because operations such as "castable",
31 * and validation of values in a union, cause validation failures on a success path and it is costly to throw,
32 * or even to create, exception objects on a success path.</p>
33 */
34
35 public class ValidationFailure
36 implements Location, ConversionResult, Invalidity {
37
38 private String message;
39 private String systemId;
40 private String publicId;
41 private int lineNumber = -1;
42 private int columnNumber = -1;
43 private AbsolutePath path;
44 private AbsolutePath contextPath;
45 private NodeInfo invalidNode;
46 private List<NodeInfo> offendingNodes;
47
48 private int schemaPart = -1;
49 private String constraintName;
50 private String clause;
51 private SchemaType schemaType;
52 /*@Nullable*/ private StructuredQName errorCode;
53 private ValidationException exception;
54 private boolean hasBeenReported;
55
56
57 /**
58 * Creates a new ValidationException with the given message.
59 *
60 * @param message the message for this Exception
61 */
62 public ValidationFailure(String message) {
63 this.message = message;
64 setErrorCode("FORG0001");
65 }
66
67 /**
68 * Creates a new ValidationFailure with the given nested
69 * exception.
70 *
71 * @param exception the nested exception
72 */
73 public static ValidationFailure fromException(/*@NotNull*/ Exception exception) {
74 if (exception instanceof ValidationException) {
75 return ((ValidationException)exception).getValidationFailure();
76 } else if (exception instanceof XPathException) {
77 ValidationFailure failure = new ValidationFailure(exception.getMessage());
78 if (((XPathException) exception).getErrorCodeQName() == null) {
79 failure.setErrorCode("FORG0001");
80 } else {
81 failure.setErrorCodeQName(((XPathException) exception).getErrorCodeQName());
82 }
83 failure.setLocator(((XPathException) exception).getLocator());
84 return failure;
85 } else {
86 return new ValidationFailure(exception.getMessage());
87 }
88 }
89
90 /**
91 * Set a reference to the constraint in XML Schema that is not satisfied
92 *
93 * @param schemaPart - 1 or 2, depending whether the constraint is in XMLSchema part 1 or part 2
94 * @param constraintName - the short name of the constraint in XMLSchema, as a fragment identifier in the
95 * HTML of the XML Schema Part 1 specification
96 * @param clause - the clause number within the description of that constraint
97 */
98
99 public void setConstraintReference(int schemaPart, String constraintName, String clause) {
100 this.schemaPart = schemaPart;
101 this.constraintName = constraintName;
102 this.clause = clause;
103 }
104
105 /**
106 * Copy the constraint reference from another exception object
107 *
108 * @param e the other exception object from which to copy the information
109 */
110
111 public void setConstraintReference(/*@NotNull*/ ValidationFailure e) {
112 schemaPart = e.schemaPart;
113 constraintName = e.constraintName;
114 clause = e.clause;
115 }
116
117 /**
118 * Get the "schema part" component of the constraint reference
119 *
120 * @return 1 or 2 depending on whether the violated constraint is in XML Schema Part 1 or Part 2;
121 * or -1 if there is no constraint reference
122 */
123
124 public int getSchemaPart() {
125 return schemaPart;
126 }
127
128 /**
129 * Get the constraint name
130 *
131 * @return the name of the violated constraint, in the form of a fragment identifier within
132 * the published XML Schema specification; or null if the information is not available.
133 */
134
135 public String getConstraintName() {
136 return constraintName;
137 }
138
139 /**
140 * Get the constraint clause number
141 *
142 * @return the section number of the clause containing the constraint that has been violated.
143 * Generally a decimal number in the form n.n.n; possibly a sequence of such numbers separated
144 * by semicolons. Or null if the information is not available.
145 */
146
147 public String getConstraintClauseNumber() {
148 return clause;
149 }
150
151 /**
152 * Get the constraint name and clause in the format defined in XML Schema Part C (Outcome Tabulations).
153 * This mandates the format validation-rule-name.clause-number
154 *
155 * @return the constraint reference, for example "cos-ct-extends.1.2"; or null if the reference
156 * is not known.
157 */
158
159 /*@NotNull*/
160 public String getConstraintReference() {
161 return constraintName + '.' + clause;
162 }
163
164 /**
165 * Get the constraint reference as a string for inserting into an error message.
166 *
167 * @return the reference as a message, or null if no information is available
168 */
169
170 /*@Nullable*/
171 public String getConstraintReferenceMessage() {
172 if (schemaPart == -1) {
173 return null;
174 }
175 return "See http://www.w3.org/TR/xmlschema11-" + schemaPart + "/#" + constraintName
176 + " clause " + clause;
177 }
178
179
180 /**
181 * Add information about an "offending node". This is used for assertions, where the assertion on a
182 * particular node A places conditions on descendant nodes D: for example <code>every $n in .//* satisfies self::x</code>.
183 * With that kind of assertion, the nodes in <code>.//*</code> that do not satisfy the condition are reported
184 * as "offending nodes", although it is the root node containing the assertion that is technically invalid.
185 * @param node a node that fails to satisfy the conditions specified in an assertion
186 */
187
188 public void addOffendingNode(NodeInfo node) {
189 if (offendingNodes == null) {
190 offendingNodes = new ArrayList<NodeInfo>();
191 }
192 offendingNodes.add(node);
193 }
194
195 /**
196 * Get the list of "offending nodes". This is used for assertions, where the assertion on a
197 * particular node A places conditions on descendant nodes D: for example <code>every $n in .//* satisfies self::x</code>.
198 * With that kind of assertion, the nodes in <code>.//*</code> that do not satisfy the condition are reported
199 * as "offending nodes", although it is the root node containing the assertion that is technically invalid.
200 * @return the list of offending nodes
201 */
202
203 public List<NodeInfo> getOffendingNodes() {
204 if (offendingNodes == null) {
205 return Collections.emptyList();
206 } else {
207 return offendingNodes;
208 }
209 }
210
211
212 public AbsolutePath getPath() {
213 return path;
214 }
215
216 public void setPath(AbsolutePath path) {
217 this.path = path;
218 }
219
220 public AbsolutePath getContextPath() {
221 return contextPath;
222 }
223
224 public void setContextPath(AbsolutePath contextPath) {
225 this.contextPath = contextPath;
226 }
227
228 public NodeInfo getInvalidNode() {
229 return invalidNode;
230 }
231
232 public void setInvalidNode(NodeInfo invalidNode) {
233 this.invalidNode = invalidNode;
234 }
235
236
237
238 public String getMessage() {
239 return message;
240 }
241
242 public void setMessage(String message) {
243 this.message = message;
244 }
245
246 /**
247 * Returns the String representation of this Exception
248 *
249 * @return the String representation of this Exception
250 */
251 public String toString() {
252 FastStringBuffer sb = new FastStringBuffer("ValidationException: ");
253 String message = getMessage();
254 if (message != null) {
255 sb.append(message);
256 }
257 return sb.toString();
258 }
259
260 public String getPublicId() {
261 SourceLocator loc = getLocator();
262 if (publicId == null && loc != null && loc != this) {
263 return loc.getPublicId();
264 } else {
265 return publicId;
266 }
267 }
268
269 public String getSystemId() {
270 SourceLocator loc = getLocator();
271 if (systemId == null && loc != null && loc != this) {
272 return loc.getSystemId();
273 } else {
274 return systemId;
275 }
276 }
277
278 public int getLineNumber() {
279 SourceLocator loc = getLocator();
280 if (lineNumber == -1 && loc != null && loc != this) {
281 return loc.getLineNumber();
282 } else {
283 return lineNumber;
284 }
285 }
286
287 public int getColumnNumber() {
288 SourceLocator loc = getLocator();
289 if (columnNumber == -1 && loc != null && loc != this) {
290 return loc.getColumnNumber();
291 } else {
292 return columnNumber;
293 }
294 }
295
296 /**
297 * Get an immutable copy of this Location object. By default Location objects may be mutable, so they
298 * should not be saved for later use. The result of this operation holds the same location information,
299 * but in an immutable form.
300 */
301 public Location saveLocation() {
302 return new ExplicitLocation(this);
303 }
304
305 public void setPublicId(String id) {
306 publicId = id;
307 }
308
309 public void setSystemId(String id) {
310 systemId = id;
311 }
312
313 public void setLineNumber(int line) {
314 lineNumber = line;
315 }
316
317 public void setColumnNumber(int column) {
318 columnNumber = column;
319 }
320
321 public void setLocator(/*@Nullable*/ SourceLocator locator) {
322 if (locator != null) {
323 setPublicId(locator.getPublicId());
324 setSystemId(locator.getSystemId());
325 setLineNumber(locator.getLineNumber());
326 setColumnNumber(locator.getColumnNumber());
327 }
328 }
329
330 public void setSourceLocator(/*@Nullable*/ SourceLocator locator) {
331 if (locator != null) {
332 setPublicId(locator.getPublicId());
333 setSystemId(locator.getSystemId());
334 setLineNumber(locator.getLineNumber());
335 setColumnNumber(locator.getColumnNumber());
336 }
337 }
338
339 /*@NotNull*/
340 public Location getLocator() {
341 return this;
342 }
343
344 public void setErrorCode(String errorCode) {
345 if (errorCode == null) {
346 this.errorCode = null;
347 } else {
348 this.errorCode = new StructuredQName("err", NamespaceConstant.ERR, errorCode);
349 }
350 }
351
352 public void setErrorCodeQName(StructuredQName errorCode) {
353 this.errorCode = errorCode;
354 }
355
356 /**
357 * Get the error code associated with the validity error. This is relevant only when validation
358 * is run from within XSLT or XQuery, which define different error codes for validition errors
359 *
360 * @return the error code associated with the error, if any. The error is returned as a simple
361 * string if it is in the standard error namespace, or as an EQName (that is Q{uri}local) otherwise.
362 */
363 public String getErrorCode() {
364 if (errorCode == null) {
365 return null;
366 } else if (errorCode.hasURI(NamespaceConstant.ERR)) {
367 return errorCode.getLocalPart();
368 } else {
369 return errorCode.getEQName();
370 }
371 }
372
373 /*@Nullable*/
374 public StructuredQName getErrorCodeQName() {
375 return errorCode;
376 }
377
378 public void setSchemaType(SchemaType type) {
379 schemaType = type;
380 }
381
382 public SchemaType getSchemaType() {
383 return schemaType;
384 }
385
386 /*@NotNull*/
387 public ValidationException makeException() {
388 if (exception != null) {
389 exception.maybeSetLocation(this);
390 return exception;
391 }
392 ValidationException ve = new ValidationException(this);
393 if (errorCode == null) {
394 ve.setErrorCode("FORG0001");
395 } else {
396 ve.setErrorCodeQName(errorCode);
397 }
398 ve.setHasBeenReported(hasBeenReported);
399 exception = ve;
400 return ve;
401 }
402
403 /**
404 * Calling this method on a ConversionResult returns the AtomicValue that results
405 * from the conversion if the conversion was successful, and throws a ValidationException
406 * explaining the conversion error otherwise.
407 * <p>Use this method if you are calling a conversion method that returns a ConversionResult,
408 * and if you want to throw an exception if the conversion fails.</p>
409 *
410 * @return the atomic value that results from the conversion if the conversion was successful
411 * @throws net.sf.saxon.type.ValidationException
412 * if the conversion was not successful
413 */
414
415 /*@NotNull*/
416 public AtomicValue asAtomic() throws ValidationException {
417 throw makeException();
418 }
419
420 public boolean hasBeenReported() {
421 return hasBeenReported;
422 }
423
424 public void setHasBeenReported(boolean reported) {
425 hasBeenReported = reported;
426 if (exception != null) {
427 exception.setHasBeenReported(reported);
428 }
429 }
430
431 /**
432 * Get additional location text, if any. This gives extra information about the position of the error
433 * in textual form. Where XPath is embedded within a host language such as XSLT, the
434 * formal location information identifies the location of the error in the XSLT module,
435 * while this string locates the error within a specific XPath expression. The information
436 * is typically used only for static errors.
437 *
438 * @return additional information about the location of the error, designed to be output
439 * as a prefix to the error message if desired. (It is not concatenated with the message, because
440 * it may be superfluous in an IDE environment.)
441 */
442
443 public String getValidationLocationText() {
444 FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C256);
445 AbsolutePath valPath = getAbsolutePath();
446 if (valPath != null) {
447 fsb.append("Validating ");
448 fsb.append(valPath.getPathUsingPrefixes());
449 if (valPath.getSystemId() != null) {
450 fsb.append(" in ");
451 fsb.append(valPath.getSystemId());
452 }
453 }
454 return fsb.toString();
455 }
456
457
458 /**
459 * Get additional location text, if any. This gives extra information about the position of the error
460 * in textual form. Where XPath is embedded within a host language such as XSLT, the
461 * formal location information identifies the location of the error in the XSLT module,
462 * while this string locates the error within a specific XPath expression. The information
463 * is typically used only for static errors.
464 *
465 * @return additional information about the location of the error, designed to be output
466 * as a prefix to the error message if desired. (It is not concatenated with the message, because
467 * it may be superfluous in an IDE environment.)
468 */
469
470 public String getContextLocationText() {
471 FastStringBuffer fsb = new FastStringBuffer(FastStringBuffer.C256);
472 AbsolutePath contextPath = getContextPath();
473 if (contextPath != null) {
474 fsb.append("Currently processing ");
475 fsb.append(contextPath.getPathUsingPrefixes());
476 if (contextPath.getSystemId() != null) {
477 fsb.append(" in ");
478 fsb.append(contextPath.getSystemId());
479 }
480 }
481 return fsb.toString();
482 }
483
484 /**
485 * Get the location of the error as a structured path object
486 *
487 * @return the location, as a structured path object indicating the position of the error within the containing document
488 */
489
490 public AbsolutePath getAbsolutePath() {
491 if (path != null) {
492 return path;
493 // } else if (node != null) {
494 // return Navigator.getAbsolutePath(node);
495 } else {
496 return null;
497 }
498 }
499
500
501 }
502