ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r348914 - in /ant/core/trunk: ./ docs/manual/ docs/manual/CoreTasks/ src/main/org/apache/tools/ant/taskdefs/ src/testcases/org/apache/tools/ant/taskdefs/
Date Fri, 25 Nov 2005 08:48:03 GMT
Author: bodewig
Date: Fri Nov 25 00:47:50 2005
New Revision: 348914

URL: http://svn.apache.org/viewcvs?rev=348914&view=rev
Log:
new task loadresource

Added:
    ant/core/trunk/docs/manual/CoreTasks/loadresource.html   (with props)
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/LoadResource.java   (with props)
Modified:
    ant/core/trunk/WHATSNEW
    ant/core/trunk/docs/manual/CoreTasks/loadfile.html
    ant/core/trunk/docs/manual/coretasklist.html
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/LoadFile.java
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/defaults.properties
    ant/core/trunk/src/testcases/org/apache/tools/ant/taskdefs/LoadFileTest.java

Modified: ant/core/trunk/WHATSNEW
URL: http://svn.apache.org/viewcvs/ant/core/trunk/WHATSNEW?rev=348914&r1=348913&r2=348914&view=diff
==============================================================================
--- ant/core/trunk/WHATSNEW (original)
+++ ant/core/trunk/WHATSNEW Fri Nov 25 00:47:50 2005
@@ -317,6 +317,8 @@
   Bugzilla report 37604.
 
 
+* New task loadresource that accompanies loadfile for non file resources.
+
 Changes from Ant 1.6.4 to Ant 1.6.5
 ===================================
 

Modified: ant/core/trunk/docs/manual/CoreTasks/loadfile.html
URL: http://svn.apache.org/viewcvs/ant/core/trunk/docs/manual/CoreTasks/loadfile.html?rev=348914&r1=348913&r2=348914&view=diff
==============================================================================
--- ant/core/trunk/docs/manual/CoreTasks/loadfile.html (original)
+++ ant/core/trunk/docs/manual/CoreTasks/loadfile.html Fri Nov 25 00:47:50 2005
@@ -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 @@
       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"

Added: ant/core/trunk/docs/manual/CoreTasks/loadresource.html
URL: http://svn.apache.org/viewcvs/ant/core/trunk/docs/manual/CoreTasks/loadresource.html?rev=348914&view=auto
==============================================================================
--- ant/core/trunk/docs/manual/CoreTasks/loadresource.html (added)
+++ ant/core/trunk/docs/manual/CoreTasks/loadresource.html Fri Nov 25 00:47:50 2005
@@ -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>
+

Propchange: ant/core/trunk/docs/manual/CoreTasks/loadresource.html
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ant/core/trunk/docs/manual/CoreTasks/loadresource.html
------------------------------------------------------------------------------
    svn:executable = *

Modified: ant/core/trunk/docs/manual/coretasklist.html
URL: http://svn.apache.org/viewcvs/ant/core/trunk/docs/manual/coretasklist.html?rev=348914&r1=348913&r2=348914&view=diff
==============================================================================
--- ant/core/trunk/docs/manual/coretasklist.html (original)
+++ ant/core/trunk/docs/manual/coretasklist.html Fri Nov 25 00:47:50 2005
@@ -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>

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/LoadFile.java
URL: http://svn.apache.org/viewcvs/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/LoadFile.java?rev=348914&r1=348913&r2=348914&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/LoadFile.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/LoadFile.java Fri Nov 25 00:47:50
2005
@@ -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 @@
  * @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 @@
      * @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
 }

Added: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/LoadResource.java
URL: http://svn.apache.org/viewcvs/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/LoadResource.java?rev=348914&view=auto
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/LoadResource.java (added)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/LoadResource.java Fri Nov 25 00:47:50
2005
@@ -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();
+    }
+
+}

Propchange: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/LoadResource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/LoadResource.java
------------------------------------------------------------------------------
    svn:executable = *

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/defaults.properties
URL: http://svn.apache.org/viewcvs/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/defaults.properties?rev=348914&r1=348913&r2=348914&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/defaults.properties (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/defaults.properties Fri Nov 25 00:47:50
2005
@@ -44,6 +44,7 @@
 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

Modified: ant/core/trunk/src/testcases/org/apache/tools/ant/taskdefs/LoadFileTest.java
URL: http://svn.apache.org/viewcvs/ant/core/trunk/src/testcases/org/apache/tools/ant/taskdefs/LoadFileTest.java?rev=348914&r1=348913&r2=348914&view=diff
==============================================================================
--- ant/core/trunk/src/testcases/org/apache/tools/ant/taskdefs/LoadFileTest.java (original)
+++ ant/core/trunk/src/testcases/org/apache/tools/ant/taskdefs/LoadFileTest.java Fri Nov 25
00:47:50 2005
@@ -78,7 +78,7 @@
     public void testNoSourcefilefound() {
         expectBuildExceptionContaining("testNoSourcefilefound",
                 "File not found",
-                "Unable to load file");
+                "Unable to load resource");
     }
 
     /**



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message