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. /**
  19. * reference - any variable expression
  20. *
  21. * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
  22. * @version $Id: ASTReference.java,v 1.4 2004/09/01 00:35:38 dion Exp $
  23. */
  24. public class ASTReference extends SimpleNode
  25. {
  26. SimpleNode root;
  27. public ASTReference(int id)
  28. {
  29. super(id);
  30. }
  31. public ASTReference(Parser p, int id)
  32. {
  33. super(p, id);
  34. }
  35. /** Accept the visitor. **/
  36. public Object jjtAccept(ParserVisitor visitor, Object data)
  37. {
  38. return visitor.visit(this, data);
  39. }
  40. public Object value(JexlContext jc)
  41. throws Exception
  42. {
  43. return execute(null,jc);
  44. }
  45. public void jjtClose()
  46. {
  47. root = (SimpleNode) jjtGetChild(0);
  48. }
  49. public Object execute(Object obj, JexlContext jc)
  50. throws Exception
  51. {
  52. Object o = root.value(jc);
  53. /*
  54. * ignore the first child - it's our identifier
  55. */
  56. for(int i=1; i<jjtGetNumChildren(); i++)
  57. {
  58. o = ( (SimpleNode) jjtGetChild(i)).execute(o,jc);
  59. // check for a variable in the context named
  60. // child0.child1.child2 etc
  61. if(o == null) {
  62. String varName = getIdentifierToDepth(i);
  63. o = jc.getVars().get(varName);
  64. }
  65. }
  66. return o;
  67. }
  68. /**
  69. * This method returns a variable from this identifier and
  70. * it's children. For an expression like 'a.b.c', a is child
  71. * zero, b is child 1 and c is child 2.
  72. *
  73. * @param i the depth of the child nodes to go to
  74. * @return the a dotted variable from this identifier and it's
  75. * child nodes.
  76. */
  77. private String getIdentifierToDepth(int i) {
  78. StringBuffer varName = new StringBuffer();
  79. for (int j = 0; j <=i; j++) {
  80. SimpleNode node = (SimpleNode) jjtGetChild(j);
  81. if (node instanceof ASTIdentifier) {
  82. varName.append(((ASTIdentifier)node).getIdentifierString());
  83. if (j != i) varName.append('.');
  84. }
  85. }
  86. return varName.toString();
  87. }
  88. public String getRootString()
  89. throws Exception
  90. {
  91. if ( root instanceof ASTIdentifier)
  92. return ((ASTIdentifier) root).getIdentifierString();
  93. if (root instanceof ASTArrayAccess)
  94. return ((ASTArrayAccess) root).getIdentifierString();
  95. throw new Exception("programmer error : ASTReference : root not known"
  96. + root );
  97. }
  98. }