Package net.sf.saxon.type
Class ValidationFailure
- java.lang.Object
-
- net.sf.saxon.type.ValidationFailure
-
- All Implemented Interfaces:
javax.xml.transform.SourceLocator
,Invalidity
,Location
,ConversionResult
,org.xml.sax.Locator
public class ValidationFailure extends java.lang.Object implements Location, ConversionResult, Invalidity
This exception indicates a failure when validating an instance against a type defined in a schema.This class holds the same information as a ValidationException, except that it is not an exception, and does not carry system overheads such as a stack trace. It is used because operations such as "castable", and validation of values in a union, cause validation failures on a success path and it is costly to throw, or even to create, exception objects on a success path.
-
-
Constructor Summary
Constructors Constructor Description ValidationFailure(java.lang.String message)
Creates a new ValidationException with the given message.ValidationFailure(java.lang.String message, java.lang.String errorCode)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addOffendingNode(NodeInfo node)
Add information about an "offending node".AtomicValue
asAtomic()
Calling this method on a ConversionResult returns the AtomicValue that results from the conversion if the conversion was successful, and throws a ValidationException explaining the conversion error otherwise.static ValidationFailure
fromException(java.lang.Exception exception)
Creates a new ValidationFailure with the given nested exception.AbsolutePath
getAbsolutePath()
Get the location of the error as a structured path objectint
getColumnNumber()
Get the column number.java.lang.String
getConstraintClauseNumber()
Get the constraint clause numberjava.lang.String
getConstraintName()
Get the constraint namejava.lang.String
getConstraintReference()
Get the constraint name and clause in the format defined in XML Schema Part C (Outcome Tabulations).java.lang.String
getConstraintReferenceMessage()
Get the constraint reference as a string for inserting into an error message.java.lang.String
getContextLocationText()
Get additional location text, if any.AbsolutePath
getContextPath()
Get a hierarchic path giving the node being validated (the validation root).java.lang.String
getErrorCode()
Get the error code associated with the validity error.StructuredQName
getErrorCodeQName()
NodeInfo
getInvalidNode()
Get the node that was found to be invalid (if any).int
getLineNumber()
Get the line number.Location
getLocator()
java.lang.String
getMessage()
Get the text of a message explaining what is wrongjava.util.List<NodeInfo>
getOffendingNodes()
Get the list of "offending nodes".AbsolutePath
getPath()
Get a hierarchic path giving the logical position in the instance document where the validation error was foundjava.lang.String
getPublicId()
Get the Public IDint
getSchemaPart()
Get the "schema part" component of the constraint referenceSchemaType
getSchemaType()
java.lang.String
getSystemId()
Get the system ID.java.lang.String
getValidationLocationText()
Get additional location text, if any.boolean
hasBeenReported()
ValidationException
makeException()
Location
saveLocation()
Get an immutable copy of this Location object.void
setColumnNumber(int column)
void
setConstraintReference(int schemaPart, java.lang.String constraintName, java.lang.String clause)
Set a reference to the constraint in XML Schema that is not satisfiedvoid
setConstraintReference(ValidationFailure e)
Copy the constraint reference from another exception objectvoid
setContextPath(AbsolutePath contextPath)
void
setErrorCode(java.lang.String errorCode)
void
setErrorCodeQName(StructuredQName errorCode)
void
setHasBeenReported(boolean reported)
void
setInvalidNode(NodeInfo invalidNode)
void
setLineNumber(int line)
void
setLocator(javax.xml.transform.SourceLocator locator)
void
setMessage(java.lang.String message)
void
setPath(AbsolutePath path)
void
setPublicId(java.lang.String id)
void
setSchemaType(SchemaType type)
void
setSourceLocator(javax.xml.transform.SourceLocator locator)
void
setSystemId(java.lang.String id)
java.lang.String
toString()
Returns the String representation of this Exception
-
-
-
Method Detail
-
fromException
public static ValidationFailure fromException(java.lang.Exception exception)
Creates a new ValidationFailure with the given nested exception.- Parameters:
exception
- the nested exception
-
setConstraintReference
public void setConstraintReference(int schemaPart, java.lang.String constraintName, java.lang.String clause)
Set a reference to the constraint in XML Schema that is not satisfied- Parameters:
schemaPart
- - 1 or 2, depending whether the constraint is in XMLSchema part 1 or part 2constraintName
- - the short name of the constraint in XMLSchema, as a fragment identifier in the HTML of the XML Schema Part 1 specificationclause
- - the clause number within the description of that constraint
-
setConstraintReference
public void setConstraintReference(ValidationFailure e)
Copy the constraint reference from another exception object- Parameters:
e
- the other exception object from which to copy the information
-
getSchemaPart
public int getSchemaPart()
Get the "schema part" component of the constraint reference- Specified by:
getSchemaPart
in interfaceInvalidity
- Returns:
- 1 or 2 depending on whether the violated constraint is in XML Schema Part 1 or Part 2; or -1 if there is no constraint reference
-
getConstraintName
public java.lang.String getConstraintName()
Get the constraint name- Specified by:
getConstraintName
in interfaceInvalidity
- Returns:
- the name of the violated constraint, in the form of a fragment identifier within the published XML Schema specification; or null if the information is not available.
-
getConstraintClauseNumber
public java.lang.String getConstraintClauseNumber()
Get the constraint clause number- Specified by:
getConstraintClauseNumber
in interfaceInvalidity
- Returns:
- the section number of the clause containing the constraint that has been violated. Generally a decimal number in the form n.n.n; possibly a sequence of such numbers separated by semicolons. Or null if the information is not available.
-
getConstraintReference
public java.lang.String getConstraintReference()
Get the constraint name and clause in the format defined in XML Schema Part C (Outcome Tabulations). This mandates the format validation-rule-name.clause-number- Specified by:
getConstraintReference
in interfaceInvalidity
- Returns:
- the constraint reference, for example "cos-ct-extends.1.2"; or null if the reference is not known.
-
getConstraintReferenceMessage
public java.lang.String getConstraintReferenceMessage()
Get the constraint reference as a string for inserting into an error message.- Returns:
- the reference as a message, or null if no information is available
-
addOffendingNode
public void addOffendingNode(NodeInfo node)
Add information about an "offending node". This is used for assertions, where the assertion on a particular node A places conditions on descendant nodes D: for exampleevery $n in .//* satisfies self::x
. With that kind of assertion, the nodes in.//*
that do not satisfy the condition are reported as "offending nodes", although it is the root node containing the assertion that is technically invalid.- Parameters:
node
- a node that fails to satisfy the conditions specified in an assertion
-
getOffendingNodes
public java.util.List<NodeInfo> getOffendingNodes()
Get the list of "offending nodes". This is used for assertions, where the assertion on a particular node A places conditions on descendant nodes D: for exampleevery $n in .//* satisfies self::x
. With that kind of assertion, the nodes in.//*
that do not satisfy the condition are reported as "offending nodes", although it is the root node containing the assertion that is technically invalid.- Returns:
- the list of offending nodes
-
getPath
public AbsolutePath getPath()
Description copied from interface:Invalidity
Get a hierarchic path giving the logical position in the instance document where the validation error was found- Specified by:
getPath
in interfaceInvalidity
- Returns:
- a path to the location in the document where the error was found. This is not necessarily the same thing as the node that is invalid: for example if the schema type for element A does not allow a child element named B, but in the instance document there is an A element with a B child, then the path returned may be the path to B, whereas it is A that is invalid.
-
setPath
public void setPath(AbsolutePath path)
-
getContextPath
public AbsolutePath getContextPath()
Description copied from interface:Invalidity
Get a hierarchic path giving the node being validated (the validation root). If validation is invoked from XSLT this might be a node selected by the instruction<xsl:copy-of select="X" validation="strict"/>
; in XQuery it might be the target of avalidate
expression.- Specified by:
getContextPath
in interfaceInvalidity
- Returns:
- a path to the location in the document
-
setContextPath
public void setContextPath(AbsolutePath contextPath)
-
getInvalidNode
public NodeInfo getInvalidNode()
Description copied from interface:Invalidity
Get the node that was found to be invalid (if any). This will be available only when validating an input document supplied in the form of an in-memory tree- Specified by:
getInvalidNode
in interfaceInvalidity
- Returns:
- the invalid node, if it is known, or null
-
setInvalidNode
public void setInvalidNode(NodeInfo invalidNode)
-
getMessage
public java.lang.String getMessage()
Description copied from interface:Invalidity
Get the text of a message explaining what is wrong- Specified by:
getMessage
in interfaceInvalidity
- Returns:
- a human-readable message explaining the validity error
-
setMessage
public void setMessage(java.lang.String message)
-
toString
public java.lang.String toString()
Returns the String representation of this Exception- Overrides:
toString
in classjava.lang.Object
- Returns:
- the String representation of this Exception
-
getPublicId
public java.lang.String getPublicId()
Description copied from interface:Location
Get the Public ID- Specified by:
getPublicId
in interfaceLocation
- Specified by:
getPublicId
in interfaceorg.xml.sax.Locator
- Specified by:
getPublicId
in interfacejavax.xml.transform.SourceLocator
- Returns:
- usually null
-
getSystemId
public java.lang.String getSystemId()
Description copied from interface:Location
Get the system ID. This should be the system identifier of an XML external entity; if a stylesheet module comprises multiple external entities, the distinction should be retained. This means, for example, that an instruction in a stylesheet can have a different system identifier from its parent instruction. However, SAX parsers only provide location information at the element level, so when part of an XPath expression is in a different external entity from other parts of the same expression, this distinction is lost.The system identifier of a node is in general not the same as its base URI. The base URI is affected by xml:base attributes; the system identifier is not.
- Specified by:
getSystemId
in interfaceLocation
- Specified by:
getSystemId
in interfaceorg.xml.sax.Locator
- Specified by:
getSystemId
in interfacejavax.xml.transform.SourceLocator
- Returns:
- the system ID, or null if the information is not available.
-
getLineNumber
public int getLineNumber()
Description copied from interface:Location
Get the line number. This line number is relative to the external entity identified by the system identifier. Line numbers start at 1. The value may be an approximation; SAX parsers only provide line number information at the level of element nodes.- Specified by:
getLineNumber
in interfaceLocation
- Specified by:
getLineNumber
in interfaceorg.xml.sax.Locator
- Specified by:
getLineNumber
in interfacejavax.xml.transform.SourceLocator
- Returns:
- the line number, or -1 if the information is not available.
-
getColumnNumber
public int getColumnNumber()
Description copied from interface:Location
Get the column number. This column number is relative to the line identified by the line number. Column numbers start at 1.- Specified by:
getColumnNumber
in interfaceLocation
- Specified by:
getColumnNumber
in interfaceorg.xml.sax.Locator
- Specified by:
getColumnNumber
in interfacejavax.xml.transform.SourceLocator
- Returns:
- the column number, or -1 if the information is not available.
-
saveLocation
public Location saveLocation()
Get an immutable copy of this Location object. By default Location objects may be mutable, so they should not be saved for later use. The result of this operation holds the same location information, but in an immutable form.- Specified by:
saveLocation
in interfaceLocation
- Returns:
- an immutable copy (which may be the original object, if it is already immutable)
-
setPublicId
public void setPublicId(java.lang.String id)
-
setSystemId
public void setSystemId(java.lang.String id)
-
setLineNumber
public void setLineNumber(int line)
-
setColumnNumber
public void setColumnNumber(int column)
-
setLocator
public void setLocator(javax.xml.transform.SourceLocator locator)
-
setSourceLocator
public void setSourceLocator(javax.xml.transform.SourceLocator locator)
-
getLocator
public Location getLocator()
-
setErrorCode
public void setErrorCode(java.lang.String errorCode)
-
setErrorCodeQName
public void setErrorCodeQName(StructuredQName errorCode)
-
getErrorCode
public java.lang.String getErrorCode()
Get the error code associated with the validity error. This is relevant only when validation is run from within XSLT or XQuery, which define different error codes for validation errors- Specified by:
getErrorCode
in interfaceInvalidity
- Returns:
- the error code associated with the error, if any. The error is returned as a simple string if it is in the standard error namespace, or as an EQName (that is Q{uri}local) otherwise.
-
getErrorCodeQName
public StructuredQName getErrorCodeQName()
-
setSchemaType
public void setSchemaType(SchemaType type)
-
getSchemaType
public SchemaType getSchemaType()
-
makeException
public ValidationException makeException()
-
asAtomic
public AtomicValue asAtomic() throws ValidationException
Calling this method on a ConversionResult returns the AtomicValue that results from the conversion if the conversion was successful, and throws a ValidationException explaining the conversion error otherwise.Use this method if you are calling a conversion method that returns a ConversionResult, and if you want to throw an exception if the conversion fails.
- Specified by:
asAtomic
in interfaceConversionResult
- Returns:
- the atomic value that results from the conversion if the conversion was successful
- Throws:
ValidationException
- if the conversion was not successful
-
hasBeenReported
public boolean hasBeenReported()
-
setHasBeenReported
public void setHasBeenReported(boolean reported)
-
getValidationLocationText
public java.lang.String getValidationLocationText()
Get additional location text, if any. This gives extra information about the position of the error in textual form. Where XPath is embedded within a host language such as XSLT, the formal location information identifies the location of the error in the XSLT module, while this string locates the error within a specific XPath expression. The information is typically used only for static errors.- Returns:
- additional information about the location of the error, designed to be output as a prefix to the error message if desired. (It is not concatenated with the message, because it may be superfluous in an IDE environment.)
-
getContextLocationText
public java.lang.String getContextLocationText()
Get additional location text, if any. This gives extra information about the position of the error in textual form. Where XPath is embedded within a host language such as XSLT, the formal location information identifies the location of the error in the XSLT module, while this string locates the error within a specific XPath expression. The information is typically used only for static errors.- Returns:
- additional information about the location of the error, designed to be output as a prefix to the error message if desired. (It is not concatenated with the message, because it may be superfluous in an IDE environment.)
-
getAbsolutePath
public AbsolutePath getAbsolutePath()
Get the location of the error as a structured path object- Returns:
- the location, as a structured path object indicating the position of the error within the containing document
-
-