Browse Source

new task loadresource

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@348914 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 19 years ago
parent
commit
5c23d3d1ca
8 changed files with 285 additions and 171 deletions
  1. +2
    -0
      WHATSNEW
  2. +10
    -4
      docs/manual/CoreTasks/loadfile.html
  3. +64
    -0
      docs/manual/CoreTasks/loadresource.html
  4. +1
    -0
      docs/manual/coretasklist.html
  5. +5
    -166
      src/main/org/apache/tools/ant/taskdefs/LoadFile.java
  6. +201
    -0
      src/main/org/apache/tools/ant/taskdefs/LoadResource.java
  7. +1
    -0
      src/main/org/apache/tools/ant/taskdefs/defaults.properties
  8. +1
    -1
      src/testcases/org/apache/tools/ant/taskdefs/LoadFileTest.java

+ 2
- 0
WHATSNEW View File

@@ -317,6 +317,8 @@ Other changes:
Bugzilla report 37604.


* New task loadresource that accompanies loadfile for non file resources.

Changes from Ant 1.6.4 to Ant 1.6.5
===================================



+ 10
- 4
docs/manual/CoreTasks/loadfile.html View File

@@ -10,9 +10,10 @@
<h2><a name="loadfile">LoadFile</a></h2>
<h3>Description</h3>
<p>
Load a text file into a single property. Unless an encoding is specified,
the encoding of the current locale is used.

Specialization of <a href="loadresource.html">loadresource</a> that
works on files exclusively and provides a srcFile attribute for
convenience. Unless an encoding is specified, the encoding of the
current locale is used.
</p>

<h3>Parameters</h3>
@@ -52,7 +53,12 @@ FilterChain</a>s.
srcFile="message.txt"/&gt;
</pre>
Load file message.txt into property "message"; an <tt>&lt;echo&gt;</tt>
can print this.
can print this. This is identical to
<pre> &lt;loadresource property="message"&gt;
&lt;file file="message.txt"/&gt;
&lt;/loadresource&gt;
</pre>
</p>

<pre> &lt;loadfile property="encoded-file"
srcFile="loadfile.xml"


+ 64
- 0
docs/manual/CoreTasks/loadresource.html View File

@@ -0,0 +1,64 @@
<html>
<head>
<link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
<title>LoadResource Task</title>
</head>

<body>


<h2><a name="loadresource">LoadResource</a></h2>
<h3>Description</h3>
<p>
Load a text resource into a single property. Unless an encoding is
specified, the encoding of the current locale is used. Resources to
load are specified as nested <a
href="../CoreTypes/resources.html">resource</a> elements or single
element resource collections.
</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">property</td>
<td valign="top">property to save to</td>
<td valign="top" align="center">Yes</td>
</tr>
<tr>
<td valign="top">encoding</td>
<td valign="top">encoding to use when loading the resource</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">failonerror</td>
<td valign="top">Whether to halt the build on failure</td>
<td align="center" valign="top">No, default "true"</td>
</tr>
</table>
<p>
The LoadResource task supports nested <a href="../CoreTypes/filterchain.html">
FilterChain</a>s.

<h3>Examples</h3>
<pre>
&lt;loadresource property="homepage"&gt;
&lt;url url="http://ant.apache.org/index.html"/&gt;
&lt;/loadresource&gt;
</pre>
Load the entry point of Ant's homepage into property "homepage"; an
<tt>&lt;echo&gt;</tt> can print this.

<p>For more examples see the <a href="loadfile.html">loadfile</a> task.</p>
<hr>

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

</body>
</html>


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

@@ -65,6 +65,7 @@
<a href="CoreTasks/libraries.html">Libraries</a><br>
<a href="CoreTasks/loadfile.html">LoadFile</a><br>
<a href="CoreTasks/loadproperties.html">LoadProperties</a><br>
<a href="CoreTasks/loadresource.html">LoadResource</a><br>
<a href="CoreTasks/makeurl.html">MakeURL</a><br>
<a href="CoreTasks/mail.html">Mail</a><br>
<a href="CoreTasks/macrodef.html">MacroDef</a><br>


+ 5
- 166
src/main/org/apache/tools/ant/taskdefs/LoadFile.java View File

@@ -1,5 +1,5 @@
/*
* Copyright 2001-2002,2004 The Apache Software Foundation
* Copyright 2001-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.
@@ -16,18 +16,9 @@
*/
package org.apache.tools.ant.taskdefs;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Vector;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.filters.util.ChainReaderHelper;
import org.apache.tools.ant.types.FilterChain;
import org.apache.tools.ant.types.resources.FileResource;;


/**
* Load a file into a property
@@ -35,60 +26,7 @@ import org.apache.tools.ant.types.FilterChain;
* @since Ant 1.5
* @ant.task category="utility"
*/
public class LoadFile extends Task {

/**
* source file, usually null
*/
private File srcFile = null;

/**
* what to do when it goes pear-shaped
*/
private boolean failOnError = true;

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

/**
* name of property
*/
private String property = null;

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

/**
* Encoding to use for input, defaults to the platform's default
* encoding. <p>
*
* For a list of possible values see
* <a href="http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html">
* http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html
* </a>.</p>
*
* @param encoding The new Encoding value
*/

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


/**
* Property name to save to.
*
* @param property The new Property value
*/
public final void setProperty(final String property) {
this.property = property;
}

public class LoadFile extends LoadResource {

/**
* Sets the file to load.
@@ -96,105 +34,6 @@ public class LoadFile extends Task {
* @param srcFile The new SrcFile value
*/
public final void setSrcFile(final File srcFile) {
this.srcFile = srcFile;
addConfigured(new FileResource(srcFile));
}


/**
* If true, fail on load error.
*
* @param fail The new Failonerror value
*/
public final void setFailonerror(final boolean fail) {
failOnError = fail;
}


/**
* read in a source file to a property
*
* @exception BuildException if something goes wrong with the build
*/
public final void execute()
throws BuildException {
//validation
if (srcFile == null) {
throw new BuildException("source file not defined");
}
if (property == null) {
throw new BuildException("output property not defined");
}
FileInputStream fis = null;
BufferedInputStream bis = null;
Reader instream = null;
log("loading " + srcFile + " into property " + property,
Project.MSG_VERBOSE);
try {
final long len = srcFile.length();
log("file size = " + len, Project.MSG_DEBUG);
//discard most of really big files
final int size = (int) len;
//open up the file
fis = new FileInputStream(srcFile);
bis = new BufferedInputStream(fis);
if (encoding == null) {
instream = new InputStreamReader(bis);
} else {
instream = new InputStreamReader(bis, encoding);
}

String text = "";
if (size != 0) {
ChainReaderHelper crh = new ChainReaderHelper();
crh.setBufferSize(size);
crh.setPrimaryReader(instream);
crh.setFilterChains(filterChains);
crh.setProject(getProject());
instream = crh.getAssembledReader();

text = crh.readFully(instream);
}

if (text != null) {
if (text.length() > 0) {
getProject().setNewProperty(property, text);
log("loaded " + text.length() + " characters",
Project.MSG_VERBOSE);
log(property + " := " + text, Project.MSG_DEBUG);
}
}

} catch (final IOException ioe) {
final String message = "Unable to load file: " + ioe.toString();
if (failOnError) {
throw new BuildException(message, ioe, getLocation());
} else {
log(message, Project.MSG_ERR);
}
} catch (final BuildException be) {
if (failOnError) {
throw be;
} else {
log(be.getMessage(), Project.MSG_ERR);
}
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException ioex) {
//ignore
}
}
}

/**
* Add the FilterChain element.
* @param filter the filter to add
*/
public final void addFilterChain(FilterChain filter) {
filterChains.addElement(filter);
}

//end class
}

+ 201
- 0
src/main/org/apache/tools/ant/taskdefs/LoadResource.java View File

@@ -0,0 +1,201 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.tools.ant.taskdefs;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Vector;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.filters.util.ChainReaderHelper;
import org.apache.tools.ant.types.FilterChain;
import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.ResourceCollection;
import org.apache.tools.ant.util.FileUtils;

/**
* Load a resource into a property
*
* @since Ant 1.7
* @ant.task category="utility"
*/
public class LoadResource extends Task {

/**
* The resource to load.
*/
private Resource src;

/**
* what to do when it goes pear-shaped
*/
private boolean failOnError = true;

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

/**
* name of property
*/
private String property = null;

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

/**
* Encoding to use for input, defaults to the platform's default
* encoding. <p>
*
* For a list of possible values see
* <a href="http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html">
* http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html
* </a>.</p>
*
* @param encoding The new Encoding value
*/

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


/**
* Property name to save to.
*
* @param property The new Property value
*/
public final void setProperty(final String property) {
this.property = property;
}

/**
* If true, fail on load error.
*
* @param fail The new Failonerror value
*/
public final void setFailonerror(final boolean fail) {
failOnError = fail;
}


/**
* read in a source file to a property
*
* @exception BuildException if something goes wrong with the build
*/
public final void execute()
throws BuildException {
//validation
if (src == null) {
throw new BuildException("source resource not defined");
}
if (property == null) {
throw new BuildException("output property not defined");
}
InputStream is = null;
BufferedInputStream bis = null;
Reader instream = null;
log("loading " + src + " into property " + property,
Project.MSG_VERBOSE);
try {
final long len = src.getSize();
log("resource size = "
+ (len != Resource.UNKNOWN_SIZE ? String.valueOf(len)
: "unknown"), Project.MSG_DEBUG);
//discard most of really big resources
final int size = (int) len;
//open up the resource
is = src.getInputStream();
bis = new BufferedInputStream(is);
if (encoding == null) {
instream = new InputStreamReader(bis);
} else {
instream = new InputStreamReader(bis, encoding);
}

String text = "";
if (size != 0) {
ChainReaderHelper crh = new ChainReaderHelper();
if (len != Resource.UNKNOWN_SIZE) {
crh.setBufferSize(size);
}
crh.setPrimaryReader(instream);
crh.setFilterChains(filterChains);
crh.setProject(getProject());
instream = crh.getAssembledReader();

text = crh.readFully(instream);
}

if (text != null) {
if (text.length() > 0) {
getProject().setNewProperty(property, text);
log("loaded " + text.length() + " characters",
Project.MSG_VERBOSE);
log(property + " := " + text, Project.MSG_DEBUG);
}
}

} catch (final IOException ioe) {
final String message = "Unable to load resource: "
+ ioe.toString();
if (failOnError) {
throw new BuildException(message, ioe, getLocation());
} else {
log(message, Project.MSG_ERR);
}
} catch (final BuildException be) {
if (failOnError) {
throw be;
} else {
log(be.getMessage(), Project.MSG_ERR);
}
} finally {
FileUtils.close(is);
}
}

/**
* Add the FilterChain element.
* @param filter the filter to add
*/
public final void addFilterChain(FilterChain filter) {
filterChains.addElement(filter);
}

/**
* Set the source resource.
* @param a the resource to load 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");
}
src = (Resource) a.iterator().next();
}

}

+ 1
- 0
src/main/org/apache/tools/ant/taskdefs/defaults.properties View File

@@ -44,6 +44,7 @@ length=org.apache.tools.ant.taskdefs.Length
libraries=org.apache.tools.ant.taskdefs.repository.Libraries
loadfile=org.apache.tools.ant.taskdefs.LoadFile
loadproperties=org.apache.tools.ant.taskdefs.LoadProperties
loadresource=org.apache.tools.ant.taskdefs.LoadResource
macrodef=org.apache.tools.ant.taskdefs.MacroDef
mail=org.apache.tools.ant.taskdefs.email.EmailTask
manifest=org.apache.tools.ant.taskdefs.ManifestTask


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

@@ -78,7 +78,7 @@ public class LoadFileTest extends BuildFileTest {
public void testNoSourcefilefound() {
expectBuildExceptionContaining("testNoSourcefilefound",
"File not found",
"Unable to load file");
"Unable to load resource");
}

/**


Loading…
Cancel
Save