Top Description Inners Fields Constructors Methods
org.apache.tools.ant.taskdefs.optional

public Class XMLValidateTask

extends Task
Class Inheritance
Known Direct Subclasses
org.apache.tools.ant.taskdefs.optional.SchemaValidate
Imports
java.io.File, .IOException, java.lang.reflect.InvocationTargetException, java.nio.file.Files, java.util.Vector, org.apache.tools.ant.AntClassLoader, .BuildException, .DirectoryScanner, .Project, .Task, org.apache.tools.ant.types.DTDLocation, .FileSet, .Path, .Reference, .XMLCatalog, org.apache.tools.ant.util.FileUtils, .JAXPUtils, .XmlConstants, org.xml.sax.EntityResolver, .ErrorHandler, .InputSource, .Parser, .SAXException, .SAXNotRecognizedException, .SAXNotSupportedException, .SAXParseException, .XMLReader, org.xml.sax.helpers.ParserAdapter

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 and Inner Type Summary

Modifier and TypeClass and Description
public static class
XMLValidateTask.Attribute

The class to create to set a feature of the parser.

public static class
XMLValidateTask.Property

A Parser property.

protected class
XMLValidateTask.ValidatorErrorHandler

ValidatorErrorHandler role :

  • log SAX parse exceptions,
  • remember if an error occurred

Field Summary

Modifier and TypeField and Description
private Vector<XMLValidateTask.Attribute>
attributeList

The vector to store all attributes (features) to be set on the parser.

protected Path
protected XMLValidateTask.ValidatorErrorHandler
protected boolean
protected File
file

file to be validated

private static final FileUtils
FILE_UTILS

helper for path -> URI and URI -> path conversions.

protected Vector<FileSet>
filesets

sets of file to be validated

protected static final String
protected boolean
public static final String
MESSAGE_FILES_VALIDATED

Message for successful validation

private final Vector<XMLValidateTask.Property>
propertyList

List of properties.

protected String
private AntClassLoader
protected boolean
private XMLCatalog
protected XMLReader
xmlReader

the parser is viewed as a SAX2 XMLReader.

Inherited from org.apache.tools.ant.Task:
targettaskNametaskTypewrapper

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
addConfiguredXMLCatalog(XMLCatalog
XMLCatalog to use
catalog
)

add an XMLCatalog as a nested element; optional.

public void
addFileset(FileSet
the fileset to check
set
)

specify a set of file to be checked

protected void
cleanup()

Cleans up resources.

public XMLValidateTask.Attribute

Returns:

attribute created
createAttribute
()

Add an attribute nested element.

public Path

Returns:

the classpath created
createClasspath
()

protected XMLReader

Returns:

a new reader.
createDefaultReader
()

Create a reader if the use of the class did not specify another one.

private Object

Returns:

reader or parser
createDefaultReaderOrParser
()

Returns a SAX-based XMLReader or a SAX-based Parser.

public DTDLocation

Returns:

created DTD location
createDTD
()

Create a DTD location record; optional.

public XMLValidateTask.Property

Returns:

a property.
createProperty
()

Creates a property.

protected XMLReader

Returns:

the new XMLReader.
createXmlReader
()

create the XML reader.

protected boolean

Returns:

true if the file validates.
doValidate
(File
the file to validate.
afile
)

parse the file

public void
execute()

Overrides org.apache.tools.ant.Task.execute.

execute the task
protected EntityResolver

Returns:

xmlCatalog reference
getEntityResolver
()

accessor to the xmlCatalog used in the task

protected XMLReader

Returns:

the XML reader or null.
getXmlReader
()

get the XML reader.

public void
init()

Overrides org.apache.tools.ant.Task.init.

Called by the project to let the task initialize properly.
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

protected boolean

Returns:

true when a SAX1 parser is in use
isSax1Parser
()

test that returns true if we are using a SAX1 parser.

protected void
onSuccessfulValidation(int
number of files processed.
fileProcessed
)

handler called on successful file validation.

public void
setClassName(String
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.

className
)

Specify the class name of the SAX parser to be used.

public void
setClasspath(Path
the classpath to load the parser
classpath
)

Specify the classpath to be searched to load the parser (optional)

public void
setClasspathRef(Reference
reference to a classpath defined elsewhere
r
)

Where to find the parser class; optional.

public void
setFailOnError(boolean
if set to false do not fail on error
fail
)

Specify how parser error are to be handled.

protected void
setFeature(String
the name of the feature to set
feature
,
boolean
the value of the feature
value
)

Set a feature on the parser.

public void
setFile(File
the file to be checked
file
)

specify the file to be checked; optional.

public void
setLenient(boolean
if set to false only fail on malformed XML
bool
)

Specify whether the parser should be validating.

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

Sets a property.

public void
setWarn(boolean
if set to false do not send warnings
bool
)

Specify how parser error are to be handled.

Inherited from org.apache.tools.ant.Task:
bindToOwnergetOwningTargetgetRuntimeConfigurableWrappergetTaskNamegetTaskTypegetWrapperhandleErrorFlushhandleErrorOutputhandleFlushhandleInputhandleOutputisInvalidloglogloglogmaybeConfigureperformreconfiguresetOwningTargetsetRuntimeConfigurableWrappersetTaskNamesetTaskType

Field Detail

attributeListback to summary
private Vector<XMLValidateTask.Attribute> attributeList

The vector to store all attributes (features) to be set on the parser.

classpathback to summary
protected Path classpath
errorHandlerback to summary
protected XMLValidateTask.ValidatorErrorHandler errorHandler
failOnErrorback to summary
protected boolean failOnError
fileback to summary
protected File file

file to be validated

FILE_UTILSback to summary
private static final FileUtils FILE_UTILS

helper for path -> URI and URI -> path conversions.

filesetsback to summary
protected Vector<FileSet> filesets

sets of file to be validated

INIT_FAILED_MSGback to summary
protected static final String INIT_FAILED_MSG
lenientback to summary
protected boolean lenient
MESSAGE_FILES_VALIDATEDback to summary
public static final String MESSAGE_FILES_VALIDATED

Message for successful validation

propertyListback to summary
private final Vector<XMLValidateTask.Property> propertyList

List of properties.

readerClassNameback to summary
protected String readerClassName
readerLoaderback to summary
private AntClassLoader readerLoader
warnback to summary
protected boolean warn
xmlCatalogback to summary
private XMLCatalog xmlCatalog
xmlReaderback to summary
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.

Constructor Detail

XMLValidateTaskback to summary
public XMLValidateTask()

Method Detail

addConfiguredXMLCatalogback to summary
public void addConfiguredXMLCatalog(XMLCatalog catalog)

add an XMLCatalog as a nested element; optional.

Parameters
catalog:XMLCatalog

XMLCatalog to use

addFilesetback to summary
public void addFileset(FileSet set)

specify a set of file to be checked

Parameters
set:FileSet

the fileset to check

cleanupback to summary
protected void cleanup()

Cleans up resources.

Since
Ant 1.8.0
createAttributeback to summary
public XMLValidateTask.Attribute createAttribute()

Add an attribute nested element. This is used for setting arbitrary features of the SAX parser. Valid attributes include

Returns:XMLValidateTask.Attribute

attribute created

Since
ant1.6
createClasspathback to summary
public Path createClasspath()
Returns:Path

the classpath created

See Also
setClasspath
createDefaultReaderback to summary
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:XMLReader

a new reader.

Exceptions
BuildException:
if something went wrong
createDefaultReaderOrParserback to summary
private Object createDefaultReaderOrParser()

Returns a SAX-based XMLReader or a SAX-based Parser.

Returns:Object

reader or parser

createDTDback to summary
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:DTDLocation

created DTD location

createPropertyback to summary
public XMLValidateTask.Property createProperty()

Creates a property.

Returns:XMLValidateTask.Property

a property.

Since
ant 1.6.2
createXmlReaderback to summary
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:XMLReader

the new XMLReader.

doValidateback to summary
protected boolean doValidate(File afile)

parse the file

Parameters
afile:File

the file to validate.

Returns:boolean

true if the file validates.

executeback to summary
public void execute() throws BuildException

Overrides org.apache.tools.ant.Task.execute.

execute the task

Exceptions
BuildException:
if failonerror is true and an error happens
getEntityResolverback to summary
protected EntityResolver getEntityResolver()

accessor to the xmlCatalog used in the task

Returns:EntityResolver

xmlCatalog reference

getXmlReaderback to summary
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.

Returns:XMLReader

the XML reader or null.

initback to summary
public void init() throws BuildException

Overrides org.apache.tools.ant.Task.init.

Called by the project to let the task initialize properly.

Exceptions
BuildException:
if something goes wrong with the build
initValidatorback to summary
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

Exceptions
BuildException:
if something went wrong
isSax1Parserback to summary
protected boolean isSax1Parser()

test that returns true if we are using a SAX1 parser.

Returns:boolean

true when a SAX1 parser is in use

onSuccessfulValidationback to summary
protected void onSuccessfulValidation(int fileProcessed)

handler called on successful file validation.

Parameters
fileProcessed:int

number of files processed.

setClassNameback to summary
public void setClassName(String className)

Specify the class name of the SAX parser to be used. (optional)

Parameters
className:String

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
org.xml.sax.XMLReader, org.xml.sax.Parser
setClasspathback to summary
public void setClasspath(Path classpath)

Specify the classpath to be searched to load the parser (optional)

Parameters
classpath:Path

the classpath to load the parser

setClasspathRefback to summary
public void setClasspathRef(Reference r)

Where to find the parser class; optional.

Parameters
r:Reference

reference to a classpath defined elsewhere

See Also
setClasspath
setFailOnErrorback to summary
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:boolean

if set to false do not fail on error

setFeatureback to summary
protected void setFeature(String feature, boolean value) throws BuildException

Set a feature on the parser.

Parameters
feature:String

the name of the feature to set

value:boolean

the value of the feature

Exceptions
BuildException:
if the feature was not supported
setFileback to summary
public void setFile(File file)

specify the file to be checked; optional.

Parameters
file:File

the file to be checked

setLenientback to summary
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.

Parameters
bool:boolean

if set to false only fail on malformed XML

setPropertyback to summary
protected void setProperty(String name, String value) throws BuildException

Sets a property.

Parameters
name:String

a property name

value:String

a property value.

Exceptions
BuildException:
  • if an error occurs.
  • if the property was not supported
setWarnback to summary
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:boolean

if set to false do not send warnings

org.apache.tools.ant.taskdefs.optional back to summary

public Class XMLValidateTask.Attribute

extends Object
Class Inheritance
  • java.lang.Object
  • org.apache.tools.ant.taskdefs.optional.XMLValidateTask.Attribute

The class to create to set a feature of the parser.
Since
ant1.6

Field Summary

Modifier and TypeField and Description
private String
attributeName

The name of the attribute to set.

private boolean
attributeValue

The value of the feature.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public String

Returns:

the feature name
getName
()

Gets the attribute name.

public boolean

Returns:

the feature value
getValue
()

Gets the attribute value.

public void
setName(String
the name to set
name
)

Set the feature name.

public void
setValue(boolean
feature value
value
)

Set the feature value to true or false.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

attributeNameback to summary
private String attributeName

The name of the attribute to set. Valid attributes include

attributeValueback to summary
private boolean attributeValue

The value of the feature.

Constructor Detail

Attributeback to summary
public Attribute()

Method Detail

getNameback to summary
public String getName()

Gets the attribute name.

Returns:String

the feature name

getValueback to summary
public boolean getValue()

Gets the attribute value.

Returns:boolean

the feature value

setNameback to summary
public void setName(String name)

Set the feature name.

Parameters
name:String

the name to set

setValueback to summary
public void setValue(boolean value)

Set the feature value to true or false.

Parameters
value:boolean

feature value

org.apache.tools.ant.taskdefs.optional back to summary

public final Class XMLValidateTask.Property

extends Object
Class Inheritance
  • java.lang.Object
  • org.apache.tools.ant.taskdefs.optional.XMLValidateTask.Property

A Parser property. See XML parser properties for usable properties
Since
ant 1.6.2

Field Summary

Modifier and TypeField and Description
private String
private String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public String

Returns:

name of the property
getName
()

accessor to the name of the property

public String

Returns:

value of the property
getValue
()

getter for the value of the property

public void
setName(String
name of the property
name
)

setter for the name of the property

public void
setValue(String
value of the property
value
)

sets the value of the property

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

nameback to summary
private String name
valueback to summary
private String value

Constructor Detail

Propertyback to summary
public Property()

Method Detail

getNameback to summary
public String getName()

accessor to the name of the property

Returns:String

name of the property

getValueback to summary
public String getValue()

getter for the value of the property

Returns:String

value of the property

setNameback to summary
public void setName(String name)

setter for the name of the property

Parameters
name:String

name of the property

setValueback to summary
public void setValue(String value)

sets the value of the property

Parameters
value:String

value of the property

org.apache.tools.ant.taskdefs.optional back to summary

protected Class XMLValidateTask.ValidatorErrorHandler

extends Object
implements ErrorHandler
Class Inheritance
  • java.lang.Object
  • org.apache.tools.ant.taskdefs.optional.XMLValidateTask.ValidatorErrorHandler
All Implemented Interfaces
org.xml.sax.ErrorHandler

ValidatorErrorHandler role :

Field Summary

Modifier and TypeField and Description
protected File
protected boolean
protected String

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Modifier and TypeMethod and Description
private void
doLog(SAXParseException e, int logLevel)

public void
error(SAXParseException
the error
exception
)

Implements org.xml.sax.ErrorHandler.error.

receive notification of a recoverable error
public void
fatalError(SAXParseException
the fatal error
exception
)

Implements org.xml.sax.ErrorHandler.fatalError.

record a fatal error
public boolean

Returns:

did an error happen during last parsing ?
getFailure
()

did an error happen during last parsing ?

private String
public void
init(File
file used
file
)

initialises the class

public void
warning(SAXParseException
the warning
exception
)

Implements org.xml.sax.ErrorHandler.warning.

receive notification of a warning
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

currentFileback to summary
protected File currentFile
failedback to summary
protected boolean failed
lastErrorMessageback to summary
protected String lastErrorMessage

Constructor Detail

ValidatorErrorHandlerback to summary
protected ValidatorErrorHandler()

Method Detail

doLogback to summary
private void doLog(SAXParseException e, int logLevel)
errorback to summary
public void error(SAXParseException exception)

Implements org.xml.sax.ErrorHandler.error.

receive notification of a recoverable error

Parameters
exception:SAXParseException

the error

fatalErrorback to summary
public void fatalError(SAXParseException exception)

Implements org.xml.sax.ErrorHandler.fatalError.

record a fatal error

Parameters
exception:SAXParseException

the fatal error

getFailureback to summary
public boolean getFailure()

did an error happen during last parsing ?

Returns:boolean

did an error happen during last parsing ?

getMessageback to summary
private String getMessage(SAXParseException e)
initback to summary
public void init(File file)

initialises the class

Parameters
file:File

file used

warningback to summary
public void warning(SAXParseException exception)

Implements org.xml.sax.ErrorHandler.warning.

receive notification of a warning

Parameters
exception:SAXParseException

the warning