Home | History | Annotate | Download | only in impl
      1 //  2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html#License
      3 /**
      4  *******************************************************************************
      5  * Copyright (C) 2001-2010, International Business Machines Corporation and    *
      6  * others. All Rights Reserved.                                                *
      7  *******************************************************************************
      8  */
      9 package com.ibm.icu.impl;
     10 
     11 import com.ibm.icu.util.VersionInfo;
     12 
     13 public final class ICUDebug {
     14     private static String params;
     15     static {
     16         try {
     17             params = System.getProperty("ICUDebug");
     18         }
     19         catch (SecurityException e) {
     20         }
     21     }
     22     private static boolean debug = params != null;
     23     private static boolean help = debug && (params.equals("") || params.indexOf("help") != -1);
     24 
     25     static {
     26         if (debug) {
     27             System.out.println("\nICUDebug=" + params);
     28         }
     29     }
     30 
     31     public static final String javaVersionString = System.getProperty("java.version", "0");
     32     public static final boolean isJDK14OrHigher;
     33     public static final VersionInfo javaVersion;
     34 
     35     public static VersionInfo getInstanceLenient(String s) {
     36         // Extracting ASCII numbers up to 4 delimited by
     37         // any non digit characters
     38         int[] ver = new int[4];
     39         boolean numeric = false;
     40         int i = 0, vidx = 0;
     41         while (i < s.length()) {
     42             char c = s.charAt(i++);
     43             if (c < '0' || c > '9') {
     44                 if (numeric) {
     45                     if (vidx == 3) {
     46                         // up to 4 numbers
     47                         break;
     48                     }
     49                     numeric = false;
     50                     vidx++;
     51                 }
     52             } else {
     53                 if (numeric) {
     54                     ver[vidx] = ver[vidx] * 10 + (c - '0');
     55                     if (ver[vidx] > 255) {
     56                         // VersionInfo does not support numbers
     57                         // greater than 255.  In such case, we
     58                         // ignore the number and the rest
     59                         ver[vidx] = 0;
     60                         break;
     61                     }
     62                 } else {
     63                     numeric = true;
     64                     ver[vidx] = c - '0';
     65                 }
     66             }
     67         }
     68 
     69         return VersionInfo.getInstance(ver[0], ver[1], ver[2], ver[3]);
     70     }
     71 
     72     static {
     73         javaVersion = getInstanceLenient(javaVersionString);
     74 
     75         VersionInfo java14Version = VersionInfo.getInstance("1.4.0");
     76 
     77         isJDK14OrHigher = javaVersion.compareTo(java14Version) >= 0;
     78     }
     79 
     80     public static boolean enabled() {
     81         return debug;
     82     }
     83 
     84     public static boolean enabled(String arg) {
     85         if (debug) {
     86             boolean result = params.indexOf(arg) != -1;
     87             if (help) System.out.println("\nICUDebug.enabled(" + arg + ") = " + result);
     88             return result;
     89         }
     90         return false;
     91     }
     92 
     93     public static String value(String arg) {
     94         String result = "false";
     95         if (debug) {
     96             int index = params.indexOf(arg);
     97             if (index != -1) {
     98                 index += arg.length();
     99                 if (params.length() > index && params.charAt(index) == '=') {
    100                     index += 1;
    101                     int limit = params.indexOf(",", index);
    102                     result = params.substring(index, limit == -1 ? params.length() : limit);
    103                 } else {
    104                     result = "true";
    105                 }
    106             }
    107 
    108             if (help) System.out.println("\nICUDebug.value(" + arg + ") = " + result);
    109         }
    110         return result;
    111     }
    112 
    113 //    static public void main(String[] args) {
    114 //        // test
    115 //        String[] tests = {
    116 //            "1.3.0",
    117 //            "1.3.0_02",
    118 //            "1.3.1ea",
    119 //            "1.4.1b43",
    120 //            "___41___5",
    121 //            "x1.4.51xx89ea.7f",
    122 //            "1.6_2009",
    123 //            "10-100-1000-10000",
    124 //            "beta",
    125 //            "0",
    126 //        };
    127 //        for (int i = 0; i < tests.length; ++i) {
    128 //            System.out.println(tests[i] + " => " + getInstanceLenient(tests[i]));
    129 //        }
    130 //    }
    131 }
    132