From 5c23d3d1ca65bf9d163712c9c6de72e6c3201f75 Mon Sep 17 00:00:00 2001
From: Stefan Bodewig
- Load a text file into a single property. Unless an encoding is specified,
- the encoding of the current locale is used.
-
+Specialization of loadresource 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.
LoadFile
Description
Parameters
@@ -52,7 +53,12 @@ FilterChains.
srcFile="message.txt"/>
Load file message.txt into property "message"; an <echo>
-can print this.
+can print this. This is identical to
+ <loadresource property="message">
+ <file file="message.txt"/>
+ </loadresource>
+
+
<loadfile property="encoded-file" srcFile="loadfile.xml" diff --git a/docs/manual/CoreTasks/loadresource.html b/docs/manual/CoreTasks/loadresource.html new file mode 100755 index 000000000..d244332e5 --- /dev/null +++ b/docs/manual/CoreTasks/loadresource.html @@ -0,0 +1,64 @@ + + + +LoadResource Task + + + + + +LoadResource
+Description
++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 resource elements or single +element resource collections. +
+ +Parameters
+
Attribute | +Description | +Required | +
property | +property to save to | +Yes | +
encoding | +encoding to use when loading the resource | +No | +
failonerror | +Whether to halt the build on failure | +No, default "true" | +
+The LoadResource task supports nested +FilterChains. + +
+<loadresource property="homepage"> + <url url="http://ant.apache.org/index.html"/> +</loadresource> ++Load the entry point of Ant's homepage into property "homepage"; an +<echo> can print this. + +
For more examples see the loadfile task.
+Copyright © 2001-2002,2004-2005 The Apache Software Foundation. All rights +Reserved.
+ + + + diff --git a/docs/manual/coretasklist.html b/docs/manual/coretasklist.html index b97e1c288..5e4609f53 100644 --- a/docs/manual/coretasklist.html +++ b/docs/manual/coretasklist.html @@ -65,6 +65,7 @@ Libraries- * - * For a list of possible values see - * - * http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html - * .
- * - * @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 } diff --git a/src/main/org/apache/tools/ant/taskdefs/LoadResource.java b/src/main/org/apache/tools/ant/taskdefs/LoadResource.java new file mode 100755 index 000000000..f17722d38 --- /dev/null +++ b/src/main/org/apache/tools/ant/taskdefs/LoadResource.java @@ -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.+ * + * For a list of possible values see + * + * http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html + * .
+ * + * @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(); + } + +} diff --git a/src/main/org/apache/tools/ant/taskdefs/defaults.properties b/src/main/org/apache/tools/ant/taskdefs/defaults.properties index ee0d59953..89675ef1b 100644 --- a/src/main/org/apache/tools/ant/taskdefs/defaults.properties +++ b/src/main/org/apache/tools/ant/taskdefs/defaults.properties @@ -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 diff --git a/src/testcases/org/apache/tools/ant/taskdefs/LoadFileTest.java b/src/testcases/org/apache/tools/ant/taskdefs/LoadFileTest.java index 327d968a9..dd2ed5db6 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/LoadFileTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/LoadFileTest.java @@ -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"); } /**