Patch Description: Improved Available task that allows for multiple classes, files, or attachments to be specified. The supplied property is set only when all classes, files, are resources are present. Useful when the compilation of a source file depends on more than one library to be present.
Changes to Available.java: 1. Added support for multiple classes, resources, and files to Available. 2. Added a static parseMultiple class to handle multiple elements in an input string which are separated by a comma or a space. This function could be moved into a TaskUtil.java class at a later date, but it was not present at the time of this change. 3. Available now uses Project.resolveFile() to locate file objects. Changes to index.html: 1. Changed documentation of Built in Task Available to reflect multiple inputs. 2. Added more examples to Available section to describe use for resources and file. Tim O'Brien [EMAIL PROTECTED] Patch begins here: Index: docs/index.html =================================================================== RCS file: /home/cvspublic/jakarta-ant/docs/index.html,v retrieving revision 1.32 diff -u -r1.32 index.html --- docs/index.html 2000/06/27 13:07:34 1.32 +++ docs/index.html 2000/06/28 01:38:37 @@ -19,6 +19,7 @@ <li>Arnout J. Kuiper (<a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>)</li> <li>Conor MacNeill (<a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>)</li> <li>Stefano Mazzocchi (<a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>)</li> + <li>Tim O'Brien (<a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>)</li> <li>Sam Ruby (<a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a>)</li> </ul> @@ -581,7 +582,7 @@ <h3>Description</h3> <p>Sets a property if a resource is available at runtime. This resource can be a file resource, a class in classpath or a JVM system resource.</p> -<p>The value part of the properties being set is <i>true</i> if the resource is +<p>The value part of the properties being set is <i>true</i> if all specified resources are present, otherwise, the property is not set.</p> <p>Normally, this task is used to set properties that are useful to avoid target execution depending on system parameters.</p> @@ -599,22 +600,35 @@ </tr> <tr> <td valign="top">classname</td> - <td valign="top">the class to look for in classpath.</td> + <td valign="top">comma separated list of classes to look for in classpath.</td> <td valign="middle" align="center" rowspan="3">Yes</td> </tr> <tr> <td valign="top">resource</td> - <td valign="top">the resource to look for in the JVM</td> + <td valign="top">comma separated list of resources to look for in the JVM</td> </tr> <tr> <td valign="top">file</td> - <td valign="top">the file to look for.</td> + <td valign="top">comma separated list of files to look for.</td> </tr> </table> <h3>Examples</h3> -<pre> <available classname="org.whatever.Myclass" property="Myclass.present" /></pre> +<pre> <available classname="org.whatever.Myclass" + property="Myclass.present" /></pre> <p>sets the property <code><i>Myclass.present</i></code> to the value "true" if the <i>org.whatever.Myclass</i> is found in Ant's classpath.</p> +<pre> <available classname="org.whatever.Myclass,org.whatever.MySecondClass" + property="Myclasses.present" /></pre> +<p>sets the property <code><i>Myclasses.present</i></code> to the value "true" +if <i>org.whatever.Myclass</i> and <i>org.whatever.MySecondClass</i> are found in Ant's classpath.</p> +<pre> <available file="testfile.txt" + property="testfile.present" /></pre> +<p>sets the property <code><i>testfile.present</i></code> to the value "true" +if the file <i>testfile.txt</i> exists.</p> +<pre> <available resource="org/whatever/myresource.properties" + property="myresource.present" /></pre> +<p>sets the property <code><i>myresource.present</i></code> to the value "true" +if the resource <i>myresource.properties</i> can be located in the JVM.</p> <hr> <h2><a name="chmod">Chmod</a></h2> <h3>Description</h3> Index: src/main/org/apache/tools/ant/taskdefs/Available.java =================================================================== RCS file: /home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Available.java,v retrieving revision 1.5 diff -u -r1.5 Available.java --- src/main/org/apache/tools/ant/taskdefs/Available.java 2000/06/14 01:40:21 1.5 +++ src/main/org/apache/tools/ant/taskdefs/Available.java 2000/06/28 01:38:43 @@ -62,14 +62,15 @@ * Will set the given property if the requested resource is available at runtime. * * @author Stefano Mazzocchi <a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a> + * @author Tim O'Brien <a href="mailto:[EMAIL PROTECTED]">[EMAIL PROTECTED]</a> */ public class Available extends Task { private String property; - private String classname; - private String file; - private String resource; + private String classnames[]; + private String files[]; + private String resources[]; public void setProperty(String property) { this.property = property; @@ -79,46 +80,84 @@ project.log("The class attribute is deprecated. " + "Please use the classname attribute.", Project.MSG_WARN); - this.classname = classname; + this.classnames = parseMultiple( classname ); } public void setClassname(String classname) { - this.classname = classname; + this.classnames = parseMultiple( classname ); } public void setFile(String filename) { - this.file = filename; + this.files = parseMultiple( filename ); } public void setResource(String resource) { - this.resource = resource; + this.resources = parseMultiple( resource ); } + /** + * Transforms a String with multiple entries to an array of Strings. + * Multiple entries may be separated by either a comma or a space. + * @param multiInput String containing multiple entries + * @return string array of entries + **/ + public static String[] parseMultiple( String multiInput ) { + + String[] newArray; + Vector tokens = new Vector(); + + StringTokenizer sToke = new StringTokenizer( multiInput, ", " ); + while( sToke.hasMoreElements() ) { + tokens.addElement( sToke.nextElement() ); + } + + newArray = new String[ tokens.size() ]; + for( int i = 0; i < tokens.size(); i++ ) { + newArray[i] = (String) tokens.elementAt( i ); + } + + return( newArray ); + + } + public void init() throws BuildException { - if ((classname != null) && !checkClass(classname)) return; - if ((file != null) && !checkFile(file)) return; - if ((resource != null) && !checkResource(resource)) return; + if ((classnames != null) && !checkClasses(classnames)) return; + if ((files != null) && !checkFiles(files)) return; + if ((resources != null) && !checkResources(resources)) return; this.project.setProperty(property, "true"); } - private boolean checkFile(String file) { + private boolean checkFiles(String[] files) { try { - File f = new File(file); - return f.exists(); + for( int i = 0; i < files.length; i++ ) { + File f = project.resolveFile( files[i] ); + if( !f.exists() ) { return false; } + } + return true; } catch (Exception e) { project.log(e.toString(), "available", Project.MSG_VERBOSE); return false; } } + + private boolean checkResources(String[] resources) { + + for( int i = 0; i < resources.length; i++ ) { + if( ClassLoader.getSystemResource(resources[i]) == null ) { + return false; + } + } - private boolean checkResource(String resource) { - return (ClassLoader.getSystemResource(resource) != null); + return true; } - private boolean checkClass(String classname) { + private boolean checkClasses(String[] classnames) { try { - Class.forName(classname); + + for( int i = 0; i < classnames.length; i++ ) { + Class.forName(classnames[i]); + } return true; } catch (Throwable t) { project.log(t.toString(), "available", Project.MSG_VERBOSE); @@ -126,3 +165,7 @@ } } } + + + +
