Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 210   Methods: 7
NCLOC: 87   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
WhichResource.java 0% 0% 0% 0%
 1   
 /*
 2   
  *  The Apache Software License, Version 1.1
 3   
  *
 4   
  *  Copyright (c) 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   
 
 56   
 package org.apache.tools.ant.taskdefs;
 57   
 
 58   
 import org.apache.tools.ant.types.Path;
 59   
 import org.apache.tools.ant.Task;
 60   
 import org.apache.tools.ant.BuildException;
 61   
 import org.apache.tools.ant.Project;
 62   
 import org.apache.tools.ant.AntClassLoader;
 63   
 
 64   
 import java.net.URL;
 65   
 
 66   
 /**
 67   
  * Find a class or resource on the supplied classpath, or the
 68   
  * system classpath if none is supplied. The named property is set if
 69   
  * the item can be found. For example
 70   
  * <pre>
 71   
  * &lt;whichresource resource="/log4j.properties" 
 72   
  *   property="log4j.url" &gt;
 73   
  * </pre>
 74   
  * @author steve loughran while stuck in Enumclaw, WA, with a broken down car
 75   
  */
 76   
 public class WhichResource extends Task {
 77   
     /**
 78   
      * our classpath
 79   
      */
 80   
     private Path classpath;
 81   
 
 82   
     /**
 83   
      * class to look for
 84   
      */
 85   
     private String classname;
 86   
 
 87   
     /**
 88   
      * resource to look for
 89   
      */
 90   
     private String resource;
 91   
 
 92   
     /**
 93   
      * property to set
 94   
      */
 95   
     private String property;
 96   
 
 97   
 
 98   
     /**
 99   
      * Set the classpath to be used for this compilation.
 100   
      */
 101  0
     public void setClasspath(Path cp) {
 102  0
         if (classpath == null) {
 103  0
             classpath = cp;
 104   
         } else {
 105  0
             classpath.append(cp);
 106   
         }
 107   
     }
 108   
 
 109   
     /**
 110   
      * Adds a path to the classpath.
 111   
      */
 112  0
     public Path createClasspath() {
 113  0
         if (classpath == null) {
 114  0
             classpath = new Path(getProject());
 115   
         }
 116  0
         return classpath.createPath();
 117   
     }
 118   
 
 119   
 
 120   
     /**
 121   
      * validate
 122   
      */
 123  0
     private void validate() {
 124  0
         int setcount=0;
 125  0
         if(classname != null) {
 126  0
             setcount++;
 127   
         }
 128  0
         if(resource!=null) {
 129  0
             setcount++;
 130   
         }
 131   
 
 132   
 
 133  0
         if(setcount == 0) {
 134  0
             throw new BuildException(
 135   
                     "One of classname or resource must be specified");
 136   
         }
 137  0
         if (setcount > 1) {
 138  0
             throw new BuildException(
 139   
                     "Only one of classname or resource can be specified");
 140   
         }
 141  0
         if(property==null) {
 142  0
             throw new BuildException("No property defined");
 143   
         }
 144   
     }
 145   
 
 146   
     /**
 147   
      * execute it
 148   
      * @throws BuildException
 149   
      */
 150  0
     public void execute() throws BuildException {
 151  0
         validate();
 152  0
         if (classpath!= null) {
 153  0
             getProject().log("using user supplied classpath: " + classpath,
 154   
                     Project.MSG_DEBUG);
 155  0
             classpath=classpath.concatSystemClasspath("ignore");
 156   
         } else {
 157  0
             classpath = new Path(getProject());
 158  0
             classpath = classpath.concatSystemClasspath("only");
 159  0
             getProject().log("using system classpath: " + classpath, Project.MSG_DEBUG);
 160   
         }
 161  0
         AntClassLoader loader;
 162  0
         loader = new AntClassLoader(getProject().getCoreLoader(),
 163   
                     getProject(),
 164   
                     classpath, false);
 165  0
         String location=null;
 166  0
         if(classname!=null) {
 167   
             //convert a class name into a resource
 168  0
             classname= classname.replace('.', '/');
 169  0
             resource="/"+ classname +".class";
 170   
         } else {
 171  0
             if(!resource.startsWith("/")) {
 172  0
                 resource="/"+resource;
 173   
             }
 174   
         }
 175  0
         log("Searching for "+resource,Project.MSG_VERBOSE);
 176  0
         URL url;
 177  0
         url=loader.getResource(resource);
 178  0
         if(url!=null) {
 179   
             //set the property
 180  0
             location = url.toExternalForm();
 181  0
             getProject().setNewProperty(property,location);
 182   
         }
 183   
     }
 184   
 
 185   
     /**
 186   
      * name the resource to look for
 187   
      * @param resource
 188   
      */
 189  0
     public void setResource(String resource) {
 190  0
         this.resource = resource;
 191   
     }
 192   
 
 193   
     /**
 194   
      * name the class to look for
 195   
      * @param classname
 196   
      */
 197  0
     public void setClass(String classname) {
 198  0
         this.classname = classname;
 199   
     }
 200   
 
 201   
     /**
 202   
      * the property to fill with the URL of the resource or class
 203   
      * @param property
 204   
      */
 205  0
     public void setProperty(String property) {
 206  0
         this.property = property;
 207   
     }
 208   
 
 209   
 }
 210