Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 215   Methods: 4
NCLOC: 96   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
LoadProperties.java 60% 81.6% 75% 75.3%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 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.ByteArrayInputStream;
 58   
 import java.io.File;
 59   
 import java.io.FileInputStream;
 60   
 import java.io.IOException;
 61   
 import java.io.InputStreamReader;
 62   
 import java.io.Reader;
 63   
 import java.util.Enumeration;
 64   
 import java.util.Properties;
 65   
 import java.util.Vector;
 66   
 import org.apache.tools.ant.BuildException;
 67   
 import org.apache.tools.ant.Task;
 68   
 import org.apache.tools.ant.filters.util.ChainReaderHelper;
 69   
 import org.apache.tools.ant.types.FilterChain;
 70   
 
 71   
 /**
 72   
  * Load a file's contents as Ant properties.
 73   
  *
 74   
  * @author Magesh Umasankar
 75   
  * @since Ant 1.5
 76   
  * @ant.task category="utility"
 77   
  */
 78   
 public final class LoadProperties extends Task {
 79   
 
 80   
     /**
 81   
      * Source file
 82   
      */
 83   
     private File srcFile = null;
 84   
 
 85   
     /**
 86   
      * Holds filterchains
 87   
      */
 88   
     private final Vector filterChains = new Vector();
 89   
 
 90   
     /**
 91   
      * Encoding to use for filenames, defaults to the platform's default
 92   
      * encoding.
 93   
      */
 94   
     private String encoding = null;
 95   
 
 96   
     /**
 97   
      * Sets the file to load.
 98   
      *
 99   
      * @param srcFile The new SrcFile value
 100   
      */
 101  1
     public final void setSrcFile(final File srcFile) {
 102  1
         this.srcFile = srcFile;
 103   
     }
 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  0
     public final void setEncoding(final String encoding) {
 117  0
         this.encoding = encoding;
 118   
     }
 119   
 
 120   
     /**
 121   
      * read in a source file's contents and load them up as Ant properties
 122   
      *
 123   
      * @exception BuildException if something goes wrong with the build
 124   
      */
 125  1
     public final void execute() throws BuildException {
 126   
         //validation
 127  1
         if (srcFile == null) {
 128  0
             throw new BuildException("Source file not defined.");
 129   
         }
 130   
 
 131  1
         if (!srcFile.exists()) {
 132  0
             throw new BuildException("Source file does not exist.");
 133   
         }
 134   
 
 135  1
         if (!srcFile.isFile()) {
 136  0
             throw new BuildException("Source file is not a file.");
 137   
         }
 138   
 
 139  1
         FileInputStream fis = null;
 140  1
         BufferedInputStream bis = null;
 141  1
         Reader instream = null;
 142   
 
 143  1
         try {
 144  1
             final long len = srcFile.length();
 145  1
             final int size = (int) len;
 146   
 
 147   
             //open up the file
 148  1
             fis = new FileInputStream(srcFile);
 149  1
             bis = new BufferedInputStream(fis);
 150  1
             if (encoding == null) {
 151  1
                 instream = new InputStreamReader(bis);
 152   
             } else {
 153  0
                 instream = new InputStreamReader(bis, encoding);
 154   
             }
 155   
 
 156  1
             ChainReaderHelper crh = new ChainReaderHelper();
 157  1
             crh.setBufferSize(size);
 158  1
             crh.setPrimaryReader(instream);
 159  1
             crh.setFilterChains(filterChains);
 160  1
             crh.setProject(getProject());
 161  1
             instream = crh.getAssembledReader();
 162   
 
 163  1
             String text = crh.readFully(instream);
 164   
 
 165  1
             if (text != null) {
 166  1
                 if (!text.endsWith("\n")) {
 167  1
                     text = text + "\n";
 168   
                 }
 169   
 
 170  1
                 ByteArrayInputStream tis = null;
 171  1
                 if ( encoding == null ) {
 172  1
                     tis = new ByteArrayInputStream(text.getBytes());
 173   
                 } else {
 174  0
                     tis = new ByteArrayInputStream(text.getBytes(encoding));
 175   
                 }
 176  1
                 final Properties props = new Properties();
 177  1
                 props.load(tis);
 178  1
                 final Enumeration e = props.keys();
 179  1
                 while (e.hasMoreElements()) {
 180  3
                     final String key = (String) e.nextElement();
 181  3
                     final String value = props.getProperty(key);
 182  3
                     if (key != null && value != null
 183   
                             && value.trim().length() > 0) {
 184  2
                         getProject().setNewProperty(key, value);
 185   
                     }
 186   
                 }
 187  1
                 tis.close();
 188   
             }
 189   
 
 190   
         } catch (final IOException ioe) {
 191  0
             final String message = "Unable to load file: " + ioe.toString();
 192  0
             throw new BuildException(message, ioe, getLocation());
 193   
         } catch (final BuildException be) {
 194  0
             throw be;
 195   
         } finally {
 196  1
             try {
 197  1
                 if (fis != null) {
 198  1
                     fis.close();
 199   
                 }
 200   
             } catch (IOException ioex) {
 201   
                 //ignore
 202   
             }
 203   
         }
 204   
     }
 205   
 
 206   
     /**
 207   
      * Adds a FilterChain.
 208   
      */
 209  1
     public final void addFilterChain(FilterChain filter) {
 210  1
         filterChains.addElement(filter);
 211   
     }
 212   
 
 213   
 //end class
 214   
 }
 215