3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync/* ***** BEGIN LICENSE BLOCK *****
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * The contents of this file are subject to the Mozilla Public License Version
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * 1.1 (the "License"); you may not use this file except in compliance with
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * the License. You may obtain a copy of the License at
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * Software distributed under the License is distributed on an "AS IS" basis,
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * for the specific language governing rights and limitations under the
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * The Original Code is Java XPCOM Bindings.
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * The Initial Developer of the Original Code is
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * IBM Corporation.
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * Portions created by the Initial Developer are Copyright (C) 2005
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * IBM Corporation. All Rights Reserved.
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * Contributor(s):
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * Javier Pedemonte (jhpedemonte@gmail.com)
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * Alternatively, the contents of this file may be used under the terms of
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * either the GNU General Public License Version 2 or later (the "GPL"), or
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * in which case the provisions of the GPL or the LGPL are applicable instead
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * of those above. If you wish to allow use of your version of this file only
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * under the terms of either the GPL or the LGPL, and not to allow others to
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * use your version of this file under the terms of the MPL, indicate your
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * decision by deleting the provisions above and replace them with the notice
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * and other provisions required by the GPL or the LGPL. If you do not delete
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * the provisions above, a recipient may use your version of this file under
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * the terms of any one of the MPL, the GPL or the LGPL.
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * ***** END LICENSE BLOCK ***** */
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync/* import org.mozilla.interfaces.nsIVersionComparator; */
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * Version strings are dot-separated sequences of version-parts.
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * A version-part consists of up to four parts, all of which are optional:
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * <br><code>
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * <number-a><string-b><number-c>
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * <string-d (everything else)>
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * </code> <p>
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * A version-part may also consist of a single asterisk "*" which indicates
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * "infinity".
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * Numbers are base-10, and are zero if left out.
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * Strings are compared bytewise.
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * For additional backwards compatibility, if "string-b" is "+" then
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * "number-a" is incremented by 1 and "string-b" becomes "pre".
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * <p> <pre>
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * < 1.0pre2
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * < 1.0 == 1.0.0 == 1.0.0.0
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * < 1.1pre == 1.1pre0 == 1.0+
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * < 1.1pre1a
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * < 1.1pre1
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * < 1.1pre10a
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * < 1.1pre10
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * Although not required by this interface, it is recommended that
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * numbers remain within the limits of a signed char, i.e. -127 to 128.
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsyncpublic class VersionComparator implements nsISupports /* implements nsIVersionComparator */ {
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * Compare two version strings
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * @param A a version string
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * @param B a version string
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * @return a value less than 0 if A < B;
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * the value 0 if A == B;
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * or a value greater than 0 if A > B
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync String a = A, b = B;
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync private static String parseVersionPart(String aVersion, VersionPart result) {
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync StringTokenizer tok = new StringTokenizer(aVersion.trim(), ".");
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync VersionPartTokenizer vertok = new VersionPartTokenizer(part);
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync result.numA = Integer.parseInt(vertok.nextToken());
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync // parsing error; default to zero like 'strtol' C function
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync // if part is of type "<num>+"
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync // else if part is of type "<num><alpha>..."
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync result.numC = Integer.parseInt(vertok.nextToken());
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync // parsing error; default to zero like 'strtol' C function
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync // return everything after "."
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync private int compareVersionPart(VersionPart va, VersionPart vb) {
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync private int compareString(String str1, String str2) {
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync // any string is *before* no string
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * Specialized tokenizer for Mozilla version strings. A token can
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * consist of one of the four sections of a version string: <code>
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * <number-a><string-b><number-c>
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * <string-d (everything else)></code>.
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync // if string starts with a number...
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync if (part.charAt(0) == '+' || part.charAt(0) == '-') {
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync while (index < part.length() && Character.isDigit(part.charAt(index))) {
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync // ... or if this is the non-numeric part of version string
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync while (index < part.length() && !Character.isDigit(part.charAt(index))) {
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * Returns what remains of the original string, without tokenization. This
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * method is useful for getting the <code><string-d (everything else)>
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * </code> section of a version string.
3ac4cd918f9ffed59c4cc988665d1e09c5d7304avboxsync * @return remaining version string