ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Tim Fennell" <tfe...@rcn.com>
Subject [PATCH] Available.java - to detect JDK versions
Date Mon, 24 Jul 2000 03:43:53 GMT
Here is the patch. It supports the syntax suggested:
	1.2.2 - matches specified version
	1.2+  - matches 1.2 or higher
	1.2-	- matches 1.2 or lower

Note both the +/- version are *inclusive*, so check for 1.99- if you want pre
1.2 etc!

-t

---------------------------Begin Patch----------------------------
Index: Available.java
===================================================================
RCS file:
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Available.jav
a,v
retrieving revision 1.6
diff -u -r1.6 Available.java
--- Available.java	2000/07/06 16:48:13	1.6
+++ Available.java	2000/07/24 03:43:23
@@ -62,6 +62,7 @@
  * Will set the given property if the requested resource is available at
runtime.
  *
  * @author Stefano Mazzocchi <a
href="mailto:stefano@apache.org">stefano@apache.org</a>
+ * @author Tim Fennell <a
href="mailto:tfennell@sapient.com">tfennell@sapient.com</a>
  */

 public class Available extends Task {
@@ -70,6 +71,7 @@
     private String classname;
     private String file;
     private String resource;
+    private String jdk;

     public void setProperty(String property) {
         this.property = property;
@@ -94,10 +96,16 @@
         this.resource = resource;
     }

+    /** Setter method to set the value of the JDK to check for. */
+    public void setJdk(String jdk) {
+        this.jdk = jdk;
+    }
+
     public void init() throws BuildException {
         if ((classname != null) && !checkClass(classname)) return;
         if ((file != null) && !checkFile(file)) return;
         if ((resource != null) && !checkResource(resource)) return;
+        if ((jdk != null) && !checkJdk(jdk)) return;

         this.project.setProperty(property, "true");
     }
@@ -125,4 +133,75 @@
             return false;
         }
     }
+
+    /**
+     * Checks to see if the current JDK version (the one this method is called
+     * within) matches a criteria. Either that it is 'equal', 'equal or higher'
+     * or 'equal of lower'.
+     * @param jdk version of the JDK to check for. Terminate with a '+' to
check
+     *        for a version or higher. Terminate with a '-' to check for a
+     *         version or lower.
+     * @return boolean true if the current JDK matches the criteria.
+     */
+    private boolean checkJdk(String jdk) {
+        double checkVersion = 0.0;
+        double currentVersion = 0.0;
+        int    returnCheck  = 0;
+        int    returnValue  = 0;
+
+        // Determine if we are looking for an exact match, greater than or less
+        // than... And convert the jdk string to a double.
+        if (jdk.endsWith("-")) {
+            returnCheck = -1;
+            checkVersion = stringToDouble(jdk.substring(0, jdk.length() -1));
+        }
+        else if (jdk.endsWith("+")) {
+            returnCheck = +1;
+            checkVersion = stringToDouble(jdk.substring(0, jdk.length() -1));
+        }
+        else {
+            returnCheck = 0;
+            checkVersion = stringToDouble(jdk);
+        }
+
+        // Get the current JDK version as a double
+        currentVersion = stringToDouble(System.getProperty("java.version"));
+
+        // Check the current version against the checkVersion
+        if (checkVersion == currentVersion)
+            returnValue = 0;
+        else if (currentVersion < checkVersion)
+            returnValue = -1;
+        else if (currentVersion > checkVersion)
+            returnValue = +1;
+
+        // If the versions are an exact match, return true. Otherwise check to
+        // see if the returnValue is equal to the checkValue.
+        if (returnValue == 0) return true;
+        else return (returnValue == returnCheck);
+
+    }
+
+    /**
+     * Utility method to convert strings with multiple decimal places into
+     * doubles that can be safely compared with one another for numerical
+     * equality.
+     * @param inString the String to convert to a decimal.
+     * @return double the value of the string as a double.
+     */
+    public static double stringToDouble(String inString) {
+        double returnValue = 0.0;
+        int    divisor     = 1;
+        String digits      = null;
+        StringTokenizer tokenizer = new StringTokenizer(inString, ".");
+
+        while (tokenizer.hasMoreTokens()) {
+            digits = tokenizer.nextToken();
+            returnValue += (Double.parseDouble(digits) / (divisor *
digits.length()) );
+            divisor *= 10;
+        }
+
+        return returnValue;
+    }
+
 }


Mime
View raw message