| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class StandardInvalidityHandler implements InvalidityHandler { |
| 36 |
|
| 37 |
private Configuration config; |
| 38 |
private Logger logger; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public StandardInvalidityHandler(Configuration config) { |
| 46 |
this.config = config; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public void setLogger(Logger logger) { |
| 56 |
this.logger = logger; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public Logger getLogger() { |
| 66 |
return logger; |
| 67 |
} |
| 68 |
|
| 69 |
public Configuration getConfiguration(){ |
| 70 |
return config; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public void startReporting(String systemId) throws XPathException { |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 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 |
|
| 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 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 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 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 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 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public String getExpandedMessage(Invalidity err) { |
| 216 |
String code = err.getErrorCode(); |
| 217 |
return (code==null ? "" : code + ": ") + err.getMessage(); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
private static String wordWrap(String message) { |
| 228 |
return StandardErrorListener.wordWrap(message); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 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 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public Sequence<?> endReporting() throws XPathException { |
| 255 |
return EmptySequence.getInstance(); |
| 256 |
} |
| 257 |
} |
| 258 |
|