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

📄 property.java

📁 Use the links below to download a source distribution of Ant from one of our mirrors. It is good pra
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    public void setClasspathRef(Reference r) {        createClasspath().setRefid(r);    }    /**     * Get the classpath used when looking up a resource.     * @return the classpath     * @since Ant 1.5     */    public Path getClasspath() {        return classpath;    }    /**     * @param userProperty ignored     * @deprecated since 1.5.x.     *             This was never a supported feature and has been     *             deprecated without replacement.     * @ant.attribute ignore="true"     */    public void setUserProperty(boolean userProperty) {        log("DEPRECATED: Ignoring request to set user property in Property"            + " task.", Project.MSG_WARN);    }    /**     * get the value of this property     * @return the current value or the empty string     */    public String toString() {        return value == null ? "" : value;    }    /**     * set the property in the project to the value.     * if the task was give a file, resource or env attribute     * here is where it is loaded     * @throws BuildException on error     */    public void execute() throws BuildException {        if (getProject() == null) {            throw new IllegalStateException("project has not been set");        }        if (name != null) {            if (value == null && ref == null) {                throw new BuildException("You must specify value, location or "                                         + "refid with the name attribute",                                         getLocation());            }        } else {            if (url == null && file == null && resource == null && env == null) {                throw new BuildException("You must specify url, file, resource or "                                         + "environment when not using the "                                         + "name attribute", getLocation());            }        }        if (url == null && file == null && resource == null && prefix != null) {            throw new BuildException("Prefix is only valid when loading from "                                     + "a url, file or resource", getLocation());        }        if ((name != null) && (value != null)) {            addProperty(name, value);        }        if (file != null) {            loadFile(file);        }        if (url != null) {            loadUrl(url);        }        if (resource != null) {            loadResource(resource);        }        if (env != null) {            loadEnvironment(env);        }        if ((name != null) && (ref != null)) {            try {                addProperty(name,                            ref.getReferencedObject(getProject()).toString());            } catch (BuildException be) {                if (fallback != null) {                    addProperty(name,                                ref.getReferencedObject(fallback).toString());                } else {                    throw be;                }            }        }    }    /**     * load properties from a url     * @param url url to load from     * @throws BuildException on error     */    protected void loadUrl(URL url) throws BuildException {        Properties props = new Properties();        log("Loading " + url, Project.MSG_VERBOSE);        try {            InputStream is = url.openStream();            try {                props.load(is);            } finally {                if (is != null) {                    is.close();                }            }            addProperties(props);        } catch (IOException ex) {            throw new BuildException(ex, getLocation());        }    }    /**     * load properties from a file     * @param file file to load     * @throws BuildException on error     */    protected void loadFile(File file) throws BuildException {        Properties props = new Properties();        log("Loading " + file.getAbsolutePath(), Project.MSG_VERBOSE);        try {            if (file.exists()) {                FileInputStream  fis = null;                try {                    fis = new FileInputStream(file);                    props.load(fis);                } finally {                    if (fis != null) {                        fis.close();                    }                }                addProperties(props);            } else {                log("Unable to find property file: " + file.getAbsolutePath(),                    Project.MSG_VERBOSE);            }        } catch (IOException ex) {            throw new BuildException(ex, getLocation());        }    }    /**     * load properties from a resource in the current classpath     * @param name name of resource to load     */    protected void loadResource(String name) {        Properties props = new Properties();        log("Resource Loading " + name, Project.MSG_VERBOSE);        InputStream is = null;        try {            ClassLoader cL = null;            if (classpath != null) {                cL = getProject().createClassLoader(classpath);            } else {                cL = this.getClass().getClassLoader();            }            if (cL == null) {                is = ClassLoader.getSystemResourceAsStream(name);            } else {                is = cL.getResourceAsStream(name);            }            if (is != null) {                props.load(is);                addProperties(props);            } else {                log("Unable to find resource " + name, Project.MSG_WARN);            }        } catch (IOException ex) {            throw new BuildException(ex, getLocation());        } finally {            if (is != null) {                try {                    is.close();                } catch (IOException e) {                    // ignore                }            }        }    }    /**     * load the environment values     * @param prefix prefix to place before them     */    protected void loadEnvironment(String prefix) {        Properties props = new Properties();        if (!prefix.endsWith(".")) {            prefix += ".";        }        log("Loading Environment " + prefix, Project.MSG_VERBOSE);        Vector osEnv = Execute.getProcEnvironment();        for (Enumeration e = osEnv.elements(); e.hasMoreElements();) {            String entry = (String) e.nextElement();            int pos = entry.indexOf('=');            if (pos == -1) {                log("Ignoring: " + entry, Project.MSG_WARN);            } else {                props.put(prefix + entry.substring(0, pos),                entry.substring(pos + 1));            }        }        addProperties(props);    }    /**     * iterate through a set of properties,     * resolve them then assign them     * @param props the properties to iterate over     */    protected void addProperties(Properties props) {        resolveAllProperties(props);        Enumeration e = props.keys();        while (e.hasMoreElements()) {            String propertyName = (String) e.nextElement();            String propertyValue = props.getProperty(propertyName);            String v = getProject().replaceProperties(propertyValue);            if (prefix != null) {                propertyName = prefix + propertyName;            }            addProperty(propertyName, v);        }    }    /**     * add a name value pair to the project property set     * @param n name of property     * @param v value to set     */    protected void addProperty(String n, String v) {        if (userProperty) {            if (getProject().getUserProperty(n) == null) {                getProject().setInheritedProperty(n, v);            } else {                log("Override ignored for " + n, Project.MSG_VERBOSE);            }        } else {            getProject().setNewProperty(n, v);        }    }    /**     * resolve properties inside a properties hashtable     * @param props properties object to resolve     */    private void resolveAllProperties(Properties props) throws BuildException {        for (Enumeration e = props.keys(); e.hasMoreElements();) {            String propertyName = (String) e.nextElement();            Stack referencesSeen = new Stack();            resolve(props, propertyName, referencesSeen);        }    }    /**     * Recursively expand the named property using the project's     * reference table and the given set of properties - fail if a     * circular definition is detected.     *     * @param props properties object to resolve     * @param name of the property to resolve     * @param referencesSeen stack of all property names that have     * been tried to expand before coming here.     */    private void resolve(Properties props, String name, Stack referencesSeen)        throws BuildException {        if (referencesSeen.contains(name)) {            throw new BuildException("Property " + name + " was circularly "                                     + "defined.");        }        String propertyValue = props.getProperty(name);        Vector fragments = new Vector();        Vector propertyRefs = new Vector();        PropertyHelper.getPropertyHelper(            this.getProject()).parsePropertyString(                propertyValue, fragments, propertyRefs);        if (propertyRefs.size() != 0) {            referencesSeen.push(name);            StringBuffer sb = new StringBuffer();            Enumeration i = fragments.elements();            Enumeration j = propertyRefs.elements();            while (i.hasMoreElements()) {                String fragment = (String) i.nextElement();                if (fragment == null) {                    String propertyName = (String) j.nextElement();                    fragment = getProject().getProperty(propertyName);                    if (fragment == null) {                        if (props.containsKey(propertyName)) {                            resolve(props, propertyName, referencesSeen);                            fragment = props.getProperty(propertyName);                        } else {                            fragment = "${" + propertyName + "}";                        }                    }                }                sb.append(fragment);            }            propertyValue = sb.toString();            props.put(name, propertyValue);            referencesSeen.pop();        }    }}

⌨️ 快捷键说明

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