| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 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 |
private StructuredQName errorCode; |
| 53 |
private ValidationException exception; |
| 54 |
private boolean hasBeenReported; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public ValidationFailure(String message) { |
| 63 |
this.message = message; |
| 64 |
setErrorCode("FORG0001"); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public static ValidationFailure fromException( 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 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 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 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public void setConstraintReference( ValidationFailure e) { |
| 112 |
schemaPart = e.schemaPart; |
| 113 |
constraintName = e.constraintName; |
| 114 |
clause = e.clause; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public int getSchemaPart() { |
| 125 |
return schemaPart; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public String getConstraintName() { |
| 136 |
return constraintName; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public String getConstraintClauseNumber() { |
| 148 |
return clause; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public String getConstraintReference() { |
| 161 |
return constraintName + '.' + clause; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 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 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 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 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 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 |
|
| 248 |
|
| 249 |
|
| 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 |
|
| 298 |
|
| 299 |
|
| 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( 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( 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 |
|
| 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 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 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 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 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 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 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 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
public AbsolutePath getAbsolutePath() { |
| 491 |
if (path != null) { |
| 492 |
return path; |
| 493 |
|
| 494 |
|
| 495 |
} else { |
| 496 |
return null; |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
} |
| 502 |
|