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