Browse Source

add resource support to xmlproperty

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@344333 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 19 years ago
parent
commit
86ffd42246
4 changed files with 100 additions and 11 deletions
  1. +6
    -1
      docs/manual/CoreTasks/xmlproperty.html
  2. +7
    -0
      src/etc/testcases/taskdefs/xmlproperty.xml
  3. +77
    -8
      src/main/org/apache/tools/ant/taskdefs/XmlProperty.java
  4. +10
    -2
      src/testcases/org/apache/tools/ant/taskdefs/XmlPropertyTest.java

+ 6
- 1
docs/manual/CoreTasks/xmlproperty.html View File

@@ -85,7 +85,7 @@ is roughly equivalent to the following fragments in a build.xml file:
<tr> <tr>
<td valign="top">file</td> <td valign="top">file</td>
<td valign="top">The XML file to parse.</td> <td valign="top">The XML file to parse.</td>
<td valign="top" align="center">Yes</td>
<td valign="top" align="center">Yes, or a nested resource collection.</td>
</tr> </tr>
<tr> <tr>
<td valign="top">prefix</td> <td valign="top">prefix</td>
@@ -137,6 +137,11 @@ is roughly equivalent to the following fragments in a build.xml file:
<p>The <a href="../CoreTypes/xmlcatalog.html"><tt>&lt;xmlcatalog&gt;</tt></a> <p>The <a href="../CoreTypes/xmlcatalog.html"><tt>&lt;xmlcatalog&gt;</tt></a>
element is used to perform entity resolution.</p> element is used to perform entity resolution.</p>


<h4>any <a href="../CoreTypes/resources.html">resource</a> or single element
resource collection</h4>

<p>The specified resource will be used as input.</p>

<a name="examples"> <a name="examples">
<h3>Examples</h3> <h3>Examples</h3>
</a> </a>


+ 7
- 0
src/etc/testcases/taskdefs/xmlproperty.xml View File

@@ -8,6 +8,13 @@
<xmlproperty file="xmlproperty_withdtd.xml"/> <xmlproperty file="xmlproperty_withdtd.xml"/>
</target> </target>


<target name="testResource">
<loadfile srcfile="xmlproperty_data.xml" property="prop"/>
<xmlproperty>
<string value="${prop}"/>
</xmlproperty>
</target>

<target name="testneedscat"> <target name="testneedscat">
<xmlproperty file="xmlproperty_needscat.xml"> <xmlproperty file="xmlproperty_needscat.xml">
<xmlcatalog> <xmlcatalog>


+ 77
- 8
src/main/org/apache/tools/ant/taskdefs/XmlProperty.java View File

@@ -26,7 +26,10 @@ import javax.xml.parsers.ParserConfigurationException;
import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project; import org.apache.tools.ant.Project;
import org.apache.tools.ant.types.Path; import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.ResourceCollection;
import org.apache.tools.ant.types.XMLCatalog; import org.apache.tools.ant.types.XMLCatalog;
import org.apache.tools.ant.types.resources.FileResource;
import org.apache.tools.ant.util.FileUtils; import org.apache.tools.ant.util.FileUtils;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import org.w3c.dom.Element; import org.w3c.dom.Element;
@@ -171,7 +174,7 @@ import org.xml.sax.EntityResolver;


public class XmlProperty extends org.apache.tools.ant.Task { public class XmlProperty extends org.apache.tools.ant.Task {


private File src;
private Resource src;
private String prefix = ""; private String prefix = "";
private boolean keepRoot = true; private boolean keepRoot = true;
private boolean validate = false; private boolean validate = false;
@@ -226,22 +229,29 @@ public class XmlProperty extends org.apache.tools.ant.Task {
public void execute() public void execute()
throws BuildException { throws BuildException {


if (getFile() == null) {
String msg = "XmlProperty task requires a file attribute";
Resource r = getResource();

if (r == null) {
String msg = "XmlProperty task requires a source resource";
throw new BuildException(msg); throw new BuildException(msg);
} }


try { try {
log("Loading " + src.getAbsolutePath(), Project.MSG_VERBOSE);
log("Loading " + src, Project.MSG_VERBOSE);


if (src.exists()) {
if (r.isExists()) {


DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(validate); factory.setValidating(validate);
factory.setNamespaceAware(false); factory.setNamespaceAware(false);
DocumentBuilder builder = factory.newDocumentBuilder(); DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(getEntityResolver()); builder.setEntityResolver(getEntityResolver());
Document document = builder.parse(src);
Document document = null;
if (src instanceof FileResource) {
document = builder.parse(((FileResource) src).getFile());
} else {
document = builder.parse(src.getInputStream());
}
Element topElement = document.getDocumentElement(); Element topElement = document.getDocumentElement();


// Keep a hashtable of attributes added by this task. // Keep a hashtable of attributes added by this task.
@@ -261,7 +271,7 @@ public class XmlProperty extends org.apache.tools.ant.Task {
} }


} else { } else {
log("Unable to find property file: " + src.getAbsolutePath(),
log("Unable to find property resource: " + r,
Project.MSG_VERBOSE); Project.MSG_VERBOSE);
} }


@@ -547,9 +557,36 @@ public class XmlProperty extends org.apache.tools.ant.Task {
* @param src the file to parse * @param src the file to parse
*/ */
public void setFile(File src) { 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; 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 * the prefix to prepend to each property
* @param prefix the prefix to prepend to each property * @param prefix the prefix to prepend to each property
@@ -626,7 +663,25 @@ public class XmlProperty extends org.apache.tools.ant.Task {
* @return the file attribute. * @return the file attribute.
*/ */
protected File getFile () { protected File getFile () {
return this.src;
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;
}
} }


/** /**
@@ -689,4 +744,18 @@ public class XmlProperty extends org.apache.tools.ant.Task {
return FILE_UTILS.resolveFile(rootDirectory, 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;gzip&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>
*
* @since Ant 1.7
*/
protected boolean supportsNonFileResources() {
return getClass().equals(XmlProperty.class);
}
} }

+ 10
- 2
src/testcases/org/apache/tools/ant/taskdefs/XmlPropertyTest.java View File

@@ -44,8 +44,16 @@ public class XmlPropertyTest extends BuildFileTest {
configureProject("src/etc/testcases/taskdefs/xmlproperty.xml"); configureProject("src/etc/testcases/taskdefs/xmlproperty.xml");
} }


public void testProperties() {
executeTarget("test");
public void testFile() {
testProperties("test");
}

public void testResource() {
testProperties("testResource");
}

private void testProperties(String target) {
executeTarget(target);
assertEquals("true", getProject().getProperty("root-tag(myattr)")); assertEquals("true", getProject().getProperty("root-tag(myattr)"));
assertEquals("Text", getProject().getProperty("root-tag.inner-tag")); assertEquals("Text", getProject().getProperty("root-tag.inner-tag"));
assertEquals("val", assertEquals("val",


Loading…
Cancel
Save