001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036: package com.sun.xml.ws.util;
037:
038: import java.util.StringTokenizer;
039:
040: /**
041: * Provides some version utilities.
042: *
043: * @author JAX-WS Development Team
044: */
045:
046: public final class VersionUtil {
047:
048: public static boolean isVersion20(String version) {
049: return JAXWS_VERSION_20.equals(version);
050: }
051:
052: /**
053: * @param version
054: * @return true if version is a 2.0 version
055: */
056: public static boolean isValidVersion(String version) {
057: return isVersion20(version);
058: }
059:
060: public static String getValidVersionString() {
061: return JAXWS_VERSION_20;
062: }
063:
064: /**
065: * BugFix# 4948171
066: * Method getCanonicalVersion.
067: *
068: * Converts a given version to the format "a.b.c.d"
069: * a - major version
070: * b - minor version
071: * c - minor minor version
072: * d - patch version
073: *
074: * @return int[] Canonical version number
075: */
076: public static int[] getCanonicalVersion(String version) {
077: int[] canonicalVersion = new int[4];
078:
079: // initialize the default version numbers
080: canonicalVersion[0] = 1;
081: canonicalVersion[1] = 1;
082: canonicalVersion[2] = 0;
083: canonicalVersion[3] = 0;
084:
085: final String DASH_DELIM = "_";
086: final String DOT_DELIM = ".";
087:
088: StringTokenizer tokenizer = new StringTokenizer(version,
089: DOT_DELIM);
090: String token = tokenizer.nextToken();
091:
092: // first token is major version and must not have "_"
093: canonicalVersion[0] = Integer.parseInt(token);
094:
095: // resolve the minor version
096: token = tokenizer.nextToken();
097: if (token.indexOf(DASH_DELIM) == -1) {
098: // a.b
099: canonicalVersion[1] = Integer.parseInt(token);
100: } else {
101: // a.b_c
102: StringTokenizer subTokenizer = new StringTokenizer(token,
103: DASH_DELIM);
104: canonicalVersion[1] = Integer.parseInt(subTokenizer
105: .nextToken());
106: // leave minorMinor default
107:
108: canonicalVersion[3] = Integer.parseInt(subTokenizer
109: .nextToken());
110: }
111:
112: // resolve the minorMinor and patch version, if any
113: if (tokenizer.hasMoreTokens()) {
114: token = tokenizer.nextToken();
115: if (token.indexOf(DASH_DELIM) == -1) {
116: // minorMinor
117: canonicalVersion[2] = Integer.parseInt(token);
118:
119: // resolve patch, if any
120: if (tokenizer.hasMoreTokens())
121: canonicalVersion[3] = Integer.parseInt(tokenizer
122: .nextToken());
123: } else {
124: // a.b.c_d
125: StringTokenizer subTokenizer = new StringTokenizer(
126: token, DASH_DELIM);
127: // minorMinor
128: canonicalVersion[2] = Integer.parseInt(subTokenizer
129: .nextToken());
130:
131: // patch
132: canonicalVersion[3] = Integer.parseInt(subTokenizer
133: .nextToken());
134: }
135: }
136:
137: return canonicalVersion;
138: }
139:
140: /**
141: *
142: * @param version1
143: * @param version2
144: * @return -1, 0 or 1 based upon the comparison results
145: * -1 if version1 is less than version2
146: * 0 if version1 is equal to version2
147: * 1 if version1 is greater than version2
148: */
149: public static int compare(String version1, String version2) {
150: int[] canonicalVersion1 = getCanonicalVersion(version1);
151: int[] canonicalVersion2 = getCanonicalVersion(version2);
152:
153: if (canonicalVersion1[0] < canonicalVersion2[0]) {
154: return -1;
155: } else if (canonicalVersion1[0] > canonicalVersion2[0]) {
156: return 1;
157: } else {
158: if (canonicalVersion1[1] < canonicalVersion2[1]) {
159: return -1;
160: } else if (canonicalVersion1[1] > canonicalVersion2[1]) {
161: return 1;
162: } else {
163: if (canonicalVersion1[2] < canonicalVersion2[2]) {
164: return -1;
165: } else if (canonicalVersion1[2] > canonicalVersion2[2]) {
166: return 1;
167: } else {
168: if (canonicalVersion1[3] < canonicalVersion2[3]) {
169: return -1;
170: } else if (canonicalVersion1[3] > canonicalVersion2[3]) {
171: return 1;
172: } else
173: return 0;
174: }
175: }
176: }
177: }
178:
179: public static final String JAXWS_VERSION_20 = "2.0";
180: // the latest version is default
181: public static final String JAXWS_VERSION_DEFAULT = JAXWS_VERSION_20;
182: }
|