You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Adam Rzadkowolski <ad...@axit.pl> on 2003/02/18 17:11:27 UTC

XMLProperty and CDATA

hi,
in my xml file i have nodes which contains CDATA,
so i use xmlproperty.

my question is how to get to the content of xml tag with CDATA.

<xml>
....
<xxx><![CDATA[blebleble]]></xxx>

how to get content of xml.xxx.CDATA

regards,
Adam


RE: XMLProperty and CDATA

Posted by Markku Saarela <ma...@entra.fi>.
Hi,

By replacing org.apache.tools.ant.taskdefs.XmlProperty task method:

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

By version below where is cdata section handled:

    /**
     * Process the given node, adding any required attributes from
     * this child node alone -- but <em>not</em> processing any
     * children.
     *
     * @param node the XML Node to parse
     * @param prefix A string to prepend to any properties that get
     * added by this node.
     * @param container Optionally, an object that a parent node
     * generated that this node might belong to.  For example, this
     * node could be within a node that generated a Path.
     * @return the Object created by this node.  Generally, this is
     * either a String if this node resulted in setting an attribute,
     * or a Path.
     */
    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 != null && 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 (nodeName.equals(ID)) {
                        // ID has already been found above.
                        continue;
                    } else if (containingPath != null
                               && nodeName.equals(PATH)) {
                        // A "path" attribute for a node within a Path
object.
                        containingPath.setPath(attributeValue);
                    } else if (container instanceof Path
                               && nodeName.equals(REF_ID)) {
                        // A "refid" attribute for a node within a Path
object.
                        containingPath.setPath(attributeValue);
                    } 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);
                    }
                }
            }
        }

        if (node.getNodeType() == Node.TEXT_NODE) {
            // If the containing object was a String, then use it as the ID.
            if (semanticAttributes && id == null
                && container instanceof String) {
                id = (String) container;
            }
            // For the text node, add a property.
            String nodeText = getAttributeValue(node);
            if (nodeText.trim().length() != 0) {
              addProperty(prefix, nodeText, id);
            }
        } else if ((node.getNodeType() == Node.ELEMENT_NODE)
            && (node.getChildNodes().getLength() == 1)
            && (node.getFirstChild().getNodeType() ==
Node.CDATA_SECTION_NODE)) {
            // CDATA section
            // If the containing object was a String, then use it as the ID.
            if (semanticAttributes && id == null
                && container instanceof String) {
                id = (String) container;
            }
            // For the cdata-section node, add a property.
            String nodeText = node.getFirstChild().getNodeValue();
            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);
    }

regards,

Markku Saarela
markku.saarela@entra.fi

-----Original Message-----
From: Adam Rzadkowolski [mailto:adam.rzadkowolski@axit.pl]
Sent: 18. helmikuuta 2003 18:11
To: user@ant.apache.org
Subject: XMLProperty and CDATA


hi,
in my xml file i have nodes which contains CDATA,
so i use xmlproperty.

my question is how to get to the content of xml tag with CDATA.

<xml>
....
<xxx><![CDATA[blebleble]]></xxx>

how to get content of xml.xxx.CDATA

regards,
Adam


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org