FileDocCategorySizeDatePackage
SchemaValidate.javaAPI DocApache Ant 1.7017263Wed Dec 13 06:16:22 GMT 2006org.apache.tools.ant.taskdefs.optional

SchemaValidate

public class SchemaValidate extends XMLValidateTask
Validate XML Schema documents. This task validates XML schema documents. It requires an XML parser that handles the relevant SAx, Xerces or JAXP options. To resolve remote referencies, Ant may need its proxy set up, using the setproxy task. Hands off most of the work to its parent, {@link XMLValidateTask}
since
Ant1.7

Fields Summary
private HashMap
schemaLocations
map of all declared schemas; we catch and complain about redefinitions
private boolean
fullChecking
full checking of a schema
private boolean
disableDTD
flag to disable DTD support. Best left enabled.
private SchemaLocation
anonymousSchema
default URL for nonamespace schemas
public static final String
ERROR_SAX_1
SAX1 not supported
public static final String
ERROR_NO_XSD_SUPPORT
schema features not supported
public static final String
ERROR_TOO_MANY_DEFAULT_SCHEMAS
too many default schemas
public static final String
ERROR_PARSER_CREATION_FAILURE
unable to create parser
public static final String
MESSAGE_ADDING_SCHEMA
adding schema
public static final String
ERROR_DUPLICATE_SCHEMA
Duplicate declaration of schema
Constructors Summary
Methods Summary
public voidaddConfiguredSchema(org.apache.tools.ant.taskdefs.optional.SchemaValidate$SchemaLocation location)
add the schema

param
location the schema location.
throws
BuildException if there is no namespace, or if there already is a declaration of this schema with a different value

        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 voidaddSchemaLocations()
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 voidcreateAnonymousSchema()
create a schema location to hold the anonymous schema

        if (anonymousSchema == null) {
            anonymousSchema = new SchemaLocation();
        }
        anonymousSchema.setNamespace("(no namespace)");
    
protected org.xml.sax.XMLReadercreateDefaultReader()
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.

return
a default XML parser

        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 booleanenableJAXP12SchemaValidation()
Set schema attributes in a JAXP 1.2 engine.

see
JAXP 1.2 Approved CHANGES
return
true on success, false on failure

        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 booleanenableXercesSchemaValidation()
Turn on XSD support in Xerces.

return
true on success, false on failure

        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.StringgetNoNamespaceSchemaURL()
get the URL of the no namespace schema

return
the schema URL

        if (anonymousSchema == null) {
            return null;
        } else {
            return anonymousSchema.getSchemaLocationURL();
        }
    
public voidinit()
Called by the project to let the task initialize properly. The default implementation is a no-op.

throws
BuildException if something goes wrong with the build


                                  
         
        super.init();
        //validating
        setLenient(false);
    
protected voidinitValidator()
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

        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 voidonSuccessfulValidation(int fileProcessed)
handler called on successful file validation.

param
fileProcessed number of files processed.

        log(fileProcessed + MESSAGE_FILES_VALIDATED, Project.MSG_VERBOSE);
    
public voidsetDisableDTD(boolean disableDTD)
flag to disable DTD support.

param
disableDTD a boolean value.

        this.disableDTD = disableDTD;
    
protected voidsetFeatureIfSupported(java.lang.String feature, boolean value)
set a feature if it is supported, log at verbose level if not

param
feature the feature.
param
value a boolean value.

        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 voidsetFullChecking(boolean fullChecking)
enable full schema checking. Slower but better.

param
fullChecking a boolean value.

        this.fullChecking = fullChecking;
    
public voidsetNoNamespaceFile(java.io.File defaultSchemaFile)
identify a file containing the default schema

param
defaultSchemaFile the location of the default schema.

        createAnonymousSchema();
        this.anonymousSchema.setFile(defaultSchemaFile);
    
private voidsetNoNamespaceSchemaProperty(java.lang.String property)
set nonamespace handling up for xerces or other parsers

param
property name of the property to set

        String anonSchema = getNoNamespaceSchemaURL();
        if (anonSchema != null) {
            setProperty(property, anonSchema);
        }
    
public voidsetNoNamespaceURL(java.lang.String defaultSchemaURL)
identify the URL of the default schema

param
defaultSchemaURL the URL of the default schema.

        createAnonymousSchema();
        this.anonymousSchema.setUrl(defaultSchemaURL);