1 55 56 package org.apache.commons.el; 57 58 import java.lang.reflect.Array ; 59 import java.lang.reflect.InvocationTargetException ; 60 import java.util.List ; 61 import java.util.Map ; 62 import javax.servlet.jsp.el.VariableResolver ; 63 import javax.servlet.jsp.el.ELException ; 64 import javax.servlet.jsp.el.FunctionMapper ; 65 66 110 111 public class ArraySuffix 112 extends ValueSuffix 113 { 114 118 static Object [] sNoArgs = new Object [0]; 120 121 126 Expression mIndex; 127 public Expression getIndex () 128 { return mIndex; } 129 public void setIndex (Expression pIndex) 130 { mIndex = pIndex; } 131 132 137 public ArraySuffix (Expression pIndex) 138 { 139 mIndex = pIndex; 140 } 141 142 147 Object evaluateIndex (VariableResolver pResolver, 148 FunctionMapper functions, 149 Logger pLogger) 150 throws ELException 151 { 152 return mIndex.evaluate (pResolver, functions, pLogger); 153 } 154 155 160 String getOperatorSymbol () 161 { 162 return "[]"; 163 } 164 165 172 public String getExpressionString () 173 { 174 return "[" + mIndex.getExpressionString () + "]"; 175 } 176 177 183 public Object evaluate (Object pValue, 184 VariableResolver pResolver, 185 FunctionMapper functions, 186 Logger pLogger) 187 throws ELException 188 { 189 Object indexVal; 190 String indexStr; 191 BeanInfoProperty property; 192 BeanInfoIndexedProperty ixproperty; 193 194 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 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 else if (pValue instanceof Map ) { 217 Map val = (Map ) pValue; 218 return val.get (indexVal); 219 } 220 221 else if (pValue instanceof List || 223 pValue.getClass ().isArray ()) { 224 Integer 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 ) { 235 try { 236 return ((List ) pValue).get (indexObj.intValue ()); 237 } 238 catch (ArrayIndexOutOfBoundsException 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 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 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 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 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 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 302 else if ((indexStr = Coercions.coerceToString (indexVal, pLogger)) == 303 null) { 304 return null; 305 } 306 307 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 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 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 } 352 | Popular Tags |