1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.el.unified.resolver; |
20 | |
|
21 | |
import java.beans.FeatureDescriptor; |
22 | |
import java.util.ArrayList; |
23 | |
import java.util.Iterator; |
24 | |
import java.util.Map; |
25 | |
import java.util.ResourceBundle; |
26 | |
import javax.el.ELContext; |
27 | |
import javax.el.ELException; |
28 | |
import javax.el.ELResolver; |
29 | |
import javax.el.PropertyNotFoundException; |
30 | |
import javax.el.PropertyNotWritableException; |
31 | |
import javax.faces.application.Application; |
32 | |
import javax.faces.context.FacesContext; |
33 | |
|
34 | |
import org.apache.myfaces.config.RuntimeConfig; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public final class ResourceBundleResolver extends ELResolver |
42 | |
{ |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
private RuntimeConfig runtimeConfig; |
48 | |
|
49 | |
|
50 | |
public ResourceBundleResolver() |
51 | 0 | { |
52 | 0 | } |
53 | |
|
54 | |
@Override |
55 | |
public void setValue(final ELContext context, final Object base, final Object property, final Object value) |
56 | |
throws NullPointerException, PropertyNotFoundException, PropertyNotWritableException, ELException |
57 | |
{ |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | 0 | if (base != null) |
66 | |
{ |
67 | 0 | return; |
68 | |
} |
69 | |
|
70 | 0 | if ((base == null) && (property == null)) |
71 | |
{ |
72 | 0 | throw new PropertyNotFoundException(); |
73 | |
} |
74 | |
|
75 | 0 | if (!(property instanceof String)) |
76 | |
{ |
77 | 0 | return; |
78 | |
} |
79 | |
|
80 | |
|
81 | 0 | final ResourceBundle bundle = getResourceBundle(context, (String)property); |
82 | |
|
83 | 0 | if (bundle != null) |
84 | |
{ |
85 | 0 | throw new PropertyNotWritableException("ResourceBundles are read-only"); |
86 | |
} |
87 | 0 | } |
88 | |
|
89 | |
@Override |
90 | |
public boolean isReadOnly(final ELContext context, final Object base, final Object property) |
91 | |
throws NullPointerException, PropertyNotFoundException, ELException |
92 | |
{ |
93 | |
|
94 | 0 | if (base != null) |
95 | |
{ |
96 | 0 | return false; |
97 | |
} |
98 | 0 | if (property == null) |
99 | |
{ |
100 | 0 | throw new PropertyNotFoundException(); |
101 | |
} |
102 | 0 | if (!(property instanceof String)) |
103 | |
{ |
104 | 0 | return false; |
105 | |
} |
106 | |
|
107 | 0 | final ResourceBundle bundle = getResourceBundle(context, (String)property); |
108 | |
|
109 | 0 | if (bundle != null) |
110 | |
{ |
111 | 0 | context.setPropertyResolved(true); |
112 | 0 | return true; |
113 | |
} |
114 | |
|
115 | 0 | return false; |
116 | |
} |
117 | |
|
118 | |
@Override |
119 | |
public Object getValue(final ELContext context, final Object base, final Object property) |
120 | |
throws NullPointerException, PropertyNotFoundException, ELException |
121 | |
{ |
122 | |
|
123 | 0 | if (base != null) |
124 | |
{ |
125 | 0 | return null; |
126 | |
} |
127 | 0 | if (property == null) |
128 | |
{ |
129 | 0 | throw new PropertyNotFoundException(); |
130 | |
} |
131 | 0 | if (!(property instanceof String)) |
132 | |
{ |
133 | 0 | return null; |
134 | |
} |
135 | |
|
136 | 0 | final ResourceBundle bundle = getResourceBundle(context, (String)property); |
137 | |
|
138 | 0 | if (bundle != null) |
139 | |
{ |
140 | 0 | context.setPropertyResolved(true); |
141 | 0 | return bundle; |
142 | |
} |
143 | |
|
144 | 0 | return null; |
145 | |
} |
146 | |
|
147 | |
@Override |
148 | |
public Class<?> getType(final ELContext context, final Object base, final Object property) |
149 | |
throws NullPointerException, PropertyNotFoundException, ELException |
150 | |
{ |
151 | |
|
152 | 0 | if (base != null) |
153 | |
{ |
154 | 0 | return null; |
155 | |
} |
156 | 0 | if (property == null) |
157 | |
{ |
158 | 0 | throw new PropertyNotFoundException(); |
159 | |
} |
160 | 0 | if (!(property instanceof String)) |
161 | |
{ |
162 | 0 | return null; |
163 | |
} |
164 | |
|
165 | 0 | final ResourceBundle bundle = getResourceBundle(context, (String)property); |
166 | |
|
167 | 0 | if (bundle != null) |
168 | |
{ |
169 | 0 | context.setPropertyResolved(true); |
170 | 0 | return ResourceBundle.class; |
171 | |
} |
172 | |
|
173 | 0 | return null; |
174 | |
} |
175 | |
|
176 | |
@Override |
177 | |
public Iterator<FeatureDescriptor> getFeatureDescriptors(final ELContext context, final Object base) |
178 | |
{ |
179 | |
|
180 | 0 | if (base != null) |
181 | |
{ |
182 | 0 | return null; |
183 | |
} |
184 | |
|
185 | 0 | final ArrayList<FeatureDescriptor> descriptors = new ArrayList<FeatureDescriptor>(); |
186 | |
|
187 | 0 | final Map<String, org.apache.myfaces.config.element.ResourceBundle> resourceBundles = |
188 | |
runtimeConfig(context).getResourceBundles(); |
189 | |
|
190 | 0 | for (org.apache.myfaces.config.element.ResourceBundle resourceBundle : resourceBundles.values()) |
191 | |
{ |
192 | 0 | descriptors.add(makeDescriptor(resourceBundle)); |
193 | 0 | } |
194 | |
|
195 | 0 | return descriptors.iterator(); |
196 | |
} |
197 | |
|
198 | |
@Override |
199 | |
public Class<?> getCommonPropertyType(final ELContext context, final Object base) |
200 | |
{ |
201 | |
|
202 | 0 | if (base != null) |
203 | |
{ |
204 | 0 | return null; |
205 | |
} |
206 | |
|
207 | 0 | return String.class; |
208 | |
} |
209 | |
|
210 | |
|
211 | |
private static FacesContext facesContext(final ELContext context) |
212 | |
{ |
213 | 0 | return (FacesContext)context.getContext(FacesContext.class); |
214 | |
} |
215 | |
|
216 | |
private static ResourceBundle getResourceBundle(final ELContext context, final String property) |
217 | |
{ |
218 | 0 | final FacesContext facesContext = facesContext(context); |
219 | 0 | if (facesContext != null) |
220 | |
{ |
221 | 0 | final Application application = facesContext.getApplication(); |
222 | 0 | return application.getResourceBundle(facesContext, property); |
223 | |
} |
224 | |
|
225 | 0 | return null; |
226 | |
} |
227 | |
|
228 | |
protected RuntimeConfig runtimeConfig(ELContext context) |
229 | |
{ |
230 | 0 | final FacesContext facesContext = facesContext(context); |
231 | |
|
232 | |
|
233 | 0 | if (this.runtimeConfig == null) |
234 | |
{ |
235 | 0 | this.runtimeConfig = RuntimeConfig.getCurrentInstance(facesContext.getExternalContext()); |
236 | |
} |
237 | |
|
238 | 0 | return runtimeConfig; |
239 | |
} |
240 | |
|
241 | |
private static FeatureDescriptor makeDescriptor( |
242 | |
org.apache.myfaces.config.element.ResourceBundle bundle) |
243 | |
{ |
244 | 0 | final FeatureDescriptor fd = new FeatureDescriptor(); |
245 | 0 | fd.setValue(ELResolver.RESOLVABLE_AT_DESIGN_TIME, Boolean.TRUE); |
246 | 0 | fd.setName(bundle.getVar()); |
247 | 0 | fd.setDisplayName(bundle.getDisplayName()); |
248 | 0 | fd.setValue(ELResolver.TYPE, ResourceBundle.class); |
249 | 0 | fd.setShortDescription(""); |
250 | 0 | fd.setExpert(false); |
251 | 0 | fd.setHidden(false); |
252 | 0 | fd.setPreferred(true); |
253 | 0 | return fd; |
254 | |
} |
255 | |
} |