KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > el > StackELResolver


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.el;
31
32 import javax.el.ELContext;
33 import javax.el.ELResolver;
34 import java.beans.FeatureDescriptor JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Iterator JavaDoc;
37
38 /**
39  * Stack-based composite resolver,
40  */

41 public class StackELResolver extends ELResolver {
42   public final ArrayList JavaDoc<ELResolver> _resolverStack
43     = new ArrayList JavaDoc<ELResolver>();
44   
45   public StackELResolver()
46   {
47   }
48
49   public void push(ELResolver elResolver)
50   {
51     if (elResolver == null)
52       throw new NullPointerException JavaDoc();
53
54     _resolverStack.add(elResolver);
55   }
56
57   public ELResolver pop()
58   {
59     if (_resolverStack.size() > 0)
60       return _resolverStack.remove(_resolverStack.size() - 1);
61     else
62       return null;
63   }
64   
65   @Override JavaDoc
66   public Class JavaDoc<?> getCommonPropertyType(ELContext env, Object JavaDoc base)
67   {
68     for (int i = _resolverStack.size() - 1; i >= 0; i--) {
69       ELResolver resolver = _resolverStack.get(i);
70
71       return getCommonPropertyType(env, base);
72     }
73
74     return null;
75   }
76
77   @Override JavaDoc
78   public Iterator JavaDoc<FeatureDescriptor JavaDoc> getFeatureDescriptors(ELContext env,
79                                Object JavaDoc base)
80   {
81     ArrayList JavaDoc<FeatureDescriptor JavaDoc> descriptors = null;
82
83     for (int i = _resolverStack.size() - 1; i >= 0; i--) {
84       ELResolver resolver = _resolverStack.get(i);
85
86       Iterator JavaDoc<FeatureDescriptor JavaDoc> iter
87     = resolver.getFeatureDescriptors(env, base);
88
89       if (iter == null)
90     continue;
91
92       if (descriptors == null)
93     descriptors = new ArrayList JavaDoc<FeatureDescriptor JavaDoc>();
94
95       while (iter.hasNext()) {
96     FeatureDescriptor JavaDoc desc = iter.next();
97
98     descriptors.add(desc);
99       }
100     }
101
102     if (descriptors != null)
103       return descriptors.iterator();
104     else
105       return null;
106   }
107
108   @Override JavaDoc
109   public Class JavaDoc<?> getType(ELContext context,
110               Object JavaDoc base,
111               Object JavaDoc property)
112   {
113     context.setPropertyResolved(false);
114
115     for (int i = _resolverStack.size() - 1; i >= 0; i--) {
116       ELResolver resolver = _resolverStack.get(i);
117
118       Class JavaDoc type = resolver.getType(context, base, property);
119
120       if (context.isPropertyResolved())
121     return type;
122     }
123
124     return null;
125   }
126
127   @Override JavaDoc
128   public Object JavaDoc getValue(ELContext context,
129              Object JavaDoc base,
130              Object JavaDoc property)
131   {
132     context.setPropertyResolved(false);
133
134     for (int i = _resolverStack.size() - 1; i >= 0; i--) {
135       ELResolver resolver = _resolverStack.get(i);
136
137       Object JavaDoc value = resolver.getValue(context, base, property);
138
139       if (context.isPropertyResolved()) {
140     return value;
141       }
142     }
143
144     return null;
145   }
146
147   @Override JavaDoc
148   public boolean isReadOnly(ELContext context,
149                 Object JavaDoc base,
150                 Object JavaDoc property)
151   {
152     context.setPropertyResolved(false);
153
154     for (int i = _resolverStack.size() - 1; i >= 0; i--) {
155       ELResolver resolver = _resolverStack.get(i);
156
157       boolean isReadOnly = resolver.isReadOnly(context, base, property);
158
159       if (context.isPropertyResolved())
160     return isReadOnly;
161     }
162
163     return false;
164   }
165
166   @Override JavaDoc
167   public void setValue(ELContext context,
168                Object JavaDoc base,
169                Object JavaDoc property,
170                Object JavaDoc value)
171   {
172     context.setPropertyResolved(false);
173
174     for (int i = _resolverStack.size() - 1; i >= 0; i--) {
175       ELResolver resolver = _resolverStack.get(i);
176
177       resolver.setValue(context, base, property, value);
178
179       if (context.isPropertyResolved()) {
180     return;
181       }
182     }
183   }
184 }
185
Popular Tags