Class Validator

  • All Implemented Interfaces:
    org.xml.sax.ContentHandler, org.xml.sax.DTDHandler, org.xml.sax.EntityResolver, org.xml.sax.ErrorHandler

    public class Validator
    extends org.xml.sax.helpers.DefaultHandler
    Validates XML against its internal or external DOCTYPE, or a completely different DOCTYPE. Usage:
    • new Validator(readerForXML);
      to validate some XML that contains or references an accessible DTD or schema
    • new Validator(readerForXML, systemIdForValidation);
      to validate some XML that references a DTD but using a local systemId to perform the validation
    • new Validator(readerForXML, systemIdForValidation, doctypeName);
      to validate some XML against a completely different DTD
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
        Validator​(java.io.Reader readerForValidation)
      Basic constructor.
        Validator​(java.io.Reader readerForValidation, java.lang.String systemID)
      Extended constructor.
        Validator​(java.io.Reader readerForValidation, java.lang.String systemID, java.lang.String doctype)
      Full constructor.
        Validator​(java.lang.String stringForValidation)
      Basic constructor.
        Validator​(java.lang.String stringForValidation, java.lang.String systemID)
      Extended constructor.
        Validator​(org.w3c.dom.Document document, java.lang.String systemID, java.lang.String doctype)
      DOM-style constructor: allows Document validation post-manipulation of the DOM tree's contents.
        Validator​(org.xml.sax.InputSource sourceForValidation)
      Basic constructor.
        Validator​(org.xml.sax.InputSource sourceForValidation, java.lang.String systemID)
      Extended constructor.
      protected Validator​(org.xml.sax.InputSource inputSource, java.lang.String systemId, boolean usingDoctypeReader)
      Baseline constructor: called by all others
        Validator​(org.xml.sax.InputSource sourceForValidation, java.lang.String systemID, java.lang.String doctype)
      Full constructor.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private static void addSchemaSources​(java.lang.Object schemaSources, java.util.List<javax.xml.transform.Source> targetList)  
      private java.lang.StringBuilder appendMessage​(java.lang.StringBuilder toAppendTo)
      Append any validation message(s) to the specified StringBuffer.
      void assertIsValid()
      Assert that a document is valid.
      private static java.lang.String getOriginalContent​(org.xml.sax.InputSource s)  
      private void invalidate​(java.lang.String message)
      Set the validation status flag to false and capture the message for use later.
      boolean isValid()
      Perform the validation of the source against DTD / Schema.
      void setJAXP12SchemaSource​(java.lang.Object schemaSource)
      As per JAXP 1.2 changes, which introduced a standard way for parsers to support schema validation.
      java.lang.String toString()  
      void useXMLSchema​(boolean use)
      Turn on XML Schema validation.
      private void validate()
      Actually perform validation.
      private void validationProblem​(ValidationProblem p)  
      • Methods inherited from class org.xml.sax.helpers.DefaultHandler

        characters, endDocument, endElement, endPrefixMapping, error, fatalError, ignorableWhitespace, notationDecl, processingInstruction, resolveEntity, setDocumentLocator, skippedEntity, startDocument, startElement, startPrefixMapping, unparsedEntityDecl, warning
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • validationInputSource

        private final org.xml.sax.InputSource validationInputSource
      • messages

        private final java.lang.StringBuilder messages
      • usingDoctypeReader

        private final boolean usingDoctypeReader
      • systemId

        private final java.lang.String systemId
      • schemaSource

        private java.lang.Object schemaSource
      • useSchema

        private boolean useSchema
      • isValid

        private java.lang.Boolean isValid
    • Constructor Detail

      • Validator

        protected Validator​(org.xml.sax.InputSource inputSource,
                            java.lang.String systemId,
                            boolean usingDoctypeReader)
        Baseline constructor: called by all others
        Parameters:
        inputSource -
        systemId -
        usingDoctypeReader -
      • Validator

        public Validator​(org.w3c.dom.Document document,
                         java.lang.String systemID,
                         java.lang.String doctype)
        DOM-style constructor: allows Document validation post-manipulation of the DOM tree's contents. This takes a fairly tortuous route to validation as DOM level 2 does not allow creation of Doctype nodes. The supplied systemId and doctype name will replace any Doctype settings in the Document.
        Parameters:
        document -
        systemID -
        doctype -
      • Validator

        public Validator​(java.io.Reader readerForValidation)
        Basic constructor. Validates the contents of the Reader using the DTD or schema referenced by those contents.
        Parameters:
        readerForValidation -
      • Validator

        public Validator​(java.lang.String stringForValidation)
        Basic constructor. Validates the contents of the String using the DTD or schema referenced by those contents.
        Parameters:
        stringForValidation -
      • Validator

        public Validator​(org.xml.sax.InputSource sourceForValidation)
        Basic constructor. Validates the contents of the InputSource using the DTD or schema referenced by those contents.
        Parameters:
        sourceForValidation -
      • Validator

        public Validator​(java.io.Reader readerForValidation,
                         java.lang.String systemID)
        Extended constructor. Validates the contents of the Reader using the DTD specified with the systemID. There must be DOCTYPE instruction in the markup that references the DTD or else the markup will be considered invalid: if there is no DOCTYPE in the markup use the 3-argument constructor
        Parameters:
        readerForValidation -
        systemID -
      • Validator

        public Validator​(java.lang.String stringForValidation,
                         java.lang.String systemID)
        Extended constructor. Validates the contents of the String using the DTD specified with the systemID. There must be DOCTYPE instruction in the markup that references the DTD or else the markup will be considered invalid: if there is no DOCTYPE in the markup use the 3-argument constructor
        Parameters:
        stringForValidation -
        systemID -
      • Validator

        public Validator​(org.xml.sax.InputSource sourceForValidation,
                         java.lang.String systemID)
        Extended constructor. Validates the contents of the InputSource using the DTD specified with the systemID. There must be DOCTYPE instruction in the markup that references the DTD or else the markup will be considered invalid: if there is no DOCTYPE in the markup use the 3-argument constructor
        Parameters:
        sourceForValidation -
        systemID -
      • Validator

        public Validator​(org.xml.sax.InputSource sourceForValidation,
                         java.lang.String systemID,
                         java.lang.String doctype)
        Full constructor. Validates the contents of the InputSource using the DTD specified with the systemID and named with the doctype name.
        Parameters:
        sourceForValidation -
        systemID -
        doctype -
      • Validator

        public Validator​(java.io.Reader readerForValidation,
                         java.lang.String systemID,
                         java.lang.String doctype)
        Full constructor. Validates the contents of the Reader using the DTD specified with the systemID and named with the doctype name.
        Parameters:
        readerForValidation -
        systemID -
        doctype -
    • Method Detail

      • useXMLSchema

        public void useXMLSchema​(boolean use)
        Turn on XML Schema validation.

        This feature should work with any XML parser that is JAXP 1.2 compliant and supports XML Schema validation.

        For a fully JAXP 1.2 compliant parser the property http://java.sun.com/xml/jaxp/properties/schemaLanguage is set, if this fails the method falls back to the features http://apache.org/xml/features/validation/schema & http://apache.org/xml/features/validation/dynamic which should cover early versions of Xerces 2 as well.

        Parameters:
        use - indicate that XML Schema should be used to validate documents.
        See Also:
        setJAXP12SchemaSource(Object)
      • isValid

        public boolean isValid()
        Perform the validation of the source against DTD / Schema.
        Returns:
        true if the input supplied to the constructor passes validation, false otherwise
      • assertIsValid

        public void assertIsValid()
        Assert that a document is valid.
      • appendMessage

        private java.lang.StringBuilder appendMessage​(java.lang.StringBuilder toAppendTo)
        Append any validation message(s) to the specified StringBuffer.
        Parameters:
        toAppendTo -
        Returns:
        specified StringBuffer with message(s) appended
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
        Returns:
        class name appended with validation messages
      • validate

        private void validate()
        Actually perform validation.
      • invalidate

        private void invalidate​(java.lang.String message)
        Set the validation status flag to false and capture the message for use later.
        Parameters:
        message -
      • setJAXP12SchemaSource

        public void setJAXP12SchemaSource​(java.lang.Object schemaSource)
        As per JAXP 1.2 changes, which introduced a standard way for parsers to support schema validation. Since only W3C Schema support was included in JAXP 1.2, this is the only mechanism currently supported by this method.
        Parameters:
        schemaSource - This can be one of the following:
        • String that points to the URI of the schema
        • InputStream with the contents of the schema
        • SAX InputSource
        • File
        • an array of Objects with the contents being one of the types defined above. An array of Objects can be used only when the schema language has the ability to assemble a schema at runtime. When an array of Objects is passed it is illegal to have two schemas that share the same namespace.
        See Also:
        "http://java.sun.com/webservices/jaxp/change-requests-11.html"
      • addSchemaSources

        private static void addSchemaSources​(java.lang.Object schemaSources,
                                             java.util.List<javax.xml.transform.Source> targetList)
      • getOriginalContent

        private static java.lang.String getOriginalContent​(org.xml.sax.InputSource s)
                                                    throws java.io.IOException
        Throws:
        java.io.IOException