📄 definer.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.taskdefs;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.net.URL;import java.util.Map;import java.util.HashMap;import java.util.Enumeration;import java.util.Locale;import java.util.NoSuchElementException;import java.util.Properties;import org.apache.tools.ant.AntTypeDefinition;import org.apache.tools.ant.ComponentHelper;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.Project;import org.apache.tools.ant.ProjectHelper;import org.apache.tools.ant.MagicNames;import org.apache.tools.ant.util.FileUtils;import org.apache.tools.ant.types.EnumeratedAttribute;/** * Base class for Taskdef and Typedef - handles all * the attributes for Typedef. The uri and class * handling is handled by DefBase * * @since Ant 1.4 */public abstract class Definer extends DefBase { /** * the extension of an antlib file for autoloading. * {@value[ */ private static final String ANTLIB_XML = "/antlib.xml"; private static class ResourceStack extends ThreadLocal { public Object initialValue() { return new HashMap(); } Map getStack() { return (Map) get(); } } private static ResourceStack resourceStack = new ResourceStack(); private String name; private String classname; private File file; private String resource; private int format = Format.PROPERTIES; private boolean definerSet = false; private int onError = OnError.FAIL; private String adapter; private String adaptTo; private Class adapterClass; private Class adaptToClass; /** * Enumerated type for onError attribute * * @see EnumeratedAttribute */ public static class OnError extends EnumeratedAttribute { /** Enumerated values */ public static final int FAIL = 0, REPORT = 1, IGNORE = 2, FAIL_ALL = 3; /** * text value of onerror option {@value} */ public static final String POLICY_FAIL = "fail"; /** * text value of onerror option {@value} */ public static final String POLICY_REPORT = "report"; /** * text value of onerror option {@value} */ public static final String POLICY_IGNORE = "ignore"; /** * text value of onerror option {@value} */ public static final String POLICY_FAILALL = "failall"; /** * Constructor */ public OnError() { super(); } /** * Constructor using a string. * @param value the value of the attribute */ public OnError(String value) { setValue(value); } /** * get the values * @return an array of the allowed values for this attribute. */ public String[] getValues() { return new String[] {POLICY_FAIL, POLICY_REPORT, POLICY_IGNORE, POLICY_FAILALL}; } } /** * Enumerated type for format attribute * * @see EnumeratedAttribute */ public static class Format extends EnumeratedAttribute { /** Enumerated values */ public static final int PROPERTIES = 0, XML = 1; /** * get the values * @return an array of the allowed values for this attribute. */ public String[] getValues() { return new String[] {"properties", "xml"}; } } /** * What to do if there is an error in loading the class. * <dl> * <li>error - throw build exception</li> * <li>report - output at warning level</li> * <li>ignore - output at debug level</li> * </dl> * * @param onError an <code>OnError</code> value */ public void setOnError(OnError onError) { this.onError = onError.getIndex(); } /** * Sets the format of the file or resource * @param format the enumerated value - xml or properties */ public void setFormat(Format format) { this.format = format.getIndex(); } /** * @return the name for this definition */ public String getName() { return name; } /** * @return the file containing definitions */ public File getFile() { return file; } /** * @return the resource containing definitions */ public String getResource() { return resource; } /** * Run the definition. * * @exception BuildException if an error occurs */ public void execute() throws BuildException { ClassLoader al = createLoader(); if (!definerSet) { //we arent fully defined yet. this is an error unless //we are in an antlib, in which case the resource name is determined //automatically. //NB: URIs in the ant core package will be "" at this point. if (getURI() == null) { throw new BuildException( "name, file or resource attribute of " + getTaskName() + " is undefined", getLocation()); } if (getURI().startsWith(MagicNames.ANTLIB_PREFIX)) { //convert the URI to a resource String uri1 = getURI(); setResource(makeResourceFromURI(uri1)); } else { throw new BuildException( "Only antlib URIs can be located from the URI alone," + "not the URI " + getURI()); } } if (name != null) { if (classname == null) { throw new BuildException( "classname attribute of " + getTaskName() + " element " + "is undefined", getLocation()); } addDefinition(al, name, classname); } else { if (classname != null) { String msg = "You must not specify classname " + "together with file or resource."; throw new BuildException(msg, getLocation()); } Enumeration/*<URL>*/ urls = null; if (file != null) { final URL url = fileToURL(); if (url == null) { return; } urls = new Enumeration() { private boolean more = true; public boolean hasMoreElements() { return more; } public Object nextElement() throws NoSuchElementException { if (more) { more = false; return url; } else { throw new NoSuchElementException(); } } }; } else { urls = resourceToURLs(al); } while (urls.hasMoreElements()) { URL url = (URL) urls.nextElement(); int fmt = this.format; if (url.toString().toLowerCase(Locale.US).endsWith(".xml")) { fmt = Format.XML; } if (fmt == Format.PROPERTIES) { loadProperties(al, url); break; } else { if (resourceStack.getStack().get(url) != null) { log("Warning: Recursive loading of " + url + " ignored" + " at " + getLocation() + " originally loaded at " + resourceStack.getStack().get(url), Project.MSG_WARN); } else { try { resourceStack.getStack().put(url, getLocation()); loadAntlib(al, url); } finally { resourceStack.getStack().remove(url); } } } } } } /** * This is where the logic to map from a URI to an antlib resource * is kept. * @param uri the xml namespace uri that to convert. * @return the name of a resource. It may not exist */ public static String makeResourceFromURI(String uri) { String path = uri.substring(MagicNames.ANTLIB_PREFIX.length()); String resource; if (path.startsWith("//")) { //handle new style full paths to an antlib, in which //all but the forward slashes are allowed. resource = path.substring("//".length()); if (!resource.endsWith(".xml")) { //if we haven't already named an XML file, it gets antlib.xml resource = resource + ANTLIB_XML; } } else { //convert from a package to a path resource = path.replace('.', '/') + ANTLIB_XML; } return resource; } /** * Convert a file to a file: URL. * * @return the URL, or null if it isn't valid and the active error policy * is not to raise a fault * @throws BuildException if the file is missing/not a file and the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -