KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > olap > query > ExprStringConverter


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.query;
14
15 import java.util.HashSet JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import com.tonbeller.jpivot.olap.model.BooleanExpr;
19 import com.tonbeller.jpivot.olap.model.Dimension;
20 import com.tonbeller.jpivot.olap.model.DoubleExpr;
21 import com.tonbeller.jpivot.olap.model.Expression;
22 import com.tonbeller.jpivot.olap.model.FunCallExpr;
23 import com.tonbeller.jpivot.olap.model.Hierarchy;
24 import com.tonbeller.jpivot.olap.model.IntegerExpr;
25 import com.tonbeller.jpivot.olap.model.Level;
26 import com.tonbeller.jpivot.olap.model.Member;
27 import com.tonbeller.jpivot.olap.model.ParameterExpr;
28 import com.tonbeller.jpivot.olap.model.StringExpr;
29 import com.tonbeller.jpivot.olap.model.VisitorSupportStrict;
30
31
32 /**
33  * Expression to String converter
34  *
35  */

36 public class ExprStringConverter extends VisitorSupportStrict {
37   static final Set JavaDoc infix = new HashSet JavaDoc();
38   static final Set JavaDoc prefix = new HashSet JavaDoc();
39   static {
40     String JavaDoc[] s =
41       new String JavaDoc[] { "and", "or", "xor", "*", "/", "+", "-", "%", "<", ">", "<=", ">=", "<>", "=" };
42     for (int i = 0; i < s.length; i++)
43       infix.add(s[i]);
44
45     prefix.add("-");
46     prefix.add("not");
47   };
48
49   protected StringBuffer JavaDoc strbuf = null;
50
51   public String JavaDoc exprToString(Expression e) {
52     strbuf = new StringBuffer JavaDoc();
53     e.accept(this);
54     return strbuf.toString();
55   }
56
57   public void visitBooleanExpr(BooleanExpr v) {
58     if (v.getValue())
59       strbuf.append("true");
60     else
61       strbuf.append("false");
62   }
63   public void visitDimension(Dimension v) {
64     strbuf.append(((MDXElement) v).getUniqueName());
65   }
66
67   public void visitDoubleExpr(DoubleExpr v) {
68     strbuf.append(Double.toString(v.getValue()));
69   }
70
71   public void visitFunCallExpr(FunCallExpr v) {
72     String JavaDoc name = v.getName();
73     //ExprStringConverter strcnv = new ExprStringConverter();
74
Expression[] args = v.getArgs();
75
76     // infix operator (not including unary minus)
77
if (infix.contains(name) && args.length == 2) {
78
79       //argstr = strcnv.exprToString(args[0]);
80
//strbuf.append(argstr);
81
args[0].accept(this);
82       strbuf.append(' ');
83       strbuf.append(name);
84       strbuf.append(' ');
85       args[1].accept(this);
86       //argstr = strcnv.exprToString(args[1]);
87
//strbuf.append(argstr);
88
return;
89     } else if (v.getName().startsWith(".")) {
90       name = name.substring(1);
91       if (args.length == 1) {
92         args[0].accept(this);
93         //argstr = strcnv.exprToString(args[0]);
94
//strbuf.append(argstr);
95
strbuf.append('.');
96         strbuf.append(name);
97         return;
98       }
99     } else if (prefix.contains(name)) {
100       strbuf.append(name);
101       strbuf.append(' ');
102       args[0].accept(this);
103       //argstr = strcnv.exprToString(args[0]);
104
//strbuf.append(argstr);
105
return;
106     } else if ("{}".equals(name)) {
107       strbuf.append('{');
108       for (int i = 0; i < args.length; i++) {
109         if (i > 0)
110           strbuf.append(',');
111         args[i].accept(this);
112         //argstr = strcnv.exprToString(args[i]);
113
//strbuf.append(argstr);
114
}
115       strbuf.append('}');
116       return;
117     } else if ("()".equals(name)) {
118       strbuf.append('(');
119       for (int i = 0; i < args.length; i++) {
120         if (i > 0)
121           strbuf.append(',');
122         args[i].accept(this);
123         //argstr = strcnv.exprToString(args[i]);
124
//strbuf.append(argstr);
125
}
126       strbuf.append(')');
127       return;
128     }
129     strbuf.append(name);
130     strbuf.append('(');
131     for (int i = 0; i < args.length; i++) {
132       if (i > 0)
133         strbuf.append(',');
134       args[i].accept(this);
135       //argstr = strcnv.exprToString(args[i]);
136
//strbuf.append(argstr);
137
}
138     strbuf.append(')');
139   }
140
141   public void visitHierarchy(Hierarchy v) {
142     strbuf.append(((MDXElement) v).getUniqueName());
143   }
144
145   public void visitIntegerExpr(IntegerExpr v) {
146     strbuf.append(Integer.toString(v.getValue()));
147   }
148
149   public void visitLevel(Level v) {
150     strbuf.append(((MDXElement) v).getUniqueName());
151   }
152
153   public void visitMember(Member v) {
154     strbuf.append(((MDXElement) v).getUniqueName());
155   }
156
157   public void visitParameterExpr(ParameterExpr v) {
158     Expression val = v.getValue();
159     ExprStringConverter strcnv = new ExprStringConverter();
160     String JavaDoc valstr = strcnv.exprToString(val);
161     strbuf.append(valstr);
162   }
163
164   public void visitStringExpr(StringExpr v) {
165     strbuf.append(v.getValue());
166   }
167
168 } // ExprStringConverter
169
Popular Tags