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