/*
 * 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);
    }

    private Object logChildren(final SimpleNode node, Object data) {
        indent++;
        data = node.childrenAccept(this, data);
        indent--;

        return data;
    }

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

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

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

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

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

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

@@ -1150,67 +1150,8 @@
 ing;
-%0Aimport org.apache.geronimo.gshell.commandline.parser.Node;
 %0A%0Aim
@@ -1714,12 +1714,14 @@
 ate 
-void
+Object
  log
@@ -1745,16 +1745,22 @@
 , final 
+Simple
 Node nod
@@ -1761,22 +1761,16 @@
 de node,
- final
  Object 
@@ -1832,16 +1832,21 @@
   return
+ data
 ;%0A      
@@ -2175,21 +2175,9 @@
 DO: 
-May wany to e
+E
 xpos
@@ -2195,16 +2195,17 @@
 O switch
+?
 %0A       
@@ -2238,83 +2238,8 @@
 f);%0A
-    %7D%0A%0A    private Object logChildren(final SimpleNode node, Object data) %7B
 %0A   
@@ -2414,24 +2414,31 @@
 ) %7B%0A        
+return 
 log(SimpleNo
@@ -2464,48 +2464,8 @@
 a);%0A
-        return logChildren(node, data);%0A
     
@@ -2537,24 +2537,31 @@
 ) %7B%0A        
+return 
 log(ASTComma
@@ -2591,48 +2591,8 @@
 a);%0A
-        return logChildren(node, data);%0A
     
@@ -2663,24 +2663,31 @@
 ) %7B%0A        
+return 
 log(ASTExpre
@@ -2716,48 +2716,8 @@
 a);%0A
-        return logChildren(node, data);%0A
     
@@ -2790,24 +2790,31 @@
 ) %7B%0A        
+return 
 log(ASTQuote
@@ -2845,48 +2845,8 @@
 a);%0A
-        return logChildren(node, data);%0A
     
@@ -2919,24 +2919,31 @@
 ) %7B%0A        
+return 
 log(ASTOpaqu
@@ -2974,48 +2974,8 @@
 a);%0A
-        return logChildren(node, data);%0A
     
@@ -3051,16 +3051,23 @@
         
+return 
 log(ASTP
@@ -3101,48 +3101,8 @@
 a);%0A
-        return logChildren(node, data);%0A
     

Simplify node logging impl (again)