Browse Source

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
master
Diane Holt 24 years ago
parent
commit
b482bf23bd
3 changed files with 42 additions and 32 deletions
  1. +17
    -11
      docs/manual/CoreTasks/available.html
  2. +3
    -3
      src/etc/testcases/taskdefs/available.xml
  3. +22
    -18
      src/main/org/apache/tools/ant/taskdefs/Available.java

+ 17
- 11
docs/manual/CoreTasks/available.html View File

@@ -12,8 +12,8 @@
<p>Sets a property if a resource is available at runtime. This resource can be a <p>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.</p> file, a directory, a class in the classpath, or a JVM system resource.</p>
<p>If the resource is present, the property value is set to true by <p>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 <code>value</code> attribute.</p>
default; otherwise, the property is not set. You can set the value to
something other than the default by specifying the <code>value</code> attribute.</p>
<p>Normally, this task is used to set properties that are useful to avoid target <p>Normally, this task is used to set properties that are useful to avoid target
execution depending on system parameters.</p> execution depending on system parameters.</p>
<h3>Parameters</h3> <h3>Parameters</h3>
@@ -36,11 +36,7 @@ execution depending on system parameters.</p>
<tr> <tr>
<td valign="top">classname</td> <td valign="top">classname</td>
<td valign="top">The class to look for in the classpath.</td> <td valign="top">The class to look for in the classpath.</td>
<td valign="middle" align="center" rowspan="4">Yes</td>
</tr>
<tr>
<td valign="top">dir</td>
<td valign="top">The directory to look for.</td>
<td valign="middle" align="center" rowspan="3">Yes</td>
</tr> </tr>
<tr> <tr>
<td valign="top">file</td> <td valign="top">file</td>
@@ -51,13 +47,18 @@ execution depending on system parameters.</p>
<td valign="top">The resource to look for in the JVM.</td> <td valign="top">The resource to look for in the JVM.</td>
</tr> </tr>
<tr> <tr>
<td valign="top">classpath</td> <td valign="top">The classpath to
use when looking up <code>classname</code> or <code>resource</code>.</td> <td
align="center" valign="top">No</td>
<td valign="top">classpath</td>
<td valign="top">The classpath to use when looking up <code>classname</code> or <code>resource</code>.</td>
<td align="center" valign="top">No</td>
</tr> </tr>
<tr> <tr>
<td valign="top">classpathref</td> <td valign="top">classpathref</td>
<td valign="top">The classpath to use, given as a <a href="../using.html#references">reference</a> to a path defined elsewhere.</td>
<td valign="top">The classpath to use, given as a <a href="../using.html#references">reference</a> to a path defined elsewhere.</td>
<td align="center" valign="top">No</td>
</tr>
<tr>
<td valign="top">type</td>
<td valign="top">The type of <code>file</code> to look for, either a directory (<code>type="dir"</code>) or a file (<code>type="file"</code>). If not set, the property will be set if the name specified in the <code>file</code> attribute exists as either a file or a directory.</td>
<td align="center" valign="top">No</td> <td align="center" valign="top">No</td>
</tr> </tr>
@@ -78,6 +79,11 @@ if the class <code>org.whatever.Myclass</code> is found in Ant's classpath.</p>
<p>sets the <code>jaxp.jar.present</code> property to the value &quot;true&quot; <p>sets the <code>jaxp.jar.present</code> property to the value &quot;true&quot;
if the file <code>./lib/jaxp11/jaxp.jar</code> is found.</p> if the file <code>./lib/jaxp11/jaxp.jar</code> is found.</p>
<pre> <pre>
&lt;available file=&quot;/usr/local/lib&quot; type=&quot;dir&quot; property=&quot;local.lib.present&quot;/&gt;
</pre>
<p>sets the <code>local.lib.present</code> property to the value &quot;true&quot;
if the directory <code>/usr/local/lib</code> is found.</p>
<pre>
...in project ... ...in project ...
&lt;property name=&quot;jaxp.jar&quot; value=&quot;./lib/jaxp11/jaxp.jar&quot;/&gt; &lt;property name=&quot;jaxp.jar&quot; value=&quot;./lib/jaxp11/jaxp.jar&quot;/&gt;
&lt;path id=&quot;jaxp&quot; location=&quot;${jaxp.jar}&quot;/&gt; &lt;path id=&quot;jaxp&quot; location=&quot;${jaxp.jar}&quot;/&gt;


+ 3
- 3
src/etc/testcases/taskdefs/available.xml View File

@@ -80,17 +80,17 @@


<target name="test16"> <target name="test16">
<available property="test" <available property="test"
dir=""/>
file="" type="dir"/>
</target> </target>


<target name="test17"> <target name="test17">
<available property="test" <available property="test"
dir="../taskdefs"/>
file="../taskdefs" type="dir"/>
</target> </target>


<target name="test18"> <target name="test18">
<available property="test" <available property="test"
dir="../this_dir_should_never_exist"/>
file="../this_dir_should_never_exist" type="dir"/>
</target> </target>


</project> </project>

+ 22
- 18
src/main/org/apache/tools/ant/taskdefs/Available.java View File

@@ -70,8 +70,8 @@ public class Available extends Task {
private String property; private String property;
private String classname; private String classname;
private File file; private File file;
private File dir;
private String resource; private String resource;
private String type;
private Path classpath; private Path classpath;
private AntClassLoader loader; private AntClassLoader loader;
private String value = "true"; private String value = "true";
@@ -113,21 +113,27 @@ public class Available extends Task {
this.file = file; this.file = file;
} }


public void setDir(File dir) {
this.dir = dir;
}

public void setResource(String resource) { public void setResource(String resource) {
this.resource = resource; this.resource = resource;
} }


public void setType(String type) {
this.type = type;
}

public void execute() throws BuildException { public void execute() throws BuildException {
if (property == null) { if (property == null) {
throw new BuildException("property attribute is required", location); 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) { if (classpath != null) {
@@ -140,12 +146,7 @@ public class Available extends Task {
} }
if ((file != null) && !checkFile(file)) { 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; return;
} }
@@ -158,11 +159,14 @@ public class Available extends Task {
} }


private boolean checkFile(File file) { 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) { private boolean checkResource(String resource) {


Loading…
Cancel
Save