KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > el > CompositeELResolver


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.Iterator JavaDoc;
22
23 public class CompositeELResolver extends ELResolver {
24
25     private int size;
26
27     private ELResolver[] resolvers;
28
29     public CompositeELResolver() {
30         this.size = 0;
31         this.resolvers = new ELResolver[2];
32     }
33
34     public void add(ELResolver elResolver) {
35         if (elResolver == null) {
36             throw new NullPointerException JavaDoc();
37         }
38
39         if (this.size >= this.resolvers.length) {
40             ELResolver[] nr = new ELResolver[this.size * 2];
41             System.arraycopy(this.resolvers, 0, nr, 0, this.size);
42             this.resolvers = nr;
43         }
44         this.resolvers[this.size++] = elResolver;
45     }
46
47     public Object JavaDoc getValue(ELContext context, Object JavaDoc base, Object JavaDoc property)
48             throws NullPointerException JavaDoc, PropertyNotFoundException, ELException {
49         context.setPropertyResolved(false);
50         int sz = this.size;
51         Object JavaDoc result = null;
52         for (int i = 0; i < sz; i++) {
53             result = this.resolvers[i].getValue(context, base, property);
54             if (context.isPropertyResolved()) {
55                 return result;
56             }
57         }
58         return null;
59     }
60
61     public void setValue(ELContext context, Object JavaDoc base, Object JavaDoc property,
62             Object JavaDoc value) throws NullPointerException JavaDoc,
63             PropertyNotFoundException, PropertyNotWritableException,
64             ELException {
65         context.setPropertyResolved(false);
66         int sz = this.size;
67         for (int i = 0; i < sz; i++) {
68             this.resolvers[i].setValue(context, base, property, value);
69             if (context.isPropertyResolved()) {
70                 return;
71             }
72         }
73     }
74
75     public boolean isReadOnly(ELContext context, Object JavaDoc base, Object JavaDoc property)
76             throws NullPointerException JavaDoc, PropertyNotFoundException, ELException {
77         context.setPropertyResolved(false);
78         int sz = this.size;
79         boolean readOnly = false;
80         for (int i = 0; i < sz; i++) {
81             readOnly = this.resolvers[i].isReadOnly(context, base, property);
82             if (context.isPropertyResolved()) {
83                 return readOnly;
84             }
85         }
86         return false;
87     }
88
89     public Iterator JavaDoc<FeatureDescriptor JavaDoc> getFeatureDescriptors(ELContext context, Object JavaDoc base) {
90         return new FeatureIterator(context, base, this.resolvers, this.size);
91     }
92
93     public Class JavaDoc<?> getCommonPropertyType(ELContext context, Object JavaDoc base) {
94         int sz = this.size;
95         Class JavaDoc<?> commonType = null, type = null;
96         for (int i = 0; i < sz; i++) {
97             type = this.resolvers[i].getCommonPropertyType(context, base);
98             if (type != null
99                     && (commonType == null || commonType.isAssignableFrom(type))) {
100                 commonType = type;
101             }
102         }
103         return commonType;
104     }
105
106     public Class JavaDoc<?> getType(ELContext context, Object JavaDoc base, Object JavaDoc property)
107             throws NullPointerException JavaDoc, PropertyNotFoundException, ELException {
108         context.setPropertyResolved(false);
109         int sz = this.size;
110         Class JavaDoc<?> type;
111         for (int i = 0; i < sz; i++) {
112             type = this.resolvers[i].getType(context, base, property);
113             if (context.isPropertyResolved()) {
114                 return type;
115             }
116         }
117         return null;
118     }
119
120     private final static class FeatureIterator implements Iterator JavaDoc<FeatureDescriptor JavaDoc> {
121
122         private final ELContext context;
123
124         private final Object JavaDoc base;
125
126         private final ELResolver[] resolvers;
127
128         private final int size;
129
130         private Iterator JavaDoc itr;
131
132         private int idx;
133
134         public FeatureIterator(ELContext context, Object JavaDoc base,
135                 ELResolver[] resolvers, int size) {
136             this.context = context;
137             this.base = base;
138             this.resolvers = resolvers;
139             this.size = size;
140
141             this.idx = 0;
142             this.guaranteeIterator();
143         }
144         
145         private void guaranteeIterator() {
146             while (this.itr == null && this.idx < this.size) {
147                 this.itr = this.resolvers[this.idx].getFeatureDescriptors(
148                         this.context, this.base);
149                 this.idx++;
150             }
151         }
152
153         public boolean hasNext() {
154             return this.itr != null;
155         }
156
157         public FeatureDescriptor JavaDoc next() {
158             Object JavaDoc result = null;
159             if (this.itr != null) {
160                 if (this.itr.hasNext()) {
161                     result = this.itr.next();
162                     if (!this.itr.hasNext()) {
163                         this.itr = null;
164                         this.guaranteeIterator();
165                     }
166                 }
167             }
168             return (FeatureDescriptor JavaDoc) result;
169         }
170
171         public void remove() {
172             throw new UnsupportedOperationException JavaDoc();
173         }
174     }
175
176 }
177
Popular Tags