Fields Summary |
---|
private HashMap | schemaLocationsmap of all declared schemas; we catch and complain about redefinitions |
private boolean | fullCheckingfull checking of a schema |
private boolean | disableDTDflag to disable DTD support. Best left enabled. |
private SchemaLocation | anonymousSchemadefault URL for nonamespace schemas |
public static final String | ERROR_SAX_1SAX1 not supported |
public static final String | ERROR_NO_XSD_SUPPORTschema features not supported |
public static final String | ERROR_TOO_MANY_DEFAULT_SCHEMAStoo many default schemas |
public static final String | ERROR_PARSER_CREATION_FAILUREunable to create parser |
public static final String | MESSAGE_ADDING_SCHEMAadding schema |
public static final String | ERROR_DUPLICATE_SCHEMADuplicate declaration of schema |
Methods Summary |
---|
public void | addConfiguredSchema(org.apache.tools.ant.taskdefs.optional.SchemaValidate$SchemaLocation location)add the schema
log("adding schema " + location, Project.MSG_DEBUG);
location.validateNamespace();
SchemaLocation old = (SchemaLocation) schemaLocations.get(location.getNamespace());
if (old != null && !old.equals(location)) {
throw new BuildException(ERROR_DUPLICATE_SCHEMA + location);
}
schemaLocations.put(location.getNamespace(), location);
|
protected void | addSchemaLocations()build a string list of all schema locations, then set the relevant
property.
Iterator it = schemaLocations.values().iterator();
StringBuffer buffer = new StringBuffer();
int count = 0;
while (it.hasNext()) {
if (count > 0) {
buffer.append(' ");
}
SchemaLocation schemaLocation = (SchemaLocation) it.next();
String tuple = schemaLocation.getURIandLocation();
buffer.append(tuple);
log("Adding schema " + tuple, Project.MSG_VERBOSE);
count++;
}
if (count > 0) {
setProperty(XmlConstants.PROPERTY_SCHEMA_LOCATION, buffer.toString());
}
|
protected void | createAnonymousSchema()create a schema location to hold the anonymous
schema
if (anonymousSchema == null) {
anonymousSchema = new SchemaLocation();
}
anonymousSchema.setNamespace("(no namespace)");
|
protected org.xml.sax.XMLReader | createDefaultReader()Create a reader if the use of the class did not specify another one.
The reason to not use {@link JAXPUtils#getXMLReader()} was to
create our own factory with our own options.
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true);
XMLReader reader = null;
try {
SAXParser saxParser = factory.newSAXParser();
reader = saxParser.getXMLReader();
} catch (ParserConfigurationException e) {
throw new BuildException(ERROR_PARSER_CREATION_FAILURE, e);
} catch (SAXException e) {
throw new BuildException(ERROR_PARSER_CREATION_FAILURE, e);
}
return reader;
|
public boolean | enableJAXP12SchemaValidation()Set schema attributes in a JAXP 1.2 engine.
try {
//enable XSD
setProperty(XmlConstants.FEATURE_JAXP12_SCHEMA_LANGUAGE, XmlConstants.URI_XSD);
//set the schema source for the doc
setNoNamespaceSchemaProperty(XmlConstants.FEATURE_JAXP12_SCHEMA_SOURCE);
} catch (BuildException e) {
log(e.toString(), Project.MSG_VERBOSE);
return false;
}
return true;
|
public boolean | enableXercesSchemaValidation()Turn on XSD support in Xerces.
try {
setFeature(XmlConstants.FEATURE_XSD, true);
//set the schema source for the doc
setNoNamespaceSchemaProperty(XmlConstants.PROPERTY_NO_NAMESPACE_SCHEMA_LOCATION);
} catch (BuildException e) {
log(e.toString(), Project.MSG_VERBOSE);
return false;
}
return true;
|
protected java.lang.String | getNoNamespaceSchemaURL()get the URL of the no namespace schema
if (anonymousSchema == null) {
return null;
} else {
return anonymousSchema.getSchemaLocationURL();
}
|
public void | init()Called by the project to let the task initialize properly. The default
implementation is a no-op.
super.init();
//validating
setLenient(false);
|
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
super.initValidator();
//validate the parser type
if (isSax1Parser()) {
throw new BuildException(ERROR_SAX_1);
}
//enable schema
//setFeature(XmlConstants.FEATURE_VALIDATION, false);
setFeature(XmlConstants.FEATURE_NAMESPACES, true);
if (!enableXercesSchemaValidation() && !enableJAXP12SchemaValidation()) {
//couldnt use the xerces or jaxp calls
throw new BuildException(ERROR_NO_XSD_SUPPORT);
}
//enable schema checking
setFeature(XmlConstants.FEATURE_XSD_FULL_VALIDATION, fullChecking);
//turn off DTDs if desired
setFeatureIfSupported(XmlConstants.FEATURE_DISALLOW_DTD, disableDTD);
//schema declarations go in next
addSchemaLocations();
|
protected void | onSuccessfulValidation(int fileProcessed)handler called on successful file validation.
log(fileProcessed + MESSAGE_FILES_VALIDATED, Project.MSG_VERBOSE);
|
public void | setDisableDTD(boolean disableDTD)flag to disable DTD support.
this.disableDTD = disableDTD;
|
protected void | setFeatureIfSupported(java.lang.String feature, boolean value)set a feature if it is supported, log at verbose level if
not
try {
getXmlReader().setFeature(feature, value);
} catch (SAXNotRecognizedException e) {
log("Not recognizied: " + feature, Project.MSG_VERBOSE);
} catch (SAXNotSupportedException e) {
log("Not supported: " + feature, Project.MSG_VERBOSE);
}
|
public void | setFullChecking(boolean fullChecking)enable full schema checking. Slower but better.
this.fullChecking = fullChecking;
|
public void | setNoNamespaceFile(java.io.File defaultSchemaFile)identify a file containing the default schema
createAnonymousSchema();
this.anonymousSchema.setFile(defaultSchemaFile);
|
private void | setNoNamespaceSchemaProperty(java.lang.String property)set nonamespace handling up for xerces or other parsers
String anonSchema = getNoNamespaceSchemaURL();
if (anonSchema != null) {
setProperty(property, anonSchema);
}
|
public void | setNoNamespaceURL(java.lang.String defaultSchemaURL)identify the URL of the default schema
createAnonymousSchema();
this.anonymousSchema.setUrl(defaultSchemaURL);
|