📄 definer.java
字号:
* policy requires failure at this point. */ private URL fileToURL() { String message = null; if (!(file.exists())) { message = "File " + file + " does not exist"; } if (message == null && !(file.isFile())) { message = "File " + file + " is not a file"; } try { if (message == null) { return file.toURL(); } } catch (Exception ex) { message = "File " + file + " cannot use as URL: " + ex.toString(); } // Here if there is an error switch (onError) { case OnError.FAIL_ALL: throw new BuildException(message); case OnError.FAIL: // Fall Through case OnError.REPORT: log(message, Project.MSG_WARN); break; case OnError.IGNORE: // log at a lower level log(message, Project.MSG_VERBOSE); break; default: // Ignore the problem break; } return null; } private Enumeration/*<URL>*/ resourceToURLs(ClassLoader classLoader) { Enumeration ret; try { ret = classLoader.getResources(resource); } catch (IOException e) { throw new BuildException( "Could not fetch resources named " + resource, e, getLocation()); } if (!ret.hasMoreElements()) { String message = "Could not load definitions from resource " + resource + ". It could not be found."; switch (onError) { case OnError.FAIL_ALL: throw new BuildException(message); case OnError.FAIL: case OnError.REPORT: log(message, Project.MSG_WARN); break; case OnError.IGNORE: log(message, Project.MSG_VERBOSE); break; default: // Ignore the problem break; } } return ret; } /** * Load type definitions as properties from a URL. * * @param al the classloader to use * @param url the url to get the definitions from */ protected void loadProperties(ClassLoader al, URL url) { InputStream is = null; try { is = url.openStream(); if (is == null) { log("Could not load definitions from " + url, Project.MSG_WARN); return; } Properties props = new Properties(); props.load(is); Enumeration keys = props.keys(); while (keys.hasMoreElements()) { name = ((String) keys.nextElement()); classname = props.getProperty(name); addDefinition(al, name, classname); } } catch (IOException ex) { throw new BuildException(ex, getLocation()); } finally { FileUtils.close(is); } } /** * Load an antlib from a URL. * * @param classLoader the classloader to use. * @param url the url to load the definitions from. */ private void loadAntlib(ClassLoader classLoader, URL url) { try { Antlib antlib = Antlib.createAntlib(getProject(), url, getURI()); antlib.setClassLoader(classLoader); antlib.setURI(getURI()); antlib.execute(); } catch (BuildException ex) { throw ProjectHelper.addLocationToBuildException( ex, getLocation()); } } /** * Name of the property file to load * ant name/classname pairs from. * @param file the file */ public void setFile(File file) { if (definerSet) { tooManyDefinitions(); } definerSet = true; this.file = file; } /** * Name of the property resource to load * ant name/classname pairs from. * @param res the resource to use */ public void setResource(String res) { if (definerSet) { tooManyDefinitions(); } definerSet = true; this.resource = res; } /** * Antlib attribute, sets resource and uri. * uri is set the antlib value and, resource is set * to the antlib.xml resource in the classpath. * For example antlib="antlib:org.acme.bland.cola" * corresponds to uri="antlib:org.acme.bland.cola" * resource="org/acme/bland/cola/antlib.xml". * ASF Bugzilla Bug 31999 * @param antlib the value to set. */ public void setAntlib(String antlib) { if (definerSet) { tooManyDefinitions(); } if (!antlib.startsWith("antlib:")) { throw new BuildException( "Invalid antlib attribute - it must start with antlib:"); } setURI(antlib); this.resource = antlib.substring("antlib:".length()).replace('.', '/') + "/antlib.xml"; definerSet = true; } /** * Name of the definition * @param name the name of the definition */ public void setName(String name) { if (definerSet) { tooManyDefinitions(); } definerSet = true; this.name = name; } /** * Returns the classname of the object we are defining. * May be <code>null</code>. * @return the class name */ public String getClassname() { return classname; } /** * The full class name of the object being defined. * Required, unless file or resource have * been specified. * @param classname the name of the class */ public void setClassname(String classname) { this.classname = classname; } /** * Set the class name of the adapter class. * An adapter class is used to proxy the * definition class. It is used if the * definition class is not assignable to * the adaptto class, or if the adaptto * class is not present. * * @param adapter the name of the adapter class */ public void setAdapter(String adapter) { this.adapter = adapter; } /** * Set the adapter class. * * @param adapterClass the class to use to adapt the definition class */ protected void setAdapterClass(Class adapterClass) { this.adapterClass = adapterClass; } /** * Set the classname of the class that the definition * must be compatible with, either directly or * by use of the adapter class. * * @param adaptTo the name of the adaptto class */ public void setAdaptTo(String adaptTo) { this.adaptTo = adaptTo; } /** * Set the class for adaptToClass, to be * used by derived classes, used instead of * the adaptTo attribute. * * @param adaptToClass the class for adapto. */ protected void setAdaptToClass(Class adaptToClass) { this.adaptToClass = adaptToClass; } /** * Add a definition using the attributes of Definer * * @param al the ClassLoader to use * @param name the name of the definition * @param classname the classname of the definition * @exception BuildException if an error occurs */ protected void addDefinition(ClassLoader al, String name, String classname) throws BuildException { Class cl = null; try { try { name = ProjectHelper.genComponentName(getURI(), name); if (onError != OnError.IGNORE) { cl = Class.forName(classname, true, al); } if (adapter != null) { adapterClass = Class.forName(adapter, true, al); } if (adaptTo != null) { adaptToClass = Class.forName(adaptTo, true, al); } AntTypeDefinition def = new AntTypeDefinition(); def.setName(name); def.setClassName(classname); def.setClass(cl); def.setAdapterClass(adapterClass); def.setAdaptToClass(adaptToClass); def.setClassLoader(al); if (cl != null) { def.checkClass(getProject()); } ComponentHelper.getComponentHelper(getProject()) .addDataTypeDefinition(def); } catch (ClassNotFoundException cnfe) { String msg = getTaskName() + " class " + classname + " cannot be found"; throw new BuildException(msg, cnfe, getLocation()); } catch (NoClassDefFoundError ncdfe) { String msg = getTaskName() + " A class needed by class " + classname + " cannot be found: " + ncdfe.getMessage(); throw new BuildException(msg, ncdfe, getLocation()); } } catch (BuildException ex) { switch (onError) { case OnError.FAIL_ALL: case OnError.FAIL: throw ex; case OnError.REPORT: log(ex.getLocation() + "Warning: " + ex.getMessage(), Project.MSG_WARN); break; default: log(ex.getLocation() + ex.getMessage(), Project.MSG_DEBUG); } } } /** * handle too many definitions by raising an exception. * @throws BuildException always. */ private void tooManyDefinitions() { throw new BuildException( "Only one of the attributes name, file and resource" + " can be set", getLocation()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -