1. /*
  2. * Copyright 2002,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. package org.apache.commons.jexl.parser;
  17. import org.apache.commons.jexl.JexlContext;
  18. import org.apache.commons.jexl.util.Coercion;
  19. import org.apache.commons.jexl.util.Introspector;
  20. import org.apache.commons.jexl.util.introspection.Info;
  21. import org.apache.commons.jexl.util.introspection.VelPropertyGet;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.lang.reflect.Array;
  25. /**
  26. * Like an ASTIdentifier, but with array access allowed
  27. *
  28. * $foo[2]
  29. *
  30. * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
  31. * @version $Id: ASTArrayAccess.java,v 1.7 2004/08/22 07:42:35 dion Exp $
  32. */
  33. public class ASTArrayAccess extends SimpleNode
  34. {
  35. /** dummy velocity info */
  36. private static Info DUMMY = new Info("", 1, 1);
  37. public ASTArrayAccess(int id)
  38. {
  39. super(id);
  40. }
  41. public ASTArrayAccess(Parser p, int id)
  42. {
  43. super(p, id);
  44. }
  45. /** Accept the visitor. **/
  46. public Object jjtAccept(ParserVisitor visitor, Object data)
  47. {
  48. return visitor.visit(this, data);
  49. }
  50. /*
  51. * evaluate array access upon a base object
  52. *
  53. * foo.bar[2]
  54. *
  55. * makes me rethink the array operator :)
  56. */
  57. public Object execute(Object obj, JexlContext jc)
  58. throws Exception
  59. {
  60. ASTIdentifier base = (ASTIdentifier) jjtGetChild(0);
  61. obj = base.execute(obj,jc);
  62. /*
  63. * ignore the first child - it's our identifier
  64. */
  65. for(int i=1; i<jjtGetNumChildren(); i++)
  66. {
  67. Object loc = ((SimpleNode) jjtGetChild(i)).value(jc);
  68. if(loc==null)
  69. return null;
  70. obj = evaluateExpr(obj, loc);
  71. }
  72. return obj;
  73. }
  74. /**
  75. * return the value of this node
  76. */
  77. public Object value(JexlContext jc)
  78. throws Exception
  79. {
  80. /*
  81. * get the base ASTIdentifier
  82. */
  83. ASTIdentifier base = (ASTIdentifier) jjtGetChild(0);
  84. Object o = base.value(jc);
  85. /*
  86. * ignore the first child - it's our identifier
  87. */
  88. for(int i=1; i<jjtGetNumChildren(); i++)
  89. {
  90. Object loc = ((SimpleNode) jjtGetChild(i)).value(jc);
  91. if(loc==null)
  92. return null;
  93. o = evaluateExpr(o, loc);
  94. }
  95. return o;
  96. }
  97. public static Object evaluateExpr(Object o, Object loc)
  98. throws Exception
  99. {
  100. /*
  101. * following the JSTL EL rules
  102. */
  103. if (o == null)
  104. return null;
  105. if (loc == null)
  106. return null;
  107. if (o instanceof Map)
  108. {
  109. if (!((Map)o).containsKey(loc))
  110. return null;
  111. return ((Map)o).get(loc);
  112. }
  113. else if (o instanceof List)
  114. {
  115. int idx = Coercion.coerceInteger(loc).intValue();
  116. try
  117. {
  118. return ((List)o).get(idx);
  119. }
  120. catch(IndexOutOfBoundsException iobe)
  121. {
  122. return null;
  123. }
  124. }
  125. else if (o.getClass().isArray())
  126. {
  127. int idx = Coercion.coerceInteger(loc).intValue();
  128. try
  129. {
  130. return Array.get(o, idx);
  131. }
  132. catch(ArrayIndexOutOfBoundsException aiobe)
  133. {
  134. return null;
  135. }
  136. }
  137. else
  138. {
  139. /*
  140. * "Otherwise (a JavaBean object)..." huh? :)
  141. */
  142. String s = loc.toString();
  143. VelPropertyGet vg = Introspector.getUberspect().getPropertyGet(o, s, DUMMY);
  144. if (vg != null)
  145. {
  146. return vg.invoke(o);
  147. }
  148. }
  149. throw new Exception("Unsupported object type for array [] accessor");
  150. }
  151. public String getIdentifierString()
  152. {
  153. return ((ASTIdentifier) jjtGetChild(0)).getIdentifierString();
  154. }
  155. }