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. package org.apache.commons.el;
  56. import java.lang.reflect.Array;
  57. import java.lang.reflect.InvocationTargetException;
  58. import java.util.List;
  59. import java.util.Map;
  60. import javax.servlet.jsp.el.VariableResolver;
  61. import javax.servlet.jsp.el.ELException;
  62. import javax.servlet.jsp.el.FunctionMapper;
  63. /**
  64. *
  65. * <p>Represents an operator that obtains a Map entry, an indexed
  66. * value, a property value, or an indexed property value of an object.
  67. * The following are the rules for evaluating this operator:
  68. *
  69. * <ul><pre>
  70. * Evaluating a[b] (assuming a.b == a["b"])
  71. * a is null
  72. * return null
  73. * b is null
  74. * return null
  75. * a is Map
  76. * !a.containsKey (b)
  77. * return null
  78. * a.get(b) == null
  79. * return null
  80. * otherwise
  81. * return a.get(b)
  82. * a is List or array
  83. * coerce b to int (using coercion rules)
  84. * coercion couldn't be performed
  85. * error
  86. * a.get(b) or Array.get(a, b) throws ArrayIndexOutOfBoundsException or IndexOutOfBoundsException
  87. * return null
  88. * a.get(b) or Array.get(a, b) throws other exception
  89. * error
  90. * return a.get(b) or Array.get(a, b)
  91. *
  92. * coerce b to String
  93. * b is a readable property of a
  94. * getter throws an exception
  95. * error
  96. * otherwise
  97. * return result of getter call
  98. *
  99. * otherwise
  100. * error
  101. * </pre></ul>
  102. *
  103. * @author Nathan Abramson - Art Technology Group
  104. * @author Shawn Bayern
  105. * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: luehe $
  106. **/
  107. public class ArraySuffix
  108. extends ValueSuffix
  109. {
  110. //-------------------------------------
  111. // Constants
  112. //-------------------------------------
  113. // Zero-argument array
  114. static Object [] sNoArgs = new Object [0];
  115. //-------------------------------------
  116. // Properties
  117. //-------------------------------------
  118. // property index
  119. Expression mIndex;
  120. public Expression getIndex ()
  121. { return mIndex; }
  122. public void setIndex (Expression pIndex)
  123. { mIndex = pIndex; }
  124. //-------------------------------------
  125. /**
  126. *
  127. * Constructor
  128. **/
  129. public ArraySuffix (Expression pIndex)
  130. {
  131. mIndex = pIndex;
  132. }
  133. //-------------------------------------
  134. /**
  135. *
  136. * Gets the value of the index
  137. **/
  138. Object evaluateIndex (VariableResolver pResolver,
  139. FunctionMapper functions,
  140. Logger pLogger)
  141. throws ELException
  142. {
  143. return mIndex.evaluate (pResolver, functions, pLogger);
  144. }
  145. //-------------------------------------
  146. /**
  147. *
  148. * Returns the operator symbol
  149. **/
  150. String getOperatorSymbol ()
  151. {
  152. return "[]";
  153. }
  154. //-------------------------------------
  155. // ValueSuffix methods
  156. //-------------------------------------
  157. /**
  158. *
  159. * Returns the expression in the expression language syntax
  160. **/
  161. public String getExpressionString ()
  162. {
  163. return "[" + mIndex.getExpressionString () + "]";
  164. }
  165. //-------------------------------------
  166. /**
  167. *
  168. * Evaluates the expression in the given context, operating on the
  169. * given value.
  170. **/
  171. public Object evaluate (Object pValue,
  172. VariableResolver pResolver,
  173. FunctionMapper functions,
  174. Logger pLogger)
  175. throws ELException
  176. {
  177. Object indexVal;
  178. String indexStr;
  179. BeanInfoProperty property;
  180. BeanInfoIndexedProperty ixproperty;
  181. // Check for null value
  182. if (pValue == null) {
  183. if (pLogger.isLoggingWarning ()) {
  184. pLogger.logWarning
  185. (Constants.CANT_GET_INDEXED_VALUE_OF_NULL,
  186. getOperatorSymbol ());
  187. }
  188. return null;
  189. }
  190. // Evaluate the index
  191. else if ((indexVal = evaluateIndex (pResolver, functions, pLogger))
  192. == null) {
  193. if (pLogger.isLoggingWarning ()) {
  194. pLogger.logWarning
  195. (Constants.CANT_GET_NULL_INDEX,
  196. getOperatorSymbol ());
  197. }
  198. return null;
  199. }
  200. // See if it's a Map
  201. else if (pValue instanceof Map) {
  202. Map val = (Map) pValue;
  203. return val.get (indexVal);
  204. }
  205. // See if it's a List or array
  206. else if (pValue instanceof List ||
  207. pValue.getClass ().isArray ()) {
  208. Integer indexObj = Coercions.coerceToInteger (indexVal, pLogger);
  209. if (indexObj == null) {
  210. if (pLogger.isLoggingError ()) {
  211. pLogger.logError
  212. (Constants.BAD_INDEX_VALUE,
  213. getOperatorSymbol (),
  214. indexVal.getClass ().getName ());
  215. }
  216. return null;
  217. }
  218. else if (pValue instanceof List) {
  219. try {
  220. return ((List) pValue).get (indexObj.intValue ());
  221. }
  222. catch (ArrayIndexOutOfBoundsException exc) {
  223. if (pLogger.isLoggingWarning ()) {
  224. pLogger.logWarning
  225. (Constants.EXCEPTION_ACCESSING_LIST,
  226. exc,
  227. indexObj);
  228. }
  229. return null;
  230. }
  231. catch (IndexOutOfBoundsException exc) {
  232. if (pLogger.isLoggingWarning ()) {
  233. pLogger.logWarning
  234. (Constants.EXCEPTION_ACCESSING_LIST,
  235. exc,
  236. indexObj);
  237. }
  238. return null;
  239. }
  240. catch (Exception exc) {
  241. if (pLogger.isLoggingError ()) {
  242. pLogger.logError
  243. (Constants.EXCEPTION_ACCESSING_LIST,
  244. exc,
  245. indexObj);
  246. }
  247. return null;
  248. }
  249. }
  250. else {
  251. try {
  252. return Array.get (pValue, indexObj.intValue ());
  253. }
  254. catch (ArrayIndexOutOfBoundsException exc) {
  255. if (pLogger.isLoggingWarning ()) {
  256. pLogger.logWarning
  257. (Constants.EXCEPTION_ACCESSING_ARRAY,
  258. exc,
  259. indexObj);
  260. }
  261. return null;
  262. }
  263. catch (IndexOutOfBoundsException exc) {
  264. if (pLogger.isLoggingWarning ()) {
  265. pLogger.logWarning
  266. (Constants.EXCEPTION_ACCESSING_ARRAY,
  267. exc,
  268. indexObj);
  269. }
  270. return null;
  271. }
  272. catch (Exception exc) {
  273. if (pLogger.isLoggingError ()) {
  274. pLogger.logError
  275. (Constants.EXCEPTION_ACCESSING_ARRAY,
  276. exc,
  277. indexObj);
  278. }
  279. return null;
  280. }
  281. }
  282. }
  283. // Coerce to a String for property access
  284. else if ((indexStr = Coercions.coerceToString (indexVal, pLogger)) ==
  285. null) {
  286. return null;
  287. }
  288. // Look for a JavaBean property
  289. else if ((property = BeanInfoManager.getBeanInfoProperty
  290. (pValue.getClass (),
  291. indexStr,
  292. pLogger)) != null &&
  293. property.getReadMethod () != null) {
  294. try {
  295. return property.getReadMethod ().invoke (pValue, sNoArgs);
  296. }
  297. catch (InvocationTargetException exc) {
  298. if (pLogger.isLoggingError ()) {
  299. pLogger.logError
  300. (Constants.ERROR_GETTING_PROPERTY,
  301. exc.getTargetException (),
  302. indexStr,
  303. pValue.getClass ().getName ());
  304. }
  305. return null;
  306. }
  307. catch (Exception exc) {
  308. if (pLogger.isLoggingError ()) {
  309. pLogger.logError
  310. (Constants.ERROR_GETTING_PROPERTY,
  311. exc,
  312. indexStr,
  313. pValue.getClass ().getName ());
  314. }
  315. return null;
  316. }
  317. }
  318. else {
  319. if (pLogger.isLoggingError ()) {
  320. pLogger.logError
  321. (Constants.CANT_FIND_INDEX,
  322. indexVal,
  323. pValue.getClass ().getName (),
  324. getOperatorSymbol ());
  325. }
  326. return null;
  327. }
  328. }
  329. //-------------------------------------
  330. }