Skip to content

Package: ResourceBundleELResolver

ResourceBundleELResolver

nameinstructionbranchcomplexitylinemethod
ResourceBundleELResolver()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getCommonPropertyType(ELContext, Object)
M: 7 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getFeatureDescriptors(ELContext, Object)
M: 61 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 18 C: 0
0%
M: 1 C: 0
0%
getType(ELContext, Object, Object)
M: 14 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
getValue(ELContext, Object, Object)
M: 26 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
isReadOnly(ELContext, Object, Object)
M: 16 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
setValue(ELContext, Object, Object, Object)
M: 18 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2021 Oracle and/or its affiliates and others.
3: * All rights reserved.
4: * Copyright 2004 The Apache Software Foundation
5: *
6: * Licensed under the Apache License, Version 2.0 (the "License");
7: * you may not use this file except in compliance with the License.
8: * You may obtain a copy of the License at
9: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: package jakarta.el;
20:
21: import static java.lang.Boolean.TRUE;
22:
23: import java.beans.FeatureDescriptor;
24: import java.util.ArrayList;
25: import java.util.Enumeration;
26: import java.util.Iterator;
27: import java.util.List;
28: import java.util.MissingResourceException;
29: import java.util.ResourceBundle;
30:
31: /**
32: * Defines property resolution behavior on instances of {@link java.util.ResourceBundle}.
33: *
34: * <p>
35: * This resolver handles base objects of type <code>java.util.ResourceBundle</code>. It accepts any object as a property
36: * and coerces it to a <code>java.lang.String</code> for invoking
37: * {@link java.util.ResourceBundle#getObject(java.lang.String)}.
38: *
39: * <p>
40: * This resolver is read only and will throw a {@link PropertyNotWritableException} if <code>setValue</code> is called.
41: *
42: * <p>
43: * <code>ELResolver</code>s are combined together using {@link CompositeELResolver}s, to define rich semantics for
44: * evaluating an expression. See the javadocs for {@link ELResolver} for details.
45: *
46: * @see CompositeELResolver
47: * @see ELResolver
48: * @see java.util.ResourceBundle
49: *
50: * @since Jakarta Server Pages 2.1
51: */
52: public class ResourceBundleELResolver extends ELResolver {
53:
54: /**
55: * If the base object is an instance of <code>ResourceBundle</code>, the provided property will first be coerced to a
56: * <code>String</code>. The <code>Object</code> returned by <code>getObject</code> on the base
57: * <code>ResourceBundle</code> will be returned.
58: *
59: * <p>
60: * If the base is <code>ResourceBundle</code>, the <code>propertyResolved</code> property of the <code>ELContext</code>
61: * object must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code>
62: * after this method is called, the caller should ignore the return value.
63: *
64: * @param context The context of this evaluation.
65: * @param base The ResourceBundle to analyze.
66: * @param property The name of the property to analyze. Will be coerced to a <code>String</code>.
67: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
68: * <code>null</code> if property is <code>null</code> otherwise the <code>Object</code> for the given key (property
69: * coerced to <code>String</code>) from the <code>ResourceBundle</code>. If no object for the given key can be found,
70: * then the <code>String</code> "???" + key + "???".
71: * @throws NullPointerException if context is <code>null</code>
72: * @throws ELException if an exception was thrown while performing the property or variable resolution. The thrown
73: * exception must be included as the cause property of this exception, if available.
74: */
75: @Override
76: public Object getValue(ELContext context, Object base, Object property) {
77:• if (context == null) {
78: throw new NullPointerException();
79: }
80:
81:• if (base instanceof ResourceBundle) {
82: context.setPropertyResolved(true);
83:• if (property != null) {
84: try {
85: return ((ResourceBundle) base).getObject(property.toString());
86: } catch (MissingResourceException e) {
87: return "???" + property + "???";
88: }
89: }
90: }
91:
92: return null;
93: }
94:
95: /**
96: * If the base object is an instance of <code>ResourceBundle</code>, return <code>null</code>, since the resolver is
97: * read only.
98: *
99: * <p>
100: * If the base is <code>ResourceBundle</code>, the <code>propertyResolved</code> property of the <code>ELContext</code>
101: * object must be set to <code>true</code> by this resolver, before returning. If this property is not <code>true</code>
102: * after this method is called, the caller should ignore the return value.
103: * </p>
104: *
105: * @param context The context of this evaluation.
106: * @param base The ResourceBundle to analyze.
107: * @param property The name of the property to analyze.
108: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
109: * <code>null</code> otherwise undefined.
110: * @throws NullPointerException if context is <code>null</code>
111: */
112: @Override
113: public Class<?> getType(ELContext context, Object base, Object property) {
114:• if (context == null) {
115: throw new NullPointerException();
116: }
117:
118:• if (base instanceof ResourceBundle) {
119: context.setPropertyResolved(true);
120: }
121:
122: return null;
123: }
124:
125: /**
126: * If the base object is a ResourceBundle, throw a {@link PropertyNotWritableException}.
127: *
128: * @param context The context of this evaluation.
129: * @param base The ResourceBundle to be modified. Only bases that are of type ResourceBundle are handled.
130: * @param property The String property to use.
131: * @param value The value to be set.
132: * @throws NullPointerException if context is <code>null</code>.
133: * @throws PropertyNotWritableException Always thrown if base is an instance of ReasourceBundle.
134: */
135: @Override
136: public void setValue(ELContext context, Object base, Object property, Object value) {
137:• if (context == null) {
138: throw new NullPointerException();
139: }
140:
141:• if (base instanceof ResourceBundle) {
142: context.setPropertyResolved(true);
143: throw new PropertyNotWritableException("ResourceBundles are immutable");
144: }
145: }
146:
147: /**
148: * If the base object is not null and an <code>instanceof</code> {@link ResourceBundle}, return <code>true</code>.
149: *
150: * @param context The context of this evaluation.
151: * @param base The ResourceBundle to be modified. Only bases that are of type ResourceBundle are handled.
152: * @param property The String property to use.
153: * @return If the <code>propertyResolved</code> property of <code>ELContext</code> was set to <code>true</code>, then
154: * <code>true</code> otherwise undefined.
155: * @throws NullPointerException if context is <code>null</code>
156: */
157: @Override
158: public boolean isReadOnly(ELContext context, Object base, Object property) {
159:• if (context == null) {
160: throw new NullPointerException();
161: }
162:
163:• if (base instanceof ResourceBundle) {
164: context.setPropertyResolved(true);
165: return true;
166: }
167:
168: return false;
169: }
170:
171: /**
172: * If the base object is a ResourceBundle, returns an <code>Iterator</code> containing the set of keys available in the
173: * <code>ResourceBundle</code>. Otherwise, returns <code>null</code>.
174: *
175: * <p>
176: * The <code>Iterator</code> returned must contain zero or more instances of {@link java.beans.FeatureDescriptor}. Each
177: * info object contains information about a key in the ResourceBundle, and is initialized as follows:
178: * <ul>
179: * <li>displayName - The <code>String</code> key
180: * <li>name - Same as displayName property.</li>
181: * <li>shortDescription - Empty string</li>
182: * <li>expert - <code>false</code></li>
183: * <li>hidden - <code>false</code></li>
184: * <li>preferred - <code>true</code></li>
185: * </ul>
186: *
187: * In addition, the following named attributes must be set in the returned <code>FeatureDescriptor</code>s:
188: * <ul>
189: * <li>{@link ELResolver#TYPE} - <code>String.class</code></li>
190: * <li>{@link ELResolver#RESOLVABLE_AT_DESIGN_TIME} - <code>true</code></li>
191: * </ul>
192: *
193: *
194: * @param context The context of this evaluation.
195: * @param base The bundle whose keys are to be iterated over. Only bases of type <code>ResourceBundle</code> are handled
196: * by this resolver.
197: * @return An <code>Iterator</code> containing zero or more (possibly infinitely more) <code>FeatureDescriptor</code>
198: * objects, each representing a key in this bundle, or <code>null</code> if the base object is not a ResourceBundle.
199: *
200: * @deprecated This method will be removed without replacement in EL 6.0
201: */
202: @Deprecated(forRemoval = true, since = "5.0")
203: @Override
204: public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
205:• if (base instanceof ResourceBundle) {
206: ResourceBundle bundle = (ResourceBundle) base;
207: List<FeatureDescriptor> features = new ArrayList<>();
208: String key = null;
209: FeatureDescriptor desc = null;
210:
211:• for (Enumeration<String> e = bundle.getKeys(); e.hasMoreElements();) {
212: key = e.nextElement();
213: desc = new FeatureDescriptor();
214: desc.setDisplayName(key);
215: desc.setExpert(false);
216: desc.setHidden(false);
217: desc.setName(key);
218: desc.setPreferred(true);
219: desc.setValue(TYPE, String.class);
220: desc.setValue(RESOLVABLE_AT_DESIGN_TIME, TRUE);
221: features.add(desc);
222: }
223:
224: return features.iterator();
225: }
226:
227: return null;
228: }
229:
230: /**
231: * If the base object is a ResourceBundle, returns the most general type that this resolver accepts for the
232: * <code>property</code> argument. Otherwise, returns <code>null</code>.
233: *
234: * <p>
235: * Assuming the base is a <code>ResourceBundle</code>, this method will always return <code>String.class</code>.
236: *
237: * @param context The context of this evaluation.
238: * @param base The bundle to analyze. Only bases of type <code>ResourceBundle</code> are handled by this resolver.
239: * @return <code>null</code> if base is not a <code>ResourceBundle</code> otherwise <code>String.class</code>.
240: */
241: @Override
242: public Class<?> getCommonPropertyType(ELContext context, Object base) {
243:• if (base instanceof ResourceBundle) {
244: return String.class;
245: }
246:
247: return null;
248: }
249: }