Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 193   Methods: 7
NCLOC: 62   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
TabsToSpaces.java 0% 0% 0% 0%
 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   
  * Converts tabs to spaces.
 62   
  *
 63   
  * Example:
 64   
  *
 65   
  * <pre>&lt;tabtospaces tablength=&quot;8&quot;/&gt;</pre>
 66   
  *
 67   
  * Or:
 68   
  *
 69   
  * <pre>&lt;filterreader classname=&quot;org.apache.tools.ant.filters.TabsToSpaces&quot;&gt;
 70   
  *   &lt;param name=&quot;tablength&quot; value=&quot;8&quot;/&gt;
 71   
  * &lt;/filterreader&gt;</pre>
 72   
  *
 73   
  * @author Magesh Umasankar
 74   
  */
 75   
 public final class TabsToSpaces
 76   
     extends BaseParamFilterReader
 77   
     implements ChainableReader {
 78   
     /** The default tab length. */
 79   
     private static final int DEFAULT_TAB_LENGTH = 8;
 80   
 
 81   
     /** Parameter name for the length of a tab. */
 82   
     private static final String TAB_LENGTH_KEY = "tablength";
 83   
 
 84   
     /** Tab length in this filter. */
 85   
     private int tabLength = DEFAULT_TAB_LENGTH;
 86   
 
 87   
     /** The number of spaces still to be read to represent the last-read tab. */
 88   
     private int spacesRemaining = 0;
 89   
 
 90   
     /**
 91   
      * Constructor for "dummy" instances.
 92   
      * 
 93   
      * @see BaseFilterReader#BaseFilterReader()
 94   
      */
 95  0
     public TabsToSpaces() {
 96  0
         super();
 97   
     }
 98   
 
 99   
     /**
 100   
      * Creates a new filtered reader.
 101   
      *
 102   
      * @param in A Reader object providing the underlying stream.
 103   
      *           Must not be <code>null</code>.
 104   
      */
 105  0
     public TabsToSpaces(final Reader in) {
 106  0
         super(in);
 107   
     }
 108   
 
 109   
     /**
 110   
      * Returns the next character in the filtered stream, converting tabs
 111   
      * to the specified number of spaces.
 112   
      * 
 113   
      * @return the next character in the resulting stream, or -1
 114   
      * if the end of the resulting stream has been reached
 115   
      * 
 116   
      * @exception IOException if the underlying stream throws an IOException
 117   
      * during reading     
 118   
      */
 119  0
     public final int read() throws IOException {
 120  0
         if (!getInitialized()) {
 121  0
             initialize();
 122  0
             setInitialized(true);
 123   
         }
 124   
 
 125  0
         int ch = -1;
 126   
 
 127  0
         if (spacesRemaining > 0) {
 128  0
             spacesRemaining--;
 129  0
             ch = ' ';
 130   
         } else {
 131  0
             ch = in.read();
 132  0
             if (ch == '\t') {
 133  0
                 spacesRemaining = tabLength - 1;
 134  0
                 ch = ' ';
 135   
             }
 136   
         }
 137  0
         return ch;
 138   
     }
 139   
 
 140   
     /**
 141   
      * Sets the tab length.
 142   
      * 
 143   
      * @param tabLength the number of spaces to be used when converting a tab.
 144   
      */
 145  0
     public final void setTablength(final int tabLength) {
 146  0
         this.tabLength = tabLength;
 147   
     }
 148   
 
 149   
     /**
 150   
      * Returns the tab length.
 151   
      * 
 152   
      * @return the number of spaces used when converting a tab
 153   
      */
 154  0
     private final int getTablength() {
 155  0
         return tabLength;
 156   
     }
 157   
 
 158   
     /**
 159   
      * Creates a new TabsToSpaces using the passed in
 160   
      * Reader for instantiation.
 161   
      * 
 162   
      * @param rdr A Reader object providing the underlying stream.
 163   
      *            Must not be <code>null</code>.
 164   
      * 
 165   
      * @return a new filter based on this configuration, but filtering
 166   
      *         the specified reader
 167   
      */
 168  0
     public final Reader chain(final Reader rdr) {
 169  0
         TabsToSpaces newFilter = new TabsToSpaces(rdr);
 170  0
         newFilter.setTablength(getTablength());
 171  0
         newFilter.setInitialized(true);
 172  0
         return newFilter;
 173   
     }
 174   
 
 175   
     /**
 176   
      * Parses the parameters to set the tab length.
 177   
      */
 178  0
     private final void initialize() {
 179  0
         Parameter[] params = getParameters();
 180  0
         if (params != null) {
 181  0
             for (int i = 0; i < params.length; i++) {
 182  0
                 if (params[i] != null) {
 183  0
                     if (TAB_LENGTH_KEY.equals(params[i].getName())) {
 184  0
                         tabLength =
 185   
                             new Integer(params[i].getValue()).intValue();
 186  0
                         break;
 187   
                     }
 188   
                 }
 189   
             }
 190   
         }
 191   
     }
 192   
 }
 193