1 16 package javax.faces.component; 17 18 import javax.faces.application.FacesMessage; 19 import javax.faces.context.FacesContext; 20 import java.text.MessageFormat ; 21 import java.util.Locale ; 22 import java.util.MissingResourceException ; 23 import java.util.ResourceBundle ; 24 25 36 class _MessageUtils 37 { 38 private static final String DETAIL_SUFFIX = "_detail"; 39 40 static void addErrorMessage(FacesContext facesContext, 41 UIComponent component, 42 String messageId) 43 { 44 facesContext.addMessage(component.getClientId(facesContext), 45 getMessage(facesContext, 46 facesContext.getViewRoot().getLocale(), 47 FacesMessage.SEVERITY_ERROR, 48 messageId, 49 null)); 50 } 51 52 static void addErrorMessage(FacesContext facesContext, 53 UIComponent component, 54 String messageId, Object [] args) 55 { 56 facesContext.addMessage(component.getClientId(facesContext), 57 getMessage(facesContext, 58 facesContext.getViewRoot().getLocale(), 59 FacesMessage.SEVERITY_ERROR, 60 messageId, 61 args)); 62 } 63 64 static FacesMessage getMessage(FacesContext facesContext, 65 Locale locale, 66 FacesMessage.Severity severity, 67 String messageId, 68 Object args[]) 69 { 70 ResourceBundle appBundle; 71 ResourceBundle defBundle; 72 String summary; 73 String detail; 74 75 appBundle = getApplicationBundle(facesContext, locale); 76 summary = getBundleString(appBundle, messageId); 77 if (summary != null) 78 { 79 detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX); 80 } 81 else 82 { 83 defBundle = getDefaultBundle(facesContext, locale); 84 summary = getBundleString(defBundle, messageId); 85 if (summary != null) 86 { 87 detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX); 88 } 89 else 90 { 91 detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX); 93 if (detail != null) 94 { 95 summary = null; 96 } 97 else 98 { 99 detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX); 100 if (detail != null) 101 { 102 summary = null; 103 } 104 else 105 { 106 facesContext.getExternalContext().log("No message with id " + messageId + " found in any bundle"); 108 return new FacesMessage(severity, messageId, null); 109 } 110 } 111 } 112 } 113 114 if (args != null && args.length > 0) 115 { 116 MessageFormat format; 117 118 if (summary != null) 119 { 120 format = new MessageFormat (summary, locale); 121 summary = format.format(args); 122 } 123 124 if (detail != null) 125 { 126 format = new MessageFormat (detail, locale); 127 detail = format.format(args); 128 } 129 } 130 131 return new FacesMessage(severity, summary, detail); 132 } 133 134 135 private static String getBundleString(ResourceBundle bundle, String key) 136 { 137 try 138 { 139 return bundle == null ? null : bundle.getString(key); 140 } 141 catch (MissingResourceException e) 142 { 143 return null; 144 } 145 } 146 147 148 private static ResourceBundle getApplicationBundle(FacesContext facesContext, Locale locale) 149 { 150 String bundleName = facesContext.getApplication().getMessageBundle(); 151 if (bundleName != null) 152 { 153 return getBundle(facesContext, locale, bundleName); 154 } 155 else 156 { 157 return null; 158 } 159 } 160 161 private static ResourceBundle getDefaultBundle(FacesContext facesContext, 162 Locale locale) 163 { 164 return getBundle(facesContext, locale, FacesMessage.FACES_MESSAGES); 165 } 166 167 private static ResourceBundle getBundle(FacesContext facesContext, 168 Locale locale, 169 String bundleName) 170 { 171 try 172 { 173 return ResourceBundle.getBundle(bundleName, 175 locale, 176 facesContext.getClass().getClassLoader()); 177 } 178 catch (MissingResourceException ignore1) 179 { 180 try 181 { 182 return ResourceBundle.getBundle(bundleName, 184 locale, 185 _MessageUtils.class.getClassLoader()); 186 } 187 catch (MissingResourceException ignore2) 188 { 189 try 190 { 191 return ResourceBundle.getBundle(bundleName, 193 locale, 194 Thread.currentThread().getContextClassLoader()); 195 } 196 catch (MissingResourceException damned) 197 { 198 facesContext.getExternalContext().log("resource bundle " + bundleName + " could not be found"); 199 return null; 200 } 201 } 202 } 203 } 204 205 } 206 | Popular Tags |