📄 projecthelper.java
字号:
/* * 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;import java.io.BufferedReader;import java.io.File;import java.io.InputStream;import java.io.InputStreamReader;import java.util.Hashtable;import java.util.Locale;import java.util.Vector;import org.xml.sax.AttributeList;import org.apache.tools.ant.helper.ProjectHelper2;import org.apache.tools.ant.util.LoaderUtils;/** * Configures a Project (complete with Targets and Tasks) based on * a XML build file. It'll rely on a plugin to do the actual processing * of the xml file. * * This class also provide static wrappers for common introspection. * * All helper plugins must provide backward compatibility with the * original ant patterns, unless a different behavior is explicitly * specified. For example, if namespace is used on the <project> tag * the helper can expect the entire build file to be namespace-enabled. * Namespaces or helper-specific tags can provide meta-information to * the helper, allowing it to use new ( or different policies ). * * However, if no namespace is used the behavior should be exactly * identical with the default helper. * */public class ProjectHelper { /** The URI for ant name space */ public static final String ANT_CORE_URI = "antlib:org.apache.tools.ant"; /** The URI for antlib current definitions */ public static final String ANT_CURRENT_URI = "ant:current"; /** The URI for defined types/tasks - the format is antlib:<package> */ public static final String ANTLIB_URI = "antlib:"; /** Polymorphic attribute */ public static final String ANT_TYPE = "ant-type"; /** * Name of JVM system property which provides the name of the * ProjectHelper class to use. */ public static final String HELPER_PROPERTY = MagicNames.PROJECT_HELPER_CLASS; /** * The service identifier in jars which provide Project Helper * implementations. */ public static final String SERVICE_ID = MagicNames.PROJECT_HELPER_SERVICE; /** * name of project helper reference that we add to a project */ public static final String PROJECTHELPER_REFERENCE = MagicNames.REFID_PROJECT_HELPER; /** * Configures the project with the contents of the specified XML file. * * @param project The project to configure. Must not be <code>null</code>. * @param buildFile An XML file giving the project's configuration. * Must not be <code>null</code>. * * @exception BuildException if the configuration is invalid or cannot be read */ public static void configureProject(Project project, File buildFile) throws BuildException { ProjectHelper helper = ProjectHelper.getProjectHelper(); project.addReference(PROJECTHELPER_REFERENCE, helper); helper.parse(project, buildFile); } /** Default constructor */ public ProjectHelper() { } // -------------------- Common properties -------------------- // The following properties are required by import ( and other tasks // that read build files using ProjectHelper ). // A project helper may process multiple files. We'll keep track // of them - to avoid loops and to allow caching. The caching will // probably accelerate things like <antCall>. // The key is the absolute file, the value is a processed tree. // Since the tree is composed of UE and RC - it can be reused ! // protected Hashtable processedFiles=new Hashtable(); private Vector importStack = new Vector(); // Temporary - until we figure a better API /** EXPERIMENTAL WILL_CHANGE * */// public Hashtable getProcessedFiles() {// return processedFiles;// } /** EXPERIMENTAL WILL_CHANGE * Import stack. * Used to keep track of imported files. Error reporting should * display the import path. * * @return the stack of import source objects. */ public Vector getImportStack() { return importStack; } // -------------------- Parse method -------------------- /** * Parses the project file, configuring the project as it goes. * * @param project The project for the resulting ProjectHelper to configure. * Must not be <code>null</code>. * @param source The source for XML configuration. A helper must support * at least File, for backward compatibility. Helpers may * support URL, InputStream, etc or specialized types. * * @since Ant1.5 * @exception BuildException if the configuration is invalid or cannot * be read */ public void parse(Project project, Object source) throws BuildException { throw new BuildException("ProjectHelper.parse() must be implemented " + "in a helper plugin " + this.getClass().getName()); } /** * Discovers a project helper instance. Uses the same patterns * as JAXP, commons-logging, etc: a system property, a JDK1.3 * service discovery, default. * * @return a ProjectHelper, either a custom implementation * if one is available and configured, or the default implementation * otherwise. * * @exception BuildException if a specified helper class cannot * be loaded/instantiated. */ public static ProjectHelper getProjectHelper() throws BuildException { // Identify the class loader we will be using. Ant may be // in a webapp or embedded in a different app ProjectHelper helper = null; // First, try the system property String helperClass = System.getProperty(HELPER_PROPERTY); try { if (helperClass != null) { helper = newHelper(helperClass); } } catch (SecurityException e) { System.out.println("Unable to load ProjectHelper class \"" + helperClass + " specified in system property " + HELPER_PROPERTY); } // A JDK1.3 'service' ( like in JAXP ). That will plug a helper // automatically if in CLASSPATH, with the right META-INF/services. if (helper == null) { try { ClassLoader classLoader = LoaderUtils.getContextClassLoader(); InputStream is = null; if (classLoader != null) { is = classLoader.getResourceAsStream(SERVICE_ID); } if (is == null) { is = ClassLoader.getSystemResourceAsStream(SERVICE_ID); } if (is != null) { // This code is needed by EBCDIC and other strange systems. // It's a fix for bugs reported in xerces InputStreamReader isr; try { isr = new InputStreamReader(is, "UTF-8"); } catch (java.io.UnsupportedEncodingException e) { isr = new InputStreamReader(is); } BufferedReader rd = new BufferedReader(isr); String helperClassName = rd.readLine(); rd.close(); if (helperClassName != null && !"".equals(helperClassName)) { helper = newHelper(helperClassName); } } } catch (Exception ex) { System.out.println("Unable to load ProjectHelper from service " + SERVICE_ID); } } return helper == null ? new ProjectHelper2() : helper; } /** * Creates a new helper instance from the name of the class. * It'll first try the thread class loader, then Class.forName() * will load from the same loader that loaded this class. * * @param helperClass The name of the class to create an instance * of. Must not be <code>null</code>. * * @return a new instance of the specified class. * * @exception BuildException if the class cannot be found or * cannot be appropriate instantiated. */ private static ProjectHelper newHelper(String helperClass) throws BuildException { ClassLoader classLoader = LoaderUtils.getContextClassLoader(); try { Class clazz = null; if (classLoader != null) { try { clazz = classLoader.loadClass(helperClass); } catch (ClassNotFoundException ex) { // try next method } } if (clazz == null) { clazz = Class.forName(helperClass); } return ((ProjectHelper) clazz.newInstance()); } catch (Exception e) { throw new BuildException(e); } } /** * JDK1.1 compatible access to the context class loader. Cut & paste from JAXP. * * @deprecated since 1.6.x. * Use LoaderUtils.getContextClassLoader() *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -