1 23 package com.sun.enterprise.util; 24 25 import java.util.ResourceBundle ; 26 import java.util.Locale ; 27 import java.text.MessageFormat ; 28 29 import java.util.logging.*; 31 import com.sun.logging.*; 32 34 35 39 40 public class LocalStringManagerImpl implements LocalStringManager { 41 42 private static Logger _logger = null; 47 49 private Class defaultClass; 50 51 private static Logger getLogger() { 55 if (_logger == null) { 56 _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER); 57 } 58 return _logger; 59 } 60 61 66 public LocalStringManagerImpl(Class defaultClass) { 67 this.defaultClass = defaultClass; 68 } 69 70 90 public String getLocalString( 91 Class callerClass, 92 String key, 93 String defaultValue 94 ) { 95 Class stopClass = defaultClass.getSuperclass(); 96 Class startClass = ((callerClass != null) ? callerClass : 97 defaultClass); 98 ResourceBundle resources = null; 99 boolean globalDone = false; 100 for (Class c = startClass; 101 c != stopClass && c != null; 102 c = c.getSuperclass()) { 103 globalDone = (c == defaultClass); 104 try { 105 StringBuffer resFileName = new StringBuffer ( 108 c.getName().substring(0, c.getName().lastIndexOf("."))); 109 resFileName.append(".LocalStrings"); 110 111 resources = ResourceBundle.getBundle(resFileName.toString()); 112 if ( resources != null ) { 113 String value = resources.getString(key); 114 if ( value != null ) 115 return value; 116 } 117 } catch (Exception ex) { 118 } 119 } 120 121 if ( ! globalDone ) { 123 return getLocalString(null, key, defaultValue); 124 } else { 125 128 if (getLogger().isLoggable(Level.FINE)) 130 getLogger().log(Level.FINE,"No local string for " + key); 131 return defaultValue; 133 } 134 } 135 136 142 public String getLocalString(String key, String defaultValue) { 143 return getLocalString(null, key, defaultValue); 144 } 145 146 154 155 public String getLocalString( 156 Class callerClass, 157 String key, 158 String defaultFormat, 159 Object arguments[] 160 ) { 161 MessageFormat f = new MessageFormat ( 162 getLocalString(callerClass, key, defaultFormat)); 163 for (int i = 0; i < arguments.length; i++) { 164 if ( arguments[i] == null ) { 165 arguments[i] = "null"; 166 } else if ( !(arguments[i] instanceof String ) && 167 !(arguments[i] instanceof Number ) && 168 !(arguments[i] instanceof java.util.Date )) { 169 arguments[i] = arguments[i].toString(); 170 } 171 } 172 return f.format(arguments); 173 } 174 175 183 public String getLocalString( 184 String key, 185 String defaultFormat, 186 Object arguments[] 187 ) { 188 return getLocalString(null, key, defaultFormat, arguments); 189 } 190 } 191 192 | Popular Tags |