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

📄 xmlproperty.java

📁 Use the links below to download a source distribution of Ant from one of our mirrors. It is good pra
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                    } else if (container instanceof Path                               && nodeName.equals(LOCATION)) {                        // A "location" attribute for a node within a                        // Path object.                        containingPath.setLocation(resolveFile(attributeValue));                    } else if (nodeName.equals(PATHID)) {                        // A node identifying a new path                        if (container != null) {                            throw new BuildException("XmlProperty does not "                                                     + "support nested paths");                        }                        addedPath = new Path(getProject());                        getProject().addReference(attributeValue, addedPath);                    } else {                        // An arbitrary attribute.                        String attributeName = getAttributeName(attributeNode);                        addProperty(prefix + attributeName, attributeValue, id);                    }                }            }        }        String nodeText = null;        boolean emptyNode = false;        boolean semanticEmptyOverride = false;        if (node.getNodeType() == Node.ELEMENT_NODE            && semanticAttributes            && node.hasAttributes()            && (node.getAttributes().getNamedItem(VALUE) != null                || node.getAttributes().getNamedItem(LOCATION) != null                || node.getAttributes().getNamedItem(REF_ID) != null                || node.getAttributes().getNamedItem(PATH) != null                || node.getAttributes().getNamedItem(PATHID) != null)) {            semanticEmptyOverride = true;        }        if (node.getNodeType() == Node.TEXT_NODE) {            // For the text node, add a property.            nodeText = getAttributeValue(node);        } else if ((node.getNodeType() == Node.ELEMENT_NODE)            && (node.getChildNodes().getLength() == 1)            && (node.getFirstChild().getNodeType() == Node.CDATA_SECTION_NODE)) {            nodeText = node.getFirstChild().getNodeValue();            if ("".equals(nodeText) && !semanticEmptyOverride) {                emptyNode = true;            }        } else if ((node.getNodeType() == Node.ELEMENT_NODE)                   && (node.getChildNodes().getLength() == 0)                   && !semanticEmptyOverride) {            nodeText = "";            emptyNode = true;        } else if ((node.getNodeType() == Node.ELEMENT_NODE)                   && (node.getChildNodes().getLength() == 1)                   && (node.getFirstChild().getNodeType() == Node.TEXT_NODE)                   && ("".equals(node.getFirstChild().getNodeValue()))                   && !semanticEmptyOverride) {            nodeText = "";            emptyNode = true;        }        if (nodeText != null) {            // If the containing object was a String, then use it as the ID.            if (semanticAttributes && id == null                && container instanceof String) {                id = (String) container;            }            if (nodeText.trim().length() != 0 || emptyNode) {                addProperty(prefix, nodeText, id);            }        }        // Return the Path we added or the ID of this node for        // children to reference if needed.  Path objects are        // definitely used by child path elements, and ID may be used        // for a child text node.        return (addedPath != null ? addedPath : id);    }    /**     * Actually add the given property/value to the project     * after writing a log message.     */    private void addProperty (String name, String value, String id) {        String msg = name + ":" + value;        if (id != null) {            msg += ("(id=" + id + ")");        }        log(msg, Project.MSG_DEBUG);        if (addedAttributes.containsKey(name)) {            // If this attribute was added by this task, then            // we append this value to the existing value.            // We use the setProperty method which will            // forcibly override the property if it already exists.            // We need to put these properties into the project            // when we read them, though (instead of keeping them            // outside of the project and batch adding them at the end)            // to allow other properties to reference them.            value = (String) addedAttributes.get(name) + getDelimiter() + value;            getProject().setProperty(name, value);            addedAttributes.put(name, value);        } else if (getProject().getProperty(name) == null) {            getProject().setNewProperty(name, value);            addedAttributes.put(name, value);        } else {            log("Override ignored for property " + name, Project.MSG_VERBOSE);        }        if (id != null) {            getProject().addReference(id, value);        }    }    /**     * Return a reasonable attribute name for the given node.     * If we are using semantic attributes or collapsing     * attributes, the returned name is ".nodename".     * Otherwise, we return "(nodename)".  This is long-standing     * (and default) &lt;xmlproperty&gt; behavior.     */    private String getAttributeName (Node attributeNode) {        String attributeName = attributeNode.getNodeName();        if (semanticAttributes) {            // Never include the "refid" attribute as part of the            // attribute name.            if (attributeName.equals(REF_ID)) {                return "";            // Otherwise, return it appended unless property to hide it is set.            } else if (!isSemanticAttribute(attributeName)                       || includeSemanticAttribute) {                return "." + attributeName;            } else {                return "";            }        } else if (collapseAttributes) {            return "." + attributeName;        } else {            return "(" + attributeName + ")";        }    }    /**     * Return whether the provided attribute name is recognized or not.     */    private static boolean isSemanticAttribute (String attributeName) {        for (int i = 0; i < ATTRIBUTES.length; i++) {            if (attributeName.equals(ATTRIBUTES[i])) {                return true;            }        }        return false;    }    /**     * Return the value for the given attribute.     * If we are not using semantic attributes, its just the     * literal string value of the attribute.     *     * <p>If we <em>are</em> using semantic attributes, then first     * dependent properties are resolved (i.e., ${foo} is resolved     * based on the foo property value), and then an appropriate data     * type is used.  In particular, location-based properties are     * resolved to absolute file names.  Also for refid values, look     * up the referenced object from the project.</p>     */    private String getAttributeValue (Node attributeNode) {        String nodeValue = attributeNode.getNodeValue().trim();        if (semanticAttributes) {            String attributeName = attributeNode.getNodeName();            nodeValue = getProject().replaceProperties(nodeValue);            if (attributeName.equals(LOCATION)) {                File f = resolveFile(nodeValue);                return f.getPath();            } else if (attributeName.equals(REF_ID)) {                Object ref = getProject().getReference(nodeValue);                if (ref != null) {                    return ref.toString();                }            }        }        return nodeValue;    }    /**     * The XML file to parse; required.     * @param src the file to parse     */    public void setFile(File src) {        setSrcResource(new FileResource(src));    }    /**     * The resource to pack; required.     * @param src resource to expand     */    public void setSrcResource(Resource src) {        if (src.isDirectory()) {            throw new BuildException("the source can't be a directory");        }        if (src instanceof FileResource && !supportsNonFileResources()) {            throw new BuildException("Only FileSystem resources are"                                     + " supported.");        }        this.src = src;    }    /**     * Set the source resource.     * @param a the resource to pack as a single element Resource collection.     */    public void addConfigured(ResourceCollection a) {        if (a.size() != 1) {            throw new BuildException("only single argument resource collections"                                     + " are supported as archives");        }        setSrcResource((Resource) a.iterator().next());    }    /**     * the prefix to prepend to each property     * @param prefix the prefix to prepend to each property     */    public void setPrefix(String prefix) {        this.prefix = prefix.trim();    }    /**     * flag to include the xml root tag as a     * first value in the property name; optional,     * default is true     * @param keepRoot if true (default), include the xml root tag     */    public void setKeeproot(boolean keepRoot) {        this.keepRoot = keepRoot;    }    /**     * flag to validate the XML file; optional, default false     * @param validate if true validate the XML file, default false     */    public void setValidate(boolean validate) {        this.validate = validate;    }    /**     * flag to treat attributes as nested elements;     * optional, default false     * @param collapseAttributes if true treat attributes as nested elements     */    public void setCollapseAttributes(boolean collapseAttributes) {        this.collapseAttributes = collapseAttributes;    }    /**     * Attribute to enable special handling of attributes - see ant manual.     * @param semanticAttributes if true enable the special handling.     */    public void setSemanticAttributes(boolean semanticAttributes) {        this.semanticAttributes = semanticAttributes;    }    /**     * The directory to use for resolving file references.     * Ignored if semanticAttributes is not set to true.     * @param rootDirectory the directory.     */    public void setRootDirectory(File rootDirectory) {        this.rootDirectory = rootDirectory;    }    /**     * Include the semantic attribute name as part of the property name.     * Ignored if semanticAttributes is not set to true.     * @param includeSemanticAttribute if true include the sematic attribute     *                                 name.     */    public void setIncludeSemanticAttribute(boolean includeSemanticAttribute) {        this.includeSemanticAttribute = includeSemanticAttribute;    }    /**     * add an XMLCatalog as a nested element; optional.     * @param catalog the XMLCatalog to use     */    public void addConfiguredXMLCatalog(XMLCatalog catalog) {        xmlCatalog.addConfiguredXMLCatalog(catalog);    }    /* Expose members for extensibility */    /**     * @return the file attribute.     */    protected File getFile () {        if (src instanceof FileResource) {            return ((FileResource) src).getFile();        } else {            return null;        }    }    /**     * @return the resource.     */    protected Resource getResource() {        // delegate this way around to support subclasses that        // overwrite getFile        File f = getFile();        if (f != null) {            return new FileResource(f);        } else {            return src;        }    }    /**     * @return the prefix attribute.     */    protected String getPrefix () {        return this.prefix;    }    /**     * @return the keeproot attribute.     */    protected boolean getKeeproot () {        return this.keepRoot;    }    /**     * @return the validate attribute.     */    protected boolean getValidate () {        return this.validate;    }    /**     * @return the collapse attributes attribute.     */    protected boolean getCollapseAttributes () {        return this.collapseAttributes;    }    /**     * @return the semantic attributes attribute.     */    protected boolean getSemanticAttributes () {        return this.semanticAttributes;    }    /**     * @return the root directory attribute.     */    protected File getRootDirectory () {        return this.rootDirectory;    }    /**     * @return the include semantic attribute.     */    protected boolean getIncludeSementicAttribute () {        return this.includeSemanticAttribute;    }    /**     * Let project resolve the file - or do it ourselves if     * rootDirectory has been set.     */    private File resolveFile(String fileName) {        if (rootDirectory == null) {            return FILE_UTILS.resolveFile(getProject().getBaseDir(), fileName);        }        return FILE_UTILS.resolveFile(rootDirectory, fileName);    }    /**     * Whether this task can deal with non-file resources.     *     * <p>This implementation returns true only if this task is     * &lt;xmlproperty&gt;.  Any subclass of this class that also wants to     * support non-file resources needs to override this method.  We     * need to do so for backwards compatibility reasons since we     * can't expect subclasses to support resources.</p>     * @return true for this task.     * @since Ant 1.7     */    protected boolean supportsNonFileResources() {        return getClass().equals(XmlProperty.class);    }    public String getDelimiter() {        return delimiter;    }    public void setDelimiter(String delimiter) {        this.delimiter = delimiter;    }}

⌨️ 快捷键说明

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