KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > el > MapELResolver


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.Collections JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 public class MapELResolver extends ELResolver {
29
30     private final static Class JavaDoc UNMODIFIABLE = Collections.unmodifiableMap(
31             new HashMap JavaDoc()).getClass();
32
33     private final boolean readOnly;
34
35     public MapELResolver() {
36         this.readOnly = false;
37     }
38
39     public MapELResolver(boolean readOnly) {
40         this.readOnly = readOnly;
41     }
42
43     public Object JavaDoc getValue(ELContext context, Object JavaDoc base, Object JavaDoc property)
44             throws NullPointerException JavaDoc, PropertyNotFoundException, ELException {
45         if (context == null) {
46             throw new NullPointerException JavaDoc();
47         }
48
49         if (base instanceof Map JavaDoc) {
50             context.setPropertyResolved(true);
51             return ((Map JavaDoc) base).get(property);
52         }
53         
54         return null;
55     }
56
57     public Class JavaDoc<?> getType(ELContext context, Object JavaDoc base, Object JavaDoc property)
58             throws NullPointerException JavaDoc, PropertyNotFoundException, ELException {
59         if (context == null) {
60             throw new NullPointerException JavaDoc();
61         }
62
63         if (base instanceof Map JavaDoc) {
64             context.setPropertyResolved(true);
65             Object JavaDoc obj = ((Map JavaDoc) base).get(property);
66             return (obj != null) ? obj.getClass() : null;
67         }
68         
69         return null;
70     }
71
72     public void setValue(ELContext context, Object JavaDoc base, Object JavaDoc property,
73             Object JavaDoc value) throws NullPointerException JavaDoc,
74             PropertyNotFoundException, PropertyNotWritableException,
75             ELException {
76         if (context == null) {
77             throw new NullPointerException JavaDoc();
78         }
79
80         if (base instanceof Map JavaDoc) {
81             context.setPropertyResolved(true);
82
83             if (this.readOnly) {
84                 throw new PropertyNotWritableException(message(context,
85                         "resolverNotWriteable", new Object JavaDoc[] { base.getClass()
86                                 .getName() }));
87             }
88
89             try {
90                 ((Map JavaDoc) base).put(property, value);
91             } catch (UnsupportedOperationException JavaDoc e) {
92                 throw new PropertyNotWritableException(e);
93             }
94         }
95     }
96
97     public boolean isReadOnly(ELContext context, Object JavaDoc base, Object JavaDoc property)
98             throws NullPointerException JavaDoc, PropertyNotFoundException, ELException {
99         if (context == null) {
100             throw new NullPointerException JavaDoc();
101         }
102
103         if (base instanceof Map JavaDoc) {
104             context.setPropertyResolved(true);
105             return this.readOnly || UNMODIFIABLE.equals(base.getClass());
106         }
107         
108         return this.readOnly;
109     }
110
111     public Iterator JavaDoc<FeatureDescriptor JavaDoc> getFeatureDescriptors(ELContext context, Object JavaDoc base) {
112         if (base instanceof Map JavaDoc) {
113             Iterator JavaDoc itr = ((Map JavaDoc) base).keySet().iterator();
114             List JavaDoc<FeatureDescriptor JavaDoc> feats = new ArrayList JavaDoc<FeatureDescriptor JavaDoc>();
115             Object JavaDoc key;
116             FeatureDescriptor JavaDoc desc;
117             while (itr.hasNext()) {
118                 key = itr.next();
119                 desc = new FeatureDescriptor JavaDoc();
120                 desc.setDisplayName(key.toString());
121                 desc.setExpert(false);
122                 desc.setHidden(false);
123                 desc.setName(key.toString());
124                 desc.setPreferred(true);
125                 desc.setValue(RESOLVABLE_AT_DESIGN_TIME, Boolean.FALSE);
126                 desc.setValue(TYPE, key.getClass());
127                 feats.add(desc);
128             }
129             return feats.iterator();
130         }
131         return null;
132     }
133
134     public Class JavaDoc<?> getCommonPropertyType(ELContext context, Object JavaDoc base) {
135         if (base instanceof Map JavaDoc) {
136             return Object JavaDoc.class;
137         }
138         return null;
139     }
140
141 }
142
Popular Tags