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

📄 projecthelperimpl.java

📁 Use the links below to download a source distribution of Ant from one of our mirrors. It is good pra
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* *  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.helper;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.Locale;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.IntrospectionHelper;import org.apache.tools.ant.Location;import org.apache.tools.ant.Project;import org.apache.tools.ant.ProjectHelper;import org.apache.tools.ant.RuntimeConfigurable;import org.apache.tools.ant.Target;import org.apache.tools.ant.Task;import org.apache.tools.ant.TypeAdapter;import org.apache.tools.ant.TaskContainer;import org.apache.tools.ant.UnknownElement;import org.apache.tools.ant.util.FileUtils;import org.apache.tools.ant.util.JAXPUtils;import org.xml.sax.AttributeList;import org.xml.sax.DocumentHandler;import org.xml.sax.HandlerBase;import org.xml.sax.InputSource;import org.xml.sax.Locator;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;import org.xml.sax.helpers.XMLReaderAdapter;/** * Original helper. * */public class ProjectHelperImpl extends ProjectHelper {    /**     * helper for path -> URI and URI -> path conversions.     */    private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();    /**     * SAX 1 style parser used to parse the given file. This may     * in fact be a SAX 2 XMLReader wrapped in an XMLReaderAdapter.     */    private org.xml.sax.Parser parser;    /** The project to configure. */    private Project project;    /** The configuration file to parse. */    private File buildFile;    /**     * Parent directory of the build file. Used for resolving entities     * and setting the project's base directory.     */    private File buildFileParent;    /**     * Locator for the configuration file parser.     * Used for giving locations of errors etc.     */    private Locator locator;    /**     * Target that all other targets will depend upon implicitly.     *     * <p>This holds all tasks and data type definitions that have     * been placed outside of targets.</p>     */    private Target implicitTarget = new Target();    /**     * default constructor     */    public ProjectHelperImpl() {        implicitTarget.setName("");    }    /**     * Parses the project file, configuring the project as it goes.     *     * @param project project instance to be configured.     * @param source the source from which the project is read.     * @exception BuildException if the configuration is invalid or cannot     *                           be read.     */    public void parse(Project project, Object source) throws BuildException {        if (!(source instanceof File)) {            throw new BuildException("Only File source supported by "                + "default plugin");        }        File bFile = (File) source;        FileInputStream inputStream = null;        InputSource inputSource = null;        this.project = project;        this.buildFile = new File(bFile.getAbsolutePath());        buildFileParent = new File(this.buildFile.getParent());        try {            try {                parser = JAXPUtils.getParser();            } catch (BuildException e) {                parser = new XMLReaderAdapter(JAXPUtils.getXMLReader());            }            String uri = FILE_UTILS.toURI(bFile.getAbsolutePath());            inputStream = new FileInputStream(bFile);            inputSource = new InputSource(inputStream);            inputSource.setSystemId(uri);            project.log("parsing buildfile " + bFile + " with URI = "                + uri, Project.MSG_VERBOSE);            HandlerBase hb = new RootHandler(this);            parser.setDocumentHandler(hb);            parser.setEntityResolver(hb);            parser.setErrorHandler(hb);            parser.setDTDHandler(hb);            parser.parse(inputSource);        } catch (SAXParseException exc) {            Location location =                new Location(exc.getSystemId(), exc.getLineNumber(),                    exc.getColumnNumber());            Throwable t = exc.getException();            if (t instanceof BuildException) {                BuildException be = (BuildException) t;                if (be.getLocation() == Location.UNKNOWN_LOCATION) {                    be.setLocation(location);                }                throw be;            }            throw new BuildException(exc.getMessage(), t, location);        } catch (SAXException exc) {            Throwable t = exc.getException();            if (t instanceof BuildException) {                throw (BuildException) t;            }            throw new BuildException(exc.getMessage(), t);        } catch (FileNotFoundException exc) {            throw new BuildException(exc);        } catch (UnsupportedEncodingException exc) {            throw new BuildException("Encoding of project file is invalid.",                                     exc);        } catch (IOException exc) {            throw new BuildException("Error reading project file: "                                     + exc.getMessage(), exc);        } finally {            FileUtils.close(inputStream);        }    }    /**     * The common superclass for all SAX event handlers used to parse     * the configuration file. Each method just throws an exception,     * so subclasses should override what they can handle.     *     * Each type of XML element (task, target, etc.) in Ant has     * a specific subclass.     *     * In the constructor, this class takes over the handling of SAX     * events from the parent handler and returns     * control back to the parent in the endElement method.     */    static class AbstractHandler extends HandlerBase {        // CheckStyle:VisibilityModifier OFF - bc        /**         * Previous handler for the document.         * When the next element is finished, control returns         * to this handler.         */        protected DocumentHandler parentHandler;        /** Helper impl. With non-static internal classes, the compiler will generate            this automatically - but this will fail with some compilers ( reporting            "Expecting to find object/array on stack" ). If we pass it            explicitly it'll work with more compilers.        */        ProjectHelperImpl helperImpl;        // CheckStyle:VisibilityModifier ON        /**         * Creates a handler and sets the parser to use it         * for the current element.         *         * @param helperImpl the ProjectHelperImpl instance associated         *                   with this handler.         *         * @param parentHandler The handler which should be restored to the         *                      parser at the end of the element.         *                      Must not be <code>null</code>.         */        public AbstractHandler(ProjectHelperImpl helperImpl, DocumentHandler parentHandler) {            this.parentHandler = parentHandler;            this.helperImpl = helperImpl;            // Start handling SAX events            helperImpl.parser.setDocumentHandler(this);        }        /**         * Handles the start of an element. This base implementation just         * throws an exception.         *         * @param tag The name of the element being started.         *            Will not be <code>null</code>.         * @param attrs Attributes of the element being started.         *              Will not be <code>null</code>.         *         * @exception SAXParseException if this method is not overridden, or in         *                              case of error in an overridden version         */        public void startElement(String tag, AttributeList attrs) throws SAXParseException {            throw new SAXParseException("Unexpected element \"" + tag + "\"", helperImpl.locator);        }        /**         * Handles text within an element. This base implementation just         * throws an exception.         *         * @param buf A character array of the text within the element.         *            Will not be <code>null</code>.         * @param start The start element in the array.         * @param count The number of characters to read from the array.         *         * @exception SAXParseException if this method is not overridden, or in         *                              case of error in an overridden version         */        public void characters(char[] buf, int start, int count) throws SAXParseException {            String s = new String(buf, start, count).trim();            if (s.length() > 0) {                throw new SAXParseException("Unexpected text \"" + s + "\"", helperImpl.locator);            }        }        /**         * Handles the end of an element. Any required clean-up is performed         * by the finished() method and then the original handler is restored to         * the parser.         *         * @param name The name of the element which is ending.         *             Will not be <code>null</code>.         *         * @exception SAXException in case of error (not thrown in         *                         this implementation)         */        public void endElement(String name) throws SAXException {            // Let parent resume handling SAX events            helperImpl.parser.setDocumentHandler(parentHandler);        }    }    /**     * Handler for the root element. Its only child must be the "project" element.     */    static class RootHandler extends HandlerBase {        // CheckStyle:VisibilityModifier OFF - bc        ProjectHelperImpl helperImpl;        // CheckStyle:VisibilityModifier ON        public RootHandler(ProjectHelperImpl helperImpl) {            this.helperImpl = helperImpl;        }        /**         * Resolves file: URIs relative to the build file.         *         * @param publicId The public identifier, or <code>null</code>         *                 if none is available. Ignored in this         *                 implementation.         * @param systemId The system identifier provided in the XML         *                 document. Will not be <code>null</code>.         */        public InputSource resolveEntity(String publicId,                                         String systemId) {            helperImpl.project.log("resolving systemId: " + systemId, Project.MSG_VERBOSE);            if (systemId.startsWith("file:")) {                String path = FILE_UTILS.fromURI(systemId);                File file = new File(path);                if (!file.isAbsolute()) {                    file = FILE_UTILS.resolveFile(helperImpl.buildFileParent, path);                    helperImpl.project.log(                            "Warning: '" + systemId + "' in " + helperImpl.buildFile                            + " should be expressed simply as '" + path.replace('\\', '/')                            + "' for compliance with other XML tools",                            Project.MSG_WARN);                }                try {                    InputSource inputSource = new InputSource(new FileInputStream(file));                    inputSource.setSystemId(FILE_UTILS.toURI(file.getAbsolutePath()));                    return inputSource;                } catch (FileNotFoundException fne) {                    helperImpl.project.log(file.getAbsolutePath() + " could not be found",                                Project.MSG_WARN);                }            }            // use default if not file or file not found            return null;        }        /**         * Handles the start of a project element. A project handler is created         * and initialised with the element name and attributes.         *         * @param tag The name of the element being started.         *            Will not be <code>null</code>.         * @param attrs Attributes of the element being started.         *              Will not be <code>null</code>.         *         * @exception SAXParseException if the tag given is not         *                              <code>"project"</code>         */        public void startElement(String tag, AttributeList attrs) throws SAXParseException {            if (tag.equals("project")) {                new ProjectHandler(helperImpl, this).init(tag, attrs);            } else {                throw new SAXParseException("Config file is not of expected "                    + "XML type", helperImpl.locator);            }        }        /**         * Sets the locator in the project helper for future reference.         *

⌨️ 快捷键说明

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