1 23 24 package javax.el; 25 26 import java.text.MessageFormat ; 27 import java.util.HashMap ; 28 import java.util.Locale ; 29 import java.util.Map ; 30 import java.util.MissingResourceException ; 31 import java.util.ResourceBundle ; 32 33 44 class ELUtil { 45 46 49 50 private ELUtil() { 51 } 52 53 57 private static ThreadLocal instance = new ThreadLocal () { 58 protected Object initialValue() { return (null); } 59 }; 60 61 67 68 private static Map getCurrentInstance() { 69 Map result = (Map ) instance.get(); 70 if (null == result) { 71 result = new HashMap (); 72 setCurrentInstance(result); 73 } 74 return result; 75 76 } 77 78 83 84 private static void setCurrentInstance(Map context) { 85 86 instance.set(context); 87 88 } 89 90 102 103 public static String getExceptionMessageString(ELContext context, String messageId) { 104 return getExceptionMessageString(context, messageId, null); 105 } 106 107 131 132 public static String getExceptionMessageString(ELContext context, 133 String messageId, 134 Object [] params) { 135 String result = ""; 136 Locale locale = null; 137 138 if (null == context || null == messageId) { 139 return result; 140 } 141 142 if (null == (locale = context.getLocale())) { 143 locale = Locale.getDefault(); 144 } 145 if (null != locale) { 146 Map threadMap = getCurrentInstance(); 147 ResourceBundle rb = null; 148 if (null == (rb = (ResourceBundle ) 149 threadMap.get(locale.toString()))) { 150 rb = ResourceBundle.getBundle("javax.el.PrivateMessages", 151 locale); 152 threadMap.put(locale.toString(), rb); 153 } 154 if (null != rb) { 155 try { 156 result = rb.getString(messageId); 157 if (null != params) { 158 result = MessageFormat.format(result, params); 159 } 160 } catch (IllegalArgumentException iae) { 161 result = "Can't get localized message: parameters to message appear to be incorrect. Message to format: " + messageId; 162 } catch (MissingResourceException mre) { 163 result = "Missing Resource in EL implementation: ???" + messageId + "???"; 164 } catch (Exception e) { 165 result = "Exception resolving message in EL implementation: ???" + messageId + "???"; 166 } 167 } 168 } 169 170 return result; 171 } 172 173 174 } 175 | Popular Tags |