Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 240   Methods: 7
NCLOC: 130   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ChainReaderHelper.java 71.9% 83.1% 85.7% 79.6%
 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.util;
 55   
 
 56   
 import java.io.FilterReader;
 57   
 import java.io.IOException;
 58   
 import java.io.Reader;
 59   
 import java.lang.reflect.Constructor;
 60   
 import java.lang.reflect.InvocationTargetException;
 61   
 import java.util.Vector;
 62   
 import org.apache.tools.ant.AntClassLoader;
 63   
 import org.apache.tools.ant.BuildException;
 64   
 import org.apache.tools.ant.Project;
 65   
 import org.apache.tools.ant.filters.BaseFilterReader;
 66   
 import org.apache.tools.ant.filters.ChainableReader;
 67   
 import org.apache.tools.ant.types.AntFilterReader;
 68   
 import org.apache.tools.ant.types.FilterChain;
 69   
 import org.apache.tools.ant.types.Parameter;
 70   
 import org.apache.tools.ant.types.Parameterizable;
 71   
 import org.apache.tools.ant.types.Path;
 72   
 import org.apache.tools.ant.util.FileUtils;
 73   
 
 74   
 /**
 75   
  * Process a FilterReader chain.
 76   
  *
 77   
  * @author Magesh Umasankar
 78   
  */
 79   
 public final class ChainReaderHelper {
 80   
 
 81   
     /**
 82   
      * The primary reader to which the reader chain is to be attached.
 83   
      */
 84   
     public Reader primaryReader;
 85   
 
 86   
     /**
 87   
      * The size of the buffer to be used.
 88   
      */
 89   
     public int bufferSize = 8192;
 90   
 
 91   
     /**
 92   
      * Chain of filters
 93   
      */
 94   
     public Vector filterChains = new Vector();
 95   
 
 96   
     /** The Ant project */
 97   
     private Project project = null;
 98   
 
 99   
     /**
 100   
      * Sets the primary reader
 101   
      */
 102  18
     public final void setPrimaryReader(Reader rdr) {
 103  18
         primaryReader = rdr;
 104   
     }
 105   
 
 106   
     /**
 107   
      * Set the project to work with
 108   
      */
 109  18
     public final void setProject(final Project project) {
 110  18
         this.project = project;
 111   
     }
 112   
 
 113   
     /**
 114   
      * Get the project
 115   
      */
 116  0
     public final Project getProject() {
 117  0
         return project;
 118   
     }
 119   
 
 120   
     /**
 121   
      * Sets the buffer size to be used.  Defaults to 4096,
 122   
      * if this method is not invoked.
 123   
      */
 124  18
     public final void setBufferSize(int size) {
 125  18
         bufferSize = size;
 126   
     }
 127   
 
 128   
     /**
 129   
      * Sets the collection of filter reader sets
 130   
      */
 131  18
     public final void setFilterChains(Vector fchain) {
 132  18
         filterChains = fchain;
 133   
     }
 134   
 
 135   
     /**
 136   
      * Assemble the reader
 137   
      */
 138  18
     public final Reader getAssembledReader() throws BuildException {
 139  18
         if (primaryReader == null) {
 140  0
             throw new BuildException("primaryReader must not be null.");
 141   
         }
 142   
 
 143  18
         Reader instream = primaryReader;
 144  18
         final int filterReadersCount = filterChains.size();
 145  18
         final Vector finalFilters = new Vector();
 146   
 
 147  18
         for (int i = 0; i < filterReadersCount; i++) {
 148  15
             final FilterChain filterchain =
 149   
                 (FilterChain) filterChains.elementAt(i);
 150  15
             final Vector filterReaders = filterchain.getFilterReaders();
 151  15
             final int readerCount = filterReaders.size();
 152  15
             for (int j = 0; j < readerCount; j++) {
 153  21
                 finalFilters.addElement(filterReaders.elementAt(j));
 154   
             }
 155   
         }
 156   
 
 157  18
         final int filtersCount = finalFilters.size();
 158   
 
 159  18
         if (filtersCount > 0) {
 160  15
             for (int i = 0; i < filtersCount; i++) {
 161  21
                 Object o = finalFilters.elementAt(i);
 162   
 
 163  21
                 if (o instanceof AntFilterReader) {
 164  3
                     final AntFilterReader filter = (AntFilterReader) finalFilters.elementAt(i);
 165  3
                     final String className = filter.getClassName();
 166  3
                     final Path classpath = filter.getClasspath();
 167  3
                     final Project project = filter.getProject();
 168  3
                     if (className != null) {
 169  3
                         try {
 170  3
                             Class clazz = null;
 171  3
                             if (classpath == null) {
 172  3
                                 clazz = Class.forName(className);
 173   
                             } else {
 174  0
                                 AntClassLoader al
 175   
                                     = project.createClassLoader(classpath);
 176  0
                                 clazz = al.loadClass(className);
 177  0
                                 AntClassLoader.initializeClass(clazz);
 178   
                             }
 179  3
                             if (clazz != null) {
 180  3
                                 if (!FilterReader.class.isAssignableFrom(clazz)) {
 181  0
                                     throw new BuildException(className +
 182   
                                         " does not extend java.io.FilterReader");
 183   
                                 }
 184  3
                                 final Constructor[] constructors =
 185   
                                     clazz.getConstructors();
 186  3
                                 int j = 0;
 187  6
                                 for (; j < constructors.length; j++) {
 188  6
                                     Class[] types = constructors[j]
 189   
                                                       .getParameterTypes();
 190  6
                                     if (types.length == 1 &&
 191   
                                         types[0].isAssignableFrom(Reader.class)) {
 192  3
                                         break;
 193   
                                     }
 194   
                                 }
 195  3
                                 final Reader[] rdr = {instream};
 196  3
                                 instream =
 197   
                                     (Reader) constructors[j].newInstance(rdr);
 198  3
                                 if (project != null &&
 199   
                                         instream instanceof BaseFilterReader) {
 200  3
                                     ((BaseFilterReader)
 201   
                                         instream).setProject(project);
 202   
                                 }
 203  3
                                 if (Parameterizable.class.isAssignableFrom(clazz)) {
 204  2
                                     final Parameter[] params = filter.getParams();
 205  2
                                     ((Parameterizable)
 206   
                                         instream).setParameters(params);
 207   
                                 }
 208   
                             }
 209   
                         } catch (final ClassNotFoundException cnfe) {
 210  0
                             throw new BuildException(cnfe);
 211   
                         } catch (final InstantiationException ie) {
 212  0
                             throw new BuildException(ie);
 213   
                         } catch (final IllegalAccessException iae) {
 214  0
                             throw new BuildException(iae);
 215   
                         } catch (final InvocationTargetException ite) {
 216  0
                             throw new BuildException(ite);
 217   
                         }
 218   
                     }
 219  18
                 } else if (o instanceof ChainableReader &&
 220   
                            o instanceof Reader) {
 221  18
                     if (project != null && o instanceof BaseFilterReader) {
 222  18
                         ((BaseFilterReader) o).setProject(project);
 223   
                     }
 224  18
                     instream = ((ChainableReader) o).chain(instream);
 225   
                 }
 226   
             }
 227   
         }
 228  18
         return instream;
 229   
     }
 230   
 
 231   
     /**
 232   
      * Read data from the reader and return the
 233   
      * contents as a string.
 234   
      */
 235  8
     public final String readFully(Reader rdr)
 236   
         throws IOException {
 237  8
         return FileUtils.readFully(rdr, bufferSize);
 238   
     }
 239   
 }
 240