Class SchemaValidator

java.lang.Object
org.jppf.utils.SchemaValidator

public class SchemaValidator extends Object
Utility class to validate an XML document against an XML schema.
Author:
Laurent Cohen
  • Constructor Details

    • SchemaValidator

      public SchemaValidator(JPPFErrorReporter reporter)
      Initialize this validator with the specified error reporter.
      Parameters:
      reporter - used to collect validation error and warning messages.
  • Method Details

    • main

      public static void main(String... args)
      Entry point to test this class.
      Parameters:
      args - not used.
    • usageAndExit

      public static void usageAndExit()
      Show usage instructions for this validation toll, and exit the JVM.
    • loadSchema

      public Schema loadSchema(String schemaPath) throws IOException, SAXException
      Load an XML schema from the specified path. This method looks up the schema first in the file system, then in the classpath if it is not found in the file system.
      Parameters:
      schemaPath - the path to the schema to load.
      Returns:
      a Schema instance, or null if the schema file could not be found.
      Throws:
      IOException - if an IO error occurs while looking up the schema file.
      SAXException - if an error occurs while loading the schema.
    • loadSchema

      public Schema loadSchema(InputStream schemaStream) throws IOException, SAXException
      Load an XML schema from the specified input stream. This method looks up the schema first in the file system, then in the classpath if it is not found in the file system.
      Parameters:
      schemaStream - the input stream to load the schema from.
      Returns:
      a Schema instance, or null if the schema file could not be found.
      Throws:
      IOException - if an IO error occurs while looking up the schema file.
      SAXException - if an error occurs while loading the schema.
    • loadSchema

      public Schema loadSchema(Reader schemaReader) throws IOException, SAXException
      Load an XML schema from the specified reader. This method looks up the schema first in the file system, then in the classpath if it is not found in the file system.
      Parameters:
      schemaReader - the reader to load the schema from.
      Returns:
      a Schema instance, or null if the schema file could not be found.
      Throws:
      IOException - if an IO error occurs while looking up the schema file.
      SAXException - if an error occurs while loading the schema.
    • validate

      public boolean validate(String docPath, String schemaPath) throws IOException, SAXException
      Validate an XML document in a file against an XML schema.
      Parameters:
      docPath - the path to the XML document.
      schemaPath - the path to the XML schema.
      Returns:
      true if the XML document is valid, false otherwise.
      Throws:
      IOException - if an IO error occurs while looking up one of the files.
      SAXException - if an error occurs while loading the schema or validating the document.
    • validate

      public boolean validate(Reader docReader, Reader schemaReader) throws IOException, SAXException
      Validate an XML document from a reader against an XML schema.
      Parameters:
      docReader - the reader used to access the document.
      schemaReader - the path to the XML schema.
      Returns:
      true if the XML document is valid, false otherwise.
      Throws:
      IOException - if an IO error occurs while looking up one of the files.
      SAXException - if an error occurs while loading the schema or validating the document.
    • getSchemaFactory

      public SchemaFactory getSchemaFactory()
      Get the schema factory for this validator.
      Returns:
      a SchemaFactory instance.
    • printSAXParseException

      public String printSAXParseException(SAXParseException e, String name)
      Print a trace of the specified sax parsing exception.
      Parameters:
      e - the exception to print.
      name - an identifier for the document being parsed.
      Returns:
      the resulting message as a string.