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:
- java.lang.Cloneable
 - Direct Known Subclasses:
- SchemaValidate
 
 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 SummaryNested Classes Modifier and Type Class Description static classXMLValidateTask.AttributeThe class to create to set a feature of the parser.static classXMLValidateTask.PropertyA Parser property.protected classXMLValidateTask.ValidatorErrorHandlerValidatorErrorHandler role : log SAX parse exceptions, remember if an error occurred
 - 
Field SummaryFields Modifier and Type Field Description protected Pathclasspathprotected XMLValidateTask.ValidatorErrorHandlererrorHandlerprotected booleanfailOnErrorprotected java.io.Filefilefile to be validatedprotected java.util.Vector<FileSet>filesetssets of file to be validatedprotected static java.lang.StringINIT_FAILED_MSGprotected booleanlenientstatic java.lang.StringMESSAGE_FILES_VALIDATEDMessage for successful validationprotected java.lang.StringreaderClassNameprotected booleanwarnprotected org.xml.sax.XMLReaderxmlReaderthe parser is viewed as a SAX2 XMLReader.- 
Fields inherited from class org.apache.tools.ant.ProjectComponentdescription, location, project
 
- 
 - 
Constructor SummaryConstructors Constructor Description XMLValidateTask()
 - 
Method SummaryModifier and Type Method Description voidaddConfiguredXMLCatalog(XMLCatalog catalog)add an XMLCatalog as a nested element; optional.voidaddFileset(FileSet set)specify a set of file to be checkedprotected voidcleanup()Cleans up resources.XMLValidateTask.AttributecreateAttribute()Add an attribute nested element.PathcreateClasspath()protected org.xml.sax.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 org.xml.sax.XMLReadercreateXmlReader()create the XML reader.protected booleandoValidate(java.io.File afile)parse the filevoidexecute()execute the taskprotected org.xml.sax.EntityResolvergetEntityResolver()accessor to the xmlCatalog used in the taskprotected org.xml.sax.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 validprotected booleanisSax1Parser()test that returns true if we are using a SAX1 parser.protected voidonSuccessfulValidation(int fileProcessed)handler called on successful file validation.voidsetClassName(java.lang.String className)Specify the class name of the SAX parser to be used.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(java.lang.String feature, boolean value)Set a feature on the parser.voidsetFile(java.io.File file)specify the file to be checked; optional.voidsetLenient(boolean bool)Specify whether the parser should be validating.protected voidsetProperty(java.lang.String name, java.lang.String value)Sets a property.voidsetWarn(boolean bool)Specify how parser error are to be handled.- 
Methods inherited from class org.apache.tools.ant.TaskbindToOwner, 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.ProjectComponentclone, getDescription, getLocation, getProject, setDescription, setLocation, setProject
 
- 
 
- 
- 
- 
Field Detail- 
INIT_FAILED_MSGprotected static final java.lang.String INIT_FAILED_MSG - See Also:
- Constant Field Values
 
 - 
failOnErrorprotected boolean failOnError 
 - 
warnprotected boolean warn 
 - 
lenientprotected boolean lenient 
 - 
readerClassNameprotected java.lang.String readerClassName 
 - 
fileprotected java.io.File file file to be validated
 - 
filesetsprotected java.util.Vector<FileSet> filesets sets of file to be validated
 - 
classpathprotected Path classpath 
 - 
xmlReaderprotected org.xml.sax.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.
 - 
errorHandlerprotected XMLValidateTask.ValidatorErrorHandler errorHandler 
 - 
MESSAGE_FILES_VALIDATEDpublic static final java.lang.String MESSAGE_FILES_VALIDATED Message for successful validation- See Also:
- Constant Field Values
 
 
- 
 - 
Method Detail- 
setFailOnErrorpublic 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 to- falsedo not fail on error
 
 - 
setWarnpublic 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- falsedo not send warnings
 
 - 
setLenientpublic 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 to- falseonly fail on malformed XML
 
 - 
setClassNamepublic void setClassName(java.lang.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.XMLReaderor 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:
- XMLReader,- Parser
 
 - 
setClasspathpublic void setClasspath(Path classpath) Specify the classpath to be searched to load the parser (optional)- Parameters:
- classpath- the classpath to load the parser
 
 - 
createClasspathpublic Path createClasspath() - Returns:
- the classpath created
- See Also:
- setClasspath(org.apache.tools.ant.types.Path)
 
 - 
setClasspathRefpublic void setClasspathRef(Reference r) Where to find the parser class; optional.- Parameters:
- r- reference to a classpath defined elsewhere
- See Also:
- setClasspath(org.apache.tools.ant.types.Path)
 
 - 
setFilepublic void setFile(java.io.File file) specify the file to be checked; optional.- Parameters:
- file- the file to be checked
 
 - 
addConfiguredXMLCatalogpublic void addConfiguredXMLCatalog(XMLCatalog catalog) add an XMLCatalog as a nested element; optional.- Parameters:
- catalog- XMLCatalog to use
 
 - 
addFilesetpublic void addFileset(FileSet set) specify a set of file to be checked- Parameters:
- set- the fileset to check
 
 - 
createAttributepublic 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
 
 - 
createPropertypublic XMLValidateTask.Property createProperty() Creates a property.- Returns:
- a property.
- Since:
- ant 1.6.2
 
 - 
initpublic void init() throws BuildExceptionCalled by the project to let the task initialize properly.- Overrides:
- initin class- Task
- Throws:
- BuildException- if something goes wrong with the build
 
 - 
createDTDpublic 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
 
 - 
getEntityResolverprotected org.xml.sax.EntityResolver getEntityResolver() accessor to the xmlCatalog used in the task- Returns:
- xmlCatalog reference
 
 - 
getXmlReaderprotected org.xml.sax.XMLReader getXmlReader() get the XML reader. Non-null only afterinitValidator(). If the reader is an instance ofParserAdapterthen the parser is a SAX1 parser, and you cannot callsetFeature(String, boolean)orsetProperty(String, String)on it.- Returns:
- the XML reader or null.
 
 - 
executepublic void execute() throws BuildExceptionexecute the task- Overrides:
- executein class- Task
- Throws:
- BuildException- if- failonerroris true and an error happens
 
 - 
onSuccessfulValidationprotected void onSuccessfulValidation(int fileProcessed) handler called on successful file validation.- Parameters:
- fileProcessed- number of files processed.
 
 - 
initValidatorprotected 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
 
 - 
isSax1Parserprotected boolean isSax1Parser() test that returns true if we are using a SAX1 parser.- Returns:
- true when a SAX1 parser is in use
 
 - 
createXmlReaderprotected org.xml.sax.XMLReader 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 ofParserAdapterthen we have created and wrapped a SAX1 parser.- Returns:
- the new XMLReader.
 
 - 
cleanupprotected void cleanup() Cleans up resources.- Since:
- Ant 1.8.0
 
 - 
createDefaultReaderprotected org.xml.sax.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
 
 - 
setFeatureprotected void setFeature(java.lang.String feature, boolean value) throws BuildExceptionSet 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
 
 - 
setPropertyprotected void setProperty(java.lang.String name, java.lang.String value) throws BuildExceptionSets a property.- Parameters:
- name- a property name
- value- a property value.
- Throws:
- BuildException- if an error occurs.
- BuildException- if the property was not supported
 
 - 
doValidateprotected boolean doValidate(java.io.File afile) parse the file- Parameters:
- afile- the file to validate.
- Returns:
- true if the file validates.
 
 
- 
 
-