001: /**
002: *******************************************************************************
003: * Copyright (C) 2001-2002, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */package com.ibm.icu.impl;
007:
008: import com.ibm.icu.util.VersionInfo;
009:
010: public final class ICUDebug {
011: private static String params;
012: static {
013: try {
014: params = System.getProperty("ICUDebug");
015: } catch (SecurityException e) {
016: }
017: }
018: private static boolean debug = params != null;
019: private static boolean help = debug
020: && (params.equals("") || params.indexOf("help") != -1);
021:
022: static {
023: if (debug) {
024: System.out.println("\nICUDebug=" + params);
025: }
026: }
027:
028: public static final String javaVersionString = System
029: .getProperty("java.version");
030: public static final boolean isJDK14OrHigher;
031: public static final VersionInfo javaVersion;
032:
033: public static VersionInfo getInstanceLenient(String s) {
034: // clean string
035: // preserve only digits, separated by single '.'
036: // ignore over 4 digit sequences
037: // does not test < 255, very odd...
038:
039: char[] chars = s.toCharArray();
040: int r = 0, w = 0, count = 0;
041: boolean numeric = false; // ignore leading non-numerics
042: while (r < chars.length) {
043: char c = chars[r++];
044: if (c < '0' || c > '9') {
045: if (numeric) {
046: if (count == 3) {
047: // only four digit strings allowed
048: break;
049: }
050: numeric = false;
051: chars[w++] = '.';
052: ++count;
053: }
054: } else {
055: numeric = true;
056: chars[w++] = c;
057: }
058: }
059: while (w > 0 && chars[w - 1] == '.') {
060: --w;
061: }
062:
063: String vs = new String(chars, 0, w);
064:
065: return VersionInfo.getInstance(vs);
066: }
067:
068: static {
069: javaVersion = getInstanceLenient(javaVersionString);
070:
071: VersionInfo java14Version = VersionInfo.getInstance("1.4.0");
072:
073: isJDK14OrHigher = javaVersion.compareTo(java14Version) >= 0;
074: }
075:
076: public static boolean enabled() {
077: return debug;
078: }
079:
080: public static boolean enabled(String arg) {
081: if (debug) {
082: boolean result = params.indexOf(arg) != -1;
083: if (help)
084: System.out.println("\nICUDebug.enabled(" + arg + ") = "
085: + result);
086: return result;
087: }
088: return false;
089: }
090:
091: public static String value(String arg) {
092: String result = "false";
093: if (debug) {
094: int index = params.indexOf(arg);
095: if (index != -1) {
096: index += arg.length();
097: if (params.length() > index
098: && params.charAt(index) == '=') {
099: index += 1;
100: int limit = params.indexOf(",", index);
101: result = params.substring(index,
102: limit == -1 ? params.length() : limit);
103: } else {
104: result = "true";
105: }
106: }
107:
108: if (help)
109: System.out.println("\nICUDebug.value(" + arg + ") = "
110: + result);
111: }
112: return result;
113: }
114:
115: static public void main(String[] args) {
116: // test
117: String[] tests = { "1.3.0", "1.3.0_02", "1.3.1ea", "1.4.1b43",
118: "___41___5", "x1.4.51xx89ea.7f" };
119: for (int i = 0; i < tests.length; ++i) {
120: System.out.println(tests[i] + " => "
121: + getInstanceLenient(tests[i]));
122: }
123: }
124: }
|