/*
 * Copyright 2006 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.geronimo.gshell.commandline;

import org.apache.geronimo.gshell.commandline.parser.CommandLineParserVisitor;
import org.apache.geronimo.gshell.commandline.parser.SimpleNode;
import org.apache.geronimo.gshell.commandline.parser.ASTCommandLine;
import org.apache.geronimo.gshell.commandline.parser.ASTExpression;
import org.apache.geronimo.gshell.commandline.parser.ASTQuotedString;
import org.apache.geronimo.gshell.commandline.parser.ASTOpaqueString;
import org.apache.geronimo.gshell.commandline.parser.ASTPlainString;
import org.apache.geronimo.gshell.commandline.parser.Node;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Visitor whichs logs nodes in the tree.
 *
 * @version $Id$
 */
public class LoggingVisitor
    implements CommandLineParserVisitor
{
    private final Log log;

    private int indent = 0;

    public LoggingVisitor() {
        this(LogFactory.getLog(LoggingVisitor.class));
    }

    public LoggingVisitor(final Log log) {
        if (log == null) {
            throw new IllegalArgumentException("Log is null");
        }

        this.log = log;
    }

    private void log(final Class type, final Node node, final Object data) {
        if (!log.isDebugEnabled()) {
            return;
        }

        StringBuffer buff = new StringBuffer();

        for (int i=0; i<indent; i++) {
            buff.append(" ");
        }

        buff.append(node).append(" (").append(type.getName()).append(")");
        if (data != null) {
            buff.append("; Data: ").append(data);
        }

        //
        // TODO: May wany to expose DEBUG/INFO switch
        //

        log.debug(buff);
    }

    public Object visit(final SimpleNode node, Object data) {
        log(SimpleNode.class, node, data);

        indent++;
        data = node.childrenAccept(this, data);
        indent--;

        return data;
    }

    public Object visit(final ASTCommandLine node, Object data) {
        log(ASTCommandLine.class, node, data);

        indent++;
        data = node.childrenAccept(this, data);
        indent--;

        return data;
    }

    public Object visit(final ASTExpression node, Object data) {
        log(ASTExpression.class, node, data);

        indent++;
        data = node.childrenAccept(this, data);
        indent--;

        return data;
    }

    public Object visit(final ASTQuotedString node, Object data) {
        log(ASTQuotedString.class, node, data);

        indent++;
        data = node.childrenAccept(this, data);
        indent--;

        return data;
    }

    public Object visit(final ASTOpaqueString node, Object data) {
        log(ASTOpaqueString.class, node, data);

        indent++;
        data = node.childrenAccept(this, data);
        indent--;

        return data;
    }

    public Object visit(final ASTPlainString node, Object data) {
        log(ASTPlainString.class, node, data);

        indent++;
        data = node.childrenAccept(this, data);
        indent--;

        return data;
    }
}

@@ -2305,37 +2305,38 @@
     %7D%0A%0A    p
-ublic
+rivate
  Object 
 visit(final 
@@ -2319,29 +2319,35 @@
 vate Object 
-visit
+logChildren
 (final Simpl
@@ -2377,52 +2377,8 @@
 ) %7B%0A
-        log(SimpleNode.class, node, data);%0A%0A
     
@@ -2478,32 +2478,184 @@
 rn data;%0A    %7D%0A%0A
+    public Object visit(final SimpleNode node, Object data) %7B%0A        log(SimpleNode.class, node, data);%0A        return logChildren(node, data);%0A    %7D%0A%0A
     public Objec
@@ -2747,33 +2747,32 @@
 ode, data);%0A
-%0A
         
 indent++;%0A  
@@ -2763,104 +2763,38 @@
     
-indent++;%0A        data = node.childrenAccept(this, data);%0A        indent--;%0A%0A        return
+return logChildren(node,
  data
+)
 ;%0A  
@@ -2905,33 +2905,32 @@
 ode, data);%0A
-%0A
         
 indent++;%0A  
@@ -2921,104 +2921,38 @@
     
-indent++;%0A        data = node.childrenAccept(this, data);%0A        indent--;%0A%0A        return
+return logChildren(node,
  data
+)
 ;%0A  
@@ -3067,33 +3067,32 @@
 ode, data);%0A
-%0A
         
 indent++;%0A  
@@ -3083,104 +3083,38 @@
     
-indent++;%0A        data = node.childrenAccept(this, data);%0A        indent--;%0A%0A        return
+return logChildren(node,
  data
+)
 ;%0A  
@@ -3229,33 +3229,32 @@
 ode, data);%0A
-%0A
         
 indent++;%0A  
@@ -3245,104 +3245,38 @@
     
-indent++;%0A        data = node.childrenAccept(this, data);%0A        indent--;%0A%0A        return
+return logChildren(node,
  data
+)
 ;%0A  
@@ -3393,25 +3393,24 @@
  data);%0A
-%0A
         
 indent++
@@ -3405,104 +3405,38 @@
     
-indent++;%0A        data = node.childrenAccept(this, data);%0A        indent--;%0A%0A        return
+return logChildren(node,
  data
+)
 ;%0A  

Simplify node logging impl