Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 229   Methods: 10
NCLOC: 106   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ExtendSelector.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   
 
 55   
 package org.apache.tools.ant.types.selectors;
 56   
 
 57   
 import java.io.File;
 58   
 import java.util.Vector;
 59   
 import org.apache.tools.ant.AntClassLoader;
 60   
 import org.apache.tools.ant.BuildException;
 61   
 import org.apache.tools.ant.types.Parameter;
 62   
 import org.apache.tools.ant.types.Path;
 63   
 import org.apache.tools.ant.types.Reference;
 64   
 
 65   
 /**
 66   
  * Selector that selects files by forwarding the request on to other classes.
 67   
  *
 68   
  * @author <a href="mailto:bruce@callenish.com">Bruce Atherton</a>
 69   
  * @since 1.5
 70   
  */
 71   
 public class ExtendSelector extends BaseSelector {
 72   
 
 73   
     private String classname = null;
 74   
     private FileSelector dynselector = null;
 75   
     private Vector paramVec = new Vector();
 76   
     private Path classpath = null;
 77   
 
 78   
     /**
 79   
      * Default constructor.
 80   
      */
 81  0
     public ExtendSelector() {
 82   
     }
 83   
 
 84   
     /**
 85   
      * Sets the classname of the custom selector.
 86   
      *
 87   
      * @param classname is the class which implements this selector
 88   
      */
 89  0
     public void setClassname(String classname) {
 90  0
         this.classname = classname;
 91   
     }
 92   
 
 93   
     /**
 94   
      * Instantiates the identified custom selector class.
 95   
      */
 96  0
     public void selectorCreate() {
 97  0
         if (classname != null && classname.length() > 0) {
 98  0
             try {
 99  0
                 Class c = null;
 100  0
                 if (classpath == null) {
 101  0
                     c = Class.forName(classname);
 102   
                 } else {
 103  0
                     AntClassLoader al
 104   
                         = getProject().createClassLoader(classpath);
 105  0
                     c = al.loadClass(classname);
 106  0
                     AntClassLoader.initializeClass(c);
 107   
                 }
 108  0
                 dynselector = (FileSelector) c.newInstance();
 109   
             }
 110   
             catch (ClassNotFoundException cnfexcept) {
 111  0
                 setError("Selector " + classname +
 112   
                         " not initialized, no such class");
 113   
             }
 114   
             catch (InstantiationException iexcept) {
 115  0
                 setError("Selector " + classname +
 116   
                         " not initialized, could not create class");
 117   
             }
 118   
             catch (IllegalAccessException iaexcept) {
 119  0
                 setError("Selector " + classname +
 120   
                         " not initialized, class not accessible");
 121   
             }
 122   
         } else {
 123  0
             setError("There is no classname specified");
 124   
         }
 125   
     }
 126   
 
 127   
     /**
 128   
      * Create new parameters to pass to custom selector.
 129   
      *
 130   
      * @param p The new Parameter object
 131   
      */
 132  0
     public void addParam(Parameter p) {
 133  0
         paramVec.addElement(p);
 134   
     }
 135   
 
 136   
 
 137   
     /**
 138   
      * Set the classpath to load the classname specified using an attribute.
 139   
      */
 140  0
     public final void setClasspath(Path classpath) {
 141  0
         if (isReference()) {
 142  0
             throw tooManyAttributes();
 143   
         }
 144  0
         if (this.classpath == null) {
 145  0
             this.classpath = classpath;
 146   
         } else {
 147  0
             this.classpath.append(classpath);
 148   
         }
 149   
     }
 150   
 
 151   
     /**
 152   
      * Specify the classpath to use to load the Selector (nested element).
 153   
      */
 154  0
     public final Path createClasspath() {
 155  0
         if (isReference()) {
 156  0
             throw noChildrenAllowed();
 157   
         }
 158  0
         if (this.classpath == null) {
 159  0
             this.classpath = new Path(getProject());
 160   
         }
 161  0
         return this.classpath.createPath();
 162   
     }
 163   
 
 164   
     /**
 165   
      * Get the classpath
 166   
      */
 167  0
     public final Path getClasspath() {
 168  0
         return classpath;
 169   
     }
 170   
 
 171   
     /**
 172   
      * Set the classpath to use for loading a custom selector by using
 173   
      * a reference.
 174   
      */
 175  0
     public void setClasspathref(Reference r) {
 176  0
         if (isReference()) {
 177  0
             throw tooManyAttributes();
 178   
         }
 179  0
         createClasspath().setRefid(r);
 180   
     }
 181   
 
 182   
     /**
 183   
      * These are errors specific to ExtendSelector only. If there are
 184   
      * errors in the custom selector, it should throw a BuildException
 185   
      * when isSelected() is called.
 186   
      */
 187  0
     public void verifySettings() {
 188   
         // Creation is done here rather than in isSelected() because some
 189   
         // containers may do a validation pass before running isSelected(),
 190   
         // but we need to check for the existence of the created class.
 191  0
         if (dynselector == null) {
 192  0
             selectorCreate();
 193   
         }
 194  0
         if (classname == null || classname.length() < 1) {
 195  0
             setError("The classname attribute is required");
 196   
         }
 197  0
         else if (dynselector == null) {
 198  0
             setError("Internal Error: The custom selector was not created");
 199   
         }
 200  0
         else if (!(dynselector instanceof ExtendFileSelector) &&
 201   
                 (paramVec.size() > 0)) {
 202  0
             setError("Cannot set parameters on custom selector that does not "
 203   
                     + "implement ExtendFileSelector");
 204   
         }
 205   
     }
 206   
 
 207   
 
 208   
     /**
 209   
      * Allows the custom selector to choose whether to select a file. This
 210   
      * is also where the Parameters are passed to the custom selector,
 211   
      * since we know we must have them all by now. And since we must know
 212   
      * both classpath and classname, creating the class is deferred to here
 213   
      * as well.
 214   
      */
 215  0
     public boolean isSelected(File basedir, String filename, File file)
 216   
             throws BuildException {
 217  0
         validate();
 218  0
         if (paramVec.size() > 0 && dynselector instanceof ExtendFileSelector) {
 219  0
             Parameter[] paramArray = new Parameter[paramVec.size()];
 220  0
             paramVec.copyInto(paramArray);
 221   
             // We know that dynselector must be non-null if no error message
 222  0
             ((ExtendFileSelector)dynselector).setParameters(paramArray);
 223   
         }
 224  0
         return dynselector.isSelected(basedir,filename,file);
 225   
     }
 226   
 
 227   
 }
 228   
 
 229