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