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