1 16 package org.apache.myfaces.util; 17 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 21 import javax.faces.application.FacesMessage; 22 import javax.faces.context.FacesContext; 23 import java.text.MessageFormat ; 24 import java.util.Locale ; 25 import java.util.MissingResourceException ; 26 import java.util.ResourceBundle ; 27 28 46 public class MessageUtils 47 { 48 private static final Log log = LogFactory.getLog(MessageUtils.class); 49 50 private static final String DEFAULT_BUNDLE = "javax.faces.Messages"; 51 private static final String DETAIL_SUFFIX = "_detail"; 52 53 public static FacesMessage getMessage(FacesMessage.Severity severity, 54 String messageId, 55 Object arg1) 56 { 57 return getMessage(severity, 58 messageId, 59 new Object []{arg1}, 60 FacesContext.getCurrentInstance()); 61 } 62 63 public static FacesMessage getMessage(FacesMessage.Severity severity, 64 String messageId, 65 Object [] args) 66 { 67 return getMessage(severity, 68 messageId, 69 args, 70 FacesContext.getCurrentInstance()); 71 } 72 73 public static FacesMessage getMessage(FacesMessage.Severity severity, 74 String messageId, 75 Object [] args, 76 FacesContext facesContext) 77 { 78 Locale locale; 79 if (facesContext.getViewRoot() != null) 80 { 81 locale = facesContext.getViewRoot().getLocale(); 82 } 83 else 84 { 85 locale = facesContext.getApplication().getDefaultLocale(); 86 } 87 return internalGetMessage(facesContext, 88 locale, 89 severity, 90 messageId, 91 args); 92 } 93 94 public static void addMessage(FacesMessage.Severity severity, 95 String messageId, 96 Object [] args) 97 { 98 addMessage(severity, messageId, args, null, FacesContext.getCurrentInstance()); 99 } 100 101 public static void addMessage(FacesMessage.Severity severity, 102 String messageId, 103 Object [] args, 104 FacesContext facesContext) 105 { 106 addMessage(severity, messageId, args, null, facesContext); 107 } 108 109 public static void addMessage(FacesMessage.Severity severity, 110 String messageId, 111 Object [] args, 112 String forClientId) 113 { 114 addMessage(severity, messageId, args, forClientId, FacesContext.getCurrentInstance()); 115 } 116 117 public static void addMessage(FacesMessage.Severity severity, 118 String messageId, 119 Object [] args, 120 String forClientId, 121 FacesContext facesContext) 122 { 123 facesContext.addMessage(forClientId, 124 getMessage(severity, messageId, args, facesContext)); 125 } 126 127 128 129 130 private static FacesMessage internalGetMessage(FacesContext facesContext, 131 Locale locale, 132 FacesMessage.Severity severity, 133 String messageId, 134 Object args[]) 135 { 136 ResourceBundle appBundle; 137 ResourceBundle defBundle; 138 String summary; 139 String detail; 140 141 appBundle = getApplicationBundle(facesContext, locale); 142 summary = getBundleString(appBundle, messageId); 143 if (summary != null) 144 { 145 detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX); 146 } 147 else 148 { 149 defBundle = getDefaultBundle(locale); 150 summary = getBundleString(defBundle, messageId); 151 if (summary != null) 152 { 153 detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX); 154 } 155 else 156 { 157 detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX); 159 if (detail != null) 160 { 161 summary = null; 162 } 163 else 164 { 165 detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX); 166 if (detail != null) 167 { 168 summary = null; 169 } 170 else 171 { 172 if (log.isWarnEnabled()) log.warn("No message with id " + messageId + " found in any bundle"); 174 return new FacesMessage(severity, messageId, null); 175 } 176 } 177 } 178 } 179 180 if (args != null && args.length > 0) 181 { 182 MessageFormat format; 183 184 if (summary != null) 185 { 186 format = new MessageFormat (summary, locale); 187 summary = format.format(args); 188 } 189 190 if (detail != null) 191 { 192 format = new MessageFormat (detail, locale); 193 detail = format.format(args); 194 } 195 } 196 197 return new FacesMessage(severity, summary, detail); 198 } 199 200 201 private static String getBundleString(ResourceBundle bundle, String key) 202 { 203 try 204 { 205 return bundle == null ? null : bundle.getString(key); 206 } 207 catch (MissingResourceException e) 208 { 209 return null; 210 } 211 } 212 213 214 private static ResourceBundle getApplicationBundle(FacesContext facesContext, Locale locale) 215 { 216 String bundleName = facesContext.getApplication().getMessageBundle(); 217 if (bundleName != null) 218 { 219 try 220 { 221 return ResourceBundle.getBundle(bundleName, locale, Thread.currentThread().getContextClassLoader()); 222 } 223 catch (MissingResourceException e) 224 { 225 log.error("Resource bundle " + bundleName + " could not be found."); 226 return null; 227 } 228 } 229 else 230 { 231 return null; 232 } 233 } 234 235 private static ResourceBundle getDefaultBundle(Locale locale) 236 { 237 try 238 { 239 return ResourceBundle.getBundle(DEFAULT_BUNDLE, 240 locale, 241 FacesContext.class.getClassLoader()); 242 } 243 catch (MissingResourceException e) 244 { 245 log.error("Resource bundle " + DEFAULT_BUNDLE + " could not be found."); 246 return null; 247 } 248 } 249 250 } 251 | Popular Tags |