1 3 4 package com.sun.j2ee.blueprints.catalog.validator; 5 6 import java.io.FileInputStream ; 7 import java.io.InputStream ; 8 import java.io.IOException ; 9 10 import java.util.Locale ; 11 import java.util.HashMap ; 12 import java.util.Iterator ; 13 import java.util.Properties ; 14 import java.util.ResourceBundle ; 15 import java.util.MissingResourceException ; 16 import javax.faces.FacesException; 17 import javax.faces.application.Application; 18 import javax.faces.application.FacesMessage; 19 import javax.faces.context.FacesContext; 20 import java.text.MessageFormat ; 21 import java.io.IOException ; 22 23 28 29 public class MessageFactory extends Object 30 { 31 32 33 private MessageFactory() { 34 } 35 36 44 public static String substituteParams(Locale locale, String msgtext, Object params[]) { 45 String localizedStr = null; 46 47 if (params == null || msgtext == null ) { 48 return msgtext; 49 } 50 StringBuffer b = new StringBuffer (100); 51 MessageFormat mf = new MessageFormat (msgtext); 52 if (locale != null) { 53 mf.setLocale(locale); 54 b.append(mf.format(params)); 55 localizedStr = b.toString(); 56 } 57 return localizedStr; 58 } 59 60 67 68 public static FacesMessage getMessage(String messageId, Object params[]) { 69 Locale locale = null; 70 FacesContext context = FacesContext.getCurrentInstance(); 71 if (context != null && context.getViewRoot() != null) { 73 locale = context.getViewRoot().getLocale(); 74 if (locale == null) { 75 locale = Locale.getDefault(); 76 } 77 } else { 78 locale = Locale.getDefault(); 79 } 80 81 return getMessage(locale, messageId, params); 82 } 83 84 92 public static FacesMessage getMessage(Locale locale, String messageId, 93 Object params[]) { 94 FacesMessage result = null; 95 String 96 summary = null, 97 detail = null, 98 bundleName = null; 99 ResourceBundle bundle = null; 100 101 if (null != (bundleName = getApplication().getMessageBundle())) { 103 if (null != 104 (bundle = 105 ResourceBundle.getBundle(bundleName, locale, 106 getCurrentLoader(bundleName)))) { 107 try { 109 summary = bundle.getString(messageId); 110 } 111 catch (MissingResourceException e) { 112 } 113 } 114 } 115 116 if (null == summary) { 118 bundle = ResourceBundle.getBundle(FacesMessage.FACES_MESSAGES, 120 locale, 121 getCurrentLoader(bundleName)); 122 if (null == bundle) { 123 throw new NullPointerException (); 124 } 125 try { 127 summary = bundle.getString(messageId); 128 } 129 catch (MissingResourceException e) { 130 } 131 } 132 133 if (null == summary) { 135 return null; 136 } 137 138 if (null == summary || null == bundle) { 140 throw new NullPointerException (); 141 } 142 summary = substituteParams(locale, summary, params); 143 144 try { 145 detail = substituteParams(locale, 146 bundle.getString(messageId + "_detail"), 147 params); 148 } 149 catch (MissingResourceException e) { 150 } 151 152 return (new FacesMessage(summary, detail)); 153 } 154 155 156 public static FacesMessage getMessage(FacesContext context, String messageId) { 160 return getMessage(context, messageId, null); 161 } 162 163 172 public static FacesMessage getMessage(FacesContext context, String messageId, 173 Object params[]) { 174 if (context == null || messageId == null ) { 175 throw new NullPointerException ("One or more parameters could be null"); 176 } 177 Locale locale = null; 178 if (context != null && context.getViewRoot() != null) { 180 locale = context.getViewRoot().getLocale(); 181 } else { 182 locale = Locale.getDefault(); 183 } 184 if (null == locale) { 185 throw new NullPointerException (); 186 } 187 FacesMessage message = getMessage(locale, messageId, params); 188 if (message != null) { 189 return message; 190 } 191 locale = Locale.getDefault(); 192 return (getMessage(locale, messageId, params)); 193 } 194 195 203 public static FacesMessage getMessage(FacesContext context, String messageId, 204 Object param0) { 205 return getMessage(context, messageId, new Object []{param0}); 206 } 207 208 217 public static FacesMessage getMessage(FacesContext context, String messageId, 218 Object param0, Object param1) { 219 return getMessage(context, messageId, new Object []{param0, param1}); 220 } 221 222 232 public static FacesMessage getMessage(FacesContext context, String messageId, 233 Object param0, Object param1, 234 Object param2) { 235 return getMessage(context, messageId, 236 new Object []{param0, param1, param2}); 237 } 238 239 250 public static FacesMessage getMessage(FacesContext context, String messageId, 251 Object param0, Object param1, 252 Object param2, Object param3) { 253 return getMessage(context, messageId, 254 new Object []{param0, param1, param2, param3}); 255 } 256 257 protected static Application getApplication() { 258 return (FacesContext.getCurrentInstance().getApplication()); 259 } 260 261 protected static ClassLoader getCurrentLoader(Object fallbackClass) { 262 ClassLoader loader = 263 Thread.currentThread().getContextClassLoader(); 264 if (loader == null) { 265 loader = fallbackClass.getClass().getClassLoader(); 266 } 267 return loader; 268 } 269 270 271 } | Popular Tags |