Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 181   Methods: 4
NCLOC: 65   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
StripJavaComments.java 91.7% 97.1% 100% 95.2%
 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   
 
 59   
 /**
 60   
  * This is a Java comment and string stripper reader that filters
 61   
  * those lexical tokens out for purposes of simple Java parsing.
 62   
  * (if you have more complex Java parsing needs, use a real lexer).
 63   
  * Since this class heavily relies on the single char read function,
 64   
  * you are reccomended to make it work on top of a buffered reader.
 65   
  */
 66   
 public final class StripJavaComments
 67   
     extends BaseFilterReader
 68   
     implements ChainableReader {
 69   
         
 70   
     /** 
 71   
      * The read-ahead character, used for effectively pushing a single
 72   
      * character back. A value of -1 indicates that no character is in the 
 73   
      * buffer.
 74   
      */
 75   
     private int readAheadCh = -1;
 76   
 
 77   
     /** 
 78   
      * Whether or not the parser is currently in the middle of a string
 79   
      * literal.
 80   
      */
 81   
     private boolean inString = false;
 82   
 
 83   
     /** 
 84   
      * Whether or not the last char has been a backslash.
 85   
      */
 86   
     private boolean quoted = false;
 87   
 
 88   
     /**
 89   
      * Constructor for "dummy" instances.
 90   
      * 
 91   
      * @see BaseFilterReader#BaseFilterReader()
 92   
      */
 93  1
     public StripJavaComments() {
 94  1
         super();
 95   
     }
 96   
 
 97   
     /**
 98   
      * Creates a new filtered reader.
 99   
      *
 100   
      * @param in A Reader object providing the underlying stream.
 101   
      *           Must not be <code>null</code>.
 102   
      */
 103  2
     public StripJavaComments(final Reader in) {
 104  2
         super(in);
 105   
     }
 106   
 
 107   
     /**
 108   
      * Returns the next character in the filtered stream, not including
 109   
      * Java comments.
 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  485
     public final int read() throws IOException {
 118  485
         int ch = -1;
 119  485
         if (readAheadCh != -1) {
 120  1
             ch = readAheadCh;
 121  1
             readAheadCh = -1;
 122   
         } else {
 123  484
             ch = in.read();
 124  484
             if (ch == '"' && !quoted) {
 125  8
                 inString = !inString;
 126  8
                 quoted = false;
 127  476
             } else if (ch == '\\') {
 128  2
                 quoted = !quoted;
 129   
             } else {
 130  474
                 quoted = false;
 131  474
                 if (!inString) {
 132  410
                     if (ch == '/') {
 133  11
                         ch = in.read();
 134  11
                         if (ch == '/') {
 135  5
                             while (ch != '\n' && ch != -1) {
 136  106
                                 ch = in.read();
 137   
                             }
 138  6
                         } else if (ch == '*') {
 139  211
                             while (ch != -1) {
 140  211
                                 ch = in.read();
 141  211
                                 if (ch == '*') {
 142  12
                                     ch = in.read();
 143  12
                                     while (ch == '*' && ch != -1) {
 144  0
                                         ch = in.read();
 145   
                                     }
 146   
 
 147  12
                                     if (ch == '/') {
 148  5
                                         ch = read();
 149  5
                                         break;
 150   
                                     }
 151   
                                 }
 152   
                             }
 153   
                         } else {
 154  1
                             readAheadCh = ch;
 155  1
                             ch = '/';
 156   
                         }
 157   
                     }
 158   
                 }
 159   
             }
 160   
         }
 161   
 
 162  485
         return ch;
 163   
     }
 164   
 
 165   
     /**
 166   
      * Creates a new StripJavaComments using the passed in
 167   
      * Reader for instantiation.
 168   
      * 
 169   
      * @param rdr A Reader object providing the underlying stream.
 170   
      *            Must not be <code>null</code>.
 171   
      * 
 172   
      * @return a new filter based on this configuration, but filtering
 173   
      *         the specified reader
 174   
      */
 175   
 
 176  1
     public final Reader chain(final Reader rdr) {
 177  1
         StripJavaComments newFilter = new StripJavaComments(rdr);
 178  1
         return newFilter;
 179   
     }
 180   
 }
 181