Browse Source

Add resource support to <loadproperties>.

PR: 28340


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@276365 13f79535-47bb-0310-9956-ffa450edef68
master
Matthew Jason Benson 21 years ago
parent
commit
b6298168c9
5 changed files with 147 additions and 24 deletions
  1. +2
    -0
      WHATSNEW
  2. +20
    -3
      docs/manual/CoreTasks/loadproperties.html
  3. +23
    -0
      src/etc/testcases/taskdefs/loadproperties.xml
  4. +98
    -21
      src/main/org/apache/tools/ant/taskdefs/LoadProperties.java
  5. +4
    -0
      src/testcases/org/apache/tools/ant/taskdefs/LoadPropertiesTest.java

+ 2
- 0
WHATSNEW View File

@@ -55,6 +55,8 @@ Other changes:
* New <redirector> type introduced to provide extreme I/O flexibility. * New <redirector> type introduced to provide extreme I/O flexibility.
Initial support for <exec>, <apply>, and <java> tasks. Initial support for <exec>, <apply>, and <java> tasks.


* <loadproperties> supports loading from a resource.

Changes from Ant 1.6.1 to current Ant 1.6 CVS version Changes from Ant 1.6.1 to current Ant 1.6 CVS version
============================================= =============================================




+ 20
- 3
docs/manual/CoreTasks/loadproperties.html View File

@@ -10,7 +10,7 @@
<h3>Description</h3> <h3>Description</h3>
<p> <p>
Load a file's contents as Ant properties. This is equivalent Load a file's contents as Ant properties. This is equivalent
to &lt;property file=&quot;...&quot;/&gt; except that it
to &lt;property file|resource=&quot;...&quot;/&gt; except that it
supports nested &lt;filterchain&gt; elements. supports nested &lt;filterchain&gt; elements.
</p> </p>


@@ -29,17 +29,34 @@ filter.</p>
<tr> <tr>
<td valign="top">srcFile</td> <td valign="top">srcFile</td>
<td valign="top">source file</td> <td valign="top">source file</td>
<td valign="top" align="center">Yes</td>
<td valign="top" rowspan="2" align="center">One of these</td>
</tr>
<tr>
<td valign="top">resource</td>
<td valign="top">the resource name of the property file</td>
</tr> </tr>
<tr> <tr>
<td valign="top">encoding</td> <td valign="top">encoding</td>
<td valign="top">encoding to use when loading the file</td> <td valign="top">encoding to use when loading the file</td>
<td align="center" valign="top">No</td> <td align="center" valign="top">No</td>
</tr> </tr>
<tr>
<td valign="top">classpath</td>
<td valign="top">the classpath to use when looking up a resource.</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">classpathref</td>
<td valign="top">the classpath to use when looking up a resource,
given as <a href="../using.html#references">reference</a>
to a &lt;path&gt; defined elsewhere..</td>
<td align="center" valign="top">No</td>
</tr>
</table> </table>
<p> <p>
The LoadProperties task supports nested <a href="../CoreTypes/filterchain.html"> The LoadProperties task supports nested <a href="../CoreTypes/filterchain.html">
FilterChain</a>s.
FilterChain</a>s, as well as a nested <code>&lt;classpath&gt;</code>
element for use with the <i>resource</i> attribute.


<h3>Examples</h3> <h3>Examples</h3>
<pre> &lt;loadproperties srcFile="file.properties"/&gt; <pre> &lt;loadproperties srcFile="file.properties"/&gt;


+ 23
- 0
src/etc/testcases/taskdefs/loadproperties.xml View File

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


<target name="testPropertiesFromResource" depends="init">
<echo file="properties.tmp">
#tpfr.a=a
tpfr.a=A
tpfr.b=b\
e
tpfr.c=@C@
</echo>
<loadproperties resource="properties.tmp" classpath="${basedir}">
<filterchain>
<replacetokens>
<token key="C" value="sea"/>
</replacetokens>
</filterchain>
</loadproperties>
<condition property="testPropertiesFromResource.ok">
<equals arg1="Abesea" arg2="${tpfr.a}${tpfr.b}${tpfr.c}" />
</condition>
<fail unless="testPropertiesFromResource.ok">
$${tpfr.a}$${tpfr.b}$${tpfr.c}=&quot;${tpfr.a}${tpfr.b}${tpfr.c}&quot;
</fail>
</target>

<target name="cleanup"> <target name="cleanup">
<delete file="properties.tmp"/> <delete file="properties.tmp"/>
</target> </target>


+ 98
- 21
src/main/org/apache/tools/ant/taskdefs/LoadProperties.java View File

@@ -19,6 +19,7 @@ package org.apache.tools.ant.taskdefs;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.File; import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@@ -26,9 +27,12 @@ import java.io.Reader;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Properties; import java.util.Properties;
import java.util.Vector; import java.util.Vector;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.BuildException; import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task; import org.apache.tools.ant.Task;
import org.apache.tools.ant.filters.util.ChainReaderHelper; 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.FilterChain; import org.apache.tools.ant.types.FilterChain;


/** /**
@@ -44,19 +48,28 @@ public final class LoadProperties extends Task {
*/ */
private File srcFile = null; private File srcFile = null;


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

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

/** /**
* Holds filterchains * Holds filterchains
*/ */
private final Vector filterChains = new Vector(); private final Vector filterChains = new Vector();


/** /**
* Encoding to use for filenames, defaults to the platform's default
* encoding.
* Encoding to use for input; defaults to the platform's default encoding.
*/ */
private String encoding = null; private String encoding = null;


/** /**
* Sets the file to load.
* Set the file to load.
* *
* @param srcFile The new SrcFile value * @param srcFile The new SrcFile value
*/ */
@@ -64,6 +77,15 @@ public final class LoadProperties extends Task {
this.srcFile = srcFile; this.srcFile = srcFile;
} }


/**
* Set the resource name of a property file to load.
*
* @param resource resource on classpath
*/
public void setResource(String resource) {
this.resource = resource;
}

/** /**
* Encoding to use for input, defaults to the platform's default * Encoding to use for input, defaults to the platform's default
* encoding. <p> * encoding. <p>
@@ -75,41 +97,96 @@ public final class LoadProperties extends Task {
* *
* @param encoding The new Encoding value * @param encoding The new Encoding value
*/ */

public final void setEncoding(final String encoding) { public final void setEncoding(final String encoding) {
this.encoding = encoding; this.encoding = encoding;
} }


/** /**
* read in a source file's contents and load them up as Ant properties
* Set the classpath to use when looking up a resource.
* @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);
}
}

/**
* Add a classpath to use when looking up a resource.
*/
public Path createClasspath() {
if (this.classpath == null) {
this.classpath = new Path(getProject());
}
return this.classpath.createPath();
}

/**
* Set the classpath to use when looking up a resource,
* given as reference to a &lt;path&gt; defined elsewhere
*/
public void setClasspathRef(Reference r) {
createClasspath().setRefid(r);
}

/**
* get the classpath used by this <CODE>LoadProperties</CODE>.
*/
public Path getClasspath() {
return classpath;
}

/**
* load Ant properties from the source file or resource
* *
* @exception BuildException if something goes wrong with the build * @exception BuildException if something goes wrong with the build
*/ */
public final void execute() throws BuildException { public final void execute() throws BuildException {
//validation //validation
if (srcFile == null) {
throw new BuildException("Source file not defined.");
if (srcFile == null && resource == null) {
throw new BuildException(
"One of \"srcfile\" or \"resource\" is required.");
} }


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

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

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


if (!srcFile.isFile()) {
throw new BuildException("Source file is not a file.");
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);
return;
}
} }


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


try { try {
final long len = srcFile.length();

//open up the file
fis = new FileInputStream(srcFile);
bis = new BufferedInputStream(fis);
if (encoding == null) { if (encoding == null) {
instream = new InputStreamReader(bis); instream = new InputStreamReader(bis);
} else { } else {
@@ -150,8 +227,8 @@ public final class LoadProperties extends Task {
throw be; throw be;
} finally { } finally {
try { try {
if (fis != null) {
fis.close();
if (bis != null) {
bis.close();
} }
} catch (IOException ioex) { } catch (IOException ioex) {
//ignore //ignore


+ 4
- 0
src/testcases/org/apache/tools/ant/taskdefs/LoadPropertiesTest.java View File

@@ -47,4 +47,8 @@ public class LoadPropertiesTest extends BuildFileTest {
String url = project.getProperty("server1.http.url"); String url = project.getProperty("server1.http.url");
assertEquals("http://localhost:80", url); assertEquals("http://localhost:80", url);
} }

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

Loading…
Cancel
Save