ehatcher 02/04/21 17:05:09 Modified: docs/manual coretasklist.html credits.html src/main/org/apache/tools/ant/taskdefs XmlProperty.java Added: docs/manual/CoreTasks xmlproperty.html Log: patches provided by Nicola Ken Barrozi. documentation added. the "[]" and indexed properties were removed as they did not work properly and were not desired. Revision Changes Path 1.41 +1 -0 jakarta-ant/docs/manual/coretasklist.html Index: coretasklist.html =================================================================== RCS file: /home/cvs/jakarta-ant/docs/manual/coretasklist.html,v retrieving revision 1.40 retrieving revision 1.41 diff -u -r1.40 -r1.41 --- coretasklist.html 15 Apr 2002 09:53:24 -0000 1.40 +++ coretasklist.html 22 Apr 2002 00:05:09 -0000 1.41 @@ -83,6 +83,7 @@ <a href="CoreTasks/uptodate.html">Uptodate</a><br> <a href="CoreTasks/waitfor.html">Waitfor</a><br> <a href="CoreTasks/war.html">War</a><br> +<a href="CoreTasks/xmlproperty.html">XmlProperty</a><br> <a href="CoreTasks/style.html">Xslt/Style</a><br> <a href="CoreTasks/zip.html">Zip</a><br> </body> 1.15 +2 -1 jakarta-ant/docs/manual/credits.html Index: credits.html =================================================================== RCS file: /home/cvs/jakarta-ant/docs/manual/credits.html,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- credits.html 21 Mar 2002 09:05:19 -0000 1.14 +++ credits.html 22 Apr 2002 00:05:09 -0000 1.15 @@ -14,6 +14,7 @@ <!-- Names are in alphabetical order, on last name --> <ul> <li>Stephane Bailliez (<a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>)</li> + <li>Nicola Ken Barozzi (<a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>)</li> <li>Jacques Bergeron (<a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>)</li> <li>Stefan Bodewig (<a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>)</li> <li>Patrick Chanezon (<a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>)</li> @@ -39,7 +40,7 @@ <center> <p>Version: @VERSION@<br> -$Id: credits.html,v 1.14 2002/03/21 09:05:19 bodewig Exp $</p> +$Id: credits.html,v 1.15 2002/04/22 00:05:09 ehatcher Exp $</p> </center> <hr> <p align="center">Copyright © 2001-2002 Apache Software Foundation. All rights 1.1 jakarta-ant/docs/manual/CoreTasks/xmlproperty.html Index: xmlproperty.html =================================================================== <html> <head> <title>XmlProperty Task</title> </head> <body> <h2><a name="xmlproperty">XmlProperty</a></h2> <h3>Description</h3> <p> Loads property values from a valid xml file. </p> <h3>Parameters</h3> <table border="1" cellpadding="2" cellspacing="0"> <tr> <td valign="top"><b>Attribute</b></td> <td valign="top"><b>Description</b></td> <td align="center" valign="top"><b>Required</b></td> </tr> <tr> <td valign="top">file</td> <td valign="top">The XML file to parse.</td> <td valign="top" align="center">Yes</td> </tr> <tr> <td valign="top">prefix</td> <td valign="top">The prefix to prepend to each property</td> <td valign="top" align="center">No</td> </tr> <tr> <td valign="top">keepRoot</td> <td valign="top">If false, it doesn't include the xml root tag as a first value in the property name.</td> <td valign="top" align="center">No, default is <i>true</i>.</td> </tr> <tr> <td valign="top">validate</td> <td valign="top">If true, it enables validation.</td> <td valign="top" align="center">No, default is <i>false</i>.</td> </tr> <tr> <td valign="top">collapseAttributes</td> <td valign="top">If true, it treats attributes as nested elements.</td> <td valign="top" align="center">No, default is <i>false</i>.</td> </tr> </table> <h3>Examples</h3> <pre> <xmlproperty file="somefile.xml" /></pre> <p>Load contents of somefile.xml as Ant properties.</p> <pre> <root-tag myattr="true"> <inner-tag someattr="val">Text</inner-tag> <a2><a3><a4>false</a4></a3></a2> </root-tag> </pre> <p>This is an example xml file.</p> <pre> root-tag(myattr)=true root-tag.inner-tag=Text root-tag.inner-tag(someattr)=val root-tag.a2.a3.a4=false </pre> <p>These are the properties loaded by this task from the previous example file.</p> <pre> <xmlproperty file="somefile.xml" collapseAttributes="true" /></pre> <p>Load contents of somefile.xml as Ant properties collapsing attributes as nodes.</p> <pre> root-tag.myattr=true root-tag.inner-tag=Text root-tag.inner-tag.someatt=val root-tag.a2.a3.a4=false </pre> <p>These are the properties loaded by this task from the previous example file, with attribute collapsing true.</p> <hr/> <p align="center">Copyright © 2002 Apache Software Foundation. All rights Reserved.</p> </body> </html> 1.2 +33 -43 jakarta-ant/src/main/org/apache/tools/ant/taskdefs/XmlProperty.java Index: XmlProperty.java =================================================================== RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/XmlProperty.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- XmlProperty.java 19 Apr 2002 19:51:29 -0000 1.1 +++ XmlProperty.java 22 Apr 2002 00:05:09 -0000 1.2 @@ -69,9 +69,10 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.util.Vector; /** - * Task get property values from a valid xml file. + * Task that gets property values from a valid xml file. * Example: * <root-tag myattr="true"> * <inner-tag someattr="val">Text</inner-tag> @@ -83,15 +84,18 @@ * root-tag.inner-tag(someattr)=val * root-tag.a2.a3.a4=false * - * @author <a href="mailto:[EMAIL PROTECTED]">Nicola Ken Barozzi</a> + * @author <a href="mailto:[EMAIL PROTECTED]">Nicola Ken Barozzi</a> * @author Erik Hatcher * @created 14 January 2002 */ public class XmlProperty extends org.apache.tools.ant.Task { + private File src; private String prefix = ""; private boolean keepRoot = true; + private boolean validate = false; + private boolean collapseAttributes = false; private org.w3c.dom.Document document; /** @@ -115,6 +119,7 @@ */ public void execute() throws org.apache.tools.ant.BuildException { + BufferedInputStream configurationStream = null; try { @@ -123,7 +128,7 @@ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - factory.setValidating(false); + factory.setValidating(validate); factory.setNamespaceAware(false); DocumentBuilder builder = factory.newDocumentBuilder(); @@ -133,33 +138,14 @@ NodeList topChildren = topElement.getChildNodes(); int numChildren = topChildren.getLength(); - String prefixToUse = ""; - - if (!(prefix.equals(""))) { - prefixToUse = prefix; - } - - log("Prefix to use 1: \"" + prefixToUse + "\"", Project.MSG_DEBUG); - -/* - if ((!(prefix.equals(""))) && keepRoot) { - prefixToUse += "."; - } - - log("Prefix to use 2: \"" + prefixToUse + "\"", Project.MSG_DEBUG); - - if (keepRoot) { - prefixToUse += (topElement.getNodeName()); - } + log("Using prefix: \"" + prefix + "\"", Project.MSG_DEBUG); - log("Prefix to use 3: \"" + prefixToUse + "\"", Project.MSG_VERBOSE); -*/ if (keepRoot) { - addNodeRecursively(topElement, prefixToUse, 0); + addNodeRecursively(topElement, prefix); } else { for (int i = 0; i < numChildren; i++) { - addNodeRecursively(topChildren.item(i), prefixToUse, 0); + addNodeRecursively(topChildren.item(i), prefix); } } @@ -187,13 +173,21 @@ } - void addNodeRecursively(org.w3c.dom.Node node, String prefix, int index) { + void addNodeRecursively(org.w3c.dom.Node node, String prefix) { if (node.hasAttributes()) { org.w3c.dom.NamedNodeMap nodeAttributes = node.getAttributes(); for (int i = 0; i < nodeAttributes.getLength(); i++) { Node attributeNode = nodeAttributes.item(i); - String attributeName = prefix + (prefix.trim().equals("")?"":".") + node.getNodeName() + "(" + attributeNode.getNodeName() + ")"; + String attributeName; + + if(collapseAttributes){ + attributeName = prefix + (prefix.trim().equals("")?"":".") + node.getNodeName() + "." + attributeNode.getNodeName(); + } + else{ + attributeName = prefix + (prefix.trim().equals("")?"":".") + node.getNodeName() + "(" + attributeNode.getNodeName() + ")"; + } + String attributeValue = attributeNode.getNodeValue(); log(attributeName + ":" + attributeValue, Project.MSG_DEBUG); project.setNewProperty(attributeName, attributeValue); @@ -204,32 +198,19 @@ String nodeText = node.getNodeValue(); if (nodeText.trim().length() != 0) { log(prefix + ":" + nodeText, Project.MSG_DEBUG); - if (index == 0) { - project.setNewProperty(prefix, nodeText); - } - - project.setNewProperty(prefix + "[" + String.valueOf(index) + "]", nodeText); + project.setNewProperty(prefix, nodeText); } } if (node.hasChildNodes()) { prefix += ((prefix.trim().equals("")?"":".") + node.getNodeName()); - org.w3c.dom.NodeList nodeChildren = node.getChildNodes(); + org.w3c.dom.NodeList nodeChildren = node.getChildNodes(); int numChildren = nodeChildren.getLength(); - StringBuffer childList = new StringBuffer(); - for (int i = 0; i < numChildren; i++) { - if (i != 0) { - childList.append(","); - } - childList.append(node.getNodeName() + "[" + String.valueOf(index) + "]"); - addNodeRecursively(nodeChildren.item(i), prefix, i); + addNodeRecursively(nodeChildren.item(i), prefix); } - - project.setNewProperty(prefix + "[]", childList.toString()); - } } @@ -244,4 +225,13 @@ public void setKeeproot(boolean keepRoot) { this.keepRoot = keepRoot; } + + public void setValidate(boolean validate) { + this.validate = validate; + } + + public void setCollapseAttributes(boolean collapseAttributes) { + this.collapseAttributes = collapseAttributes; + } + }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>