Browse Source

resource support for <loadproperties>

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@349636 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 19 years ago
parent
commit
861a449064
4 changed files with 112 additions and 81 deletions
  1. +32
    -5
      docs/manual/CoreTasks/loadproperties.html
  2. +20
    -1
      src/etc/testcases/taskdefs/loadproperties.xml
  3. +53
    -74
      src/main/org/apache/tools/ant/taskdefs/LoadProperties.java
  4. +7
    -1
      src/testcases/org/apache/tools/ant/taskdefs/LoadPropertiesTest.java

+ 32
- 5
docs/manual/CoreTasks/loadproperties.html View File

@@ -32,7 +32,8 @@ filter.</p>
<tr>
<td valign="top">srcFile</td>
<td valign="top">source file</td>
<td valign="top" rowspan="2" align="center">One of these</td>
<td valign="top" rowspan="2" align="center">One of these or a
nested resource</td>
</tr>
<tr>
<td valign="top">resource</td>
@@ -56,14 +57,29 @@ filter.</p>
<td align="center" valign="top">No</td>
</tr>
</table>
<p>
The LoadProperties task supports nested <a href="../CoreTypes/filterchain.html">
FilterChain</a>s, as well as a nested <code>&lt;classpath&gt;</code>
element for use with the <i>resource</i> attribute.

<h3>Parameters specified as nested elements</h3>

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

<p>The specified resource will be used as src. <em>Since Ant 1.7</em></p>

<h4><a href="../CoreTypes/filterchain.html">FilterChain</a></h4>

<h4>classpath</h4>

<p>for use with the <i>resource</i> attribute.</p>

<h3>Examples</h3>
<pre> &lt;loadproperties srcFile="file.properties"/&gt;
</pre>
or
<pre>
&lt;loadproperties&gt;
&lt;file file="file.properties"/&gt;
&lt;/loadproperties&gt;
</pre>
Load contents of file.properties as Ant properties.

<pre> &lt;loadproperties srcFile="file.properties"&gt;
@@ -77,6 +93,17 @@ Load contents of file.properties as Ant properties.
Read the lines that contain the string &quot;import.&quot;
from the file &quot;file.properties&quot; and load them as
Ant properties.

<pre>
&lt;loadproperties&gt;
&lt;<a href="../CoreTypes/resources.html#gzipresource">gzipresource</a>&gt;
&lt;<a href="../CoreTypes/resources.html#url">url</a> url="http://example.org/url.properties.gz"/&gt;
&lt;/gzipresource&gt;
&lt;/loadproperties&gt;
</pre>
Load contents of http://example.org/url.properties.gz, uncompress it
on the fly and load the contents as Ant properties.

<hr>

<p align="center">Copyright &copy; 2002-2005 The Apache Software Foundation. All rights


+ 20
- 1
src/etc/testcases/taskdefs/loadproperties.xml View File

@@ -30,7 +30,7 @@ http.@SERVER@ = ${server}
value="http://${server1.http.server}:${server1.http.port}"/>
</target>

<target name="testPropertiesFromResource" depends="init">
<target name="write properties.tmp" depends="init">
<echo file="properties.tmp">
#tpfr.a=a
tpfr.a=A
@@ -38,6 +38,9 @@ tpfr.b=b\
e
tpfr.c=@C@
</echo>
</target>

<target name="testPropertiesFromResource" depends="write properties.tmp">
<loadproperties resource="properties.tmp" classpath="${basedir}">
<filterchain>
<replacetokens>
@@ -45,6 +48,22 @@ tpfr.c=@C@
</replacetokens>
</filterchain>
</loadproperties>
</target>

<target name="testPropertiesFromFileSet" depends="write properties.tmp">
<loadproperties>
<fileset dir="${basedir}">
<include name="properties.tmp"/>
</fileset>
<filterchain>
<replacetokens>
<token key="C" value="sea"/>
</replacetokens>
</filterchain>
</loadproperties>
</target>

<target name="loadPropertiesCheck">
<condition property="testPropertiesFromResource.ok">
<equals arg1="Abesea" arg2="${tpfr.a}${tpfr.b}${tpfr.c}" />
</condition>


+ 53
- 74
src/main/org/apache/tools/ant/taskdefs/LoadProperties.java View File

@@ -32,7 +32,12 @@ import org.apache.tools.ant.Task;
import org.apache.tools.ant.filters.util.ChainReaderHelper;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;
import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.ResourceCollection;
import org.apache.tools.ant.types.FilterChain;
import org.apache.tools.ant.types.resources.FileResource;
import org.apache.tools.ant.types.resources.JavaResource;
import org.apache.tools.ant.util.FileUtils;

/**
* Load a file's contents as Ant properties.
@@ -43,19 +48,9 @@ import org.apache.tools.ant.types.FilterChain;
public class LoadProperties extends Task {

/**
* Source file
* Source resource.
*/
private File srcFile = null;

/**
* Resource
*/
private String resource = null;

/**
* Classpath
*/
private Path classpath = null;
private Resource src = null;

/**
* Holds filterchains
@@ -73,7 +68,7 @@ public class LoadProperties extends Task {
* @param srcFile The new SrcFile value
*/
public final void setSrcFile(final File srcFile) {
this.srcFile = srcFile;
addConfigured(new FileResource(srcFile));
}

/**
@@ -82,7 +77,8 @@ public class LoadProperties extends Task {
* @param resource resource on classpath
*/
public void setResource(String resource) {
this.resource = resource;
assertSrcIsJavaResource();
((JavaResource) src).setName(resource);
}

/**
@@ -105,11 +101,8 @@ public class LoadProperties extends Task {
* @param classpath to add to any existing classpath
*/
public void setClasspath(Path classpath) {
if (this.classpath == null) {
this.classpath = classpath;
} else {
this.classpath.append(classpath);
}
assertSrcIsJavaResource();
((JavaResource) src).setClasspath(classpath);
}

/**
@@ -117,10 +110,8 @@ public class LoadProperties extends Task {
* @return The classpath to be configured
*/
public Path createClasspath() {
if (this.classpath == null) {
this.classpath = new Path(getProject());
}
return this.classpath.createPath();
assertSrcIsJavaResource();
return ((JavaResource) src).createClasspath();
}

/**
@@ -129,7 +120,8 @@ public class LoadProperties extends Task {
* @param r The reference value
*/
public void setClasspathRef(Reference r) {
createClasspath().setRefid(r);
assertSrcIsJavaResource();
((JavaResource) src).setClasspathRef(r);
}

/**
@@ -137,7 +129,8 @@ public class LoadProperties extends Task {
* @return The classpath
*/
public Path getClasspath() {
return classpath;
assertSrcIsJavaResource();
return ((JavaResource) src).getClasspath();
}

/**
@@ -147,48 +140,24 @@ public class LoadProperties extends Task {
*/
public final void execute() throws BuildException {
//validation
if (srcFile == null && resource == null) {
throw new BuildException(
"One of \"srcfile\" or \"resource\" is required.");
if (src == null) {
throw new BuildException("A source resource is required.");
}

BufferedInputStream bis = null;

if (srcFile != null) {
if (!srcFile.exists()) {
throw new BuildException("Source file does not exist :"+srcFile);
}

if (!srcFile.isFile()) {
throw new BuildException("Source file is not a file :"+srcFile);
}

try {
bis = new BufferedInputStream(new FileInputStream(srcFile));
} catch (IOException eyeOhEx) {
throw new BuildException(eyeOhEx);
}
} else {
ClassLoader cL = (classpath != null)
? getProject().createClassLoader(classpath)
: LoadProperties.class.getClassLoader();

InputStream is = (cL == null)
? ClassLoader.getSystemResourceAsStream(resource)
: cL.getResourceAsStream(resource);

if (is != null) {
bis = new BufferedInputStream(is);
} else { // do it like Property
log("Unable to find resource " + resource, Project.MSG_WARN);
if (!src.isExists()) {
if (src instanceof JavaResource) {
// dreaded backwards compatibility
log("Unable to find resource " + src, Project.MSG_WARN);
return;
}
throw new BuildException("Source resource does not exist: " + src);
}

BufferedInputStream bis = null;
Reader instream = null;
ByteArrayInputStream tis = null;

try {
bis = new BufferedInputStream(src.getInputStream());
if (encoding == null) {
instream = new InputStreamReader(bis);
} else {
@@ -224,23 +193,9 @@ public class LoadProperties extends Task {
} catch (final IOException ioe) {
final String message = "Unable to load file: " + ioe.toString();
throw new BuildException(message, ioe, getLocation());
} catch (final BuildException be) {
throw be;
} finally {
try {
if (bis != null) {
bis.close();
}
} catch (IOException ioex) {
//ignore
}
try {
if (tis != null) {
tis.close();
}
} catch (IOException ioex) {
//ignore
}
FileUtils.close(bis);
FileUtils.close(tis);
}
}

@@ -252,4 +207,28 @@ public class LoadProperties extends Task {
filterChains.addElement(filter);
}

/**
* Set the source resource.
* @param a the resource to load as a single element Resource collection.
* @since Ant 1.7
*/
public void addConfigured(ResourceCollection a) {
if (src != null) {
throw new BuildException("only a single source is supported");
}
if (a.size() != 1) {
throw new BuildException("only single argument resource collections"
+ " are supported");
}
src = (Resource) a.iterator().next();
}

private void assertSrcIsJavaResource() {
if (src == null) {
src = new JavaResource();
src.setProject(getProject());
} else if (!(src instanceof JavaResource)) {
throw new BuildException("expected a java resource as source");
}
}
}

+ 7
- 1
src/testcases/org/apache/tools/ant/taskdefs/LoadPropertiesTest.java View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002,2004 The Apache Software Foundation
* Copyright 2002,2004-2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -50,5 +50,11 @@ public class LoadPropertiesTest extends BuildFileTest {

public void testPropertiesFromResource() {
executeTarget("testPropertiesFromResource");
executeTarget("loadPropertiesCheck");
}

public void testPropertiesFromFileSet() {
executeTarget("testPropertiesFromFileSet");
executeTarget("loadPropertiesCheck");
}
}

Loading…
Cancel
Save