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