org.apache.tools.ant.taskdefs.optional

Class XMLValidateTask

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.
Nested Class Summary
static classXMLValidateTask.Attribute
The class to create to set a feature of the parser.
static classXMLValidateTask.Property
A Parser property.
protected classXMLValidateTask.ValidatorErrorHandler
ValidatorErrorHandler role :
  • log SAX parse exceptions,
  • remember if an error occurred
Field Summary
protected Pathclasspath
protected XMLValidateTask.ValidatorErrorHandlererrorHandler
protected booleanfailOnError
protected Filefile
file to be validated
protected Vectorfilesets
sets of file to be validated
protected static StringINIT_FAILED_MSG
protected booleanlenient
static StringMESSAGE_FILES_VALIDATED
Message for sucessfull validation
protected StringreaderClassName
protected booleanwarn
protected XMLReaderxmlReader
the parser is viewed as a SAX2 XMLReader.
Method Summary
voidaddConfiguredXMLCatalog(XMLCatalog catalog)
add an XMLCatalog as a nested element; optional.
voidaddFileset(FileSet set)
specify a set of file to be checked
XMLValidateTask.AttributecreateAttribute()
Add an attribute nested element.
PathcreateClasspath()
protected XMLReadercreateDefaultReader()
create a reader if the use of the class did not specify another one.
DTDLocationcreateDTD()
Create a DTD location record; optional.
XMLValidateTask.PropertycreateProperty()
Creates a property.
protected XMLReadercreateXmlReader()
create the XML reader.
protected booleandoValidate(File afile)
parse the file
voidexecute()
execute the task
protected EntityResolvergetEntityResolver()
accessor to the xmlCatalog used in the task
protected XMLReadergetXmlReader()
get the XML reader.
voidinit()
Called by the project to let the task initialize properly.
protected voidinitValidator()
init the parser : load the parser class, and set features if necessary It is only after this that the reader is valid
protected booleanisSax1Parser()
test that returns true if we are using a SAX1 parser.
protected voidonSuccessfulValidation(int fileProcessed)
handler called on successful file validation.
voidsetClassName(String className)
Specify the class name of the SAX parser to be used. (optional)
voidsetClasspath(Path classpath)
Specify the classpath to be searched to load the parser (optional)
voidsetClasspathRef(Reference r)
Where to find the parser class; optional.
voidsetFailOnError(boolean fail)
Specify how parser error are to be handled.
protected voidsetFeature(String feature, boolean value)
Set a feature on the parser.
voidsetFile(File file)
specify the file to be checked; optional.
voidsetLenient(boolean bool)
Specify whether the parser should be validating.
protected voidsetProperty(String name, String value)
Sets a property.
voidsetWarn(boolean bool)
Specify how parser error are to be handled.

Field Detail

classpath

protected Path classpath

errorHandler

protected XMLValidateTask.ValidatorErrorHandler errorHandler

failOnError

protected boolean failOnError

file

protected File file
file to be validated

filesets

protected Vector filesets
sets of file to be validated

INIT_FAILED_MSG

protected static final String INIT_FAILED_MSG

lenient

protected boolean lenient

MESSAGE_FILES_VALIDATED

public static final String MESSAGE_FILES_VALIDATED
Message for sucessfull validation

readerClassName

protected String readerClassName

warn

protected boolean warn

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.

Method Detail

addConfiguredXMLCatalog

public void addConfiguredXMLCatalog(XMLCatalog catalog)
add an XMLCatalog as a nested element; optional.

Parameters: catalog XMLCatalog to use

addFileset

public void addFileset(FileSet set)
specify a set of file to be checked

Parameters: 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

Returns: attribute created

Since: ant1.6

createClasspath

public Path createClasspath()

Returns: the classpath created

See Also: XMLValidateTask

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.

Returns: a new reader.

Throws: BuildException if something went wrong

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.

Returns: created DTD location

createProperty

public XMLValidateTask.Property createProperty()
Creates a property.

Returns: a property.

Since: ant 1.6.2

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.

Returns: the new XMLReader.

doValidate

protected boolean doValidate(File afile)
parse the file

Parameters: afile the file to validate.

Returns: true if the file validates.

execute

public void execute()
execute the task

Throws: BuildException if failonerror is true and an error happens

getEntityResolver

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

Returns: 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 XMLValidateTask or XMLValidateTask on it.

Returns: the XML reader or null.

init

public void init()
Called by the project to let the task initialize properly.

Throws: BuildException if something goes wrong with the build

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

Throws: BuildException if something went wrong

isSax1Parser

protected boolean isSax1Parser()
test that returns true if we are using a SAX1 parser.

Returns: true when a SAX1 parser is in use

onSuccessfulValidation

protected void onSuccessfulValidation(int fileProcessed)
handler called on successful file validation.

Parameters: fileProcessed number of files processed.

setClassName

public void setClassName(String className)
Specify the class name of the SAX parser to be used. (optional)

Parameters: 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, XMLValidateTask, will be ignored.

if not set, the default will be used.

See Also: org.xml.sax.XMLReader org.xml.sax.Parser

setClasspath

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

Parameters: classpath the classpath to load the parser

setClasspathRef

public void setClasspathRef(Reference r)
Where to find the parser class; optional.

Parameters: r reference to a classpath defined elsewhere

See Also: XMLValidateTask

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.

Parameters: fail if set to false do not fail on error

setFeature

protected void setFeature(String feature, boolean value)
Set a feature on the parser.

Parameters: feature the name of the feature to set value the value of the feature

Throws: BuildException if the feature was not supported

setFile

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

Parameters: file the file to be checked

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 is not a SAX2 XMLReader.

Parameters: bool if set to false only fail on malformed XML

setProperty

protected void setProperty(String name, String value)
Sets a property.

Parameters: name a property name value a property value.

Throws: BuildException if an error occurs. BuildException if the property was not supported

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.

Parameters: bool if set to false do not send warnings