KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > el > ResourceBundleELResolver


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package javax.el;
19
20 import java.beans.FeatureDescriptor JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.MissingResourceException JavaDoc;
26 import java.util.ResourceBundle JavaDoc;
27
28 public class ResourceBundleELResolver extends ELResolver {
29
30     public ResourceBundleELResolver() {
31         super();
32     }
33
34     public Object JavaDoc getValue(ELContext context, Object JavaDoc base, Object JavaDoc property)
35             throws NullPointerException JavaDoc, PropertyNotFoundException, ELException {
36         if (context == null) {
37             throw new NullPointerException JavaDoc();
38         }
39         
40         if (base instanceof ResourceBundle JavaDoc) {
41             context.setPropertyResolved(true);
42
43             if (property != null) {
44                 try {
45                     return ((ResourceBundle JavaDoc) base).getObject(property
46                             .toString());
47                 } catch (MissingResourceException JavaDoc mre) {
48                     return "???" + property.toString() + "???";
49                 }
50             }
51         }
52
53         return null;
54     }
55
56     public Class JavaDoc<?> getType(ELContext context, Object JavaDoc base, Object JavaDoc property)
57             throws NullPointerException JavaDoc, PropertyNotFoundException, ELException {
58         if (context == null) {
59             throw new NullPointerException JavaDoc();
60         }
61         
62         if (base instanceof ResourceBundle JavaDoc) {
63             context.setPropertyResolved(true);
64         }
65         
66         return null;
67     }
68
69     public void setValue(ELContext context, Object JavaDoc base, Object JavaDoc property,
70             Object JavaDoc value) throws NullPointerException JavaDoc,
71             PropertyNotFoundException, PropertyNotWritableException,
72             ELException {
73         if (context == null) {
74             throw new NullPointerException JavaDoc();
75         }
76         
77         if (base instanceof ResourceBundle JavaDoc) {
78             context.setPropertyResolved(true);
79             throw new PropertyNotWritableException(message(context,
80                     "resolverNotWriteable", new Object JavaDoc[] { base.getClass()
81                             .getName() }));
82         }
83     }
84
85     public boolean isReadOnly(ELContext context, Object JavaDoc base, Object JavaDoc property)
86             throws NullPointerException JavaDoc, PropertyNotFoundException, ELException {
87         if (context == null) {
88             throw new NullPointerException JavaDoc();
89         }
90         
91         if (base instanceof ResourceBundle JavaDoc) {
92             context.setPropertyResolved(true);
93         }
94         
95         return true;
96     }
97
98     public Iterator JavaDoc<FeatureDescriptor JavaDoc> getFeatureDescriptors(ELContext context, Object JavaDoc base) {
99         if (base instanceof ResourceBundle JavaDoc) {
100             List JavaDoc<FeatureDescriptor JavaDoc> feats = new ArrayList JavaDoc<FeatureDescriptor JavaDoc>();
101             Enumeration JavaDoc e = ((ResourceBundle JavaDoc) base).getKeys();
102             FeatureDescriptor JavaDoc feat;
103             String JavaDoc key;
104             while (e.hasMoreElements()) {
105                 key = (String JavaDoc) e.nextElement();
106                 feat = new FeatureDescriptor JavaDoc();
107                 feat.setDisplayName(key);
108                 feat.setExpert(false);
109                 feat.setHidden(false);
110                 feat.setName(key);
111                 feat.setPreferred(true);
112                 feat.setValue(RESOLVABLE_AT_DESIGN_TIME, Boolean.TRUE);
113                 feat.setValue(TYPE, String JavaDoc.class);
114                 feats.add(feat);
115             }
116             return feats.iterator();
117         }
118         return null;
119     }
120
121     public Class JavaDoc<?> getCommonPropertyType(ELContext context, Object JavaDoc base) {
122         if (base instanceof ResourceBundle JavaDoc) {
123             return String JavaDoc.class;
124         }
125         return null;
126     }
127
128 }
129
Popular Tags