/*
 * Copyright 2002,2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.commons.jexl.parser;

import org.apache.commons.jexl.JexlContext;

/**
 *  A Useful implementation of {@link Node}.
 *  Mostly autogenerated by javacc
 *
 *  @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
 *  @version $Id$
 */
public class SimpleNode implements Node
{
    protected Node parent;
    protected Node[] children;
    protected int id;
    protected Parser parser;

    public SimpleNode(int i)
    {
        id = i;
    }

    public SimpleNode(Parser p, int i)
    {
        this(i);
        parser = p;
    }

    public void jjtOpen()
    {
    }

    public void jjtClose()
    {
    }

    public void jjtSetParent(Node n)
    {
        parent = n;
    }

    public Node jjtGetParent()
    {
        return parent;
    }

    public void jjtAddChild(Node n, int i)
    {
        if (children == null)
        {
            children = new Node[i + 1];
        }
        else if (i >= children.length)
        {
            Node c[] = new Node[i + 1];
            System.arraycopy(children, 0, c, 0, children.length);
            children = c;
        }

        children[i] = n;
    }

    public Node jjtGetChild(int i)
    {
        return  children[i];
    }

    public int jjtGetNumChildren()
    {
        return (children == null) ? 0 : children.length;
    }

    /** Accept the visitor. **/
    public Object jjtAccept(ParserVisitor visitor, Object data)
    {
        return visitor.visit(this, data);
    }

    /** Accept the visitor. **/
    public Object childrenAccept(ParserVisitor visitor, Object data)
    {
        if (children != null)
        {
            for (int i = 0; i < children.length; ++i)
            {
                children[i].jjtAccept(visitor, data);
            }
        }
        return data;
    }


    public String toString()
    {
        return ParserTreeConstants.jjtNodeName[id];
    }

    public String toString(String prefix)
    {
        return prefix + toString();
    }

    public void dump(String prefix)
    {
        System.out.println(toString(prefix));

        if (children != null)
        {
            for (int i = 0; i < children.length; ++i)
            {
                SimpleNode n = (SimpleNode)children[i];

                if (n != null)
                {
                    n.dump(prefix + " ");
                }
            }
        }
    }

    /**
     *  basic interpret - just invoke interpret on all children
     */
    public boolean interpret(JexlContext pc)
        throws Exception
    {
        for (int i=0; i<jjtGetNumChildren();i++)
        {
            SimpleNode node = (SimpleNode) jjtGetChild(i);
            if (!node.interpret(pc))
                return false;
        }

        return true;
    }


    /**
     *  Returns the value of the node.
     */
    public Object value(JexlContext context)
            throws Exception
    {
        return null;
    }

    /**
     *  Sets the value for the node - again, only makes sense for some nodes
     *  but lazyness tempts me to put it here.  Keeps things simple.
     */
    public Object setValue(JexlContext context, Object value)
        throws Exception
    {
        return null;
    }

    /**
     *  Used to let a node calcuate it's value..
     */
    public Object execute(Object o, JexlContext ctx)
            throws Exception
    {
        return null;
    }
}


@@ -17,13 +17,13 @@
 2002
-,2004
+-2006
  The
@@ -53,17 +53,16 @@
 tion.%0A *
- 
 %0A * Lice
@@ -234,17 +234,16 @@
 se at%0A *
- 
 %0A *     
@@ -288,17 +288,16 @@
 E-2.0%0A *
- 
 %0A * Unle
@@ -922,11 +922,35 @@
 Node
-%0A
+ 
 %7B%0A
+    /** parent node. */%0A
     
@@ -980,86 +980,265 @@
     
-protected Node%5B%5D children;%0A    protected int id;%0A    protected Parser parser;%0A
+/** children of this node. */%0A    protected Node%5B%5D children;%0A    /** id of the node. */%0A    protected int id;%0A    /** parser that created the node. */%0A    protected Parser parser;%0A%0A    /**%0A     * Create the node given an id.%0A     * @param i node id.%0A     */
 %0A   
@@ -1258,28 +1258,24 @@
 eNode(int i)
-%0A   
  %7B%0A        i
@@ -1280,32 +1280,150 @@
  id = i;%0A    %7D%0A%0A
+    /**%0A     * Create a node with the given parser and id.%0A     * @param p a parser.%0A     * @param i node id.%0A     */%0A
     public Simpl
@@ -1436,36 +1436,32 @@
 Parser p, int i)
-%0A   
  %7B%0A        this(
@@ -2252,32 +2252,40 @@
 %0A    %7D%0A%0A    /** 
+%0A     * 
 Accept the visit
@@ -2279,34 +2279,217 @@
 ept the visitor.
+%0A     * @param visitor a %7B@link ParserVisitor%7D.%0A     * @param data data to be passed along to the visitor.%0A     * @return the value from visiting.%0A     * @see ParserVisitor#visit %0A    
  
-*
 */%0A    public Ob
@@ -3895,38 +3895,194 @@
   * 
- Returns the value of the node
+Gets the value of this node.%0A     * @param context the context to retrieve values from.%0A     * @return the result of addition.%0A     * @throws Exception when evaluating the operands fails
 .%0A  
@@ -4131,28 +4131,16 @@
 context)
-%0A           
  throws 
@@ -4140,36 +4140,32 @@
 throws Exception
-%0A   
  %7B%0A        retur

Copy over basic node docs from AddNode