From b482bf23bd33f97ec56d7b41cbbb203f151465fc Mon Sep 17 00:00:00 2001 From: Diane Holt Date: Thu, 3 May 2001 07:41:11 +0000 Subject: [PATCH] Backed out previous change that added a "dir" attribute, as this caused backwards-compatibility issues. Added a "type" attribute instead, which works with the "file" attribute to specify either a file or a directory. PR: Obtained from: Submitted by: Reviewed by: git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268997 13f79535-47bb-0310-9956-ffa450edef68 --- docs/manual/CoreTasks/available.html | 28 ++++++++----- src/etc/testcases/taskdefs/available.xml | 6 +-- .../apache/tools/ant/taskdefs/Available.java | 40 ++++++++++--------- 3 files changed, 42 insertions(+), 32 deletions(-) diff --git a/docs/manual/CoreTasks/available.html b/docs/manual/CoreTasks/available.html index 0d8f70957..6652f4791 100644 --- a/docs/manual/CoreTasks/available.html +++ b/docs/manual/CoreTasks/available.html @@ -12,8 +12,8 @@

Sets a property if a resource is available at runtime. This resource can be a file, a directory, a class in the classpath, or a JVM system resource.

If the resource is present, the property value is set to true by -default, otherwise the property is not set. You can set the value to -something specific by specifying the value attribute.

+default; otherwise, the property is not set. You can set the value to +something other than the default by specifying the value attribute.

Normally, this task is used to set properties that are useful to avoid target execution depending on system parameters.

Parameters

@@ -36,11 +36,7 @@ execution depending on system parameters.

classname The class to look for in the classpath. - Yes - - - dir - The directory to look for. + Yes file @@ -51,13 +47,18 @@ execution depending on system parameters.

The resource to look for in the JVM. - classpath The classpath to - use when looking up classname or resource. No + classpath + The classpath to use when looking up classname or resource. + No classpathref - The classpath to use, given as a reference to a path defined elsewhere. + The classpath to use, given as a reference to a path defined elsewhere. + No + + + type + The type of file to look for, either a directory (type="dir") or a file (type="file"). If not set, the property will be set if the name specified in the file attribute exists as either a file or a directory. No @@ -78,6 +79,11 @@ if the class org.whatever.Myclass is found in Ant's classpath.

sets the jaxp.jar.present property to the value "true" if the file ./lib/jaxp11/jaxp.jar is found.

+<available file="/usr/local/lib" type="dir" property="local.lib.present"/>
+
+

sets the local.lib.present property to the value "true" +if the directory /usr/local/lib is found.

+
 ...in project ...
 <property name="jaxp.jar" value="./lib/jaxp11/jaxp.jar"/>
 <path id="jaxp" location="${jaxp.jar}"/>
diff --git a/src/etc/testcases/taskdefs/available.xml b/src/etc/testcases/taskdefs/available.xml
index 97f9b92d4..2b215d5e7 100644
--- a/src/etc/testcases/taskdefs/available.xml
+++ b/src/etc/testcases/taskdefs/available.xml
@@ -80,17 +80,17 @@
 
   
     
+               file="" type="dir"/>
   
 
   
     
+               file="../taskdefs" type="dir"/>
   
 
   
     
+               file="../this_dir_should_never_exist" type="dir"/>
   
 
 
diff --git a/src/main/org/apache/tools/ant/taskdefs/Available.java b/src/main/org/apache/tools/ant/taskdefs/Available.java
index 0dab4f376..7e6d94bc7 100644
--- a/src/main/org/apache/tools/ant/taskdefs/Available.java
+++ b/src/main/org/apache/tools/ant/taskdefs/Available.java
@@ -70,8 +70,8 @@ public class Available extends Task {
     private String property;
     private String classname;
     private File file;
-    private File dir;
     private String resource;
+    private String type;
     private Path classpath;
     private AntClassLoader loader;
     private String value = "true";
@@ -113,21 +113,27 @@ public class Available extends Task {
         this.file = file;
     }
 
-    public void setDir(File dir) {
-        this.dir = dir;
-    }
-
     public void setResource(String resource) {
         this.resource = resource;
     }
 
+    public void setType(String type) {
+        this.type = type;
+    }
+
     public void execute() throws BuildException {
         if (property == null) {
             throw new BuildException("property attribute is required", location);
         }
         
-        if (classname == null && file == null && dir == null && resource == null) {
-            throw new BuildException("At least one of (classname|file|dir|resource) is required", location);
+        if (classname == null && file == null && resource == null) {
+            throw new BuildException("At least one of (classname|file|resource) is required", location);
+        }
+
+        if (type != null){
+            if (!type.equalsIgnoreCase("file") && !type.equalsIgnoreCase("dir")){
+                throw new BuildException("Type must be one of either dir or file");
+            }
         }
 
         if (classpath != null) {
@@ -140,12 +146,7 @@ public class Available extends Task {
         }
         
         if ((file != null) && !checkFile(file)) {
-            log("Unable to find file " + file + " to set property " + property, Project.MSG_VERBOSE);
-            return;
-        }
-        
-        if ((dir != null) && !checkDir(dir)) {
-            log("Unable to find dir " + dir + " to set property " + property, Project.MSG_VERBOSE);
+            log("Unable to find " + file + " to set property " + property, Project.MSG_VERBOSE);
             return;
         }
         
@@ -158,11 +159,14 @@ public class Available extends Task {
     }
 
     private boolean checkFile(File file) {
-        return file.isFile();
-    }
-
-    private boolean checkDir(File dir) {
-        return dir.isDirectory();
+        if (type != null) {
+            if (type.equalsIgnoreCase("dir")){
+                return file.isDirectory();
+            } else if (type.equalsIgnoreCase("file")){
+                return file.isFile();
+            }
+        }
+        return file.exists();
     }
 
     private boolean checkResource(String resource) {