KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > el > ListELResolver


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.Arrays JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 public class ListELResolver extends ELResolver {
28
29     private final boolean readOnly;
30
31     private final static Class JavaDoc UNMODIFIABLE = Collections.unmodifiableList(
32             new ArrayList JavaDoc()).getClass();
33
34     public ListELResolver() {
35         this.readOnly = false;
36     }
37
38     public ListELResolver(boolean readOnly) {
39         this.readOnly = readOnly;
40     }
41
42     public Object JavaDoc getValue(ELContext context, Object JavaDoc base, Object JavaDoc property)
43             throws NullPointerException JavaDoc, PropertyNotFoundException, ELException {
44         if (context == null) {
45             throw new NullPointerException JavaDoc();
46         }
47
48         if (base instanceof List JavaDoc) {
49             context.setPropertyResolved(true);
50             List JavaDoc list = (List JavaDoc) base;
51             int idx = coerce(property);
52             if (idx < 0 || idx >= list.size()) {
53                 return null;
54             }
55             return list.get(idx);
56         }
57
58         return null;
59     }
60
61     public Class JavaDoc<?> getType(ELContext context, Object JavaDoc base, Object JavaDoc property)
62             throws NullPointerException JavaDoc, PropertyNotFoundException, ELException {
63         if (context == null) {
64             throw new NullPointerException JavaDoc();
65         }
66
67         if (base instanceof List JavaDoc) {
68             context.setPropertyResolved(true);
69             List JavaDoc list = (List JavaDoc) base;
70             int idx = coerce(property);
71             if (idx < 0 || idx >= list.size()) {
72                 return null;
73             }
74             Object JavaDoc obj = list.get(idx);
75             return (obj != null) ? obj.getClass() : null;
76         }
77
78         return null;
79     }
80
81     public void setValue(ELContext context, Object JavaDoc base, Object JavaDoc property,
82             Object JavaDoc value) throws NullPointerException JavaDoc,
83             PropertyNotFoundException, PropertyNotWritableException,
84             ELException {
85         if (context == null) {
86             throw new NullPointerException JavaDoc();
87         }
88
89         if (base instanceof List JavaDoc) {
90             context.setPropertyResolved(true);
91             List JavaDoc list = (List JavaDoc) base;
92
93             if (this.readOnly) {
94                 throw new PropertyNotWritableException(message(context,
95                         "resolverNotWriteable", new Object JavaDoc[] { base.getClass()
96                                 .getName() }));
97             }
98
99             int idx = coerce(property);
100             try {
101                 list.set(idx, value);
102             } catch (UnsupportedOperationException JavaDoc e) {
103                 throw new PropertyNotWritableException(e);
104             } catch (IndexOutOfBoundsException JavaDoc e) {
105                 throw new PropertyNotFoundException(e);
106             }
107         }
108     }
109
110     public boolean isReadOnly(ELContext context, Object JavaDoc base, Object JavaDoc property)
111             throws NullPointerException JavaDoc, PropertyNotFoundException, ELException {
112         if (context == null) {
113             throw new NullPointerException JavaDoc();
114         }
115
116         if (base instanceof List JavaDoc) {
117             context.setPropertyResolved(true);
118             List JavaDoc list = (List JavaDoc) base;
119             int idx = coerce(property);
120             if (idx < 0 || idx >= list.size()) {
121                 throw new PropertyNotFoundException(
122                         new ArrayIndexOutOfBoundsException JavaDoc(idx).getMessage());
123             }
124             return this.readOnly || UNMODIFIABLE.equals(list.getClass());
125         }
126
127         return this.readOnly;
128     }
129
130     public Iterator JavaDoc<FeatureDescriptor JavaDoc> getFeatureDescriptors(ELContext context, Object JavaDoc base) {
131         if (base instanceof List JavaDoc) {
132             FeatureDescriptor JavaDoc[] descs = new FeatureDescriptor JavaDoc[((List JavaDoc) base).size()];
133             for (int i = 0; i < descs.length; i++) {
134                 descs[i] = new FeatureDescriptor JavaDoc();
135                 descs[i].setDisplayName("["+i+"]");
136                 descs[i].setExpert(false);
137                 descs[i].setHidden(false);
138                 descs[i].setName(""+i);
139                 descs[i].setPreferred(true);
140                 descs[i].setValue(RESOLVABLE_AT_DESIGN_TIME, Boolean.FALSE);
141                 descs[i].setValue(TYPE, Integer JavaDoc.class);
142             }
143             return Arrays.asList(descs).iterator();
144         }
145         return null;
146     }
147
148     public Class JavaDoc<?> getCommonPropertyType(ELContext context, Object JavaDoc base) {
149         if (base != null && base instanceof List JavaDoc) {
150             return Integer JavaDoc.class;
151         }
152         return null;
153     }
154
155     private final static int coerce(Object JavaDoc property) {
156         if (property instanceof Number JavaDoc) {
157             return ((Number JavaDoc) property).intValue();
158         }
159         if (property instanceof Character JavaDoc) {
160             return ((Character JavaDoc) property).charValue();
161         }
162         if (property instanceof Boolean JavaDoc) {
163             return (((Boolean JavaDoc) property).booleanValue() ? 1 : 0);
164         }
165         if (property instanceof String JavaDoc) {
166             return Integer.parseInt((String JavaDoc) property);
167         }
168         throw new IllegalArgumentException JavaDoc(property != null ? property
169                 .toString() : "null");
170     }
171 }
172
Popular Tags