Class XMLValidateTask
java.lang.Object
org.apache.tools.ant.ProjectComponent
org.apache.tools.ant.Task
org.apache.tools.ant.taskdefs.optional.XMLValidateTask
- All Implemented Interfaces:
Cloneable
- Direct Known Subclasses:
SchemaValidate
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
Modifier and TypeClassDescriptionstatic class
The class to create to set a feature of the parser.static final class
A Parser property.protected class
ValidatorErrorHandler role : log SAX parse exceptions, remember if an error occurred -
Field Summary
Modifier and TypeFieldDescriptionprotected Path
protected XMLValidateTask.ValidatorErrorHandler
protected boolean
protected File
file to be validatedsets of file to be validatedprotected static final String
protected boolean
static final String
Message for successful validationprotected String
protected boolean
protected XMLReader
the parser is viewed as a SAX2 XMLReader.Fields inherited from class org.apache.tools.ant.ProjectComponent
description, location, project
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addConfiguredXMLCatalog
(XMLCatalog catalog) add an XMLCatalog as a nested element; optional.void
addFileset
(FileSet set) specify a set of file to be checkedprotected void
cleanup()
Cleans up resources.Add an attribute nested element.protected XMLReader
Create a reader if the use of the class did not specify another one.Create a DTD location record; optional.Creates a property.protected XMLReader
create the XML reader.protected boolean
doValidate
(File afile) parse the filevoid
execute()
execute the taskprotected EntityResolver
accessor to the xmlCatalog used in the taskprotected XMLReader
get the XML reader.void
init()
Called by the project to let the task initialize properly.protected void
init the parser : load the parser class, and set features if necessary It is only after this that the reader is validprotected boolean
test that returns true if we are using a SAX1 parser.protected void
onSuccessfulValidation
(int fileProcessed) handler called on successful file validation.void
setClassName
(String className) Specify the class name of the SAX parser to be used.void
setClasspath
(Path classpath) Specify the classpath to be searched to load the parser (optional)void
Where to find the parser class; optional.void
setFailOnError
(boolean fail) Specify how parser error are to be handled.protected void
setFeature
(String feature, boolean value) Set a feature on the parser.void
specify the file to be checked; optional.void
setLenient
(boolean bool) Specify whether the parser should be validating.protected void
setProperty
(String name, String value) Sets a property.void
setWarn
(boolean bool) Specify how parser error are to be handled.Methods inherited from class org.apache.tools.ant.Task
bindToOwner, getOwningTarget, getRuntimeConfigurableWrapper, getTaskName, getTaskType, getWrapper, handleErrorFlush, handleErrorOutput, handleFlush, handleInput, handleOutput, isInvalid, log, log, log, log, maybeConfigure, perform, reconfigure, setOwningTarget, setRuntimeConfigurableWrapper, setTaskName, setTaskType
Methods inherited from class org.apache.tools.ant.ProjectComponent
clone, getDescription, getLocation, getProject, setDescription, setLocation, setProject
-
Field Details
-
INIT_FAILED_MSG
- See Also:
-
failOnError
protected boolean failOnError -
warn
protected boolean warn -
lenient
protected boolean lenient -
readerClassName
-
file
file to be validated -
filesets
-
classpath
-
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
-
MESSAGE_FILES_VALIDATED
-
-
Constructor Details
-
XMLValidateTask
public XMLValidateTask()
-
-
Method Details
-
setFailOnError
public void setFailOnError(boolean fail) Specify how parser error are to be handled. Optional, default istrue
.If set to
true
(default), throw a buildException if the parser yields an error.- Parameters:
fail
- if set tofalse
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.- Parameters:
bool
- if set tofalse
do not send warnings
-
setLenient
public void setLenient(boolean bool) Specify whether the parser should be validating. Default istrue
.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
- if set tofalse
only fail on malformed XML
-
setClassName
Specify the class name of the SAX parser to be used. (optional)- Parameters:
className
- should be an implementation of SAX2org.xml.sax.XMLReader
or SAX2org.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
Specify the classpath to be searched to load the parser (optional)- Parameters:
classpath
- the classpath to load the parser
-
createClasspath
- Returns:
- the classpath created
- See Also:
-
setClasspathRef
Where to find the parser class; optional.- Parameters:
r
- reference to a classpath defined elsewhere- See Also:
-
setFile
specify the file to be checked; optional.- Parameters:
file
- the file to be checked
-
addConfiguredXMLCatalog
add an XMLCatalog as a nested element; optional.- Parameters:
catalog
- XMLCatalog to use
-
addFileset
specify a set of file to be checked- Parameters:
set
- the fileset to check
-
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
-
createProperty
Creates a property.- Returns:
- a property.
- Since:
- ant 1.6.2
-
init
Called by the project to let the task initialize properly.- Overrides:
init
in classTask
- Throws:
BuildException
- if something goes wrong with the build
-
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
-
getEntityResolver
accessor to the xmlCatalog used in the task- Returns:
- xmlCatalog reference
-
getXmlReader
get the XML reader. Non-null only afterinitValidator()
. If the reader is an instance ofParserAdapter
then the parser is a SAX1 parser, and you cannot callsetFeature(String, boolean)
orsetProperty(String, String)
on it.- Returns:
- the XML reader or null.
-
execute
execute the task- Overrides:
execute
in classTask
- Throws:
BuildException
- iffailonerror
is true and an error happens
-
onSuccessfulValidation
protected void onSuccessfulValidation(int fileProcessed) handler called on successful file validation.- Parameters:
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- 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
-
createXmlReader
create the XML reader. This is one by instantiating anything specified byreaderClassName
, falling back to a default reader if not. If the returned reader is an instance ofParserAdapter
then we have created and wrapped a SAX1 parser.- Returns:
- the new XMLReader.
-
cleanup
protected void cleanup()Cleans up resources.- Since:
- Ant 1.8.0
-
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
-
setFeature
Set a feature on the parser.- Parameters:
feature
- the name of the feature to setvalue
- the value of the feature- Throws:
BuildException
- if the feature was not supported
-
setProperty
Sets a property.- Parameters:
name
- a property namevalue
- a property value.- Throws:
BuildException
- if an error occurs.BuildException
- if the property was not supported
-
doValidate
parse the file- Parameters:
afile
- the file to validate.- Returns:
- true if the file validates.
-