1 4 5 42 43 package org.apache.cocoon.faces.samples.carstore; 44 45 import javax.faces.application.Application; 46 import javax.faces.application.FacesMessage; 47 import javax.faces.context.FacesContext; 48 49 import java.text.MessageFormat ; 50 import java.util.Locale ; 51 import java.util.MissingResourceException ; 52 import java.util.ResourceBundle ; 53 54 58 59 public class MessageFactory extends Object { 60 61 65 69 73 75 77 78 82 private MessageFactory() { 83 } 84 85 89 93 public static String substituteParams(Locale locale, String msgtext, Object params[]) { 94 String localizedStr = null; 95 96 if (params == null || msgtext == null) { 97 return msgtext; 98 } 99 StringBuffer b = new StringBuffer (100); 100 MessageFormat mf = new MessageFormat (msgtext); 101 if (locale != null) { 102 mf.setLocale(locale); 103 b.append(mf.format(params)); 104 localizedStr = b.toString(); 105 } 106 return localizedStr; 107 } 108 109 110 114 115 public static FacesMessage getMessage(String messageId, Object params[]) { 116 Locale locale = null; 117 FacesContext context = FacesContext.getCurrentInstance(); 118 if (context != null && context.getViewRoot() != null) { 120 locale = context.getViewRoot().getLocale(); 121 if (locale == null) { 122 locale = Locale.getDefault(); 123 } 124 } else { 125 locale = Locale.getDefault(); 126 } 127 128 return getMessage(locale, messageId, params); 129 } 130 131 132 public static FacesMessage getMessage(Locale locale, String messageId, 133 Object params[]) { 134 FacesMessage result = null; 135 String 136 summary = null, 137 detail = null, 138 bundleName = null; 139 ResourceBundle bundle = null; 140 141 if (null != (bundleName = getApplication().getMessageBundle())) { 143 if (null != 144 (bundle = 145 ResourceBundle.getBundle(bundleName, locale, 146 getCurrentLoader(bundleName)))) { 147 try { 149 summary = bundle.getString(messageId); 150 } catch (MissingResourceException e) { 151 } 152 } 153 } 154 155 if (null == summary) { 157 bundle = ResourceBundle.getBundle(FacesMessage.FACES_MESSAGES, 159 locale, 160 getCurrentLoader(bundleName)); 161 if (null == bundle) { 162 throw new NullPointerException (); 163 } 164 try { 166 summary = bundle.getString(messageId); 167 } catch (MissingResourceException e) { 168 } 169 } 170 171 if (null == summary) { 173 return null; 174 } 175 176 if (null == summary || null == bundle) { 178 throw new NullPointerException (); 179 } 180 summary = substituteParams(locale, summary, params); 181 182 try { 183 detail = substituteParams(locale, 184 bundle.getString(messageId + "_detail"), 185 params); 186 } catch (MissingResourceException e) { 187 } 188 189 return (new FacesMessage(summary, detail)); 190 } 191 192 193 public static FacesMessage getMessage(FacesContext context, String messageId) { 197 return getMessage(context, messageId, null); 198 } 199 200 201 public static FacesMessage getMessage(FacesContext context, String messageId, 202 Object params[]) { 203 if (context == null || messageId == null) { 204 throw new NullPointerException ( 205 "One or more parameters could be null"); 206 } 207 Locale locale = null; 208 if (context != null && context.getViewRoot() != null) { 210 locale = context.getViewRoot().getLocale(); 211 } else { 212 locale = Locale.getDefault(); 213 } 214 if (null == locale) { 215 throw new NullPointerException (); 216 } 217 FacesMessage message = getMessage(locale, messageId, params); 218 if (message != null) { 219 return message; 220 } 221 locale = Locale.getDefault(); 222 return (getMessage(locale, messageId, params)); 223 } 224 225 226 public static FacesMessage getMessage(FacesContext context, String messageId, 227 Object param0) { 228 return getMessage(context, messageId, new Object []{param0}); 229 } 230 231 232 public static FacesMessage getMessage(FacesContext context, String messageId, 233 Object param0, Object param1) { 234 return getMessage(context, messageId, new Object []{param0, param1}); 235 } 236 237 238 public static FacesMessage getMessage(FacesContext context, String messageId, 239 Object param0, Object param1, 240 Object param2) { 241 return getMessage(context, messageId, 242 new Object []{param0, param1, param2}); 243 } 244 245 246 public static FacesMessage getMessage(FacesContext context, String messageId, 247 Object param0, Object param1, 248 Object param2, Object param3) { 249 return getMessage(context, messageId, 250 new Object []{param0, param1, param2, param3}); 251 } 252 253 254 protected static Application getApplication() { 255 return (FacesContext.getCurrentInstance().getApplication()); 256 } 257 258 259 protected static ClassLoader getCurrentLoader(Object fallbackClass) { 260 ClassLoader loader = 261 Thread.currentThread().getContextClassLoader(); 262 if (loader == null) { 263 loader = fallbackClass.getClass().getClassLoader(); 264 } 265 return loader; 266 } 267 268 269 }
| Popular Tags
|