KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > el > ArrayELResolver


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.lang.reflect.Array JavaDoc;
22 import java.util.Arrays JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 public class ArrayELResolver extends ELResolver {
26
27     private final boolean readOnly;
28
29     public ArrayELResolver() {
30         this.readOnly = false;
31     }
32
33     public ArrayELResolver(boolean readOnly) {
34         this.readOnly = readOnly;
35     }
36
37     public Object JavaDoc getValue(ELContext context, Object JavaDoc base, Object JavaDoc property)
38             throws NullPointerException JavaDoc, PropertyNotFoundException, ELException {
39         if (context == null) {
40             throw new NullPointerException JavaDoc();
41         }
42
43         if (base != null && base.getClass().isArray()) {
44             context.setPropertyResolved(true);
45             int idx = coerce(property);
46             if (idx < 0 || idx >= Array.getLength(base)) {
47                 return null;
48             } else {
49                 return Array.get(base, idx);
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 != null && base.getClass().isArray()) {
63             context.setPropertyResolved(true);
64             int idx = coerce(property);
65             checkBounds(base, idx);
66             return base.getClass().getComponentType();
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 != null && base.getClass().isArray()) {
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             int idx = coerce(property);
90             checkBounds(base, idx);
91             Array.set(base, idx, value);
92         }
93     }
94
95     public boolean isReadOnly(ELContext context, Object JavaDoc base, Object JavaDoc property)
96             throws NullPointerException JavaDoc, PropertyNotFoundException, ELException {
97         if (context == null) {
98             throw new NullPointerException JavaDoc();
99         }
100
101         if (base != null && base.getClass().isArray()) {
102             context.setPropertyResolved(true);
103             int idx = coerce(property);
104             checkBounds(base, idx);
105         }
106
107         return this.readOnly;
108     }
109
110     public Iterator JavaDoc<FeatureDescriptor JavaDoc> getFeatureDescriptors(ELContext context, Object JavaDoc base) {
111         if (base != null && base.getClass().isArray()) {
112             FeatureDescriptor JavaDoc[] descs = new FeatureDescriptor JavaDoc[Array.getLength(base)];
113             for (int i = 0; i < descs.length; i++) {
114                 descs[i] = new FeatureDescriptor JavaDoc();
115                 descs[i].setDisplayName("["+i+"]");
116                 descs[i].setExpert(false);
117                 descs[i].setHidden(false);
118                 descs[i].setName(""+i);
119                 descs[i].setPreferred(true);
120                 descs[i].setValue(RESOLVABLE_AT_DESIGN_TIME, Boolean.FALSE);
121                 descs[i].setValue(TYPE, Integer JavaDoc.class);
122             }
123             return Arrays.asList(descs).iterator();
124         }
125         return null;
126     }
127
128     public Class JavaDoc<?> getCommonPropertyType(ELContext context, Object JavaDoc base) {
129         if (base != null && base.getClass().isArray()) {
130             return Integer JavaDoc.class;
131         }
132         return null;
133     }
134
135     private final static void checkBounds(Object JavaDoc base, int idx) {
136         if (idx < 0 || idx >= Array.getLength(base)) {
137             throw new PropertyNotFoundException(
138                     new ArrayIndexOutOfBoundsException JavaDoc(idx).getMessage());
139         }
140     }
141
142     private final static int coerce(Object JavaDoc property) {
143         if (property instanceof Number JavaDoc) {
144             return ((Number JavaDoc) property).intValue();
145         }
146         if (property instanceof Character JavaDoc) {
147             return ((Character JavaDoc) property).charValue();
148         }
149         if (property instanceof Boolean JavaDoc) {
150             return (((Boolean JavaDoc) property).booleanValue() ? 1 : 0);
151         }
152         if (property instanceof String JavaDoc) {
153             return Integer.parseInt((String JavaDoc) property);
154         }
155         throw new IllegalArgumentException JavaDoc(property != null ? property
156                 .toString() : "null");
157     }
158
159 }
160
Popular Tags