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

📄 projecthelper2.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 org.apache.tools.ant.BuildException;import org.apache.tools.ant.Location;import org.apache.tools.ant.MagicNames;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.UnknownElement;import org.apache.tools.ant.util.FileUtils;import org.apache.tools.ant.util.StringUtils;import org.apache.tools.ant.util.JAXPUtils;import org.xml.sax.Attributes;import org.xml.sax.InputSource;import org.xml.sax.Locator;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;import org.xml.sax.XMLReader;import org.xml.sax.helpers.DefaultHandler;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.io.UnsupportedEncodingException;import java.net.URL;import java.util.HashMap;import java.util.Hashtable;import java.util.Map;import java.util.Stack;/** * Sax2 based project reader * */public class ProjectHelper2 extends ProjectHelper {    /** Reference holding the (ordered) target Vector */    public static final String REFID_TARGETS = "ant.targets";    /* Stateless */    // singletons - since all state is in the context    private static AntHandler elementHandler = new ElementHandler();    private static AntHandler targetHandler = new TargetHandler();    private static AntHandler mainHandler = new MainHandler();    private static AntHandler projectHandler = new ProjectHandler();    /** Specific to ProjectHelper2 so not a true Ant "magic name:" */    private static final String REFID_CONTEXT = "ant.parsing.context";    /**     * helper for path -> URI and URI -> path conversions.     */    private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();    /**     * Parse an unknown element from a url     *     * @param project the current project     * @param source  the url containing the task     * @return a configured task     * @exception BuildException if an error occurs     */    public UnknownElement parseUnknownElement(Project project, URL source) throws BuildException {        Target dummyTarget = new Target();        dummyTarget.setProject(project);        AntXMLContext context = new AntXMLContext(project);        context.addTarget(dummyTarget);        context.setImplicitTarget(dummyTarget);        parse(context.getProject(), source, new RootHandler(context, elementHandler));        Task[] tasks = dummyTarget.getTasks();        if (tasks.length != 1) {            throw new BuildException("No tasks defined");        }        return (UnknownElement) tasks[0];    }    /**     * Parse a source xml input.     *     * @param project the current project     * @param source  the xml source     * @exception BuildException if an error occurs     */    public void parse(Project project, Object source) throws BuildException {        getImportStack().addElement(source);        AntXMLContext context = null;        context = (AntXMLContext) project.getReference(REFID_CONTEXT);        if (context == null) {            context = new AntXMLContext(project);            project.addReference(REFID_CONTEXT, context);            project.addReference(REFID_TARGETS, context.getTargets());        }        if (getImportStack().size() > 1) {            // we are in an imported file.            context.setIgnoreProjectTag(true);            Target currentTarget = context.getCurrentTarget();            Target currentImplicit = context.getImplicitTarget();            Map    currentTargets = context.getCurrentTargets();            try {                Target newCurrent = new Target();                newCurrent.setProject(project);                newCurrent.setName("");                context.setCurrentTarget(newCurrent);                context.setCurrentTargets(new HashMap());                context.setImplicitTarget(newCurrent);                parse(project, source, new RootHandler(context, mainHandler));                newCurrent.execute();            } finally {                context.setCurrentTarget(currentTarget);                context.setImplicitTarget(currentImplicit);                context.setCurrentTargets(currentTargets);            }        } else {            // top level file            context.setCurrentTargets(new HashMap());            parse(project, source, new RootHandler(context, mainHandler));            // Execute the top-level target            context.getImplicitTarget().execute();        }    }    /**     * Parses the project file, configuring the project as it goes.     *     * @param project the current project     * @param source  the xml source     * @param handler the root handler to use (contains the current context)     * @exception BuildException if the configuration is invalid or cannot     *                           be read     */    public void parse(Project project, Object source, RootHandler handler) throws BuildException {        AntXMLContext context = handler.context;        File buildFile = null;        URL  url = null;        String buildFileName = null;        if (source instanceof File) {            buildFile = (File) source;            buildFile = FILE_UTILS.normalize(buildFile.getAbsolutePath());            context.setBuildFile(buildFile);            buildFileName = buildFile.toString();//         } else if (source instanceof InputStream ) {        } else if (source instanceof URL) {            url = (URL) source;            buildFileName = url.toString();//         } else if (source instanceof InputSource ) {        } else {            throw new BuildException("Source " + source.getClass().getName()                                     + " not supported by this plugin");        }        InputStream inputStream = null;        InputSource inputSource = null;        try {            /**             * SAX 2 style parser used to parse the given file.             */            XMLReader parser = JAXPUtils.getNamespaceXMLReader();            String uri = null;            if (buildFile != null) {                uri = FILE_UTILS.toURI(buildFile.getAbsolutePath());                inputStream = new FileInputStream(buildFile);            } else {                inputStream = url.openStream();                uri = url.toString(); // ?? OK ??            }            inputSource = new InputSource(inputStream);            if (uri != null) {                inputSource.setSystemId(uri);            }            project.log("parsing buildfile " + buildFileName                        + " with URI = " + uri, Project.MSG_VERBOSE);            DefaultHandler hb = handler;            parser.setContentHandler(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 == null ? exc : t, location);        } catch (SAXException exc) {            Throwable t = exc.getException();            if (t instanceof BuildException) {                throw (BuildException) t;            }            throw new BuildException(exc.getMessage(), t == null ? exc : t);        } catch (FileNotFoundException exc) {            throw new BuildException(exc);        } catch (UnsupportedEncodingException exc) {              throw new BuildException("Encoding of project file "                                       + buildFileName + " is invalid.", exc);        } catch (IOException exc) {            throw new BuildException("Error reading project file "                                     + buildFileName + ": " + exc.getMessage(), exc);        } finally {            FileUtils.close(inputStream);        }    }    /**     * Returns main handler     * @return main handler     */    protected static AntHandler getMainHandler() {        return mainHandler;    }    /**     * Sets main handler     * @param handler  new main handler     */    protected static void setMainHandler(AntHandler handler) {        mainHandler = handler;    }    /**     * Returns project handler     * @return project handler     */    protected static AntHandler getProjectHandler() {        return projectHandler;    }    /**     * Sets project handler     * @param handler  new project handler     */    protected static void setProjectHandler(AntHandler handler) {        projectHandler = handler;    }    /**     * Returns target handler     * @return target handler     */    protected static AntHandler getTargetHandler() {        return targetHandler;    }    /**     * Sets target handler     * @param handler  new target handler     */    protected static void setTargetHandler(AntHandler handler) {        targetHandler = handler;    }    /**     * Returns element handler     * @return element handler     */    protected static AntHandler getElementHandler() {        return elementHandler;    }    /**     * Sets element handler     * @param handler  new element handler     */    protected static void setElementHandler(AntHandler handler) {        elementHandler = handler;    }    /**     * The common superclass for all SAX event handlers used to parse     * the configuration file.     *     * The context will hold all state information. At each time     * there is one active handler for the current element. It can     * use onStartChild() to set an alternate handler for the child.     */    public static class AntHandler  {        /**         * Handles the start of an element. This base implementation does         * nothing.         *         * @param uri the namespace URI for the tag         * @param tag The name of the element being started.         *            Will not be <code>null</code>.         * @param qname The qualified name of the element.         * @param attrs Attributes of the element being started.         *              Will not be <code>null</code>.         * @param context The context that this element is in.         *         * @exception SAXParseException if this method is not overridden, or in         *                              case of error in an overridden version         */        public void onStartElement(String uri, String tag, String qname, Attributes attrs,                                   AntXMLContext context) throws SAXParseException {        }        /**         * Handles the start of an element. This base implementation just         * throws an exception - you must override this method if you expect         * child elements.         *         * @param uri The namespace uri for this element.         * @param tag The name of the element being started.         *            Will not be <code>null</code>.         * @param qname The qualified name for this element.         * @param attrs Attributes of the element being started.         *              Will not be <code>null</code>.         * @param context The current context.         * @return a handler (in the derived classes)         *         * @exception SAXParseException if this method is not overridden, or in         *                              case of error in an overridden version         */        public AntHandler onStartChild(String uri, String tag, String qname, Attributes attrs,                                       AntXMLContext context) throws SAXParseException {            throw new SAXParseException("Unexpected element \"" + qname

⌨️ 快捷键说明

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