001/*
002 * Copyright (c) 2005 Einar Pehrson <einar@pehrson.nu>.
003 *
004 * This file is part of
005 * CleanSheets - a spreadsheet application for the Java platform.
006 *
007 * CleanSheets is free software; you can redistribute it and/or modify
008 * it under the terms of the GNU General Public License as published by
009 * the Free Software Foundation; either version 2 of the License, or
010 * (at your option) any later version.
011 *
012 * CleanSheets is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU General Public License
018 * along with CleanSheets; if not, write to the Free Software
019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
020 */
021package csheets.core.formula.util;
022
023import csheets.core.formula.BinaryOperation;
024import csheets.core.formula.FunctionCall;
025import csheets.core.formula.Literal;
026import csheets.core.formula.Reference;
027import csheets.core.formula.UnaryOperation;
028
029/**
030 * A class for printing expressions on multiple lines with indentation.
031 * @author Einar Pehrson
032 */
033public class ExpressionTreePrinter extends AbstractExpressionVisitor {
034
035        /** The number of spaces to use for each indentation unit */
036        public static final int INDENT_DISTANCE = 3;
037
038        /** The current indentation count */
039        private int indentCount = 0;
040
041        /**
042         * Creates a new expression printer.
043         */
044        public ExpressionTreePrinter() {}
045
046        public Object visitLiteral(Literal literal) {
047                print(literal);
048                return literal;
049        }
050
051        public Object visitUnaryOperation(UnaryOperation operation) {
052                print(operation.getOperator());
053                indentCount++;
054                super.visitUnaryOperation(operation);
055                indentCount--;
056                return operation;
057        }
058
059        public Object visitBinaryOperation(BinaryOperation operation) {
060                print(operation.getOperator());
061                indentCount++;
062                super.visitBinaryOperation(operation);
063                indentCount--;
064                return operation;
065        }
066
067        public Object visitReference(Reference reference) {
068                print(reference);
069                return reference;
070        }
071
072        public Object visitFunctionCall(FunctionCall call) {
073                print(call.getFunction());
074                indentCount++;
075                super.visitFunctionCall(call);
076                indentCount--;
077                return call;
078        }
079
080        private void print(Object o) {
081                String indentation = "";
082                for (int i = 0; i < indentCount * INDENT_DISTANCE; i++)
083                        indentation += " ";
084                System.out.println(indentation + o);
085        }
086}