KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > el > ArraySuffix


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 1999 The Apache Software Foundation. All rights
5  * reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if
20  * any, must include the following acknowlegement:
21  * "This product includes software developed by the
22  * Apache Software Foundation (http://www.apache.org/)."
23  * Alternately, this acknowlegement may appear in the software itself,
24  * if and wherever such third-party acknowlegements normally appear.
25  *
26  * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
27  * Foundation" must not be used to endorse or promote products derived
28  * from this software without prior written permission. For written
29  * permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache"
32  * nor may "Apache" appear in their names without prior written
33  * permission of the Apache Group.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation. For more
51  * information on the Apache Software Foundation, please see
52  * <http://www.apache.org/>.
53  *
54  */

55
56 package org.apache.commons.el;
57
58 import java.lang.reflect.Array JavaDoc;
59 import java.lang.reflect.InvocationTargetException JavaDoc;
60 import java.util.List JavaDoc;
61 import java.util.Map JavaDoc;
62 import javax.servlet.jsp.el.VariableResolver JavaDoc;
63 import javax.servlet.jsp.el.ELException JavaDoc;
64 import javax.servlet.jsp.el.FunctionMapper JavaDoc;
65
66 /**
67  *
68  * <p>Represents an operator that obtains a Map entry, an indexed
69  * value, a property value, or an indexed property value of an object.
70  * The following are the rules for evaluating this operator:
71  *
72  * <ul><pre>
73  * Evaluating a[b] (assuming a.b == a["b"])
74  * a is null
75  * return null
76  * b is null
77  * return null
78  * a is Map
79  * !a.containsKey (b)
80  * return null
81  * a.get(b) == null
82  * return null
83  * otherwise
84  * return a.get(b)
85  * a is List or array
86  * coerce b to int (using coercion rules)
87  * coercion couldn't be performed
88  * error
89  * a.get(b) or Array.get(a, b) throws ArrayIndexOutOfBoundsException or IndexOutOfBoundsException
90  * return null
91  * a.get(b) or Array.get(a, b) throws other exception
92  * error
93  * return a.get(b) or Array.get(a, b)
94  *
95  * coerce b to String
96  * b is a readable property of a
97  * getter throws an exception
98  * error
99  * otherwise
100  * return result of getter call
101  *
102  * otherwise
103  * error
104  * </pre></ul>
105  *
106  * @author Nathan Abramson - Art Technology Group
107  * @author Shawn Bayern
108  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: luehe $
109  **/

110
111 public class ArraySuffix
112   extends ValueSuffix
113 {
114   //-------------------------------------
115
// Constants
116
//-------------------------------------
117

118   // Zero-argument array
119
static Object JavaDoc [] sNoArgs = new Object JavaDoc [0];
120
121   //-------------------------------------
122
// Properties
123
//-------------------------------------
124
// property index
125

126   Expression mIndex;
127   public Expression getIndex ()
128   { return mIndex; }
129   public void setIndex (Expression pIndex)
130   { mIndex = pIndex; }
131
132   //-------------------------------------
133
/**
134    *
135    * Constructor
136    **/

137   public ArraySuffix (Expression pIndex)
138   {
139     mIndex = pIndex;
140   }
141
142   //-------------------------------------
143
/**
144    *
145    * Gets the value of the index
146    **/

147   Object JavaDoc evaluateIndex (VariableResolver JavaDoc pResolver,
148             FunctionMapper JavaDoc functions,
149             Logger pLogger)
150     throws ELException JavaDoc
151   {
152     return mIndex.evaluate (pResolver, functions, pLogger);
153   }
154
155   //-------------------------------------
156
/**
157    *
158    * Returns the operator symbol
159    **/

160   String JavaDoc getOperatorSymbol ()
161   {
162     return "[]";
163   }
164
165   //-------------------------------------
166
// ValueSuffix methods
167
//-------------------------------------
168
/**
169    *
170    * Returns the expression in the expression language syntax
171    **/

172   public String JavaDoc getExpressionString ()
173   {
174     return "[" + mIndex.getExpressionString () + "]";
175   }
176
177   //-------------------------------------
178
/**
179    *
180    * Evaluates the expression in the given context, operating on the
181    * given value.
182    **/

183   public Object JavaDoc evaluate (Object JavaDoc pValue,
184               VariableResolver JavaDoc pResolver,
185               FunctionMapper JavaDoc functions,
186               Logger pLogger)
187     throws ELException JavaDoc
188   {
189     Object JavaDoc indexVal;
190     String JavaDoc indexStr;
191     BeanInfoProperty property;
192     BeanInfoIndexedProperty ixproperty;
193
194     // Check for null value
195
if (pValue == null) {
196       if (pLogger.isLoggingWarning ()) {
197     pLogger.logWarning
198       (Constants.CANT_GET_INDEXED_VALUE_OF_NULL,
199        getOperatorSymbol ());
200       }
201       return null;
202     }
203
204     // Evaluate the index
205
else if ((indexVal = evaluateIndex (pResolver, functions, pLogger))
206                                 == null) {
207       if (pLogger.isLoggingWarning ()) {
208     pLogger.logWarning
209       (Constants.CANT_GET_NULL_INDEX,
210        getOperatorSymbol ());
211       }
212       return null;
213     }
214
215     // See if it's a Map
216
else if (pValue instanceof Map JavaDoc) {
217       Map JavaDoc val = (Map JavaDoc) pValue;
218       return val.get (indexVal);
219     }
220
221     // See if it's a List or array
222
else if (pValue instanceof List JavaDoc ||
223          pValue.getClass ().isArray ()) {
224       Integer JavaDoc indexObj = Coercions.coerceToInteger (indexVal, pLogger);
225       if (indexObj == null) {
226     if (pLogger.isLoggingError ()) {
227       pLogger.logError
228         (Constants.BAD_INDEX_VALUE,
229          getOperatorSymbol (),
230          indexVal.getClass ().getName ());
231     }
232     return null;
233       }
234       else if (pValue instanceof List JavaDoc) {
235     try {
236       return ((List JavaDoc) pValue).get (indexObj.intValue ());
237     }
238     catch (ArrayIndexOutOfBoundsException JavaDoc exc) {
239       if (pLogger.isLoggingWarning ()) {
240         pLogger.logWarning
241           (Constants.EXCEPTION_ACCESSING_LIST,
242            exc,
243            indexObj);
244       }
245       return null;
246     }
247     catch (IndexOutOfBoundsException JavaDoc exc) {
248       if (pLogger.isLoggingWarning ()) {
249         pLogger.logWarning
250           (Constants.EXCEPTION_ACCESSING_LIST,
251            exc,
252            indexObj);
253       }
254       return null;
255     }
256     catch (Exception JavaDoc exc) {
257       if (pLogger.isLoggingError ()) {
258         pLogger.logError
259           (Constants.EXCEPTION_ACCESSING_LIST,
260            exc,
261            indexObj);
262       }
263       return null;
264     }
265       }
266       else {
267     try {
268       return Array.get (pValue, indexObj.intValue ());
269     }
270     catch (ArrayIndexOutOfBoundsException JavaDoc exc) {
271       if (pLogger.isLoggingWarning ()) {
272         pLogger.logWarning
273           (Constants.EXCEPTION_ACCESSING_ARRAY,
274            exc,
275            indexObj);
276       }
277       return null;
278     }
279     catch (IndexOutOfBoundsException JavaDoc exc) {
280       if (pLogger.isLoggingWarning ()) {
281         pLogger.logWarning
282           (Constants.EXCEPTION_ACCESSING_ARRAY,
283            exc,
284            indexObj);
285       }
286       return null;
287     }
288     catch (Exception JavaDoc exc) {
289       if (pLogger.isLoggingError ()) {
290         pLogger.logError
291           (Constants.EXCEPTION_ACCESSING_ARRAY,
292            exc,
293            indexObj);
294       }
295       return null;
296     }
297       }
298     }
299
300     // Coerce to a String for property access
301

302     else if ((indexStr = Coercions.coerceToString (indexVal, pLogger)) ==
303          null) {
304       return null;
305     }
306
307     // Look for a JavaBean property
308
else if ((property = BeanInfoManager.getBeanInfoProperty
309           (pValue.getClass (),
310            indexStr,
311            pLogger)) != null &&
312          property.getReadMethod () != null) {
313       try {
314     return property.getReadMethod ().invoke (pValue, sNoArgs);
315       }
316       catch (InvocationTargetException JavaDoc exc) {
317     if (pLogger.isLoggingError ()) {
318       pLogger.logError
319         (Constants.ERROR_GETTING_PROPERTY,
320          exc.getTargetException (),
321          indexStr,
322          pValue.getClass ().getName ());
323     }
324     return null;
325       }
326       catch (Exception JavaDoc exc) {
327     if (pLogger.isLoggingError ()) {
328       pLogger.logError
329         (Constants.ERROR_GETTING_PROPERTY,
330          exc,
331          indexStr,
332          pValue.getClass ().getName ());
333     }
334     return null;
335       }
336     }
337
338     else {
339       if (pLogger.isLoggingError ()) {
340     pLogger.logError
341       (Constants.CANT_FIND_INDEX,
342        indexVal,
343        pValue.getClass ().getName (),
344        getOperatorSymbol ());
345       }
346       return null;
347     }
348   }
349
350   //-------------------------------------
351
}
352
Popular Tags