|
1 /* |
|
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.xml.internal.ws.util; |
|
27 |
|
28 import java.util.StringTokenizer; |
|
29 |
|
30 |
|
31 /** |
|
32 * Provides some version utilities. |
|
33 * |
|
34 * @author JAX-WS Development Team |
|
35 */ |
|
36 |
|
37 public final class VersionUtil { |
|
38 |
|
39 public static boolean isVersion20(String version) { |
|
40 return JAXWS_VERSION_20.equals(version); |
|
41 } |
|
42 |
|
43 /** |
|
44 * @param version |
|
45 * @return true if version is a 2.0 version |
|
46 */ |
|
47 public static boolean isValidVersion(String version) { |
|
48 return isVersion20(version); |
|
49 } |
|
50 |
|
51 public static String getValidVersionString() { |
|
52 return JAXWS_VERSION_20; |
|
53 } |
|
54 |
|
55 /** |
|
56 * BugFix# 4948171 |
|
57 * Method getCanonicalVersion. |
|
58 * |
|
59 * Converts a given version to the format "a.b.c.d" |
|
60 * a - major version |
|
61 * b - minor version |
|
62 * c - minor minor version |
|
63 * d - patch version |
|
64 * |
|
65 * @return int[] Canonical version number |
|
66 */ |
|
67 public static int[] getCanonicalVersion(String version) { |
|
68 int[] canonicalVersion = new int[4]; |
|
69 |
|
70 // initialize the default version numbers |
|
71 canonicalVersion[0] = 1; |
|
72 canonicalVersion[1] = 1; |
|
73 canonicalVersion[2] = 0; |
|
74 canonicalVersion[3] = 0; |
|
75 |
|
76 final String DASH_DELIM = "_"; |
|
77 final String DOT_DELIM = "."; |
|
78 |
|
79 StringTokenizer tokenizer = |
|
80 new StringTokenizer(version, DOT_DELIM); |
|
81 String token = tokenizer.nextToken(); |
|
82 |
|
83 // first token is major version and must not have "_" |
|
84 canonicalVersion[0] = Integer.parseInt(token); |
|
85 |
|
86 // resolve the minor version |
|
87 token = tokenizer.nextToken(); |
|
88 if (token.indexOf(DASH_DELIM) == -1) { |
|
89 // a.b |
|
90 canonicalVersion[1] = Integer.parseInt(token); |
|
91 } else { |
|
92 // a.b_c |
|
93 StringTokenizer subTokenizer = |
|
94 new StringTokenizer(token, DASH_DELIM); |
|
95 canonicalVersion[1] = Integer.parseInt(subTokenizer.nextToken()); |
|
96 // leave minorMinor default |
|
97 |
|
98 canonicalVersion[3] = Integer.parseInt(subTokenizer.nextToken()); |
|
99 } |
|
100 |
|
101 // resolve the minorMinor and patch version, if any |
|
102 if (tokenizer.hasMoreTokens()) { |
|
103 token = tokenizer.nextToken(); |
|
104 if (token.indexOf(DASH_DELIM) == -1) { |
|
105 // minorMinor |
|
106 canonicalVersion[2] = Integer.parseInt(token); |
|
107 |
|
108 // resolve patch, if any |
|
109 if (tokenizer.hasMoreTokens()) |
|
110 canonicalVersion[3] = Integer.parseInt(tokenizer.nextToken()); |
|
111 } else { |
|
112 // a.b.c_d |
|
113 StringTokenizer subTokenizer = |
|
114 new StringTokenizer(token, DASH_DELIM); |
|
115 // minorMinor |
|
116 canonicalVersion[2] = Integer.parseInt(subTokenizer.nextToken()); |
|
117 |
|
118 // patch |
|
119 canonicalVersion[3] = Integer.parseInt(subTokenizer.nextToken()); |
|
120 } |
|
121 } |
|
122 |
|
123 return canonicalVersion; |
|
124 } |
|
125 |
|
126 /** |
|
127 * |
|
128 * @param version1 |
|
129 * @param version2 |
|
130 * @return -1, 0 or 1 based upon the comparison results |
|
131 * -1 if version1 is less than version2 |
|
132 * 0 if version1 is equal to version2 |
|
133 * 1 if version1 is greater than version2 |
|
134 */ |
|
135 public static int compare(String version1, String version2) { |
|
136 int[] canonicalVersion1 = getCanonicalVersion(version1); |
|
137 int[] canonicalVersion2 = getCanonicalVersion(version2); |
|
138 |
|
139 if (canonicalVersion1[0] < canonicalVersion2[0]) { |
|
140 return -1; |
|
141 } else if (canonicalVersion1[0] > canonicalVersion2[0]) { |
|
142 return 1; |
|
143 } else { |
|
144 if (canonicalVersion1[1] < canonicalVersion2[1]) { |
|
145 return -1; |
|
146 } else if (canonicalVersion1[1] > canonicalVersion2[1]) { |
|
147 return 1; |
|
148 } else { |
|
149 if (canonicalVersion1[2] < canonicalVersion2[2]) { |
|
150 return -1; |
|
151 } else if (canonicalVersion1[2] > canonicalVersion2[2]) { |
|
152 return 1; |
|
153 } else { |
|
154 if (canonicalVersion1[3] < canonicalVersion2[3]) { |
|
155 return -1; |
|
156 } else if (canonicalVersion1[3] > canonicalVersion2[3]) { |
|
157 return 1; |
|
158 } else |
|
159 return 0; |
|
160 } |
|
161 } |
|
162 } |
|
163 } |
|
164 |
|
165 public static final String JAXWS_VERSION_20 = "2.0"; |
|
166 // the latest version is default |
|
167 public static final String JAXWS_VERSION_DEFAULT = JAXWS_VERSION_20; |
|
168 } |