⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 schemavalidate.java

📁 Use the links below to download a source distribution of Ant from one of our mirrors. It is good pra
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* *  Licensed to the Apache Software Foundation (ASF) under one or more *  contributor license agreements.  See the NOTICE file distributed with *  this work for additional information regarding copyright ownership. *  The ASF licenses this file to You under the Apache License, Version 2.0 *  (the "License"); you may not use this file except in compliance with *  the License.  You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * *  Unless required by applicable law or agreed to in writing, software *  distributed under the License is distributed on an "AS IS" BASIS, *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *  See the License for the specific language governing permissions and *  limitations under the License. * */package org.apache.tools.ant.taskdefs.optional;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.Project;import org.apache.tools.ant.util.FileUtils;import org.apache.tools.ant.util.XmlConstants;import org.xml.sax.XMLReader;import org.xml.sax.SAXNotRecognizedException;import org.xml.sax.SAXNotSupportedException;import org.xml.sax.SAXException;import javax.xml.parsers.SAXParserFactory;import javax.xml.parsers.SAXParser;import javax.xml.parsers.ParserConfigurationException;import java.util.Iterator;import java.util.HashMap;import java.io.File;import java.net.MalformedURLException;/** * 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 */public class SchemaValidate extends XMLValidateTask {    /** map of all declared schemas; we catch and complain about redefinitions */    private HashMap schemaLocations = new HashMap();    /** full checking of a schema */    private boolean fullChecking = true;    /**     * flag to disable DTD support. Best left enabled.     */    private boolean disableDTD = false;    /**     * default URL for nonamespace schemas     */    private SchemaLocation anonymousSchema;    // Error strings    /** SAX1 not supported */    public static final String ERROR_SAX_1 = "SAX1 parsers are not supported";    /** schema features not supported */    public static final String ERROR_NO_XSD_SUPPORT        = "Parser does not support Xerces or JAXP schema features";    /** too many default schemas */    public static final String ERROR_TOO_MANY_DEFAULT_SCHEMAS        = "Only one of defaultSchemaFile and defaultSchemaURL allowed";    /** unable to create parser */    public static final String ERROR_PARSER_CREATION_FAILURE        = "Could not create parser";    /** adding schema */    public static final String MESSAGE_ADDING_SCHEMA = "Adding schema ";    /** Duplicate declaration of schema  */    public static final String ERROR_DUPLICATE_SCHEMA        = "Duplicate declaration of schema ";    /**     * 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     */    public void init() throws BuildException {        super.init();        //validating        setLenient(false);    }    /**     * Turn on XSD support in Xerces.     * @return true on success, false on failure     */    public boolean enableXercesSchemaValidation() {        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;    }    /**     * set nonamespace handling up for xerces or other parsers     * @param property name of the property to set     */    private void setNoNamespaceSchemaProperty(String property) {        String anonSchema = getNoNamespaceSchemaURL();        if (anonSchema != null) {            setProperty(property, anonSchema);        }    }    /**     * Set schema attributes in a JAXP 1.2 engine.     * @see <A href="http://java.sun.com/xml/jaxp/change-requests-11.html">     * JAXP 1.2 Approved CHANGES</A>     * @return true on success, false on failure     */    public boolean enableJAXP12SchemaValidation() {        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;    }    /**     * 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     */    public void addConfiguredSchema(SchemaLocation location) {        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);    }    /**     * enable full schema checking. Slower but better.     * @param fullChecking a <code>boolean</code> value.     */    public void setFullChecking(boolean fullChecking) {        this.fullChecking = fullChecking;    }    /**     * create a schema location to hold the anonymous     * schema     */    protected void createAnonymousSchema() {        if (anonymousSchema == null) {            anonymousSchema = new SchemaLocation();        }        anonymousSchema.setNamespace("(no namespace)");    }    /**     * identify the URL of the default schema     * @param defaultSchemaURL the URL of the default schema.     */    public void setNoNamespaceURL(String defaultSchemaURL) {        createAnonymousSchema();        this.anonymousSchema.setUrl(defaultSchemaURL);    }    /**     * identify a file containing the default schema     * @param defaultSchemaFile the location of the default schema.     */    public void setNoNamespaceFile(File defaultSchemaFile) {        createAnonymousSchema();        this.anonymousSchema.setFile(defaultSchemaFile);    }    /**     * flag to disable DTD support.     * @param disableDTD a <code>boolean</code> value.     */    public void setDisableDTD(boolean disableDTD) {        this.disableDTD = disableDTD;    }    /**     * 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     */    protected void initValidator() {        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();    }    /**     * 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     */    protected XMLReader createDefaultReader() {        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;    }    /**     * build a string list of all schema locations, then set the relevant     * property.     */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -