[8780cbec] | 1 | package edu.stanford.rt.util; |
---|
| 2 | |
---|
| 3 | import java.io.*; |
---|
| 4 | import java.util.*; |
---|
| 5 | |
---|
| 6 | //import org.apache.xerces.validators.datatype.*; |
---|
| 7 | |
---|
| 8 | import edu.stanford.rt.credential.*; |
---|
| 9 | |
---|
| 10 | /** |
---|
| 11 | * @author Ninghui Li, Sandra Qiu<br> |
---|
| 12 | * |
---|
| 13 | * Utility class |
---|
| 14 | * |
---|
| 15 | */ |
---|
| 16 | public class RTUtil |
---|
| 17 | { |
---|
| 18 | // print debug msg to specified outstream. |
---|
| 19 | private static PrintStream out = System.out; |
---|
| 20 | |
---|
| 21 | private static boolean DEBUG = false; |
---|
| 22 | |
---|
| 23 | static public void setOutputStream(PrintStream out) |
---|
| 24 | { |
---|
| 25 | RTUtil.out = out; |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | * Method debugInfo. |
---|
| 30 | * @param s |
---|
| 31 | */ |
---|
| 32 | static public void debugInfo(String s) |
---|
| 33 | { |
---|
| 34 | if (DEBUG) |
---|
| 35 | out.println(s); |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | * Method parseLong. |
---|
| 40 | * parses the given attribute value as a long. If attrValue cannot |
---|
| 41 | * be correctly parsed, then returns the given default value. |
---|
| 42 | * @param attrValue |
---|
| 43 | * the string to be parsed. |
---|
| 44 | * @param defValue |
---|
| 45 | * the default value |
---|
| 46 | * @return long |
---|
| 47 | */ |
---|
| 48 | static public long parseLong(String attrValue, long defValue) |
---|
| 49 | { |
---|
| 50 | if (attrValue.length() == 0) |
---|
| 51 | return defValue; |
---|
| 52 | |
---|
| 53 | long res; |
---|
| 54 | try |
---|
| 55 | { |
---|
| 56 | res = Long.parseLong(attrValue); |
---|
| 57 | } |
---|
| 58 | catch (NumberFormatException e) |
---|
| 59 | { |
---|
| 60 | res = defValue; |
---|
| 61 | } |
---|
| 62 | return res; |
---|
| 63 | } |
---|
| 64 | /** |
---|
| 65 | * Method parseInt. |
---|
| 66 | * parses the given attribute value as an integer. If the attrValue |
---|
| 67 | * cannot be correctly parsed, returns the given default value. |
---|
| 68 | * @param attrValue |
---|
| 69 | * the string to be parsed. |
---|
| 70 | * @param defValue |
---|
| 71 | * the default value. |
---|
| 72 | * @return int |
---|
| 73 | */ |
---|
| 74 | static public int parseInt(String attrValue, int defValue) |
---|
| 75 | { |
---|
| 76 | if (attrValue.length() == 0) |
---|
| 77 | return defValue; |
---|
| 78 | |
---|
| 79 | int res; |
---|
| 80 | try |
---|
| 81 | { |
---|
| 82 | res = Integer.parseInt(attrValue); |
---|
| 83 | } |
---|
| 84 | catch (NumberFormatException e) |
---|
| 85 | { |
---|
| 86 | res = defValue; |
---|
| 87 | } |
---|
| 88 | return res; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | /** |
---|
| 92 | * Method parseBoolean. |
---|
| 93 | * parses the given attribute value as a boolean. |
---|
| 94 | * If attrValue is null or an empty string, return the |
---|
| 95 | * given default value. |
---|
| 96 | * @param attrValue |
---|
| 97 | * the string to be parsed. |
---|
| 98 | * @param defValue |
---|
| 99 | * the default value |
---|
| 100 | * @return boolean |
---|
| 101 | */ |
---|
| 102 | static public boolean parseBoolean( |
---|
| 103 | String attrValue, |
---|
| 104 | boolean defValue) |
---|
| 105 | { |
---|
| 106 | if (attrValue == null || attrValue.length() == 0) |
---|
| 107 | return defValue; |
---|
| 108 | return Boolean.valueOf(attrValue).booleanValue(); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | // static public boolean validYear(String year) |
---|
| 112 | // { |
---|
| 113 | // boolean validYear = true; |
---|
| 114 | // try |
---|
| 115 | // { |
---|
| 116 | // int yr = Integer.parseInt(year); |
---|
| 117 | // if (yr < 0 || yr > 9999) |
---|
| 118 | // validYear = false; |
---|
| 119 | // } |
---|
| 120 | // catch (NumberFormatException e) |
---|
| 121 | // { |
---|
| 122 | // validYear = false; |
---|
| 123 | // } |
---|
| 124 | // return validYear; |
---|
| 125 | // } |
---|
| 126 | // |
---|
| 127 | // static public boolean isLeapYear(String year) |
---|
| 128 | // { |
---|
| 129 | // boolean isLeap = true; |
---|
| 130 | // |
---|
| 131 | // try |
---|
| 132 | // { |
---|
| 133 | // int yr = Integer.parseInt(year); |
---|
| 134 | // if (((yr % 4 == 0) && (yr % 100 != 0)) |
---|
| 135 | // || (yr % 400 == 0)) |
---|
| 136 | // isLeap = true; |
---|
| 137 | // else |
---|
| 138 | // isLeap = false; |
---|
| 139 | // } |
---|
| 140 | // catch (NumberFormatException e) |
---|
| 141 | // { |
---|
| 142 | // isLeap = false; |
---|
| 143 | // } |
---|
| 144 | // return isLeap; |
---|
| 145 | // |
---|
| 146 | // } |
---|
| 147 | // |
---|
| 148 | // static public boolean validMonth(String month) |
---|
| 149 | // { |
---|
| 150 | // boolean validMonth = true; |
---|
| 151 | // try |
---|
| 152 | // { |
---|
| 153 | // int mon = Integer.parseInt(month); |
---|
| 154 | // if (mon < 1 || mon > 12) |
---|
| 155 | // validMonth = false; |
---|
| 156 | // } |
---|
| 157 | // catch (NumberFormatException e) |
---|
| 158 | // { |
---|
| 159 | // validMonth = false; |
---|
| 160 | // } |
---|
| 161 | // return validMonth; |
---|
| 162 | // } |
---|
| 163 | // |
---|
| 164 | // static int[] legalDaysOfMonthInLeapYear = |
---|
| 165 | // { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; |
---|
| 166 | // static int[] legalDaysOfMonth = |
---|
| 167 | // { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; |
---|
| 168 | // static public boolean validDay( |
---|
| 169 | // String year, |
---|
| 170 | // String month, |
---|
| 171 | // String day) |
---|
| 172 | // { |
---|
| 173 | // boolean validDay = true; |
---|
| 174 | // if (!validYear(year)) |
---|
| 175 | // validDay = false; |
---|
| 176 | // if (!validMonth(month)) |
---|
| 177 | // validDay = false; |
---|
| 178 | // |
---|
| 179 | // try |
---|
| 180 | // { |
---|
| 181 | // int mon = Integer.parseInt(month); |
---|
| 182 | // int dy = Integer.parseInt(day); |
---|
| 183 | // if (dy < 0) |
---|
| 184 | // validDay = false; |
---|
| 185 | // if (isLeapYear(year)) |
---|
| 186 | // { |
---|
| 187 | // if (dy > legalDaysOfMonthInLeapYear[mon]) |
---|
| 188 | // validDay = false; |
---|
| 189 | // } |
---|
| 190 | // else |
---|
| 191 | // { |
---|
| 192 | // if (dy > legalDaysOfMonth[mon]) |
---|
| 193 | // validDay = false; |
---|
| 194 | // } |
---|
| 195 | // } |
---|
| 196 | // catch (NumberFormatException e) |
---|
| 197 | // { |
---|
| 198 | // validDay = false; |
---|
| 199 | // } |
---|
| 200 | // return validDay; |
---|
| 201 | // } |
---|
| 202 | // |
---|
| 203 | // static public boolean validHour(String hour) |
---|
| 204 | // { |
---|
| 205 | // boolean validHour = true; |
---|
| 206 | // try |
---|
| 207 | // { |
---|
| 208 | // int hr = Integer.parseInt(hour); |
---|
| 209 | // if (hr < 0 || hr > 23) |
---|
| 210 | // validHour = false; |
---|
| 211 | // } |
---|
| 212 | // catch (NumberFormatException e) |
---|
| 213 | // { |
---|
| 214 | // validHour = false; |
---|
| 215 | // } |
---|
| 216 | // return validHour; |
---|
| 217 | // } |
---|
| 218 | // |
---|
| 219 | // static public boolean validMinSec(String ms) |
---|
| 220 | // { |
---|
| 221 | // boolean valid = true; |
---|
| 222 | // try |
---|
| 223 | // { |
---|
| 224 | // int minSec = Integer.parseInt(ms); |
---|
| 225 | // if (minSec < 0 || minSec > 59) |
---|
| 226 | // valid = false; |
---|
| 227 | // } |
---|
| 228 | // catch (NumberFormatException e) |
---|
| 229 | // { |
---|
| 230 | // valid = false; |
---|
| 231 | // } |
---|
| 232 | // return valid; |
---|
| 233 | // } |
---|
| 234 | } |
---|