Debian Codegraph @

net/sf/saxon/lib/StandardInvalidityHandler.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.lib;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.expr.instruct.*;
12 import net.sf.saxon.om.NodeInfo;
13 import net.sf.saxon.om.Sequence;
14 import net.sf.saxon.om.StructuredQName;
15 import net.sf.saxon.trace.InstructionInfo;
16 import net.sf.saxon.trans.KeyDefinition;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.tree.util.Navigator;
19 import net.sf.saxon.type.ValidationException;
20 import net.sf.saxon.type.ValidationFailure;
21 import net.sf.saxon.value.EmptySequence;
22
23 import javax.xml.transform.SourceLocator;
24 import javax.xml.transform.dom.DOMLocator;
25
26 /**
27 * This class <code>StandardInvalidityHandler</code>, despite its name, is not directly used by Saxon, and in particular
28 * it is NOT the default InvalidityHandler. It is however available for use by applications, either directly or by
29 * subclassing. (The default InvalidityHandler wraps a supplied ErrorListener).
30 *
31 * This InvalidityHandler logs validation error messages to a supplied {@link Logger}, using the Logger belonging
32 * to the supplied {@link Configuration} as the default destination.
33 */
34
35 public class StandardInvalidityHandler implements InvalidityHandler {
36
37 private Configuration config;
38 private Logger logger;
39
40 /**
41 * Create a Standard Invalidity Handler
42 * @param config the Saxon configuration
43 */
44
45 public StandardInvalidityHandler(Configuration config) {
46 this.config = config;
47 }
48
49 /**
50 * Set output destination for error messages (default is the Logger registered with the Configuration)
51 *
52 * @param logger The Logger to use for error messages
53 */
54
55 public void setLogger(Logger logger) {
56 this.logger = logger;
57 }
58
59 /**
60 * Get the error output stream
61 *
62 * @return the error output stream
63 */
64
65 public Logger getLogger() {
66 return logger;
67 }
68
69 public Configuration getConfiguration(){
70 return config;
71 }
72
73 /**
74 * At the start of a validation episode, initialize the handler
75 *
76 * @param systemId optional; may be used to represent the destination of any report produced
77 * @throws XPathException if initialization of the invalidity handler fails for any reason
78 */
79 public void startReporting(String systemId) throws XPathException {
80 // no action
81 }
82
83 /**
84 * Receive notification of a validity error.
85 * @param failure Information about the nature of the invalidity
86 */
87
88 public void reportInvalidity(Invalidity failure) throws XPathException{
89 Logger localLogger = logger;
90 if (localLogger == null) {
91 localLogger = config.getLogger();
92 }
93
94 String explanation = getExpandedMessage(failure);
95 String constraintReference = getConstraintReferenceMessage(failure);
96 String validationLocation = ((ValidationFailure) failure).getValidationLocationText();
97 String contextLocation = ((ValidationFailure) failure).getContextLocationText();
98 String finalMessage = "Validation error " +
99 getLocationMessage(failure) +
100 "\n " +
101 wordWrap(explanation) +
102 //wordWrap(validationLocation.isEmpty() ? "" : "\n " + validationLocation) +
103 wordWrap(contextLocation.isEmpty() ? "" : "\n " + contextLocation) +
104 wordWrap(constraintReference == null ? "" : "\n " + constraintReference) +
105 StandardErrorListener.getOffenderListText((ValidationFailure)failure);
106
107 localLogger.error(finalMessage);
108 }
109
110
111 /**
112 * Get a string identifying the location of an error.
113 *
114 * @param err the exception containing the location information
115 * @return a message string describing the location
116 */
117
118 public String getLocationMessage(Invalidity err) {
119 String locMessage = "";
120 String systemId = null;
121 NodeInfo node = ((ValidationFailure)err).getInvalidNode();
122 String path;
123 String nodeMessage = null;
124 int lineNumber = -1;
125 SourceLocator loc = err;
126 if (loc instanceof DOMLocator) {
127 nodeMessage = "at " + ((DOMLocator) loc).getOriginatingNode().getNodeName() + ' ';
128 } else if (loc instanceof ValidationException && loc.getLineNumber() == -1 && (path = ((ValidationException) loc).getPath()) != null) {
129 nodeMessage = "at " + path + ' ';
130 } else if (loc instanceof Instruction) {
131 String instructionName = StandardErrorListener.getInstructionName((Instruction) loc);
132 if (!"".equals(instructionName)) {
133 nodeMessage = "at " + instructionName + ' ';
134 }
135 systemId = loc.getSystemId();
136 lineNumber = loc.getLineNumber();
137 } else if (loc instanceof Actor) {
138 String kind = "procedure";
139 if (loc instanceof UserFunction) {
140 kind = "function";
141 } else if (loc instanceof NamedTemplate) {
142 kind = "template";
143 } else if (loc instanceof AttributeSet) {
144 kind = "attribute-set";
145 } else if (loc instanceof KeyDefinition) {
146 kind = "key";
147 }
148 systemId = loc.getSystemId();
149 lineNumber = loc.getLineNumber();
150 nodeMessage = "at " + kind + " ";
151 StructuredQName name = ((InstructionInfo) loc).getObjectName();
152 if (name != null) {
153 nodeMessage += name.toString();
154 nodeMessage += " ";
155 }
156 }
157 if (lineNumber == -1) {
158 lineNumber = loc.getLineNumber();
159 }
160 boolean containsLineNumber = lineNumber != -1;
161 if (node != null) {
162 nodeMessage = "at " + Navigator.getPath(node) + ' ';
163 }
164 if (nodeMessage != null) {
165 locMessage += nodeMessage;
166 }
167 if (containsLineNumber) {
168 locMessage += "on line " + lineNumber + ' ';
169 if (loc.getColumnNumber() != -1) {
170 locMessage += "column " + loc.getColumnNumber() + ' ';
171 }
172 }
173
174 if (systemId != null && systemId.isEmpty()) {
175 systemId = null;
176 }
177 if (systemId == null) {
178 systemId = loc.getSystemId();
179 }
180 if (systemId != null && systemId.length() != 0) {
181 locMessage += (containsLineNumber ? "of " : "in ") + abbreviatePath(systemId) + ':';
182 }
183 return locMessage;
184 }
185
186 /**
187 * Abbreviate a URI (if requested)
188 *
189 * @param uri the URI to be abbreviated
190 * @return the abbreviated URI, unless full path names were requested, in which case
191 * the URI as supplied
192 */
193
194 /*@Nullable*/
195 public static String abbreviatePath(String uri) {
196 if (uri == null) {
197 return "*unknown*";
198 }
199 int slash = uri.lastIndexOf('/');
200 if (slash >= 0 && slash < uri.length() - 1) {
201 return uri.substring(slash + 1);
202 } else {
203 return uri;
204 }
205 }
206
207 /**
208 * Get a string containing the message for this exception and all contained exceptions
209 *
210 * @param err the exception containing the required information
211 * @return a message that concatenates the message of this exception with its contained exceptions,
212 * also including information about the error code and location.
213 */
214
215 public String getExpandedMessage(Invalidity err) {
216 String code = err.getErrorCode();
217 return (code==null ? "" : code + ": ") + err.getMessage();
218 }
219
220 /**
221 * Wordwrap an error message into lines of 72 characters or less (if possible)
222 *
223 * @param message the message to be word-wrapped
224 * @return the message after applying word-wrapping
225 */
226
227 private static String wordWrap(String message) {
228 return StandardErrorListener.wordWrap(message);
229 }
230
231 /**
232 * Get the constraint reference as a string for inserting into an error message.
233 *
234 * @return the reference as a message, or null if no information is available
235 */
236
237 /*@Nullable*/
238 public static String getConstraintReferenceMessage(Invalidity err) {
239 if (err.getSchemaPart() == -1) {
240 return null;
241 }
242 return "See http://www.w3.org/TR/xmlschema-" + err.getSchemaPart() + "/#" + err.getConstraintName()
243 + " clause " + err.getConstraintClauseNumber();
244 }
245
246 /**
247 * Get the value to be associated with a validation exception. May return null.
248 * In the case of the InvalidityReportGenerator, this returns the XML document
249 * containing the validation report
250 *
251 * @return a value (or null). This will be the value returned as the value of
252 * the variable $err:value during try/catch processing
253 */
254 public Sequence<?> endReporting() throws XPathException {
255 return EmptySequence.getInstance();
256 }
257 }
258