Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 260   Methods: 10
NCLOC: 88   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
StripLineComments.java 68.2% 82.1% 90% 78.9%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2002-2003 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 java.util.Vector;
 59   
 import org.apache.tools.ant.types.Parameter;
 60   
 
 61   
 /**
 62   
  * This filter strips line comments.
 63   
  *
 64   
  * Example:
 65   
  *
 66   
  * <pre>&lt;striplinecomments&gt;
 67   
  *   &lt;comment value=&quot;#&quot;/&gt;
 68   
  *   &lt;comment value=&quot;--&quot;/&gt;
 69   
  *   &lt;comment value=&quot;REM &quot;/&gt;
 70   
  *   &lt;comment value=&quot;rem &quot;/&gt;
 71   
  *   &lt;comment value=&quot;//&quot;/&gt;
 72   
  * &lt;/striplinecomments&gt;</pre>
 73   
  *
 74   
  * Or:
 75   
  *
 76   
  * <pre>&lt;filterreader classname=&quot;org.apache.tools.ant.filters.StripLineComments&quot;&gt;
 77   
  *   &lt;param type=&quot;comment&quot; value="#&quot;/&gt;
 78   
  *   &lt;param type=&quot;comment&quot; value=&quot;--&quot;/&gt;
 79   
  *   &lt;param type=&quot;comment&quot; value=&quot;REM &quot;/&gt;
 80   
  *   &lt;param type=&quot;comment&quot; value=&quot;rem &quot;/&gt;
 81   
  *   &lt;param type=&quot;comment&quot; value=&quot;//&quot;/&gt;
 82   
  * &lt;/filterreader&gt;</pre>
 83   
  *
 84   
  * @author Magesh Umasankar
 85   
  */
 86   
 public final class StripLineComments
 87   
     extends BaseParamFilterReader
 88   
     implements ChainableReader {
 89   
     /** Parameter name for the comment prefix. */
 90   
     private static final String COMMENTS_KEY = "comment";
 91   
 
 92   
     /** Vector that holds the comment prefixes. */
 93   
     private Vector comments = new Vector();
 94   
 
 95   
     /** The line that has been read ahead. */
 96   
     private String line = null;
 97   
 
 98   
     /**
 99   
      * Constructor for "dummy" instances.
 100   
      * 
 101   
      * @see BaseFilterReader#BaseFilterReader()
 102   
      */
 103  2
     public StripLineComments() {
 104  2
         super();
 105   
     }
 106   
 
 107   
     /**
 108   
      * Creates a new filtered reader.
 109   
      *
 110   
      * @param in A Reader object providing the underlying stream.
 111   
      *           Must not be <code>null</code>.
 112   
      */
 113  2
     public StripLineComments(final Reader in) {
 114  2
         super(in);
 115   
     }
 116   
 
 117   
     /**
 118   
      * Returns the next character in the filtered stream, only including
 119   
      * lines from the original stream which don't start with any of the 
 120   
      * specified comment prefixes.
 121   
      * 
 122   
      * @return the next character in the resulting stream, or -1
 123   
      * if the end of the resulting stream has been reached
 124   
      * 
 125   
      * @exception IOException if the underlying stream throws an IOException
 126   
      * during reading     
 127   
      */
 128  79
     public final int read() throws IOException {
 129  79
         if (!getInitialized()) {
 130  0
             initialize();
 131  0
             setInitialized(true);
 132   
         }
 133   
 
 134  79
         int ch = -1;
 135   
 
 136  79
         if (line != null) {
 137  67
             ch = line.charAt(0);
 138  67
             if (line.length() == 1) {
 139  6
                 line = null;
 140   
             } else {
 141  61
                 line = line.substring(1);
 142   
             }
 143   
         } else {
 144  12
             line = readLine();
 145  12
             final int commentsSize = comments.size();
 146   
 
 147  12
             while (line != null) {
 148  10
                 for (int i = 0; i < commentsSize; i++) {
 149  17
                     String comment = (String) comments.elementAt(i);
 150  17
                     if (line.startsWith(comment)) {
 151  4
                         line = null;
 152  4
                         break;
 153   
                     }
 154   
                 }
 155   
 
 156  10
                 if (line == null) {
 157   
                     // line started with comment
 158  4
                     line = readLine();
 159   
                 } else {
 160  6
                     break;
 161   
                 }
 162   
             }
 163   
 
 164  12
             if (line != null) {
 165  6
                 return read();
 166   
             }
 167   
         }
 168   
 
 169  73
         return ch;
 170   
     }
 171   
 
 172   
     /**
 173   
      * Adds a <code>comment</code> element to the list of prefixes.
 174   
      * 
 175   
      * @param comment The <code>comment</code> element to add to the
 176   
      * list of comment prefixes to strip. Must not be <code>null</code>.
 177   
      */
 178  4
     public final void addConfiguredComment(final Comment comment) {
 179  4
         comments.addElement(comment.getValue());
 180   
     }
 181   
 
 182   
     /**
 183   
      * Sets the list of comment prefixes to strip.
 184   
      * 
 185   
      * @param comments A list of strings, each of which is a prefix
 186   
      * for a comment line. Must not be <code>null</code>.
 187   
      */
 188  2
     private void setComments(final Vector comments) {
 189  2
         this.comments = comments;
 190   
     }
 191   
 
 192   
     /**
 193   
      * Returns the list of comment prefixes to strip.
 194   
      * 
 195   
      * @return the list of comment prefixes to strip.
 196   
      */
 197  2
     private final Vector getComments() {
 198  2
         return comments;
 199   
     }
 200   
 
 201   
     /**
 202   
      * Creates a new StripLineComments using the passed in
 203   
      * Reader for instantiation.
 204   
      * 
 205   
      * @param rdr A Reader object providing the underlying stream.
 206   
      *            Must not be <code>null</code>.
 207   
      * 
 208   
      * @return a new filter based on this configuration, but filtering
 209   
      *         the specified reader
 210   
      */
 211  2
     public final Reader chain(final Reader rdr) {
 212  2
         StripLineComments newFilter = new StripLineComments(rdr);
 213  2
         newFilter.setComments(getComments());
 214  2
         newFilter.setInitialized(true);
 215  2
         return newFilter;
 216   
     }
 217   
 
 218   
     /**
 219   
      * Parses the parameters to set the comment prefixes.
 220   
      */
 221  0
     private final void initialize() {
 222  0
         Parameter[] params = getParameters();
 223  0
         if (params != null) {
 224  0
             for (int i = 0; i < params.length; i++) {
 225  0
                 if (COMMENTS_KEY.equals(params[i].getType())) {
 226  0
                     comments.addElement(params[i].getValue());
 227   
                 }
 228   
             }
 229   
         }
 230   
     }
 231   
 
 232   
     /**
 233   
      * The class that holds a comment representation.
 234   
      */
 235   
     public static class Comment {
 236   
 
 237   
         /** The prefix for a line comment. */
 238   
         private String value;
 239   
 
 240   
         /**
 241   
          * Sets the prefix for this type of line comment.
 242   
          * 
 243   
          * @param comment The prefix for a line comment of this type.
 244   
          * Must not be <code>null</code>.
 245   
          */
 246  4
         public final void setValue(String comment) {
 247  4
             value = comment;
 248   
         }
 249   
 
 250   
         /**
 251   
          * Returns the prefix for this type of line comment.
 252   
          * 
 253   
          * @return the prefix for this type of line comment.
 254   
          */
 255  4
         public final String getValue() {
 256  4
             return value;
 257   
         }
 258   
     }
 259   
 }
 260