1 | /*
|
---|
2 | * Copyright 2005 Paul Hinds
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | */
|
---|
16 | package org.tp23.antinstaller.runtime;
|
---|
17 |
|
---|
18 | import java.util.StringTokenizer;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Version helper accepts version numbers of the following format.
|
---|
22 | * [major][clause].[minor][clause].[minor][clause].... ad infinitum
|
---|
23 | *
|
---|
24 | * If the Java flag is set to true an attempt is made to parse the string as if it were
|
---|
25 | * returned by System.getProperty("java.version");
|
---|
26 | * Since pre 1.3.1 the system is not parsable the default is to accept the string
|
---|
27 | * if there is a format error.
|
---|
28 | *
|
---|
29 | * For the non java syntax getting the string wrong will result in failed tests
|
---|
30 | * @author teknopaul
|
---|
31 | *
|
---|
32 | */
|
---|
33 | public class VersionHelper {
|
---|
34 |
|
---|
35 | public static final String CLAUSE_ALPHA = "alpha";
|
---|
36 | public static final String CLAUSE_BETA = "beta";
|
---|
37 | public static final String CLAUSE_GAMMA = "gamma";
|
---|
38 | public static final String CLAUSE_JAVA_BETA = "ea";
|
---|
39 |
|
---|
40 | public boolean equalOrHigher(final String test, final String version) {
|
---|
41 | return equalOrHigher(test, version, false);
|
---|
42 | }
|
---|
43 |
|
---|
44 | public boolean majorVersionCompatible(final String test, final String version) {
|
---|
45 | return getMajorVersion(test) == getMajorVersion(version);
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | *
|
---|
50 | * @param test java version string being tested
|
---|
51 | * @param version java version string being used as reference
|
---|
52 | * @param javaSyntax
|
---|
53 | * @return true if the value of <code>test</code> is greater than or equal to the value of <code>version</code>
|
---|
54 | */
|
---|
55 | public boolean equalOrHigher(final String test, final String version, final boolean javaSyntax) {
|
---|
56 | try {
|
---|
57 | StringTokenizer testSt = new StringTokenizer(test, ".");
|
---|
58 | StringTokenizer verSt = new StringTokenizer(version, ".");
|
---|
59 |
|
---|
60 | while (true) {
|
---|
61 | boolean testMore = testSt.hasMoreTokens();
|
---|
62 | boolean verMore = verSt.hasMoreTokens();
|
---|
63 | if( ! testMore || ! verMore ){
|
---|
64 | break;
|
---|
65 | }
|
---|
66 | String testToken = testSt.nextToken();
|
---|
67 | String verToken = verSt.nextToken();
|
---|
68 | short testVer = getVersion(testToken);
|
---|
69 | short versionVer = getVersion(verToken);
|
---|
70 | if( testVer == versionVer ) {
|
---|
71 | if ( equalClause(getClause( testToken ), getClause(verToken)) ) {
|
---|
72 | continue;
|
---|
73 | }
|
---|
74 | else {
|
---|
75 | return higherClause(getClause( testToken ), getClause( verToken ), javaSyntax);
|
---|
76 | }
|
---|
77 | }
|
---|
78 | return testVer > versionVer ;
|
---|
79 | }
|
---|
80 | // equal up to one not having minor details
|
---|
81 | if( countDots(test) >= countDots(version) ){
|
---|
82 | return true;
|
---|
83 | }
|
---|
84 | return test.equals(version);
|
---|
85 | } catch (Exception e) {
|
---|
86 | // syntax exceptions
|
---|
87 | if(javaSyntax){
|
---|
88 | return true; // return true for Java since pre 1.3.1 or other JVMs could get any old rubbish
|
---|
89 | }
|
---|
90 | return false;
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | public boolean isValid(final String version) {
|
---|
95 | try {
|
---|
96 | StringTokenizer verSt = new StringTokenizer(version, ".");
|
---|
97 |
|
---|
98 | boolean verMore = false;
|
---|
99 | int i = 0;
|
---|
100 | for (; verMore = verSt.hasMoreTokens(); i++) {
|
---|
101 |
|
---|
102 | String verToken = verSt.nextToken();
|
---|
103 | if("".equals(verToken)){
|
---|
104 | return false;
|
---|
105 | }
|
---|
106 | // may throw NumberFormatExceptions
|
---|
107 | getVersion(verToken);
|
---|
108 | String clause = getClause(verToken);
|
---|
109 | if( ! "".equals(clause)){
|
---|
110 | short clauseS = clauseToShort(clause);
|
---|
111 | if( ! (clauseS == 1 || clauseS == 2 || clauseS == 3 )){
|
---|
112 | return false;
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|
116 | if( ! verMore && i == 0) {
|
---|
117 | return false; // nothing there!
|
---|
118 | }
|
---|
119 | return true;
|
---|
120 |
|
---|
121 | }
|
---|
122 | catch (Exception e) {
|
---|
123 | // syntax exceptions
|
---|
124 | return false;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | /**
|
---|
128 | * @return the number part of the clause
|
---|
129 | */
|
---|
130 | private short getVersion(final String section) {
|
---|
131 | StringBuffer sb = new StringBuffer();
|
---|
132 | for (int i = 0; i < section.length(); i++) {
|
---|
133 | char c = section.charAt(i);
|
---|
134 | if(Character.isDigit(c)) {
|
---|
135 | sb.append(c);
|
---|
136 | }
|
---|
137 | else{
|
---|
138 | return Short.parseShort( sb.toString() );
|
---|
139 | }
|
---|
140 | }
|
---|
141 | if(sb.length() > 0) {
|
---|
142 | return Short.parseShort( sb.toString() );
|
---|
143 | }
|
---|
144 | return 0;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * @return the clause eg beta or ""
|
---|
149 | */
|
---|
150 | private String getClause(final String section) {
|
---|
151 | for (int i = 0; i < section.length(); i++) {
|
---|
152 | char c = section.charAt(i);
|
---|
153 | if(Character.isDigit(c)) {
|
---|
154 | continue;
|
---|
155 | }
|
---|
156 | else {
|
---|
157 | return section.substring(i);
|
---|
158 | }
|
---|
159 | }
|
---|
160 | return "";
|
---|
161 | }
|
---|
162 |
|
---|
163 | private boolean higherClause(final String test, final String clause, final boolean javaSyntax) {
|
---|
164 | if(javaSyntax) {
|
---|
165 | return clauseJavaToShort(test) > clauseJavaToShort(clause);
|
---|
166 | }
|
---|
167 | else {
|
---|
168 | return clauseToShort(test) > clauseToShort(clause);
|
---|
169 | }
|
---|
170 |
|
---|
171 | }
|
---|
172 | private boolean equalClause(final String test, final String clause) {
|
---|
173 | return clauseToShort(test) == clauseToShort(clause);
|
---|
174 | }
|
---|
175 |
|
---|
176 | private short clauseToShort(String clause) {
|
---|
177 | if(clause.startsWith("-")){
|
---|
178 | clause = clause.substring(1); // knock off java style 1-beta dashes
|
---|
179 | }
|
---|
180 | if( CLAUSE_ALPHA.equals(clause) ) {
|
---|
181 | return 3;
|
---|
182 | }
|
---|
183 | else if( CLAUSE_BETA.equals(clause) ) {
|
---|
184 | return 2;
|
---|
185 | }
|
---|
186 | else if( CLAUSE_GAMMA.equals(clause) ) {
|
---|
187 | return 1;
|
---|
188 | }
|
---|
189 | if(clause.startsWith("_")) { // java build version support 1_03-ea-beta (discarding the sub sub clause "-ea-beta" because I'm lazy)
|
---|
190 | int hasDash = clause.indexOf('-');
|
---|
191 | if(hasDash > -1) {
|
---|
192 | return Short.parseShort(clause.substring(1, hasDash));
|
---|
193 | }
|
---|
194 | else {
|
---|
195 | return Short.parseShort(clause.substring(1));
|
---|
196 | }
|
---|
197 | }
|
---|
198 | else return Short.MAX_VALUE; // no clause assumes higher
|
---|
199 | }
|
---|
200 |
|
---|
201 | private short clauseJavaToShort(String clause) {
|
---|
202 | if(clause.startsWith("-")){
|
---|
203 | clause = clause.substring(1); // knock off java style 1-beta dashes
|
---|
204 | }
|
---|
205 | else if( CLAUSE_JAVA_BETA.equals(clause) ) { // -ea early access are assumed to be less good
|
---|
206 | return -2;
|
---|
207 | }
|
---|
208 | if(clause.startsWith("_")) { // java build version support 1_03-ea-beta (discarding the sub sub clause "-ea-beta" because I'm lazy)
|
---|
209 | int hasDash = clause.indexOf('-');
|
---|
210 | if(hasDash > -1) {
|
---|
211 | return Short.parseShort(clause.substring(1, hasDash));
|
---|
212 | }
|
---|
213 | else {
|
---|
214 | return Short.parseShort(clause.substring(1));
|
---|
215 | }
|
---|
216 | }
|
---|
217 | else return 0; // no clause assumes lower in Java speak
|
---|
218 | }
|
---|
219 |
|
---|
220 | private short countDots(final String fullver){
|
---|
221 | short count = 0;
|
---|
222 | for (int i = 0; i < fullver.length(); i++) {
|
---|
223 | if(fullver.charAt(i) == '.') {
|
---|
224 | count++;
|
---|
225 | }
|
---|
226 | }
|
---|
227 | return count;
|
---|
228 | }
|
---|
229 |
|
---|
230 | private short getMajorVersion(String test){
|
---|
231 | return getVersion(test);
|
---|
232 | }
|
---|
233 |
|
---|
234 | }
|
---|