KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jsp2_1 > examples > elresolver > ColorELResolver


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  *
21  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
22  *
23  * Portions Copyright Apache Software Foundation.
24  */

25 package jsp2_1.examples.elresolver;
26
27 import java.util.Arrays JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import javax.el.ELContext;
30 import javax.el.ELException;
31 import javax.el.ELResolver;
32 import javax.el.PropertyNotWritableException;
33
34 /**
35  * Introduces a new ${Color} implicit object and resolves properties
36  * on that object.
37  *
38  * <p>Features include:
39  * <ul>
40  * <li>Look up common colors by name, e.g. ${Color.MintCream}</li>
41  * <li>Look up colors by hex, e.g. ${Color["#f0f0f0"]}</li>
42  * <li>Specify r, g, and b values numerically, e.g.
43  * ${Color[240][255][255]}</li>
44  * <li>Retrieve red, green, or blue individually, e.g. ${myColor.red}</li>
45  * <li>Retrieve HTML-style hex value for a color, e.g. ${myColor.hex}</li>
46  * <li>Retrieve java.awt.Color object for a color, e.g. ${myColor.color}</li>
47  * <li>Retrieve brigter and darker colors, e.g.
48  * ${Color.AliceBlue.darker.hex}</li>
49  * </ul>
50  * </p>
51  *
52  * @author Mark Roth
53  */

54 public class ColorELResolver
55     extends ELResolver
56 {
57     public Object JavaDoc getValue(ELContext context, Object JavaDoc base, Object JavaDoc property)
58         throws ELException
59     {
60         if(context == null) throw new NullPointerException JavaDoc();
61         
62         Object JavaDoc result = null;
63         
64         if(base == null) {
65             // Resolving first variable (e.g. ${Color}).
66
// We only handle "Color"
67
String JavaDoc propertyName = (String JavaDoc)property;
68             if(propertyName.equals("Color")) {
69                 result = new ColorImplicitObject();
70                 context.setPropertyResolved(true);
71             }
72         }
73         else if(base instanceof ColorImplicitObject) {
74             // Resolving a property on ${Color}
75
ColorImplicitObject color = (ColorImplicitObject)base;
76             
77             if(property instanceof Long JavaDoc) {
78                 // Handle ${Color[100]}
79
int red = ((Long JavaDoc)property).intValue();
80                 result = new ColorImplicitObject.ColorR(red);
81                 context.setPropertyResolved(true);
82             }
83             else {
84                 String JavaDoc colorName = property.toString();
85                 
86                 if(colorName.startsWith("#")) {
87                     // Handle ${Color['#f0f0f0']}
88
result = ColorImplicitObject.fromHex(colorName);
89                     context.setPropertyResolved(true);
90                 }
91                 else {
92                     // Handle ${Color.MintCream}
93
result = ColorImplicitObject.fromName(colorName);
94                     context.setPropertyResolved(true);
95                 }
96             }
97         }
98         else if(base instanceof ColorImplicitObject.ColorRGB) {
99             ColorImplicitObject.ColorRGB rgb =
100                 (ColorImplicitObject.ColorRGB)base;
101             // The rest is handled by the BeanPropertyResolver.
102
}
103         else if(base instanceof ColorImplicitObject.ColorRG) {
104             ColorImplicitObject.ColorRG rg = (ColorImplicitObject.ColorRG)base;
105             if(property instanceof Long JavaDoc) {
106                 // Handle ${Color[100][150][200]}
107
int blue = ((Long JavaDoc)property).intValue();
108                 result = new ColorImplicitObject.ColorRGB(rg.getRed(),
109                     rg.getGreen(), blue);
110                 context.setPropertyResolved(true);
111             }
112         }
113         else if(base instanceof ColorImplicitObject.ColorR) {
114             ColorImplicitObject.ColorR r = (ColorImplicitObject.ColorR)base;
115             if(property instanceof Long JavaDoc) {
116                 // Handle ${Color[100][150]}
117
int green = ((Long JavaDoc)property).intValue();
118                 result = new ColorImplicitObject.ColorRG(r.getRed(), green);
119                 context.setPropertyResolved(true);
120             }
121         }
122         
123         return result;
124     }
125
126     public Class JavaDoc getType(ELContext context, Object JavaDoc base, Object JavaDoc property)
127         throws ELException
128     {
129         Class JavaDoc result = null;
130         
131         if(context == null) throw new NullPointerException JavaDoc();
132         
133         if(base == null) {
134             // We don't handle setting top-level implicit objects.
135
}
136         else if(base instanceof ColorImplicitObject) {
137             // None of the properties of the ${Color} implicit object are
138
// ever writable.
139
context.setPropertyResolved(true);
140         }
141         else if(
142                 (base instanceof ColorImplicitObject.ColorR)
143             || (base instanceof ColorImplicitObject.ColorRG))
144         {
145             // Don't allow setting of
146
// ${Color[100][150]} or ${Color[100][150][200]}
147
if(property instanceof Long JavaDoc) {
148                 context.setPropertyResolved(true);
149             }
150         }
151         // The rest is handled by BeanELResolver, etc.
152

153         return result;
154     }
155
156     public void setValue(ELContext context, Object JavaDoc base, Object JavaDoc property,
157         Object JavaDoc value)
158         throws ELException
159     {
160         if(context == null) throw new NullPointerException JavaDoc();
161         
162         if(base == null) {
163             // We don't handle setting top-level implicit objects.
164
}
165         else if(base instanceof ColorImplicitObject) {
166             // None of the properties of the ${Color} implicit object are
167
// ever writable.
168
throw new PropertyNotWritableException();
169         }
170         else if(
171                 (base instanceof ColorImplicitObject.ColorR)
172             || (base instanceof ColorImplicitObject.ColorRG))
173         {
174             // Don't allow setting of
175
// ${Color[100][150]} or ${Color[100][150][200]}
176
if(property instanceof Long JavaDoc) {
177                 throw new PropertyNotWritableException();
178             }
179         }
180     }
181
182     public boolean isReadOnly(ELContext context, Object JavaDoc base,
183         Object JavaDoc property)
184         throws ELException
185     {
186         boolean result = false;
187         
188         if(context == null) throw new NullPointerException JavaDoc();
189         
190         if(base == null) {
191             // We don't handle setting top-level implicit objects.
192
}
193         else if(base instanceof ColorImplicitObject) {
194             // None of the properties of the ${Color} implicit object are
195
// ever writable.
196
result = false;
197             context.setPropertyResolved(true);
198         }
199         else if(
200                 (base instanceof ColorImplicitObject.ColorR)
201             || (base instanceof ColorImplicitObject.ColorRG))
202         {
203             // Don't allow setting of
204
// ${Color[100][150]} or ${Color[100][150][200]}
205
if(property instanceof Long JavaDoc) {
206                 result = false;
207                 context.setPropertyResolved(true);
208             }
209         }
210         
211         return result;
212     }
213
214     public Iterator JavaDoc getFeatureDescriptors(ELContext context, Object JavaDoc base) {
215         Iterator JavaDoc result = null;
216
217         if(context == null) throw new NullPointerException JavaDoc();
218         
219         if(base == null) {
220             result = Arrays.asList(new String JavaDoc[] {"Color"}).iterator();
221         }
222         else if(base instanceof ColorImplicitObject) {
223             // Return all color names
224
result = ColorImplicitObject.colorNameIterator();
225             
226             // XXX - There's no way to say we also accept 0-255
227
}
228         else if(
229                 (base instanceof ColorImplicitObject.ColorR)
230             || (base instanceof ColorImplicitObject.ColorRG)
231             || (base instanceof ColorImplicitObject.ColorRGB))
232         {
233             // We accept integers 0-255, but don't enumerate them.
234
// The tool will call getCommonPropertyType() instead.
235
result = null;
236         }
237         
238         // BeanELResolver will add to this iterator with the bean properties.
239

240         return result;
241     }
242
243     public Class JavaDoc getCommonPropertyType(ELContext context,
244                                                 Object JavaDoc base)
245     {
246         Class JavaDoc result = null;
247         
248         if(base == null) {
249             // Resolving first variable (e.g. ${Color}).
250
// We only handle "Color"
251
result = String JavaDoc.class;
252         }
253         else if(base instanceof ColorImplicitObject) {
254             // We handle either integers or strings, so return Object
255
result = Object JavaDoc.class;
256         }
257         else if(base instanceof ColorImplicitObject.ColorR) {
258             // We handle only integers in this case.
259
result = Long JavaDoc.class;
260         }
261         else if(base instanceof ColorImplicitObject.ColorRG) {
262             // We handle only integers in this case.
263
result = Long JavaDoc.class;
264         }
265         else if(base instanceof ColorImplicitObject.ColorRGB) {
266             // We don't do anything with these - the BeanELResolver
267
// takes it from here.
268
}
269         
270         return result;
271     }
272 }
273
Popular Tags