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. * useful interface to node. most autogened by javacc
  20. *
  21. * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
  22. * @version $Id: SimpleNode.java,v 1.3 2004/02/28 13:45:21 yoavs Exp $
  23. */
  24. public class SimpleNode implements Node
  25. {
  26. protected Node parent;
  27. protected Node[] children;
  28. protected int id;
  29. protected Parser parser;
  30. public SimpleNode(int i)
  31. {
  32. id = i;
  33. }
  34. public SimpleNode(Parser p, int i)
  35. {
  36. this(i);
  37. parser = p;
  38. }
  39. public void jjtOpen()
  40. {
  41. }
  42. public void jjtClose()
  43. {
  44. }
  45. public void jjtSetParent(Node n)
  46. {
  47. parent = n;
  48. }
  49. public Node jjtGetParent()
  50. {
  51. return parent;
  52. }
  53. public void jjtAddChild(Node n, int i)
  54. {
  55. if (children == null)
  56. {
  57. children = new Node[i + 1];
  58. }
  59. else if (i >= children.length)
  60. {
  61. Node c[] = new Node[i + 1];
  62. System.arraycopy(children, 0, c, 0, children.length);
  63. children = c;
  64. }
  65. children[i] = n;
  66. }
  67. public Node jjtGetChild(int i)
  68. {
  69. return children[i];
  70. }
  71. public int jjtGetNumChildren()
  72. {
  73. return (children == null) ? 0 : children.length;
  74. }
  75. /** Accept the visitor. **/
  76. public Object jjtAccept(ParserVisitor visitor, Object data)
  77. {
  78. return visitor.visit(this, data);
  79. }
  80. /** Accept the visitor. **/
  81. public Object childrenAccept(ParserVisitor visitor, Object data)
  82. {
  83. if (children != null)
  84. {
  85. for (int i = 0; i < children.length; ++i)
  86. {
  87. children[i].jjtAccept(visitor, data);
  88. }
  89. }
  90. return data;
  91. }
  92. public String toString()
  93. {
  94. return ParserTreeConstants.jjtNodeName[id];
  95. }
  96. public String toString(String prefix)
  97. {
  98. return prefix + toString();
  99. }
  100. public void dump(String prefix)
  101. {
  102. System.out.println(toString(prefix));
  103. if (children != null)
  104. {
  105. for (int i = 0; i < children.length; ++i)
  106. {
  107. SimpleNode n = (SimpleNode)children[i];
  108. if (n != null)
  109. {
  110. n.dump(prefix + " ");
  111. }
  112. }
  113. }
  114. }
  115. /**
  116. * basic interpret - just invoke interpret on all children
  117. */
  118. public boolean interpret(JexlContext pc)
  119. throws Exception
  120. {
  121. for (int i=0; i<jjtGetNumChildren();i++)
  122. {
  123. SimpleNode node = (SimpleNode) jjtGetChild(i);
  124. if (!node.interpret(pc))
  125. return false;
  126. }
  127. return true;
  128. }
  129. /**
  130. * Returns the value of the node.
  131. */
  132. public Object value(JexlContext context)
  133. throws Exception
  134. {
  135. return null;
  136. }
  137. /**
  138. * Sets the value for the node - again, only makes sense for some nodes
  139. * but lazyness tempts me to put it here. Keeps things simple.
  140. */
  141. public Object setValue(JexlContext context, Object value)
  142. throws Exception
  143. {
  144. return null;
  145. }
  146. /**
  147. * Used to let a node calcuate it's value..
  148. */
  149. public Object execute(Object o, JexlContext ctx)
  150. throws Exception
  151. {
  152. return null;
  153. }
  154. }