KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > expression > lib > ExpressionPrinter


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2003 France Telecom R&D
5  * Contact: alexandre.lefebvre@rd.francetelecom.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Initial developers: M. Alia, A. Lefebvre, S. Chassande-Barrioz
22  */

23 package org.objectweb.medor.expression.lib;
24
25 import org.objectweb.jorm.type.api.PType;
26 import org.objectweb.medor.expression.api.BinaryOperator;
27 import org.objectweb.medor.expression.api.Operator;
28 import org.objectweb.medor.expression.api.ParameterOperand;
29 import org.objectweb.medor.expression.api.UnaryOperator;
30 import org.objectweb.medor.expression.api.Expression;
31 import org.objectweb.medor.expression.api.Operand;
32 import org.objectweb.medor.expression.api.CalculatedParameterOperand;
33
34 /**
35  * @author S.Chassande-Barrioz
36  */

37 public class ExpressionPrinter {
38     public static String JavaDoc e2str(Expression e) {
39
40         if (e instanceof Operand) {
41             PType type = e.getType();
42             if (e instanceof ParameterOperand) {
43                 return
44                     (e instanceof CalculatedParameterOperand
45                         ? "CalculatedParam("
46                         : "Param(")
47                     + ((ParameterOperand) e).getName() + ", "
48                     + (type == null ? "nullType" : type.getJormName()) + ", "
49                     + e.toString() + ")";
50             }
51             else
52                 return e.toString();
53         } else if (e instanceof BinaryOperator) {
54             BinaryOperator bo = (BinaryOperator) e;
55             return "("
56                 + e2str(bo.getExpression(0))
57                 + " " + bo.getOperatorString() + " "
58                 + e2str(bo.getExpression(1))
59                 + ")";
60         } else if (e instanceof UnaryOperator) {
61             UnaryOperator uo = (UnaryOperator) e;
62             return uo.getOperatorString() + "("
63                 + e2str(uo.getExpression(0)) + ")";
64         } else if (e instanceof Operator) {
65             Operator op = (Operator) e;
66             StringBuffer JavaDoc s = new StringBuffer JavaDoc();
67             s.append(op.getOperatorString());
68             s.append("(");
69             String JavaDoc sep = "";
70             for (int i=0; i< op.getOperandNumber(); i++) {
71                 s.append(sep);
72                 sep = ", ";
73                 s.append(e2str(op.getExpression(i)));
74             }
75             s.append(")");
76             return s.toString();
77         }
78         return "";
79     }
80
81 }
82
Popular Tags