From ef30b77fab27262f8b5ef3f98880c0caec30bab5 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Thu, 27 Oct 2011 04:59:48 +0200 Subject: Fix VersionNumber cstr w/ string parsing: cutt-off non-digit-parts, suppress exceptions --- src/java/com/jogamp/common/util/VersionNumber.java | 41 +++++++++++----------- 1 file changed, 21 insertions(+), 20 deletions(-) (limited to 'src/java/com') diff --git a/src/java/com/jogamp/common/util/VersionNumber.java b/src/java/com/jogamp/common/util/VersionNumber.java index 57c83da..1225231 100644 --- a/src/java/com/jogamp/common/util/VersionNumber.java +++ b/src/java/com/jogamp/common/util/VersionNumber.java @@ -47,26 +47,27 @@ public class VersionNumber implements Comparable { * @param delim the delimiter, e.g. "." */ public VersionNumber(String versionString, String delim) { - try { - StringTokenizer tok = new StringTokenizer(versionString, delim); - if (!tok.hasMoreTokens()) { - major = 0; - return; - } - major = Integer.valueOf(tok.nextToken()).intValue(); - if (!tok.hasMoreTokens()) { - minor = 0; - return; - } - minor = Integer.valueOf(tok.nextToken()).intValue(); - if (!tok.hasMoreTokens()) { - sub = 0; - return; - } - sub = Integer.valueOf(tok.nextToken()).intValue(); - } catch (Exception e) { - throw new IllegalArgumentException("version <"+versionString+">, delim <"+delim+">", e); + final java.util.regex.Pattern nonDigitsCutOff = java.util.regex.Pattern.compile("\\D.*"); + major = 0; + minor = 0; + sub = 0; + + StringTokenizer tok = new StringTokenizer(versionString, delim); + if (tok.hasMoreTokens()) { + try { + major = Integer.parseInt(nonDigitsCutOff.matcher(tok.nextToken()).replaceAll("")); + } catch (NumberFormatException e) { } } + if (tok.hasMoreTokens()) { + try { + minor = Integer.parseInt(nonDigitsCutOff.matcher(tok.nextToken()).replaceAll("")); + } catch (NumberFormatException e) { } + } + if (tok.hasMoreTokens()) { + try { + sub = Integer.parseInt(nonDigitsCutOff.matcher(tok.nextToken()).replaceAll("")); + } catch (NumberFormatException e) { } + } } protected VersionNumber() { } @@ -86,7 +87,7 @@ public class VersionNumber implements Comparable { public final int compareTo(Object o) { if ( ! ( o instanceof VersionNumber ) ) { - Class c = (null != o) ? o.getClass() : null ; + Class c = (null != o) ? o.getClass() : null ; throw new ClassCastException("Not a Capabilities object: " + c); } -- cgit v1.2.3