1 21 package org.apache.derby.iapi.tools.i18n; 22 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.io.UnsupportedEncodingException ; 26 27 import java.util.ResourceBundle ; 28 import java.util.Date ; 29 import java.util.Locale ; 30 import java.util.StringTokenizer ; 31 32 import java.text.MessageFormat ; 33 import java.text.NumberFormat ; 34 import java.text.DecimalFormat ; 35 import java.text.DateFormat ; 36 import java.text.ParseException ; 37 import java.text.FieldPosition ; 38 39 import java.sql.Timestamp ; 40 import java.sql.ResultSet ; 41 import java.sql.ResultSetMetaData ; 42 import java.sql.SQLException ; 43 import java.sql.Types ; 44 45 46 public final class LocalizedResource implements java.security.PrivilegedAction { 47 48 private static final boolean HAVE_BIG_DECIMAL; 49 50 static { 51 boolean haveBigDecimal; 52 try { 53 Class.forName("java.math.BigDecimal"); 54 haveBigDecimal = true; 55 } catch (Throwable t) { 56 haveBigDecimal = false; 57 } 58 HAVE_BIG_DECIMAL = haveBigDecimal; 59 } 60 61 private ResourceBundle res; 62 private Locale locale; 63 private String encode; 64 private final static String MESSAGE_FILE = "org.apache.derby.loc.toolsmessages"; 65 private final static String ENV_CODESET = "derby.ui.codeset"; 66 private final static String ENV_LOCALE = "derby.ui.locale"; 67 private String messageFileName; 68 private String resourceKey; 69 private LocalizedOutput out; 70 private LocalizedInput in; 71 private boolean enableLocalized; 72 private boolean unicodeEscape; 73 private static LocalizedResource local; 74 private int dateSize; 75 private int timeSize; 76 private int timestampSize; 77 private DateFormat formatDate; 78 private DateFormat formatTime; 79 private DateFormat formatTimestamp; 80 private NumberFormat formatNumber; 81 private DecimalFormat formatDecimal; 82 public LocalizedResource(){ 83 init(); 84 } 85 public LocalizedResource(String encStr, String locStr, String msgF){ 86 init(encStr,locStr,msgF); 87 } 88 public static LocalizedResource getInstance(){ 89 if (local == null){ 90 local = new LocalizedResource(); 91 } 92 return local; 93 } 94 public void init(){ 95 init(null,null,null); 96 } 97 public void init (String encStr, String locStr, String msgF){ 98 if (encStr != null){ 99 encode = encStr; 100 } 101 if (encode == null) { 103 String eEncode = getEnvProperty(ENV_CODESET); 104 if ( eEncode != null ){ 105 encode = eEncode; 106 } 107 } 108 109 112 locale = getNewLocale(locStr); 114 115 if (locale==null) { 117 String s = getEnvProperty(ENV_LOCALE); 118 locale = getNewLocale(s); 119 } 120 if (locale==null){ 122 locale = Locale.getDefault(); 123 } 124 if (msgF != null) { 125 messageFileName = msgF; 126 } 127 else { 128 messageFileName = MESSAGE_FILE; 129 } 130 out = getNewOutput(System.out); 132 in = getNewInput(System.in); 133 134 if (enableLocalized && locale != null){ 136 formatDecimal = (DecimalFormat )DecimalFormat.getInstance(locale); 137 formatNumber = NumberFormat.getInstance(locale); 138 formatDate = DateFormat.getDateInstance(DateFormat.LONG,locale); 139 formatTime = DateFormat.getTimeInstance(DateFormat.LONG,locale); 140 formatTimestamp = DateFormat.getDateTimeInstance(DateFormat.LONG, 141 DateFormat.LONG, locale); 142 } 143 else { 144 formatDecimal = (DecimalFormat )DecimalFormat.getInstance(); 145 formatNumber = NumberFormat.getInstance(); 146 formatDate = DateFormat.getDateInstance(DateFormat.LONG); 147 formatTime = DateFormat.getTimeInstance(DateFormat.LONG); 148 formatTimestamp = DateFormat.getDateTimeInstance(DateFormat.LONG, 149 DateFormat.LONG); 150 } 151 initMaxSizes2(); 153 } 154 private void setResource(){ 157 if (res != null){ 158 return; 159 } 160 if ( locale == null || locale.toString().equals("none") ){ 161 res = ResourceBundle.getBundle(MESSAGE_FILE); 162 } 163 else 164 try { 165 res = ResourceBundle.getBundle(messageFileName,locale); 166 } 167 catch(java.util.MissingResourceException e){ 168 res = ResourceBundle.getBundle(messageFileName,Locale.ENGLISH); 169 } 170 } 171 private void initMaxSizes2(){ 172 dateSize = 0; 173 timeSize = 0; 174 timestampSize = 0; 175 176 int len; 177 178 Date d = new Date (60907276800000L); 181 Timestamp t = new Timestamp (d.getTime()); 182 for(int month = 0 ; month <=11 ; month++, d.setTime(d.getTime() + (30L * 24L * 60L * 60L * 1000L))) { 183 len=getDateAsString(d).length(); 184 185 if(len > dateSize ) { 186 dateSize=len; 187 } 188 189 t.setTime(d.getTime() + ((((21L * 60L) + 59L) * 60L) + 59L)); 190 len=getTimestampAsString(t).length(); 191 192 if(len > timestampSize) { 193 timestampSize=len; 194 } 195 } 196 197 len = 18; 200 for (int hour = 0 ; hour < 24; hour++) { 201 202 long secs = (hour * 3600L) + (59 * 60L) + 59L; 203 204 long ms = secs * 1000L; 205 206 Date td = new Date (ms); 207 208 String fd = formatTime.format(td); 209 210 if (fd.length() > len) 211 len = fd.length(); 212 } 213 timeSize=len; 214 215 } 216 217 public LocalizedInput getNewInput(InputStream i) { 218 try { 219 if (encode != null) 220 return new LocalizedInput(i,encode); 221 } 222 catch (UnsupportedEncodingException e){ 223 224 } 225 return new LocalizedInput(i); 226 } 227 228 public LocalizedInput getNewEncodedInput(InputStream i, String encoding) { 229 try { 230 return new LocalizedInput(i,encoding); 231 } 232 catch (UnsupportedEncodingException e){ 233 234 } 235 return new LocalizedInput(i); 236 } 237 238 public LocalizedOutput getNewOutput(OutputStream o){ 239 try { 240 if (encode != null) 241 return new LocalizedOutput(o,encode); 242 } 243 catch(UnsupportedEncodingException e){ 244 } 245 return new LocalizedOutput(o); 246 } 247 251 public LocalizedOutput getNewEncodedOutput(OutputStream o, 252 String encoding) throws UnsupportedEncodingException { 253 return new LocalizedOutput(o, encoding); 254 } 255 public String getTextMessage(String key ) { 256 if ( res == null){ 257 setResource(); 258 } 259 String s = key; 260 try{ 261 s = res.getString(key); 262 } catch (Exception e) { 263 s = key; 264 } 265 return s; 268 } 269 public String getTextMessage(String key, Object o){ 270 Object [] att=new Object [] {o}; 271 return getTextMessage(key,att); 272 } 273 public String getTextMessage(String key, Object o1, Object o2){ 274 Object [] att=new Object [] {o1,o2}; 275 return getTextMessage(key,att); 276 } 277 public String getTextMessage(String key, Object o1, Object o2, Object o3){ 278 Object [] att=new Object [] {o1,o2,o3}; 279 return getTextMessage(key,att); 280 } 281 public String getTextMessage(String key, Object o1, Object o2, Object o3, Object o4){ 282 Object [] att=new Object [] {o1,o2,o3,o4}; 283 return getTextMessage(key,att); 284 } 285 private Locale getNewLocale(String locStr){ 286 String l="", r="", v=""; 287 StringTokenizer st; 288 if (locStr==null) { 289 return null; 290 } 291 st=new StringTokenizer (locStr, "_"); 292 try { 293 l=st.nextToken(); 294 if(st.hasMoreTokens()==true) 295 r=st.nextToken(); 296 if(st.hasMoreTokens()==true) 297 v=st.nextToken(); 298 return new Locale (l,r,v); 299 } catch (Exception e) { 300 return null; 301 } 302 } 303 public String getTextMessage(String key, Object [] objectArr) { 304 if (res == null){ 305 setResource(); 306 } 307 try{ 308 return MessageFormat.format(res.getString(key), objectArr); 309 } catch (Exception e) { 310 String tmpFormat = key; 311 for (int i=0; i<objectArr.length; i++) 312 tmpFormat = tmpFormat + ", <{" + (i) + "}>"; 313 return MessageFormat.format(tmpFormat, objectArr); 314 } 315 } 316 public String getLocalizedString(ResultSet rs, 317 ResultSetMetaData rsm, 318 int columnNumber) throws SQLException { 319 if (!enableLocalized){ 320 return rs.getString(columnNumber); 321 } 322 int type = rsm.getColumnType(columnNumber); 323 if ( type == Types.DATE ) { 324 return getDateAsString(rs.getDate(columnNumber)); 325 } 326 else if ( type == Types.INTEGER || type == Types.SMALLINT || 327 type == Types.BIGINT || type == Types.TINYINT ) { 328 return getNumberAsString(rs.getLong(columnNumber)); 329 } 330 else if (type == Types.REAL || type == Types.FLOAT || 331 type == Types.DOUBLE ) { 332 return getNumberAsString(rs.getDouble(columnNumber)); 333 } 334 else if (HAVE_BIG_DECIMAL && (type == Types.NUMERIC || type == Types.DECIMAL)) { 335 return getNumberAsString(rs.getBigDecimal(columnNumber, 336 rsm.getScale(columnNumber))); 337 } 338 else if (type == Types.TIME ) { 339 return getTimeAsString(rs.getTime(columnNumber)); 340 } 341 else if (type == Types.TIMESTAMP ) { 342 return getTimestampAsString(rs.getTimestamp(columnNumber)); 343 } 344 return rs.getString(columnNumber); 345 } 346 347 public String getDateAsString(Date d){ 348 if (!enableLocalized){ 349 return d.toString(); 350 } 351 return formatDate.format(d); 352 } 353 public String getTimeAsString(Date t){ 354 if (!enableLocalized){ 355 return t.toString(); 356 } 357 return formatTime.format(t, new StringBuffer (), 358 new java.text.FieldPosition (0)).toString(); 359 } 360 public String getNumberAsString(int o){ 361 if (enableLocalized){ 362 return formatNumber.format(o); 363 } 364 else { 365 return String.valueOf(o); 366 } 367 } 368 public String getNumberAsString(long o){ 369 if (enableLocalized){ 370 return formatNumber.format(o); 371 } 372 else{ 373 return String.valueOf(o); 374 } 375 } 376 public String getNumberAsString(Object o){ 377 if (enableLocalized){ 378 return formatNumber.format(o, new StringBuffer (), 379 new FieldPosition (0)).toString(); 380 } 381 else { 382 return o.toString(); 383 } 384 } 385 public String getNumberAsString(double o){ 386 if (!enableLocalized) { 387 return String.valueOf(o); 388 } 389 return formatDecimal.format(o); 390 } 391 public String getTimestampAsString(Timestamp t){ 392 if (!enableLocalized){ 393 return t.toString(); 394 } 395 return formatTime.format(t, new StringBuffer (), 396 new java.text.FieldPosition (0)).toString(); 397 } 398 public int getColumnDisplaySize(ResultSetMetaData rsm, 399 int columnNumber) throws SQLException { 400 if (!enableLocalized){ 401 return rsm.getColumnDisplaySize(columnNumber); 402 } 403 int type = rsm.getColumnType(columnNumber); 404 if (type == Types.DATE) 405 return dateSize; 406 if (type == Types.TIME) 407 return timeSize; 408 if (type == Types.TIMESTAMP) 409 return timestampSize; 410 return rsm.getColumnDisplaySize(columnNumber); 411 } 412 public String getStringFromDate(String dateStr) 413 throws ParseException { 414 if (!enableLocalized){ 415 return dateStr; 416 } 417 Date d = formatDate.parse(dateStr); 418 return new java.sql.Date (d.getTime()).toString(); 419 } 420 public String getStringFromTime(String timeStr) 421 throws ParseException { 422 if (!enableLocalized){ 423 return timeStr; 424 } 425 Date t = formatTime.parse(timeStr); 426 return new java.sql.Time (t.getTime()).toString(); 427 } 428 public String getStringFromValue(String val) 429 throws ParseException { 430 if (!enableLocalized){ 431 return val; 432 } 433 return formatNumber.parse(val).toString(); 434 } 435 public String getStringFromTimestamp(String timestampStr) 436 throws ParseException { 437 if (!enableLocalized){ 438 return timestampStr; 439 } 440 Date ts = formatTimestamp.parse(timestampStr); 441 return new java.sql.Timestamp (ts.getTime()).toString(); 442 } 443 public Locale getLocale(){ 444 return locale; 445 } 446 447 private final synchronized String getEnvProperty(String key) { 448 String s; 449 try 450 { 451 resourceKey = key; 452 s = (String ) java.security.AccessController.doPrivileged(this); 453 } 454 catch (SecurityException se) { 455 s = null; 456 } 457 return s; 459 } 460 public final Object run() { 461 String s = System.getProperty(resourceKey); 462 return s; 463 } 464 public static boolean enableLocalization(boolean mode) { 465 getInstance().enableLocalized = mode; 466 getInstance().init(); 468 return mode; 469 } 470 public boolean isLocalized(){ 471 return getInstance().enableLocalized; 472 } 473 public static String getMessage(String key){ 474 return getInstance().getTextMessage(key); 475 } 476 public static String getMessage(String key, Object o1){ 477 return getInstance().getTextMessage(key,o1); 478 } 479 public static String getMessage(String key, Object o1, Object o2){ 480 return getInstance().getTextMessage(key,o1,o2); 481 } 482 public static String getMessage(String key, Object o1, Object o2, Object o3){ 483 return getInstance().getTextMessage(key,o1,o2,o3); 484 } 485 public static String getMessage(String key, Object o1, Object o2, Object o3, Object o4){ 486 return getInstance().getTextMessage(key,o1,o2,o3,o4); 487 } 488 public static LocalizedOutput OutputWriter(){ 489 return getInstance().out; 490 } 491 public static LocalizedInput InputReader(){ 492 return getInstance().in; 493 } 494 public static String getNumber(long o){ 495 return getInstance().getNumberAsString(o); 496 } 497 public static String getNumber(int o){ 498 return getInstance().getNumberAsString(o); 499 } 500 public static void setUnicodeEscape(boolean u){ 501 getInstance().unicodeEscape = u; 502 } 503 public static boolean getUnicodeEscape(){ 504 return getInstance().unicodeEscape; 505 } 506 public String toString(){ 507 String s = "toString(){\n" + 508 "locale=" + (locale==null?"null":locale.toString()) + "\n" + 509 "encode=" + encode + "\n" + 510 "messageFile=" + messageFileName + "\n" + 511 "resourceKey=" + resourceKey + "\n" + 512 "enableLocalized=" + enableLocalized + " \n" + 513 "unicodeEscape=" + unicodeEscape + "\n" + 514 "dateSize=" + dateSize + "\n" + 515 "timeSize=" + timeSize + "\n" + 516 "timestampSize="+timestampSize+ "\n}"; 517 return s; 518 } 519 } 520 | Popular Tags |