KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > lang > jstl > ArraySuffix


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.standard.lang.jstl;
18
19 import java.lang.reflect.Array JavaDoc;
20 import java.lang.reflect.InvocationTargetException JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 /**
25  *
26  * <p>Represents an operator that obtains a Map entry, an indexed
27  * value, a property value, or an indexed property value of an object.
28  * The following are the rules for evaluating this operator:
29  *
30  * <ul><pre>
31  * Evaluating a[b] (assuming a.b == a["b"])
32  * a is null
33  * return null
34  * b is null
35  * return null
36  * a is Map
37  * !a.containsKey (b)
38  * return null
39  * a.get(b) == null
40  * return null
41  * otherwise
42  * return a.get(b)
43  * a is List or array
44  * coerce b to int (using coercion rules)
45  * coercion couldn't be performed
46  * error
47  * a.get(b) or Array.get(a, b) throws ArrayIndexOutOfBoundsException or IndexOutOfBoundsException
48  * return null
49  * a.get(b) or Array.get(a, b) throws other exception
50  * error
51  * return a.get(b) or Array.get(a, b)
52  *
53  * coerce b to String
54  * b is a readable property of a
55  * getter throws an exception
56  * error
57  * otherwise
58  * return result of getter call
59  *
60  * otherwise
61  * error
62  * </pre></ul>
63  *
64  * @author Nathan Abramson - Art Technology Group
65  * @author Shawn Bayern
66  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: pierred $
67  **/

68
69 public class ArraySuffix
70   extends ValueSuffix
71 {
72   //-------------------------------------
73
// Constants
74
//-------------------------------------
75

76   // Zero-argument array
77
static Object JavaDoc [] sNoArgs = new Object JavaDoc [0];
78
79   //-------------------------------------
80
// Properties
81
//-------------------------------------
82
// property index
83

84   Expression mIndex;
85   public Expression getIndex ()
86   { return mIndex; }
87   public void setIndex (Expression pIndex)
88   { mIndex = pIndex; }
89
90   //-------------------------------------
91
/**
92    *
93    * Constructor
94    **/

95   public ArraySuffix (Expression pIndex)
96   {
97     mIndex = pIndex;
98   }
99
100   //-------------------------------------
101
/**
102    *
103    * Gets the value of the index
104    **/

105   Object JavaDoc evaluateIndex (Object JavaDoc pContext,
106             VariableResolver pResolver,
107             Map JavaDoc functions,
108             String JavaDoc defaultPrefix,
109             Logger pLogger)
110     throws ELException
111   {
112     return mIndex.evaluate (pContext, pResolver, functions, defaultPrefix,
113                 pLogger);
114   }
115
116   //-------------------------------------
117
/**
118    *
119    * Returns the operator symbol
120    **/

121   String JavaDoc getOperatorSymbol ()
122   {
123     return "[]";
124   }
125
126   //-------------------------------------
127
// ValueSuffix methods
128
//-------------------------------------
129
/**
130    *
131    * Returns the expression in the expression language syntax
132    **/

133   public String JavaDoc getExpressionString ()
134   {
135     return "[" + mIndex.getExpressionString () + "]";
136   }
137
138   //-------------------------------------
139
/**
140    *
141    * Evaluates the expression in the given context, operating on the
142    * given value.
143    **/

144   public Object JavaDoc evaluate (Object JavaDoc pValue,
145               Object JavaDoc pContext,
146               VariableResolver pResolver,
147               Map JavaDoc functions,
148               String JavaDoc defaultPrefix,
149               Logger pLogger)
150     throws ELException
151   {
152     Object JavaDoc indexVal;
153     String JavaDoc indexStr;
154     BeanInfoProperty property;
155     BeanInfoIndexedProperty ixproperty;
156
157     // Check for null value
158
if (pValue == null) {
159       if (pLogger.isLoggingWarning ()) {
160     pLogger.logWarning
161       (Constants.CANT_GET_INDEXED_VALUE_OF_NULL,
162        getOperatorSymbol ());
163       }
164       return null;
165     }
166
167     // Evaluate the index
168
else if ((indexVal = evaluateIndex (pContext, pResolver,
169                     functions, defaultPrefix, pLogger)) ==
170          null) {
171       if (pLogger.isLoggingWarning ()) {
172     pLogger.logWarning
173       (Constants.CANT_GET_NULL_INDEX,
174        getOperatorSymbol ());
175       }
176       return null;
177     }
178
179     // See if it's a Map
180
else if (pValue instanceof Map JavaDoc) {
181       Map JavaDoc val = (Map JavaDoc) pValue;
182       return val.get (indexVal);
183     }
184
185     // See if it's a List or array
186
else if (pValue instanceof List JavaDoc ||
187          pValue.getClass ().isArray ()) {
188       Integer JavaDoc indexObj = Coercions.coerceToInteger (indexVal, pLogger);
189       if (indexObj == null) {
190     if (pLogger.isLoggingError ()) {
191       pLogger.logError
192         (Constants.BAD_INDEX_VALUE,
193          getOperatorSymbol (),
194          indexVal.getClass ().getName ());
195     }
196     return null;
197       }
198       else if (pValue instanceof List JavaDoc) {
199     try {
200       return ((List JavaDoc) pValue).get (indexObj.intValue ());
201     }
202     catch (ArrayIndexOutOfBoundsException JavaDoc exc) {
203       if (pLogger.isLoggingWarning ()) {
204         pLogger.logWarning
205           (Constants.EXCEPTION_ACCESSING_LIST,
206            exc,
207            indexObj);
208       }
209       return null;
210     }
211     catch (IndexOutOfBoundsException JavaDoc exc) {
212       if (pLogger.isLoggingWarning ()) {
213         pLogger.logWarning
214           (Constants.EXCEPTION_ACCESSING_LIST,
215            exc,
216            indexObj);
217       }
218       return null;
219     }
220     catch (Exception JavaDoc exc) {
221       if (pLogger.isLoggingError ()) {
222         pLogger.logError
223           (Constants.EXCEPTION_ACCESSING_LIST,
224            exc,
225            indexObj);
226       }
227       return null;
228     }
229       }
230       else {
231     try {
232       return Array.get (pValue, indexObj.intValue ());
233     }
234     catch (ArrayIndexOutOfBoundsException JavaDoc exc) {
235       if (pLogger.isLoggingWarning ()) {
236         pLogger.logWarning
237           (Constants.EXCEPTION_ACCESSING_ARRAY,
238            exc,
239            indexObj);
240       }
241       return null;
242     }
243     catch (IndexOutOfBoundsException JavaDoc exc) {
244       if (pLogger.isLoggingWarning ()) {
245         pLogger.logWarning
246           (Constants.EXCEPTION_ACCESSING_ARRAY,
247            exc,
248            indexObj);
249       }
250       return null;
251     }
252     catch (Exception JavaDoc exc) {
253       if (pLogger.isLoggingError ()) {
254         pLogger.logError
255           (Constants.EXCEPTION_ACCESSING_ARRAY,
256            exc,
257            indexObj);
258       }
259       return null;
260     }
261       }
262     }
263
264     // Coerce to a String for property access
265

266     else if ((indexStr = Coercions.coerceToString (indexVal, pLogger)) ==
267          null) {
268       return null;
269     }
270
271     // Look for a JavaBean property
272
else if ((property = BeanInfoManager.getBeanInfoProperty
273           (pValue.getClass (),
274            indexStr,
275            pLogger)) != null &&
276          property.getReadMethod () != null) {
277       try {
278     return property.getReadMethod ().invoke (pValue, sNoArgs);
279       }
280       catch (InvocationTargetException JavaDoc exc) {
281     if (pLogger.isLoggingError ()) {
282       pLogger.logError
283         (Constants.ERROR_GETTING_PROPERTY,
284          exc.getTargetException (),
285          indexStr,
286          pValue.getClass ().getName ());
287     }
288     return null;
289       }
290       catch (Exception JavaDoc exc) {
291     if (pLogger.isLoggingError ()) {
292       pLogger.logError
293         (Constants.ERROR_GETTING_PROPERTY,
294          exc,
295          indexStr,
296          pValue.getClass ().getName ());
297     }
298     return null;
299       }
300     }
301
302     else {
303       if (pLogger.isLoggingError ()) {
304     pLogger.logError
305       (Constants.CANT_FIND_INDEX,
306        indexVal,
307        pValue.getClass ().getName (),
308        getOperatorSymbol ());
309       }
310       return null;
311     }
312   }
313
314   //-------------------------------------
315
}
316
Popular Tags