MySVC
an open source UNIX framework for shell script based web services provider
»Home
»Tools
»Man Page
»User Guide
»ChangeLog
»Installation
»Source Code
»Downloads
»FAQ
»Support
»License

»My Apps

Printer.java

// $Id$

//  myexpr - My Service Expression Engine - Version 1.0 (www.mysvc.it)
//  Copyright (C) 2009 Davide Cucciniello <davide6169@gmail.com>
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.

/* **********************************************************************
 *
 *  ***   **        *** **  **  ****
 *   **  **        *  *  *  ** **  *
 *   *** **  **  * ***   *  *  *
 *   * ** *   * **   **   * *  *
 *   * *  *   * *  *  *   **   **
 *  ***  ***   **  ***     *    ****
 *             *
 *            *
 *
 *  My Service
 *
 * ***********************************************************************
 *
 *  ***   **       *****
 *   **  **         *  *
 *   *** **  **  *  ***  ***** ****  ****
 *   * ** *   * **  *     ***   *  *  * *
 *   * *  *   * *   *  *  ***   *  *  *
 *  ***  ***   **  ***** ** **  ***  ***
 *             *                *
 *            *                ***
 *
 *  My Service Expression Engine
 *
 *  File: Printer.java
 *
 *  Description:
 *    class Printer: use a Writer to get string representation
 *    of an expression corresponding to a Syntax Tree
 *    MyExpr: Java library implementing an advanced generic
 *    expression parser based on precedence operator
 *
 * ***********************************************************************
 *
 *  History:
 *    1.0               first version
 *
 * *********************************************************************** */

package it.mysvc.myexpr;

public class Printer
{
  private Parser parser;
  private Writer writer;

  public Printer(Parser parser,Writer writer)
  {
    this.parser = parser;
    this.writer = writer;
  }

  public String toString(String expr) throws Exception
  {
    return toString(expr,false);
  }

  public String toString(String expr,boolean fullParenthesis) throws Exception
  {
    return toString(parser.parse(expr),fullParenthesis);
  }

  public String toString(Expr expr)
  {
    return toString(expr,false);
  }

  public String toString(Expr expr,boolean fullParenthesis)
  {
    if(expr.isOperand())
      return writer.getOperand(expr.getCode(),expr.getInfo());
    else if(expr.isUnaryOperator())
    {
      if(expr.isPrefix())
      {
        if(fullParenthesis||expr.getLeft().isBinaryOperator())
          return writer.getUnaryOperator(expr.getCode(),expr.getInfo()) +
                 writer.getOpenedParenthesis() +
                 toString(expr.getLeft(),fullParenthesis) +
                 writer.getClosedParenthesis();
        else
          return writer.getUnaryOperator(expr.getCode(),expr.getInfo()) +
                 toString(expr.getLeft(),fullParenthesis);
      }
      else
      {
        if(fullParenthesis||expr.getLeft().isBinaryOperator())
          return writer.getOpenedParenthesis() +
                 toString(expr.getLeft(),fullParenthesis) +
                 writer.getClosedParenthesis() +
                 writer.getUnaryOperator(expr.getCode(),expr.getInfo());
        else
          return toString(expr.getLeft(),fullParenthesis) +
                 writer.getUnaryOperator(expr.getCode(),expr.getInfo());
      }
    }
    else
    {
      boolean p = expr.getLeft().isBinaryOperator()&&parser.getPrecedence().greaterThan(expr.getCode(),expr.getLeft().getCode());
      boolean q = expr.getRight().isBinaryOperator()&&parser.getPrecedence().greaterThan(expr.getCode(),expr.getRight().getCode());

      return ((fullParenthesis||p)?writer.getOpenedParenthesis():"") +
             toString(expr.getLeft(),fullParenthesis) +
             ((fullParenthesis||p)?writer.getClosedParenthesis():"") +
             writer.getBinaryOperator(expr.getCode(),expr.getInfo()) +
             ((fullParenthesis||q)?writer.getOpenedParenthesis():"") +
             toString(expr.getRight(),fullParenthesis) +
             ((fullParenthesis||q)?writer.getClosedParenthesis():"");
    }
  }
}

/* end of file */