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 javax.el.ValueExpression; |
22 | |
import javax.faces.application.FacesMessage; |
23 | |
import javax.faces.application.FacesMessage.Severity; |
24 | |
import javax.faces.component.UIComponent; |
25 | |
import javax.faces.context.FacesContext; |
26 | |
import java.text.MessageFormat; |
27 | |
import java.util.Locale; |
28 | |
import java.util.MissingResourceException; |
29 | |
import java.util.ResourceBundle; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 0 | class _MessageUtils |
36 | |
{ |
37 | |
private static final String DETAIL_SUFFIX = "_detail"; |
38 | |
|
39 | |
static FacesMessage getErrorMessage(FacesContext facesContext, |
40 | |
String messageId, |
41 | |
Object args[]) |
42 | |
{ |
43 | 0 | return getMessage(facesContext, |
44 | |
facesContext.getViewRoot().getLocale(), |
45 | |
FacesMessage.SEVERITY_ERROR, |
46 | |
messageId, |
47 | |
args); |
48 | |
} |
49 | |
|
50 | |
static FacesMessage getMessage(FacesContext facesContext, |
51 | |
Locale locale, |
52 | |
FacesMessage.Severity severity, |
53 | |
String messageId, |
54 | |
Object args[]) |
55 | |
{ |
56 | |
ResourceBundle appBundle; |
57 | |
ResourceBundle defBundle; |
58 | |
String summary; |
59 | |
String detail; |
60 | |
|
61 | 0 | if(locale == null) |
62 | |
{ |
63 | 0 | locale = Locale.getDefault(); |
64 | |
} |
65 | |
|
66 | 0 | appBundle = getApplicationBundle(facesContext, locale); |
67 | 0 | summary = getBundleString(appBundle, messageId); |
68 | 0 | if (summary != null) |
69 | |
{ |
70 | 0 | detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX); |
71 | |
} |
72 | |
else |
73 | |
{ |
74 | 0 | defBundle = getDefaultBundle(facesContext, locale); |
75 | 0 | summary = getBundleString(defBundle, messageId); |
76 | 0 | if (summary != null) |
77 | |
{ |
78 | 0 | detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX); |
79 | |
} |
80 | |
else |
81 | |
{ |
82 | |
|
83 | 0 | detail = getBundleString(appBundle, messageId + DETAIL_SUFFIX); |
84 | 0 | if (detail != null) |
85 | |
{ |
86 | 0 | summary = null; |
87 | |
} |
88 | |
else |
89 | |
{ |
90 | 0 | detail = getBundleString(defBundle, messageId + DETAIL_SUFFIX); |
91 | 0 | if (detail != null) |
92 | |
{ |
93 | 0 | summary = null; |
94 | |
} |
95 | |
else |
96 | |
{ |
97 | |
|
98 | 0 | facesContext.getExternalContext().log("No message with id " + messageId + " found in any bundle"); |
99 | 0 | return new FacesMessage(severity, messageId, null); |
100 | |
} |
101 | |
} |
102 | |
} |
103 | |
} |
104 | |
|
105 | 0 | if (args != null && args.length > 0) |
106 | |
{ |
107 | 0 | 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 | 0 | return bundle == null ? null : bundle.getString(key); |
120 | |
} |
121 | 0 | catch (MissingResourceException e) |
122 | |
{ |
123 | 0 | return null; |
124 | |
} |
125 | |
} |
126 | |
|
127 | |
|
128 | |
private static ResourceBundle getApplicationBundle(FacesContext facesContext, Locale locale) |
129 | |
{ |
130 | 0 | String bundleName = facesContext.getApplication().getMessageBundle(); |
131 | 0 | return bundleName != null ? getBundle(facesContext, locale, bundleName) : null; |
132 | |
} |
133 | |
|
134 | |
private static ResourceBundle getDefaultBundle(FacesContext facesContext, |
135 | |
Locale locale) |
136 | |
{ |
137 | 0 | 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 | 0 | 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 | return ResourceBundle.getBundle(bundleName, |
166 | |
locale, |
167 | |
Thread.currentThread().getContextClassLoader()); |
168 | |
} |
169 | 0 | catch (MissingResourceException damned) |
170 | |
{ |
171 | 0 | facesContext.getExternalContext().log("resource bundle " + bundleName + " could not be found"); |
172 | 0 | return null; |
173 | |
} |
174 | |
} |
175 | |
} |
176 | |
} |
177 | |
|
178 | |
static Object getLabel(FacesContext facesContext, UIComponent component) { |
179 | 0 | Object label = component.getAttributes().get("label"); |
180 | 0 | if(label != null) |
181 | 0 | return label; |
182 | |
|
183 | 0 | ValueExpression expression = component.getValueExpression("label"); |
184 | 0 | if(expression != null) |
185 | 0 | return expression; |
186 | |
|
187 | |
|
188 | 0 | return component.getClientId( facesContext ); |
189 | |
} |
190 | |
} |