Class SchemaValidator


  • public class SchemaValidator
    extends Object
    Validates XML documents based on a schema file, or a collection of schema files contained in a directory.
    • Field Detail

      • log

        private final org.slf4j.Logger log
        Class logger.
      • schema

        private final Schema schema
        Compiled schema object. Can be reused for multiple document validations.
    • Constructor Detail

      • SchemaValidator

        public SchemaValidator​(@Nonnull
                               SchemaBuilder.SchemaLanguage schemaLanguage,
                               @Nonnull
                               File schemaLocation)
                        throws SAXException
        Constructor.
        Parameters:
        schemaLanguage - schema language in use: either XSD or RELAX
        schemaLocation - location of the schema description (may be a directory of schema files)
        Throws:
        SAXException - if there is a problem converting the schema sources into a schema
    • Method Detail

      • schemaFileExtension

        private String schemaFileExtension​(@Nonnull
                                           SchemaBuilder.SchemaLanguage schemaLanguage)
        Provides the file name extension associated with a schema language. Results include the ".".
        Parameters:
        schemaLanguage - schema language
        Returns:
        the file name extension associated with the schema language
      • getSchemaFiles

        protected void getSchemaFiles​(@Nonnull
                                      SchemaBuilder.SchemaLanguage lang,
                                      File[] schemaFilesOrDirectories,
                                      List<File> accumulatedSchemaFiles)
        Gets all of the schema files in the given set of readable files, directories or subdirectories.
        Parameters:
        lang - schema language, must not be null
        schemaFilesOrDirectories - files and directories which may contain schema files
        accumulatedSchemaFiles - list that accumulates the schema files
      • validate

        public void validate​(@Nonnull
                             Source document)
                      throws SAXException,
                             IOException
        Validate a document against the compiled schema.
        Parameters:
        document - document to validate
        Throws:
        SAXException - if the document fails to validate
        IOException - if an I/O error occurs while reading the document