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

📄 xmlvalidatetask.java

📁 Use the links below to download a source distribution of Ant from one of our mirrors. It is good pra
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    /**     * 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.     * @return the new XMLReader.     */    protected XMLReader createXmlReader() {        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;    }    /**     *     * @return     */    private Object createDefaultReaderOrParser() {        Object reader;        try {            reader = createDefaultReader();        } catch (BuildException exc) {            reader = JAXPUtils.getParser();        }        return reader;    }    /**     * 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 a new reader.     * @throws BuildException if something went wrong     */    protected XMLReader createDefaultReader() {        return JAXPUtils.getXMLReader();    }    /**     * Set a feature on the parser.     * @param feature the name of the feature to set     * @param value the value of the feature     * @throws BuildException if the feature was not supported     */    protected void setFeature(String feature, boolean value)        throws BuildException {        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());        }    }    /**     * Sets a property.     *     * @param name a property name     * @param value a property value.     * @throws BuildException if an error occurs.     * @throws BuildException if the property was not supported     */    protected void setProperty(String name, String value) throws BuildException {        // 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());        }    }    /**     * parse the file     * @param afile the file to validate.     * @return true if the file validates.     */    protected boolean doValidate(File afile) {        //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;    }    /**     * ValidatorErrorHandler role :     * <ul>     * <li> log SAX parse exceptions,     * <li> remember if an error occurred     * </ul>     */    protected class ValidatorErrorHandler implements ErrorHandler {        // CheckStyle:VisibilityModifier OFF - bc        protected File currentFile = null;        protected String lastErrorMessage = null;        protected boolean failed = false;        // CheckStyle:VisibilityModifier ON        /**         * initialises the class         * @param file file used         */        public void init(File file) {            currentFile = file;            failed = false;        }        /**         * did an error happen during last parsing ?         * @return did an error happen during last parsing ?         */        public boolean getFailure() {            return failed;        }        /**         * record a fatal error         * @param exception the fatal error         */        public void fatalError(SAXParseException exception) {            failed = true;            doLog(exception, Project.MSG_ERR);        }        /**         * receive notification of a recoverable error         * @param exception the error         */        public void error(SAXParseException exception) {            failed = true;            doLog(exception, Project.MSG_ERR);        }        /**         * receive notification of a warning         * @param exception the warning         */        public void warning(SAXParseException exception) {            // depending on implementation, XMLReader can yield hips of warning,            // only output then if user explicitly asked for it            if (warn) {                doLog(exception, Project.MSG_WARN);            }        }        private void doLog(SAXParseException e, int logLevel) {            log(getMessage(e), logLevel);        }        private String getMessage(SAXParseException e) {            String sysID = e.getSystemId();            if (sysID != null) {                String name = sysID;                if (sysID.startsWith("file:")) {                    try {                        name = FILE_UTILS.fromURI(sysID);                    } catch (Exception ex) {                        // if this is not a valid file: just use the uri                    }                }                int line = e.getLineNumber();                int col = e.getColumnNumber();                return  name                    + (line == -1                       ? ""                       : (":" + line + (col == -1 ? "" : (":" + col))))                    + ": "                    + e.getMessage();            }            return e.getMessage();        }    }    /**     * The class to create to set a feature of the parser.     * @since ant1.6     */    public static class Attribute {        /** The name of the attribute to set.         *         * Valid attributes <a href=         * "http://www.saxproject.org/apidoc/org/xml/sax/package-summary.html#package_description"         * >include.</a>         */        private String attributeName = null;        /**         * The value of the feature.         **/        private boolean attributeValue;        /**         * Set the feature name.         * @param name the name to set         */        public void setName(String name) {            attributeName = name;        }        /**         * Set the feature value to true or false.         * @param value feature value         */        public void setValue(boolean value) {            attributeValue = value;        }        /**         * Gets the attribute name.         * @return the feature name         */        public String getName() {            return attributeName;        }        /**         * Gets the attribute value.         * @return the feature value         */        public boolean getValue() {            return attributeValue;        }    }    /**     * A Parser property.     * See <a href="http://xml.apache.org/xerces-j/properties.html">     * XML parser properties</a> for usable properties     * @since ant 1.6.2     */    public static final class Property {        private String name;        private String value;        /**         * accessor to the name of the property         * @return name of the property         */        public String getName() {            return name;        }        /**         * setter for the name of the property         * @param name name of the property         */        public void setName(String name) {            this.name = name;        }        /**         * getter for the value of the property         * @return value of the property         */        public String getValue() {            return value;        }        /**         * sets the value of the property         * @param value value of the property         */        public void setValue(String value) {            this.value = value;        }    } // Property}

⌨️ 快捷键说明

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