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