Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 246   Methods: 8
NCLOC: 87   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
LineContainsRegExp.java 0% 0% 0% 0%
 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   
 import org.apache.tools.ant.types.RegularExpression;
 61   
 import org.apache.tools.ant.util.regexp.Regexp;
 62   
 
 63   
 /**
 64   
  * Filter which includes only those lines that contain the user-specified
 65   
  * regular expression matching strings.
 66   
  *
 67   
  * Example:
 68   
  * <pre>&lt;linecontainsregexp&gt;
 69   
  *   &lt;regexp pattern=&quot;foo*&quot;&gt;
 70   
  * &lt;/linecontainsregexp&gt;</pre>
 71   
  *
 72   
  * Or:
 73   
  *
 74   
  * <pre>&lt;filterreader classname=&quot;org.apache.tools.ant.filters.LineContainsRegExp&quot;&gt;
 75   
  *    &lt;param type=&quot;regexp&quot; value=&quot;foo*&quot;/&gt;
 76   
  * &lt;/filterreader&gt;</pre>
 77   
  *
 78   
  * This will fetch all those lines that contain the pattern <code>foo</code>
 79   
  *
 80   
  * @author Magesh Umasankar
 81   
  */
 82   
 public final class LineContainsRegExp
 83   
     extends BaseParamFilterReader
 84   
     implements ChainableReader {
 85   
     /** Parameter name for the regular expression to filter on. */
 86   
     private static final String REGEXP_KEY = "regexp";
 87   
 
 88   
     /** Vector that holds the expressions that input lines must contain. */
 89   
     private Vector regexps = new Vector();
 90   
 
 91   
     /**
 92   
      * Remaining line to be read from this filter, or <code>null</code> if
 93   
      * the next call to <code>read()</code> should read the original stream
 94   
      * to find the next matching line.
 95   
      */
 96   
     private String line = null;
 97   
 
 98   
     /**
 99   
      * Constructor for "dummy" instances.
 100   
      *
 101   
      * @see BaseFilterReader#BaseFilterReader()
 102   
      */
 103  0
     public LineContainsRegExp() {
 104  0
         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  0
     public LineContainsRegExp(final Reader in) {
 114  0
         super(in);
 115   
     }
 116   
 
 117   
     /**
 118   
      * Returns the next character in the filtered stream, only including
 119   
      * lines from the original stream which match all of the specified
 120   
      * regular expressions.
 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  0
     public final int read() throws IOException {
 129  0
         if (!getInitialized()) {
 130  0
             initialize();
 131  0
             setInitialized(true);
 132   
         }
 133   
 
 134  0
         int ch = -1;
 135   
 
 136  0
         if (line != null) {
 137  0
             ch = line.charAt(0);
 138  0
             if (line.length() == 1) {
 139  0
                 line = null;
 140   
             } else {
 141  0
                 line = line.substring(1);
 142   
             }
 143   
         } else {
 144  0
             line = readLine();
 145  0
             final int regexpsSize = regexps.size();
 146   
 
 147  0
             while (line != null) {
 148  0
                 for (int i = 0; i < regexpsSize; i++) {
 149  0
                     RegularExpression regexp = (RegularExpression)
 150   
                                                         regexps.elementAt(i);
 151  0
                     Regexp re = regexp.getRegexp(getProject());
 152  0
                     boolean matches = re.matches(line);
 153  0
                     if (!matches) {
 154  0
                         line = null;
 155  0
                         break;
 156   
                     }
 157   
                 }
 158   
 
 159  0
                 if (line == null) {
 160   
                     // line didn't match
 161  0
                     line = readLine();
 162   
                 } else {
 163  0
                     break;
 164   
                 }
 165   
             }
 166   
 
 167  0
             if (line != null) {
 168  0
                 return read();
 169   
             }
 170   
         }
 171   
 
 172  0
         return ch;
 173   
     }
 174   
 
 175   
     /**
 176   
      * Adds a <code>regexp</code> element.
 177   
      * 
 178   
      * @param regExp The <code>regexp</code> element to add. 
 179   
      *               Must not be <code>null</code>.
 180   
      */
 181  0
     public final void addConfiguredRegexp(final RegularExpression regExp) {
 182  0
         this.regexps.addElement(regExp);
 183   
     }
 184   
 
 185   
     /**
 186   
      * Sets the vector of regular expressions which must be contained within 
 187   
      * a line read from the original stream in order for it to match this 
 188   
      * filter.
 189   
      * 
 190   
      * @param regexps A vector of regular expressions which must be contained 
 191   
      * within a line in order for it to match in this filter. Must not be 
 192   
      * <code>null</code>.
 193   
      */
 194  0
     private void setRegexps(final Vector regexps) {
 195  0
         this.regexps = regexps;
 196   
     }
 197   
 
 198   
     /**
 199   
      * Returns the vector of regular expressions which must be contained within 
 200   
      * a line read from the original stream in order for it to match this 
 201   
      * filter.
 202   
      * 
 203   
      * @return the vector of regular expressions which must be contained within 
 204   
      * a line read from the original stream in order for it to match this 
 205   
      * filter. The returned object is "live" - in other words, changes made to 
 206   
      * the returned object are mirrored in the filter.
 207   
      */
 208  0
     private final Vector getRegexps() {
 209  0
         return regexps;
 210   
     }
 211   
 
 212   
     /**
 213   
      * Creates a new LineContainsRegExp using the passed in
 214   
      * Reader for instantiation.
 215   
      * 
 216   
      * @param rdr A Reader object providing the underlying stream.
 217   
      *            Must not be <code>null</code>.
 218   
      * 
 219   
      * @return a new filter based on this configuration, but filtering
 220   
      *         the specified reader
 221   
      */
 222  0
     public final Reader chain(final Reader rdr) {
 223  0
         LineContainsRegExp newFilter = new LineContainsRegExp(rdr);
 224  0
         newFilter.setRegexps(getRegexps());
 225  0
         newFilter.setInitialized(true);
 226  0
         return newFilter;
 227   
     }
 228   
 
 229   
     /**
 230   
      * Parses parameters to add user defined regular expressions.
 231   
      */
 232  0
     private final void initialize() {
 233  0
         Parameter[] params = getParameters();
 234  0
         if (params != null) {
 235  0
             for (int i = 0; i < params.length; i++) {
 236  0
                 if (REGEXP_KEY.equals(params[i].getType())) {
 237  0
                     String pattern = params[i].getValue();
 238  0
                     RegularExpression regexp = new RegularExpression();
 239  0
                     regexp.setPattern(pattern);
 240  0
                     regexps.addElement(regexp);
 241   
                 }
 242   
             }
 243   
         }
 244   
     }
 245   
 }
 246