Browse Source

patches provided by Nicola Ken Barrozi. documentation added. the "[]" and indexed properties were removed as they did not work properly and were not desired.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@272539 13f79535-47bb-0310-9956-ffa450edef68
master
Erik Hatcher 23 years ago
parent
commit
422f63ab87
4 changed files with 128 additions and 43 deletions
  1. +93
    -0
      docs/manual/CoreTasks/xmlproperty.html
  2. +1
    -0
      docs/manual/coretasklist.html
  3. +1
    -0
      docs/manual/credits.html
  4. +33
    -43
      src/main/org/apache/tools/ant/taskdefs/XmlProperty.java

+ 93
- 0
docs/manual/CoreTasks/xmlproperty.html View File

@@ -0,0 +1,93 @@
<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> &lt;xmlproperty file="somefile.xml" /&gt;</pre>

<p>Load contents of somefile.xml as Ant properties.</p>

<pre>
&lt;root-tag myattr="true"&gt;
&lt;inner-tag someattr="val"&gt;Text&lt;/inner-tag&gt;
&lt;a2&gt;&lt;a3&gt;&lt;a4&gt;false&lt;/a4&gt;&lt;/a3&gt;&lt;/a2&gt;
&lt;/root-tag&gt;
</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> &lt;xmlproperty file="somefile.xml" collapseAttributes="true" /&gt;</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 &copy; 2002 Apache Software Foundation. All rights
Reserved.</p>

</body>
</html>


+ 1
- 0
docs/manual/coretasklist.html View File

@@ -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
- 0
docs/manual/credits.html View File

@@ -14,6 +14,7 @@
<!-- Names are in alphabetical order, on last name -->
<ul>
<li>Stephane Bailliez (<a href="mailto:sbailliez@imediation.com">sbailliez@imediation.com</a>)</li>
<li>Nicola Ken Barozzi (<a href="mailto:nicolaken@apache.org">nicolaken@apache.org</a>)</li>
<li>Jacques Bergeron (<a href="mailto:jacques.bergeron@dogico.com">jacques.bergeron@dogico.com</a>)</li>
<li>Stefan Bodewig (<a href="mailto:stefan.bodewig@epost.de">stefan.bodewig@epost.de</a>)</li>
<li>Patrick Chanezon (<a href="mailto:chanezon@netscape.com">chanezon@netscape.com</a>)</li>


+ 33
- 43
src/main/org/apache/tools/ant/taskdefs/XmlProperty.java View File

@@ -69,9 +69,10 @@ import java.io.BufferedInputStream;
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 @@ import java.io.IOException;
* root-tag.inner-tag(someattr)=val
* root-tag.a2.a3.a4=false
*
* @author <a href="mailto:barozzi@nicolaken.com">Nicola Ken Barozzi</a>
* @author <a href="mailto:nicolaken@apache.org">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 class XmlProperty extends org.apache.tools.ant.Task {
*/
public void execute()
throws org.apache.tools.ant.BuildException {
BufferedInputStream configurationStream = null;

try {
@@ -123,7 +128,7 @@ public class XmlProperty extends org.apache.tools.ant.Task {

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

factory.setValidating(false);
factory.setValidating(validate);
factory.setNamespaceAware(false);

DocumentBuilder builder = factory.newDocumentBuilder();
@@ -133,33 +138,14 @@ public class XmlProperty extends org.apache.tools.ant.Task {
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 @@ public class XmlProperty extends org.apache.tools.ant.Task {
}


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 @@ public class XmlProperty extends org.apache.tools.ant.Task {
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 class XmlProperty extends org.apache.tools.ant.Task {
public void setKeeproot(boolean keepRoot) {
this.keepRoot = keepRoot;
}

public void setValidate(boolean validate) {
this.validate = validate;
}

public void setCollapseAttributes(boolean collapseAttributes) {
this.collapseAttributes = collapseAttributes;
}
}

Loading…
Cancel
Save