1 23 package com.sun.enterprise.admin.util; 24 25 import java.util.ResourceBundle ; 26 import java.util.Locale ; 27 import java.text.MessageFormat ; 28 29 30 39 40 public class LocalStringsManager 41 { 42 static final String DEFAULT_PROPERTY_FILE_NAME = "LocalStrings"; 43 44 private String m_propertyFileName; 45 private String m_basePackage; 46 private ResourceBundle m_FixedResourceBundle; 47 48 53 public LocalStringsManager(String basePackage, String propertyFileName) 54 { 55 m_basePackage = basePackage; 56 m_propertyFileName = propertyFileName; 57 } 58 59 public LocalStringsManager(String basePackage) 60 { 61 this(basePackage, DEFAULT_PROPERTY_FILE_NAME); 62 } 63 64 71 public String getString(String startPackage, String key, String defaultValue) 72 { 73 if(m_FixedResourceBundle!=null) 74 { 75 String value = m_FixedResourceBundle.getString(key); 76 if(value!=null) 77 return value; 78 return defaultValue; 79 } 80 81 82 String stopPackage = m_basePackage; 83 if(startPackage==null) 84 startPackage = stopPackage; 85 else 86 if(!startPackage.startsWith(m_basePackage)) 87 stopPackage = startPackage; ResourceBundle resources = null; 89 90 while (startPackage.length()>0 && startPackage.length()>=stopPackage.length()) 91 { 92 try 93 { 94 String resFileName = startPackage+"."+m_propertyFileName; 95 startPackage = startPackage.substring(0, startPackage.lastIndexOf(".")); 96 resources = ResourceBundle.getBundle(resFileName); 97 if ( resources != null ) 98 { 99 String value = resources.getString(key); 100 if ( value != null ) 101 return value; 102 } 103 } catch (Exception ex) 104 { 105 } 106 } 107 108 if ( !stopPackage.equals(m_basePackage) ) 110 { 111 return getString(null, key, defaultValue); 112 } 113 114 return defaultValue; 115 } 116 117 123 public String getString(String key, String defaultValue) 124 { 125 return getString(null, key, defaultValue); 126 } 127 128 136 137 public String getString( 138 String startPackage, 139 String key, 140 String defaultFormat, 141 Object arguments[] 142 ) 143 { 144 MessageFormat f = new MessageFormat ( 145 getString(startPackage, key, defaultFormat)); 146 for (int i = 0; i < arguments.length; i++) 147 { 148 if ( arguments[i] == null ) 149 { 150 arguments[i] = "null"; 151 } else if ( !(arguments[i] instanceof String ) && 152 !(arguments[i] instanceof Number ) && 153 !(arguments[i] instanceof java.util.Date )) 154 { 155 arguments[i] = arguments[i].toString(); 156 } 157 } 158 return f.format(arguments); 159 } 160 161 169 public String getString( 170 String key, 171 String defaultFormat, 172 Object arguments[] 173 ) 174 { 175 return getString(null, key, defaultFormat, arguments); 176 } 177 178 protected void setFixedResourceBundle(String startPackage) 179 { 180 if(startPackage==null) 181 { 182 m_FixedResourceBundle = null; 183 return; 184 } 185 186 String stopPackage = m_basePackage; 187 if(startPackage==null) 188 startPackage = stopPackage; 189 else 190 if(!startPackage.startsWith(m_basePackage)) 191 stopPackage = startPackage; ResourceBundle resources = null; 193 194 while (startPackage.length()>0 && startPackage.length()>=stopPackage.length()) 195 { 196 try 197 { 198 String resFileName = startPackage+"."+m_propertyFileName; 199 startPackage = startPackage.substring(0, startPackage.lastIndexOf(".")); 200 resources = ResourceBundle.getBundle(resFileName); 201 if ( resources != null ) 202 { 203 m_FixedResourceBundle = resources; 204 return; 205 } 206 } catch (Exception ex) 207 { 208 } 209 } 210 211 if ( !stopPackage.equals(m_basePackage) ) 213 { 214 setFixedResourceBundle(m_basePackage); 215 } 216 217 } 218 } 219 220 | Popular Tags |