Class 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 Detail

      • ValidationFailure

        public ValidationFailure​(java.lang.String message)
        Creates a new ValidationException with the given message.
        Parameters:
        message - the message for this Exception
      • ValidationFailure

        public ValidationFailure​(java.lang.String message,
                                 java.lang.String errorCode)
    • 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 2
        constraintName - - the short name of the constraint in XMLSchema, as a fragment identifier in the HTML of the XML Schema Part 1 specification
        clause - - 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 interface Invalidity
        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 interface Invalidity
        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 interface Invalidity
        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 interface Invalidity
        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 example every $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 example every $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 interface Invalidity
        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.
      • 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 a validate expression.
        Specified by:
        getContextPath in interface Invalidity
        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 interface Invalidity
        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 interface Invalidity
        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 class java.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 interface Location
        Specified by:
        getPublicId in interface org.xml.sax.Locator
        Specified by:
        getPublicId in interface javax.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 interface Location
        Specified by:
        getSystemId in interface org.xml.sax.Locator
        Specified by:
        getSystemId in interface javax.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 interface Location
        Specified by:
        getLineNumber in interface org.xml.sax.Locator
        Specified by:
        getLineNumber in interface javax.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 interface Location
        Specified by:
        getColumnNumber in interface org.xml.sax.Locator
        Specified by:
        getColumnNumber in interface javax.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 interface Location
        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 interface Invalidity
        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.
      • setSchemaType

        public void setSchemaType​(SchemaType type)
      • getSchemaType

        public SchemaType getSchemaType()
      • 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 interface ConversionResult
        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