KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibm > icu > impl > ICUDebug


1 /**
2  *******************************************************************************
3  * Copyright (C) 2001-2002, International Business Machines Corporation and *
4  * others. All Rights Reserved. *
5  *******************************************************************************
6  */

7 package com.ibm.icu.impl;
8
9 import com.ibm.icu.util.VersionInfo;
10
11 public final class ICUDebug {
12     private static String JavaDoc params;
13     static {
14         try {
15             params = System.getProperty("ICUDebug");
16         }
17         catch (SecurityException JavaDoc e) {
18         }
19     }
20     private static boolean debug = params != null;
21     private static boolean help = debug && (params.equals("") || params.indexOf("help") != -1);
22
23     static {
24         if (debug) {
25             System.out.println("\nICUDebug=" + params);
26         }
27     }
28
29     public static final String JavaDoc javaVersionString = System.getProperty("java.version");
30     public static final boolean isJDK14OrHigher;
31     public static final VersionInfo javaVersion;
32
33     public static VersionInfo getInstanceLenient(String JavaDoc s) {
34         // clean string
35
// preserve only digits, separated by single '.'
36
// ignore over 4 digit sequences
37
// does not test < 255, very odd...
38

39         char[] chars = s.toCharArray();
40         int r = 0, w = 0, count = 0;
41         boolean numeric = false; // ignore leading non-numerics
42
while (r < chars.length) {
43             char c = chars[r++];
44             if (c < '0' || c > '9') {
45                 if (numeric) {
46                     if (count == 3) {
47                         // only four digit strings allowed
48
break;
49                     }
50                     numeric = false;
51                     chars[w++] = '.';
52                     ++count;
53                 }
54             } else {
55                 numeric = true;
56                 chars[w++] = c;
57             }
58         }
59         while (w > 0 && chars[w-1] == '.') {
60             --w;
61         }
62     
63         String JavaDoc vs = new String JavaDoc(chars, 0, w);
64
65         return VersionInfo.getInstance(vs);
66     }
67
68     static {
69         javaVersion = getInstanceLenient(javaVersionString);
70
71         VersionInfo java14Version = VersionInfo.getInstance("1.4.0");
72
73         isJDK14OrHigher = javaVersion.compareTo(java14Version) >= 0;
74     }
75
76     public static boolean enabled() {
77         return debug;
78     }
79
80     public static boolean enabled(String JavaDoc arg) {
81         if (debug) {
82             boolean result = params.indexOf(arg) != -1;
83             if (help) System.out.println("\nICUDebug.enabled(" + arg + ") = " + result);
84             return result;
85         }
86         return false;
87     }
88
89     public static String JavaDoc value(String JavaDoc arg) {
90         String JavaDoc result = "false";
91         if (debug) {
92             int index = params.indexOf(arg);
93             if (index != -1) {
94                 index += arg.length();
95                 if (params.length() > index && params.charAt(index) == '=') {
96                     index += 1;
97                     int limit = params.indexOf(",", index);
98                     result = params.substring(index, limit == -1 ? params.length() : limit);
99                 } else {
100                     result = "true";
101                 }
102             }
103
104             if (help) System.out.println("\nICUDebug.value(" + arg + ") = " + result);
105         }
106         return result;
107     }
108
109     static public void main(String JavaDoc[] args) {
110         // test
111
String JavaDoc[] tests = {
112             "1.3.0",
113             "1.3.0_02",
114             "1.3.1ea",
115             "1.4.1b43",
116             "___41___5",
117             "x1.4.51xx89ea.7f"
118         };
119         for (int i = 0; i < tests.length; ++i) {
120             System.out.println(tests[i] + " => " + getInstanceLenient(tests[i]));
121         }
122     }
123 }
124
Popular Tags