Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 238   Methods: 6
NCLOC: 99   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
LoadFile.java 72.2% 93.5% 100% 88.6%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2001-2002 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 package org.apache.tools.ant.taskdefs;
 55   
 
 56   
 import java.io.BufferedInputStream;
 57   
 import java.io.File;
 58   
 import java.io.FileInputStream;
 59   
 import java.io.IOException;
 60   
 import java.io.InputStreamReader;
 61   
 import java.io.Reader;
 62   
 import java.util.Vector;
 63   
 import org.apache.tools.ant.BuildException;
 64   
 import org.apache.tools.ant.Project;
 65   
 import org.apache.tools.ant.Task;
 66   
 import org.apache.tools.ant.filters.util.ChainReaderHelper;
 67   
 import org.apache.tools.ant.types.FilterChain;
 68   
 
 69   
 /**
 70   
  * Load a file into a property
 71   
  *
 72   
  * @author Steve Loughran
 73   
  * @author Magesh Umasankar
 74   
  * @since Ant 1.5
 75   
  * @ant.task category="utility"
 76   
  */
 77   
 public final class LoadFile extends Task {
 78   
 
 79   
     /**
 80   
      * source file, usually null
 81   
      */
 82   
     private File srcFile = null;
 83   
 
 84   
     /**
 85   
      * what to do when it goes pear-shaped
 86   
      */
 87   
     private boolean failOnError = true;
 88   
 
 89   
     /**
 90   
      * Encoding to use for filenames, defaults to the platform's default
 91   
      * encoding.
 92   
      */
 93   
     private String encoding = null;
 94   
 
 95   
     /**
 96   
      * name of property
 97   
      */
 98   
     private String property = null;
 99   
 
 100   
     /**
 101   
      * Holds FilterChains
 102   
      */
 103   
     private final Vector filterChains = new Vector();
 104   
 
 105   
     /**
 106   
      * Encoding to use for input, defaults to the platform's default
 107   
      * encoding. <p>
 108   
      *
 109   
      * For a list of possible values see <a href="http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html">
 110   
      * http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html
 111   
      * </a>.</p>
 112   
      *
 113   
      * @param encoding The new Encoding value
 114   
      */
 115   
 
 116  1
     public final void setEncoding(final String encoding) {
 117  1
         this.encoding = encoding;
 118   
     }
 119   
 
 120   
 
 121   
     /**
 122   
      * Property name to save to.
 123   
      *
 124   
      * @param property The new Property value
 125   
      */
 126  10
     public final void setProperty(final String property) {
 127  10
         this.property = property;
 128   
     }
 129   
 
 130   
 
 131   
     /**
 132   
      * Sets the file to load.
 133   
      *
 134   
      * @param srcFile The new SrcFile value
 135   
      */
 136  10
     public final void setSrcFile(final File srcFile) {
 137  10
         this.srcFile = srcFile;
 138   
     }
 139   
 
 140   
 
 141   
     /**
 142   
      * If true, fail on load error.
 143   
      *
 144   
      * @param fail The new Failonerror value
 145   
      */
 146  1
     public final void setFailonerror(final boolean fail) {
 147  1
         failOnError = fail;
 148   
     }
 149   
 
 150   
 
 151   
     /**
 152   
      * read in a source file to a property
 153   
      *
 154   
      * @exception BuildException if something goes wrong with the build
 155   
      */
 156  11
     public final void execute()
 157   
         throws BuildException {
 158   
         //validation
 159  11
         if (srcFile == null) {
 160  1
             throw new BuildException("source file not defined");
 161   
         }
 162  10
         if (property == null) {
 163  1
             throw new BuildException("output property not defined");
 164   
         }
 165  9
         FileInputStream fis = null;
 166  9
         BufferedInputStream bis = null;
 167  9
         Reader instream = null;
 168  9
         log("loading " + srcFile + " into property " + property,
 169   
             Project.MSG_VERBOSE);
 170  9
         try {
 171  9
             final long len = srcFile.length();
 172  9
             log("file size = " + len, Project.MSG_DEBUG);
 173   
             //discard most of really big files
 174  9
             final int size = (int) len;
 175   
             //open up the file
 176  9
             fis = new FileInputStream(srcFile);
 177  7
             bis = new BufferedInputStream(fis);
 178  7
             if (encoding == null) {
 179  6
                 instream = new InputStreamReader(bis);
 180   
             } else {
 181  1
                 instream = new InputStreamReader(bis, encoding);
 182   
             }
 183   
 
 184  7
             String text = "";
 185  7
             if (size != 0) {
 186  7
                 ChainReaderHelper crh = new ChainReaderHelper();
 187  7
                 crh.setBufferSize(size);
 188  7
                 crh.setPrimaryReader(instream);
 189  7
                 crh.setFilterChains(filterChains);
 190  7
                 crh.setProject(getProject());
 191  7
                 instream = crh.getAssembledReader();
 192   
     
 193  7
                 text = crh.readFully(instream);
 194   
             }
 195   
 
 196  7
             if (text != null) {
 197  7
                 if (text.length() > 0) {
 198  7
                     getProject().setNewProperty(property, text);
 199  7
                     log("loaded " + text.length() + " characters",
 200   
                         Project.MSG_VERBOSE);
 201  7
                     log(property + " := " + text, Project.MSG_DEBUG);
 202   
                 }
 203   
             }
 204   
 
 205   
         } catch (final IOException ioe) {
 206  2
             final String message = "Unable to load file: " + ioe.toString();
 207  2
             if (failOnError) {
 208  1
                 throw new BuildException(message, ioe, getLocation());
 209   
             } else {
 210  1
                 log(message, Project.MSG_ERR);
 211   
             }
 212   
         } catch (final BuildException be) {
 213  0
             if (failOnError) {
 214  0
                 throw be;
 215   
             } else {
 216  0
                 log(be.getMessage(), Project.MSG_ERR);
 217   
             }
 218   
         } finally {
 219  9
             try {
 220  9
                 if (fis != null) {
 221  7
                     fis.close();
 222   
                 }
 223   
             } catch (IOException ioex) {
 224   
                 //ignore
 225   
             }
 226   
         }
 227   
     }
 228   
 
 229   
     /**
 230   
      * Add the FilterChain element.
 231   
      */
 232  4
     public final void addFilterChain(FilterChain filter) {
 233  4
         filterChains.addElement(filter);
 234   
     }
 235   
 
 236   
 //end class
 237   
 }
 238