Methods Summary |
---|
public void | addConfiguredXMLCatalog(org.apache.tools.ant.types.XMLCatalog catalog)add an XMLCatalog as a nested element; optional.
xmlCatalog.addConfiguredXMLCatalog(catalog);
|
public void | addFileset(org.apache.tools.ant.types.FileSet set)specify a set of file to be checked
filesets.addElement(set);
|
public org.apache.tools.ant.taskdefs.optional.XMLValidateTask$Attribute | createAttribute()Add an attribute nested element. This is used for setting arbitrary
features of the SAX parser.
Valid attributes
include
final Attribute feature = new Attribute();
attributeList.addElement(feature);
return feature;
|
public org.apache.tools.ant.types.Path | createClasspath()
if (this.classpath == null) {
this.classpath = new Path(getProject());
}
return this.classpath.createPath();
|
public org.apache.tools.ant.types.DTDLocation | createDTD()Create a DTD location record; optional.
This stores the location of a DTD. The DTD is identified
by its public Id.
DTDLocation dtdLocation = new DTDLocation();
xmlCatalog.addDTD(dtdLocation);
return dtdLocation;
|
protected 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.
return JAXPUtils.getXMLReader();
|
private java.lang.Object | createDefaultReaderOrParser()
Object reader;
try {
reader = createDefaultReader();
} catch (BuildException exc) {
reader = JAXPUtils.getParser();
}
return reader;
|
public org.apache.tools.ant.taskdefs.optional.XMLValidateTask$Property | createProperty()Creates a property.
final Property prop = new Property();
propertyList.addElement(prop);
return prop;
|
protected org.xml.sax.XMLReader | createXmlReader()create the XML reader.
This is one by instantiating anything specified by {@link #readerClassName},
falling back to a default reader if not.
If the returned reader is an instance of {@link ParserAdapter} then
we have created and wrapped a SAX1 parser.
Object reader = null;
if (readerClassName == null) {
reader = createDefaultReaderOrParser();
} else {
Class readerClass = null;
try {
// load the parser class
if (classpath != null) {
AntClassLoader loader =
getProject().createClassLoader(classpath);
readerClass = Class.forName(readerClassName, true, loader);
} else {
readerClass = Class.forName(readerClassName);
}
reader = readerClass.newInstance();
} catch (ClassNotFoundException e) {
throw new BuildException(INIT_FAILED_MSG + readerClassName, e);
} catch (InstantiationException e) {
throw new BuildException(INIT_FAILED_MSG + readerClassName, e);
} catch (IllegalAccessException e) {
throw new BuildException(INIT_FAILED_MSG + readerClassName, e);
}
}
// then check it implements XMLReader
XMLReader newReader;
if (reader instanceof XMLReader) {
newReader = (XMLReader) reader;
log(
"Using SAX2 reader " + reader.getClass().getName(),
Project.MSG_VERBOSE);
} else {
// see if it is a SAX1 Parser
if (reader instanceof Parser) {
newReader = new ParserAdapter((Parser) reader);
log(
"Using SAX1 parser " + reader.getClass().getName(),
Project.MSG_VERBOSE);
} else {
throw new BuildException(
INIT_FAILED_MSG
+ reader.getClass().getName()
+ " implements nor SAX1 Parser nor SAX2 XMLReader.");
}
}
return newReader;
|
protected boolean | doValidate(java.io.File afile)parse the file
//for every file, we have a new instance of the validator
initValidator();
boolean result = true;
try {
log("Validating " + afile.getName() + "... ", Project.MSG_VERBOSE);
errorHandler.init(afile);
InputSource is = new InputSource(new FileInputStream(afile));
String uri = FILE_UTILS.toURI(afile.getAbsolutePath());
is.setSystemId(uri);
xmlReader.parse(is);
} catch (SAXException ex) {
log("Caught when validating: " + ex.toString(), Project.MSG_DEBUG);
if (failOnError) {
throw new BuildException(
"Could not validate document " + afile);
}
log("Could not validate document " + afile + ": " + ex.toString());
result = false;
} catch (IOException ex) {
throw new BuildException(
"Could not validate document " + afile,
ex);
}
if (errorHandler.getFailure()) {
if (failOnError) {
throw new BuildException(
afile + " is not a valid XML document.");
}
result = false;
log(afile + " is not a valid XML document", Project.MSG_ERR);
}
return result;
|
public void | execute()execute the task
int fileProcessed = 0;
if (file == null && (filesets.size() == 0)) {
throw new BuildException(
"Specify at least one source - " + "a file or a fileset.");
}
if (file != null) {
if (file.exists() && file.canRead() && file.isFile()) {
doValidate(file);
fileProcessed++;
} else {
String errorMsg = "File " + file + " cannot be read";
if (failOnError) {
throw new BuildException(errorMsg);
} else {
log(errorMsg, Project.MSG_ERR);
}
}
}
for (int i = 0; i < filesets.size(); i++) {
FileSet fs = (FileSet) filesets.elementAt(i);
DirectoryScanner ds = fs.getDirectoryScanner(getProject());
String[] files = ds.getIncludedFiles();
for (int j = 0; j < files.length; j++) {
File srcFile = new File(fs.getDir(getProject()), files[j]);
doValidate(srcFile);
fileProcessed++;
}
}
onSuccessfulValidation(fileProcessed);
|
protected org.xml.sax.EntityResolver | getEntityResolver()accessor to the xmlCatalog used in the task
return xmlCatalog;
|
protected org.xml.sax.XMLReader | getXmlReader()get the XML reader. Non-null only after {@link #initValidator()}.
If the reader is an instance of {@link ParserAdapter} then
the parser is a SAX1 parser, and you cannot call
{@link #setFeature(String, boolean)} or {@link #setProperty(String, String)}
on it.
return xmlReader;
|
public void | init()Called by the project to let the task initialize properly.
super.init();
xmlCatalog.setProject(getProject());
|
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
xmlReader = createXmlReader();
xmlReader.setEntityResolver(getEntityResolver());
xmlReader.setErrorHandler(errorHandler);
if (!isSax1Parser()) {
// turn validation on
if (!lenient) {
setFeature(XmlConstants.FEATURE_VALIDATION, true);
}
// set the feature from the attribute list
for (int i = 0; i < attributeList.size(); i++) {
Attribute feature = (Attribute) attributeList.elementAt(i);
setFeature(feature.getName(), feature.getValue());
}
// Sets properties
for (int i = 0; i < propertyList.size(); i++) {
final Property prop = (Property) propertyList.elementAt(i);
setProperty(prop.getName(), prop.getValue());
}
}
|
protected boolean | isSax1Parser()test that returns true if we are using a SAX1 parser.
return (xmlReader instanceof ParserAdapter);
|
protected void | onSuccessfulValidation(int fileProcessed)handler called on successful file validation.
log(fileProcessed + MESSAGE_FILES_VALIDATED);
|
public void | setClassName(java.lang.String className)Specify the class name of the SAX parser to be used. (optional)
readerClassName = className;
|
public void | setClasspath(org.apache.tools.ant.types.Path classpath)Specify the classpath to be searched to load the parser (optional)
if (this.classpath == null) {
this.classpath = classpath;
} else {
this.classpath.append(classpath);
}
|
public void | setClasspathRef(org.apache.tools.ant.types.Reference r)Where to find the parser class; optional.
createClasspath().setRefid(r);
|
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.
failOnError = fail;
|
protected void | setFeature(java.lang.String feature, boolean value)Set a feature on the parser.
log("Setting feature " + feature + "=" + value, Project.MSG_DEBUG);
try {
xmlReader.setFeature(feature, value);
} catch (SAXNotRecognizedException e) {
throw new BuildException(
"Parser "
+ xmlReader.getClass().getName()
+ " doesn't recognize feature "
+ feature,
e,
getLocation());
} catch (SAXNotSupportedException e) {
throw new BuildException(
"Parser "
+ xmlReader.getClass().getName()
+ " doesn't support feature "
+ feature,
e,
getLocation());
}
|
public void | setFile(java.io.File file)specify the file to be checked; optional.
this.file = file;
|
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 {@link #setClassName(String)} is not a SAX2 XMLReader.
lenient = bool;
|
protected void | setProperty(java.lang.String name, java.lang.String value)Sets a property.
// Validates property
if (name == null || value == null) {
throw new BuildException("Property name and value must be specified.");
}
try {
xmlReader.setProperty(name, value);
} catch (SAXNotRecognizedException e) {
throw new BuildException(
"Parser "
+ xmlReader.getClass().getName()
+ " doesn't recognize property "
+ name,
e,
getLocation());
} catch (SAXNotSupportedException e) {
throw new BuildException(
"Parser "
+ xmlReader.getClass().getName()
+ " doesn't support property "
+ name,
e,
getLocation());
}
|
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.
warn = bool;
|