Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 201   Methods: 7
NCLOC: 68   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
PrefixLines.java 50% 71.9% 85.7% 66.7%
 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.filters;
 55   
 
 56   
 import java.io.IOException;
 57   
 import java.io.Reader;
 58   
 import org.apache.tools.ant.types.Parameter;
 59   
 
 60   
 /**
 61   
  * Attaches a prefix to every line.
 62   
  *
 63   
  * Example:
 64   
  * <pre>&lt;prefixlines prefix=&quot;Foo&quot;/&gt;</pre>
 65   
  *
 66   
  * Or:
 67   
  *
 68   
  * <pre>&lt;filterreader classname=&quot;org.apache.tools.ant.filters.PrefixLines&quot;&gt;
 69   
  *  &lt;param name=&quot;prefix&quot; value=&quot;Foo&quot;/&gt;
 70   
  * &lt;/filterreader&gt;</pre>
 71   
  *
 72   
  * @author Magesh Umasankar
 73   
  */
 74   
 public final class PrefixLines
 75   
     extends BaseParamFilterReader
 76   
     implements ChainableReader {
 77   
     /** Parameter name for the prefix. */
 78   
     private static final String PREFIX_KEY = "prefix";
 79   
 
 80   
     /** The prefix to be used. */
 81   
     private String prefix = null;
 82   
 
 83   
     /** Data that must be read from, if not null. */
 84   
     private String queuedData = null;
 85   
 
 86   
     /**
 87   
      * Constructor for "dummy" instances.
 88   
      * 
 89   
      * @see BaseFilterReader#BaseFilterReader()
 90   
      */
 91  1
     public PrefixLines() {
 92  1
         super();
 93   
     }
 94   
 
 95   
     /**
 96   
      * Creates a new filtered reader.
 97   
      *
 98   
      * @param in A Reader object providing the underlying stream.
 99   
      *           Must not be <code>null</code>.
 100   
      */
 101  1
     public PrefixLines(final Reader in) {
 102  1
         super(in);
 103   
     }
 104   
 
 105   
     /**
 106   
      * Returns the next character in the filtered stream. One line is read
 107   
      * from the original input, and the prefix added. The resulting
 108   
      * line is then used until it ends, at which point the next original line
 109   
      * is read, etc.
 110   
      * 
 111   
      * @return the next character in the resulting stream, or -1
 112   
      * if the end of the resulting stream has been reached
 113   
      * 
 114   
      * @exception IOException if the underlying stream throws an IOException
 115   
      * during reading     
 116   
      */
 117  88
     public final int read() throws IOException {
 118  88
         if (!getInitialized()) {
 119  0
             initialize();
 120  0
             setInitialized(true);
 121   
         }
 122   
 
 123  88
         int ch = -1;
 124   
 
 125  88
         if (queuedData != null && queuedData.length() == 0) {
 126  0
             queuedData = null;
 127   
         }
 128   
 
 129  88
         if (queuedData != null) {
 130  80
             ch = queuedData.charAt(0);
 131  80
             queuedData = queuedData.substring(1);
 132  80
             if (queuedData.length() == 0) {
 133  4
                 queuedData = null;
 134   
             }
 135   
         } else {
 136  8
             queuedData = readLine();
 137  8
             if (queuedData == null) {
 138  4
                 ch = -1;
 139   
             } else {
 140  4
                 if (prefix != null) {
 141  4
                     queuedData = prefix + queuedData;
 142   
                 }
 143  4
                 return read();
 144   
             }
 145   
         }
 146  84
         return ch;
 147   
     }
 148   
 
 149   
     /**
 150   
      * Sets the prefix to add at the start of each input line.
 151   
      * 
 152   
      * @param prefix The prefix to add at the start of each input line.
 153   
      *               May be <code>null</code>, in which case no prefix
 154   
      *               is added.
 155   
      */
 156  2
     public final void setPrefix(final String prefix) {
 157  2
         this.prefix = prefix;
 158   
     }
 159   
 
 160   
     /**
 161   
      * Returns the prefix which will be added at the start of each input line.
 162   
      * 
 163   
      * @return the prefix which will be added at the start of each input line
 164   
      */
 165  1
     private final String getPrefix() {
 166  1
         return prefix;
 167   
     }
 168   
 
 169   
     /**
 170   
      * Creates a new PrefixLines filter using the passed in
 171   
      * Reader for instantiation.
 172   
      * 
 173   
      * @param rdr A Reader object providing the underlying stream.
 174   
      *            Must not be <code>null</code>.
 175   
      * 
 176   
      * @return a new filter based on this configuration, but filtering
 177   
      *         the specified reader
 178   
      */
 179  1
     public final Reader chain(final Reader rdr) {
 180  1
         PrefixLines newFilter = new PrefixLines(rdr);
 181  1
         newFilter.setPrefix(getPrefix());
 182  1
         newFilter.setInitialized(true);
 183  1
         return newFilter;
 184   
     }
 185   
 
 186   
     /**
 187   
      * Initializes the prefix if it is available from the parameters.
 188   
      */
 189  0
     private final void initialize() {
 190  0
         Parameter[] params = getParameters();
 191  0
         if (params != null) {
 192  0
             for (int i = 0; i < params.length; i++) {
 193  0
                 if (PREFIX_KEY.equals(params[i].getName())) {
 194  0
                     prefix = params[i].getValue();
 195  0
                     break;
 196   
                 }
 197   
             }
 198   
         }
 199   
     }
 200   
 }
 201