View Javadoc

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