FileDocCategorySizeDatePackage
WhichResource.javaAPI DocApache Ant 1.705151Wed Dec 13 06:16:22 GMT 2006org.apache.tools.ant.taskdefs

WhichResource

public class WhichResource extends org.apache.tools.ant.Task
Find a class or resource on the supplied classpath, or the system classpath if none is supplied. The named property is set if the item can be found. For example
<whichresource resource="/log4j.properties"
property="log4j.url" >
since
Ant 1.6
ant.attribute.group
name="oneof" description="Exactly one of these two"

Fields Summary
private org.apache.tools.ant.types.Path
classpath
our classpath
private String
classname
class to look for
private String
resource
resource to look for
private String
property
property to set
Constructors Summary
Methods Summary
public org.apache.tools.ant.types.PathcreateClasspath()
Adds a path to the classpath.

return
a classpath to be configured.

        if (classpath == null) {
            classpath = new Path(getProject());
        }
        return classpath.createPath();
    
public voidexecute()
execute it

throws
BuildException on error

        validate();
        if (classpath != null) {
            getProject().log("using user supplied classpath: " + classpath,
                    Project.MSG_DEBUG);
            classpath = classpath.concatSystemClasspath("ignore");
        } else {
            classpath = new Path(getProject());
            classpath = classpath.concatSystemClasspath("only");
            getProject().log("using system classpath: " + classpath, Project.MSG_DEBUG);
        }
        AntClassLoader loader;
        loader = new AntClassLoader(getProject().getCoreLoader(),
                    getProject(),
                    classpath, false);
        String loc = null;
        if (classname != null) {
            //convert a class name into a resource
            resource = classname.replace('.", '/") + ".class";
        }

        if (resource == null) {
            throw new BuildException("One of class or resource is required");
        }

        if (resource.startsWith("/")) {
            resource = resource.substring(1);
        }

        log("Searching for " + resource, Project.MSG_VERBOSE);
        URL url;
        url = loader.getResource(resource);
        if (url != null) {
            //set the property
            loc = url.toExternalForm();
            getProject().setNewProperty(property, loc);
        }
    
public voidsetClass(java.lang.String classname)
name the class to look for

param
classname the name of the class to look for.
ant.attribute
group="oneof"

        this.classname = classname;
    
public voidsetClasspath(org.apache.tools.ant.types.Path cp)
Set the classpath to be used for this compilation.

param
cp the classpath to be used.

        if (classpath == null) {
            classpath = cp;
        } else {
            classpath.append(cp);
        }
    
public voidsetProperty(java.lang.String property)
the property to fill with the URL of the resource or class

param
property the property to be set.
ant.attribute
group="required"

        this.property = property;
    
public voidsetResource(java.lang.String resource)
name the resource to look for

param
resource the name of the resource to look for.
ant.attribute
group="oneof"

        this.resource = resource;
    
private voidvalidate()
validate

        int setcount = 0;
        if (classname != null) {
            setcount++;
        }
        if (resource != null) {
            setcount++;
        }


        if (setcount == 0) {
            throw new BuildException(
                    "One of classname or resource must be specified");
        }
        if (setcount > 1) {
            throw new BuildException(
                    "Only one of classname or resource can be specified");
        }
        if (property == null) {
            throw new BuildException("No property defined");
        }