public Object processNode()

in src/main/org/apache/tools/ant/taskdefs/XmlProperty.java [334:453]


    public Object processNode(Node node, String prefix, Object container) {

        // Parse the attribute(s) and text of this node, adding
        // properties for each.
        // if the "path" attribute is specified, then return the created path
        // which will be passed to the children of this node.
        Object addedPath = null;

        // The value of an id attribute of this node.
        String id = null;

        if (node.hasAttributes()) {

            NamedNodeMap nodeAttributes = node.getAttributes();

            // Is there an id attribute?
            Node idNode = nodeAttributes.getNamedItem(ID);
            id = semanticAttributes && idNode != null ? idNode.getNodeValue() : null;

            // Now, iterate through the attributes adding them.
            for (int i = 0; i < nodeAttributes.getLength(); i++) {

                Node attributeNode = nodeAttributes.item(i);

                if (!semanticAttributes) {
                    String attributeName = getAttributeName(attributeNode);
                    String attributeValue = getAttributeValue(attributeNode);
                    addProperty(prefix + attributeName, attributeValue, null);
                } else {
                    String nodeName = attributeNode.getNodeName();
                    String attributeValue = getAttributeValue(attributeNode);

                    Path containingPath = container instanceof Path
                        ? (Path) container : null;
                    /*
                     * The main conditional logic -- if the attribute
                     * is somehow "special" (i.e., it has known
                     * semantic meaning) then deal with it
                     * appropriately.
                     */
                    if (ID.equals(nodeName)) {
                        // ID has already been found above.
                        continue;
                    }
                    if (containingPath != null && PATH.equals(nodeName)) {
                        // A "path" attribute for a node within a Path object.
                        containingPath.setPath(attributeValue);
                    } else if (containingPath != null
                               && container instanceof Path && REF_ID.equals(nodeName)) {
                        // A "refid" attribute for a node within a Path object.
                        containingPath.setPath(attributeValue);
                    } else if (containingPath != null && container instanceof Path
                               && LOCATION.equals(nodeName)) {
                        // A "location" attribute for a node within a
                        // Path object.
                        containingPath.setLocation(resolveFile(attributeValue));
                    } else if (PATHID.equals(nodeName)) {
                        // 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 = 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);
        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 (nodeText.isEmpty() && !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
               && node.getFirstChild().getNodeValue().isEmpty()
               && !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().isEmpty() || 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;
    }