KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > condition > AntVersion


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs.condition;
20
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.Project;
23 import org.apache.tools.ant.util.DeweyDecimal;
24
25 /**
26  * An Ant version condition.
27  * @since Ant 1.7
28  */

29 public class AntVersion implements Condition {
30
31     private String JavaDoc atLeast = null;
32     private String JavaDoc exactly = null;
33
34     /**
35      * Evalute the condition.
36      * @return true if the condition is true.
37      * @throws BuildException if an error occurs.
38      */

39     public boolean eval() throws BuildException {
40         validate();
41         DeweyDecimal actual = getVersion();
42         if (null != atLeast) {
43             return actual.isGreaterThanOrEqual(new DeweyDecimal(atLeast));
44         }
45         if (null != exactly) {
46             return actual.isEqual(new DeweyDecimal(exactly));
47         }
48         //default
49
return false;
50     }
51
52     private void validate() throws BuildException {
53         if (atLeast != null && exactly != null) {
54             throw new BuildException("Only one of atleast or exactly may be set.");
55         }
56         if (null == atLeast && null == exactly) {
57             throw new BuildException("One of atleast or exactly must be set.");
58         }
59         try {
60             if (atLeast != null) {
61                 new DeweyDecimal(atLeast);
62             } else {
63                 new DeweyDecimal(exactly);
64             }
65         } catch (NumberFormatException JavaDoc e) {
66             throw new BuildException("The argument is not a Dewey Decimal eg 1.1.0");
67         }
68     }
69
70     private DeweyDecimal getVersion() {
71         Project p = new Project();
72         p.init();
73         char[] versionString = p.getProperty("ant.version").toCharArray();
74         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
75         boolean foundFirstDigit = false;
76         for (int i = 0; i < versionString.length; i++) {
77             if (Character.isDigit(versionString[i])) {
78                 sb.append(versionString[i]);
79                 foundFirstDigit = true;
80             }
81             if (versionString[i] == '.' && foundFirstDigit) {
82                 sb.append(versionString[i]);
83             }
84             if (Character.isLetter(versionString[i]) && foundFirstDigit) {
85                 break;
86             }
87         }
88         return new DeweyDecimal(sb.toString());
89     }
90
91     /**
92      * Get the atleast attribute.
93      * @return the atleast attribute.
94      */

95     public String JavaDoc getAtLeast() {
96         return atLeast;
97     }
98
99     /**
100      * Set the atleast attribute.
101      * This is of the form major.minor.point.
102      * For example 1.7.0.
103      * @param atLeast the version to check against.
104      */

105     public void setAtLeast(String JavaDoc atLeast) {
106         this.atLeast = atLeast;
107     }
108
109     /**
110      * Get the exactly attribute.
111      * @return the exactly attribute.
112      */

113     public String JavaDoc getExactly() {
114         return exactly;
115     }
116
117     /**
118      * Set the exactly attribute.
119      * This is of the form major.minor.point.
120      * For example 1.7.0.
121      * @param exactly the version to check against.
122      */

123     public void setExactly(String JavaDoc exactly) {
124         this.exactly = exactly;
125     }
126 }
127
Popular Tags