ohair@286: /* alanb@368: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.ws.util; ohair@286: ohair@286: import java.util.StringTokenizer; ohair@286: ohair@286: ohair@286: /** ohair@286: * Provides some version utilities. ohair@286: * ohair@286: * @author JAX-WS Development Team ohair@286: */ ohair@286: ohair@286: public final class VersionUtil { ohair@286: ohair@286: public static boolean isVersion20(String version) { ohair@286: return JAXWS_VERSION_20.equals(version); ohair@286: } ohair@286: ohair@286: /** ohair@286: * @param version ohair@286: * @return true if version is a 2.0 version ohair@286: */ ohair@286: public static boolean isValidVersion(String version) { ohair@286: return isVersion20(version); ohair@286: } ohair@286: ohair@286: public static String getValidVersionString() { ohair@286: return JAXWS_VERSION_20; ohair@286: } ohair@286: ohair@286: /** ohair@286: * BugFix# 4948171 ohair@286: * Method getCanonicalVersion. ohair@286: * ohair@286: * Converts a given version to the format "a.b.c.d" ohair@286: * a - major version ohair@286: * b - minor version ohair@286: * c - minor minor version ohair@286: * d - patch version ohair@286: * ohair@286: * @return int[] Canonical version number ohair@286: */ ohair@286: public static int[] getCanonicalVersion(String version) { ohair@286: int[] canonicalVersion = new int[4]; ohair@286: ohair@286: // initialize the default version numbers ohair@286: canonicalVersion[0] = 1; ohair@286: canonicalVersion[1] = 1; ohair@286: canonicalVersion[2] = 0; ohair@286: canonicalVersion[3] = 0; ohair@286: ohair@286: final String DASH_DELIM = "_"; ohair@286: final String DOT_DELIM = "."; ohair@286: ohair@286: StringTokenizer tokenizer = ohair@286: new StringTokenizer(version, DOT_DELIM); ohair@286: String token = tokenizer.nextToken(); ohair@286: ohair@286: // first token is major version and must not have "_" ohair@286: canonicalVersion[0] = Integer.parseInt(token); ohair@286: ohair@286: // resolve the minor version ohair@286: token = tokenizer.nextToken(); ohair@286: if (token.indexOf(DASH_DELIM) == -1) { ohair@286: // a.b ohair@286: canonicalVersion[1] = Integer.parseInt(token); ohair@286: } else { ohair@286: // a.b_c ohair@286: StringTokenizer subTokenizer = ohair@286: new StringTokenizer(token, DASH_DELIM); ohair@286: canonicalVersion[1] = Integer.parseInt(subTokenizer.nextToken()); ohair@286: // leave minorMinor default ohair@286: ohair@286: canonicalVersion[3] = Integer.parseInt(subTokenizer.nextToken()); ohair@286: } ohair@286: ohair@286: // resolve the minorMinor and patch version, if any ohair@286: if (tokenizer.hasMoreTokens()) { ohair@286: token = tokenizer.nextToken(); ohair@286: if (token.indexOf(DASH_DELIM) == -1) { ohair@286: // minorMinor ohair@286: canonicalVersion[2] = Integer.parseInt(token); ohair@286: ohair@286: // resolve patch, if any ohair@286: if (tokenizer.hasMoreTokens()) ohair@286: canonicalVersion[3] = Integer.parseInt(tokenizer.nextToken()); ohair@286: } else { ohair@286: // a.b.c_d ohair@286: StringTokenizer subTokenizer = ohair@286: new StringTokenizer(token, DASH_DELIM); ohair@286: // minorMinor ohair@286: canonicalVersion[2] = Integer.parseInt(subTokenizer.nextToken()); ohair@286: ohair@286: // patch ohair@286: canonicalVersion[3] = Integer.parseInt(subTokenizer.nextToken()); ohair@286: } ohair@286: } ohair@286: ohair@286: return canonicalVersion; ohair@286: } ohair@286: ohair@286: /** ohair@286: * ohair@286: * @param version1 ohair@286: * @param version2 ohair@286: * @return -1, 0 or 1 based upon the comparison results ohair@286: * -1 if version1 is less than version2 ohair@286: * 0 if version1 is equal to version2 ohair@286: * 1 if version1 is greater than version2 ohair@286: */ ohair@286: public static int compare(String version1, String version2) { ohair@286: int[] canonicalVersion1 = getCanonicalVersion(version1); ohair@286: int[] canonicalVersion2 = getCanonicalVersion(version2); ohair@286: ohair@286: if (canonicalVersion1[0] < canonicalVersion2[0]) { ohair@286: return -1; ohair@286: } else if (canonicalVersion1[0] > canonicalVersion2[0]) { ohair@286: return 1; ohair@286: } else { ohair@286: if (canonicalVersion1[1] < canonicalVersion2[1]) { ohair@286: return -1; ohair@286: } else if (canonicalVersion1[1] > canonicalVersion2[1]) { ohair@286: return 1; ohair@286: } else { ohair@286: if (canonicalVersion1[2] < canonicalVersion2[2]) { ohair@286: return -1; ohair@286: } else if (canonicalVersion1[2] > canonicalVersion2[2]) { ohair@286: return 1; ohair@286: } else { ohair@286: if (canonicalVersion1[3] < canonicalVersion2[3]) { ohair@286: return -1; ohair@286: } else if (canonicalVersion1[3] > canonicalVersion2[3]) { ohair@286: return 1; ohair@286: } else ohair@286: return 0; ohair@286: } ohair@286: } ohair@286: } ohair@286: } ohair@286: ohair@286: public static final String JAXWS_VERSION_20 = "2.0"; ohair@286: // the latest version is default ohair@286: public static final String JAXWS_VERSION_DEFAULT = JAXWS_VERSION_20; ohair@286: }