KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > olap > model > ExpressionPrinter


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.jpivot.olap.model;
14
15 import java.util.HashSet JavaDoc;
16 import java.util.Set JavaDoc;
17
18 /**
19  * prints an expression for testing purposes
20  *
21  * @author av
22  */

23 public class ExpressionPrinter extends VisitorSupportStrict {
24   StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
25
26   static final Set JavaDoc infix = new HashSet JavaDoc();
27   static final Set JavaDoc prefix = new HashSet JavaDoc();
28   static {
29     String JavaDoc[] s = new String JavaDoc[] { "*", "/", "+", "-", "%", "<", ">", "<=", ">=", "<>", "=", "and", "or", "xor" };
30     for (int i = 0; i < s.length; i++)
31       infix.add(s[i]);
32     prefix.add("not");
33     prefix.add("-");
34   };
35
36   public String JavaDoc print(Visitable e) {
37     if (e == null)
38       return "(null)";
39     sb = new StringBuffer JavaDoc();
40     e.accept(this);
41     return sb.toString();
42   }
43
44   public String JavaDoc toString() {
45     return sb.toString();
46   }
47
48   void append(String JavaDoc s) {
49     sb.append(s);
50   }
51
52   public void visitBooleanExpr(BooleanExpr v) {
53     sb.append(v.getValue());
54   }
55   public void visitDimension(Dimension v) {
56     sb.append("[" + v.getLabel() + "]");
57   }
58   public void visitDoubleExpr(DoubleExpr v) {
59     sb.append(v.getValue());
60   }
61   public void visitFunCallExpr(FunCallExpr v) {
62     ExpressionPrinter ep = new ExpressionPrinter();
63     Expression[] args = v.getArgs();
64
65     // binary infix operator (not including unary minus)
66
String JavaDoc name = v.getName();
67     if (infix.contains(name) && args.length == 2) {
68       String JavaDoc left = ep.print(args[0]);
69       String JavaDoc right = ep.print(args[1]);
70       sb.append(left).append(name).append(right);
71       return;
72     }
73
74     for (int i = 0; i < args.length; i++) {
75       if (i > 0)
76         ep.append(", ");
77       args[i].accept(ep);
78     }
79     if ("()".equals(name))
80       sb.append("(").append(ep).append(")");
81     else if ("{}".equals(name))
82       sb.append("{").append(ep).append("}");
83     else if (prefix.contains(name))
84       sb.append(name).append(ep); // unary minus, not
85
else if (name.startsWith(".")) {
86       if (args.length > 1) {
87         // syntax is "object.method(arg, arg)"
88
ep = new ExpressionPrinter();
89         args[0].accept(ep);
90         sb.append(ep).append(name).append("(");
91         ep = new ExpressionPrinter();
92         for (int i = 1; i < args.length; i++) {
93           if (i > 1)
94             ep.append(", ");
95           args[i].accept(ep);
96         }
97         sb.append(ep).append(")");
98       }
99       else
100         sb.append(ep).append(name);
101     }
102     else
103       sb.append(name).append("(").append(ep).append(")");
104   }
105   public void visitHierarchy(Hierarchy v) {
106     sb.append("[" + v.getLabel() + "]");
107   }
108   public void visitIntegerExpr(IntegerExpr v) {
109     sb.append(v.getValue());
110   }
111   public void visitLevel(Level v) {
112     sb.append("[" + v.getLabel() + "]");
113   }
114   public void visitMember(Member v) {
115     sb.append("[" + v.getLabel() + "]");
116   }
117   public void visitParameterExpr(ParameterExpr v) {
118     // sb.append("Parameter(\"").append(v.getName()).append("\"");
119
// v.getName() is random, dont show
120
sb.append("Parameter(\"RandomID\"");
121     switch (v.getType()) {
122       case ParameterExpr.MEMBER :
123         sb.append(", MEMBER");
124         break;
125       case ParameterExpr.NUMBER :
126         sb.append(", NUMBER");
127         break;
128       case ParameterExpr.STRING :
129         sb.append(", STRING");
130         break;
131       default :
132         throw new IllegalArgumentException JavaDoc();
133     }
134     sb.append(", \"").append(v.getLabel()).append("\"");
135     sb.append(", ");
136     v.getValue().accept(this);
137     sb.append(")");
138   }
139   public void visitStringExpr(StringExpr v) {
140     sb.append("\"" + v.getValue() + "\"");
141   }
142
143   public void visitPropertyExpr(PropertyExpr v) {
144     sb.append(v.getName()).append("=");
145     v.getValueExpr().accept(this);
146     sb.append(";");
147     
148   }
149
150 }
151
Popular Tags