1 16 17 package org.apache.taglibs.standard.lang.jstl; 18 19 import java.lang.reflect.Array ; 20 import java.lang.reflect.InvocationTargetException ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 68 69 public class ArraySuffix 70 extends ValueSuffix 71 { 72 76 static Object [] sNoArgs = new Object [0]; 78 79 84 Expression mIndex; 85 public Expression getIndex () 86 { return mIndex; } 87 public void setIndex (Expression pIndex) 88 { mIndex = pIndex; } 89 90 95 public ArraySuffix (Expression pIndex) 96 { 97 mIndex = pIndex; 98 } 99 100 105 Object evaluateIndex (Object pContext, 106 VariableResolver pResolver, 107 Map functions, 108 String defaultPrefix, 109 Logger pLogger) 110 throws ELException 111 { 112 return mIndex.evaluate (pContext, pResolver, functions, defaultPrefix, 113 pLogger); 114 } 115 116 121 String getOperatorSymbol () 122 { 123 return "[]"; 124 } 125 126 133 public String getExpressionString () 134 { 135 return "[" + mIndex.getExpressionString () + "]"; 136 } 137 138 144 public Object evaluate (Object pValue, 145 Object pContext, 146 VariableResolver pResolver, 147 Map functions, 148 String defaultPrefix, 149 Logger pLogger) 150 throws ELException 151 { 152 Object indexVal; 153 String indexStr; 154 BeanInfoProperty property; 155 BeanInfoIndexedProperty ixproperty; 156 157 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 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 else if (pValue instanceof Map ) { 181 Map val = (Map ) pValue; 182 return val.get (indexVal); 183 } 184 185 else if (pValue instanceof List || 187 pValue.getClass ().isArray ()) { 188 Integer 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 ) { 199 try { 200 return ((List ) pValue).get (indexObj.intValue ()); 201 } 202 catch (ArrayIndexOutOfBoundsException 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 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 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 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 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 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 266 else if ((indexStr = Coercions.coerceToString (indexVal, pLogger)) == 267 null) { 268 return null; 269 } 270 271 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 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 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 } 316 | Popular Tags |