Class XMLValidateTask

All Implemented Interfaces:
Direct Known Subclasses:

public class XMLValidateTask extends Task
Checks XML files are valid (or only well formed). The task uses the SAX2 parser implementation provided by JAXP by default (probably the one that is used by Ant itself), but one can specify any SAX1/2 parser if needed.
  • Field Details


      protected static final String INIT_FAILED_MSG
      See Also:
    • failOnError

      protected boolean failOnError
    • warn

      protected boolean warn
    • lenient

      protected boolean lenient
    • readerClassName

      protected String readerClassName
    • file

      protected File file
      file to be validated
    • filesets

      protected Vector<FileSet> filesets
      sets of file to be validated
    • classpath

      protected Path classpath
    • xmlReader

      protected XMLReader xmlReader
      the parser is viewed as a SAX2 XMLReader. If a SAX1 parser is specified, it's wrapped in an adapter that make it behave as a XMLReader. a more 'standard' way of doing this would be to use the JAXP1.1 SAXParser interface.
    • errorHandler

      protected XMLValidateTask.ValidatorErrorHandler errorHandler

      public static final String MESSAGE_FILES_VALIDATED
      Message for successful validation
      See Also:
  • Constructor Details

    • XMLValidateTask

      public XMLValidateTask()
  • Method Details

    • setFailOnError

      public void setFailOnError(boolean fail)
      Specify how parser error are to be handled. Optional, default is true.

      If set to true (default), throw a buildException if the parser yields an error.

      fail - if set to false do not fail on error
    • setWarn

      public void setWarn(boolean bool)
      Specify how parser error are to be handled.

      If set to true (default), log a warn message for each SAX warn event.

      bool - if set to false do not send warnings
    • setLenient

      public void setLenient(boolean bool)
      Specify whether the parser should be validating. Default is true.

      If set to false, the validation will fail only if the parsed document is not well formed XML.

      this option is ignored if the specified class with setClassName(String) is not a SAX2 XMLReader.

      bool - if set to false only fail on malformed XML
    • setClassName

      public void setClassName(String className)
      Specify the class name of the SAX parser to be used. (optional)
      className - should be an implementation of SAX2 org.xml.sax.XMLReader or SAX2 org.xml.sax.Parser.

      If className is an implementation of org.xml.sax.Parser, setLenient(boolean), will be ignored.

      If not set, the default will be used.

      See Also:
    • setClasspath

      public void setClasspath(Path classpath)
      Specify the classpath to be searched to load the parser (optional)
      classpath - the classpath to load the parser
    • createClasspath

      public Path createClasspath()
      the classpath created
      See Also:
    • setClasspathRef

      public void setClasspathRef(Reference r)
      Where to find the parser class; optional.
      r - reference to a classpath defined elsewhere
      See Also:
    • setFile

      public void setFile(File file)
      specify the file to be checked; optional.
      file - the file to be checked
    • addConfiguredXMLCatalog

      public void addConfiguredXMLCatalog(XMLCatalog catalog)
      add an XMLCatalog as a nested element; optional.
      catalog - XMLCatalog to use
    • addFileset

      public void addFileset(FileSet set)
      specify a set of file to be checked
      set - the fileset to check
    • createAttribute

      public XMLValidateTask.Attribute createAttribute()
      Add an attribute nested element. This is used for setting arbitrary features of the SAX parser. Valid attributes include
      attribute created
    • createProperty

      public XMLValidateTask.Property createProperty()
      Creates a property.
      a property.
      ant 1.6.2
    • init

      public void init() throws BuildException
      Called by the project to let the task initialize properly.
      init in class Task
      BuildException - if something goes wrong with the build
    • createDTD

      public DTDLocation createDTD()
      Create a DTD location record; optional. This stores the location of a DTD. The DTD is identified by its public Id.
      created DTD location
    • getEntityResolver

      protected EntityResolver getEntityResolver()
      accessor to the xmlCatalog used in the task
      xmlCatalog reference
    • getXmlReader

      protected XMLReader getXmlReader()
      get the XML reader. Non-null only after initValidator(). If the reader is an instance of ParserAdapter then the parser is a SAX1 parser, and you cannot call setFeature(String, boolean) or setProperty(String, String) on it.
      the XML reader or null.
    • execute

      public void execute() throws BuildException
      execute the task
      execute in class Task
      BuildException - if failonerror is true and an error happens
    • onSuccessfulValidation

      protected void onSuccessfulValidation(int fileProcessed)
      handler called on successful file validation.
      fileProcessed - number of files processed.
    • initValidator

      protected void initValidator()
      init the parser : load the parser class, and set features if necessary It is only after this that the reader is valid
      BuildException - if something went wrong
    • isSax1Parser

      protected boolean isSax1Parser()
      test that returns true if we are using a SAX1 parser.
      true when a SAX1 parser is in use
    • createXmlReader

      protected XMLReader createXmlReader()
      create the XML reader. This is one by instantiating anything specified by readerClassName, falling back to a default reader if not. If the returned reader is an instance of ParserAdapter then we have created and wrapped a SAX1 parser.
      the new XMLReader.
    • cleanup

      protected void cleanup()
      Cleans up resources.
      Ant 1.8.0
    • createDefaultReader

      protected XMLReader createDefaultReader()
      Create a reader if the use of the class did not specify another one. If a BuildException is thrown, the caller may revert to an alternate reader.
      a new reader.
      BuildException - if something went wrong
    • setFeature

      protected void setFeature(String feature, boolean value) throws BuildException
      Set a feature on the parser.
      feature - the name of the feature to set
      value - the value of the feature
      BuildException - if the feature was not supported
    • setProperty

      protected void setProperty(String name, String value) throws BuildException
      Sets a property.
      name - a property name
      value - a property value.
      BuildException - if an error occurs.
      BuildException - if the property was not supported
    • doValidate

      protected boolean doValidate(File afile)
      parse the file
      afile - the file to validate.
      true if the file validates.