KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > mondrian > MondrianExpressionParser


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.mondrian;
14
15 import java.text.NumberFormat JavaDoc;
16 import java.text.ParseException JavaDoc;
17
18 import mondrian.olap.Query;
19 import mondrian.olap.OlapElement;
20 import mondrian.olap.Util;
21
22 import org.apache.log4j.Logger;
23
24 import com.tonbeller.jpivot.core.ExtensionSupport;
25 import com.tonbeller.jpivot.olap.model.Dimension;
26 import com.tonbeller.jpivot.olap.model.DoubleExpr;
27 import com.tonbeller.jpivot.olap.model.Expression;
28 import com.tonbeller.jpivot.olap.model.Hierarchy;
29 import com.tonbeller.jpivot.olap.model.IntegerExpr;
30 import com.tonbeller.jpivot.olap.model.Level;
31 import com.tonbeller.jpivot.olap.model.Member;
32 import com.tonbeller.jpivot.olap.model.impl.DoubleExprImpl;
33 import com.tonbeller.jpivot.olap.model.impl.IntegerExprImpl;
34 import com.tonbeller.jpivot.olap.model.impl.StringExprImpl;
35 import com.tonbeller.jpivot.olap.navi.ExpressionParser;
36
37 /**
38  * let Mondrian parse a String expression
39  */

40 public class MondrianExpressionParser extends ExtensionSupport implements ExpressionParser {
41
42   static NumberFormat JavaDoc nf = NumberFormat.getInstance();
43   static Logger logger = Logger.getLogger(MondrianSetParameter.class);
44
45   /**
46    */

47   public MondrianExpressionParser() {
48     super.setId(ExpressionParser.ID);
49   }
50
51   /**
52    * @see ExpressionParser#unparse(com.tonbeller.jpivot.olap.model.Expression)
53    */

54   public String JavaDoc unparse(Expression expr) {
55     if (expr instanceof DoubleExpr) {
56       double d = ((DoubleExpr) expr).getValue();
57       return nf.format(d);
58     } else if (expr instanceof IntegerExpr) {
59       int ii = ((IntegerExpr) expr).getValue();
60       return String.valueOf(ii);
61     } else if (expr instanceof MondrianMember) {
62       MondrianMember m = (MondrianMember) expr;
63       mondrian.olap.Member monMember = m.getMonMember();
64       return monMember.getUniqueName();
65     }
66     else if (expr instanceof MondrianLevel) {
67       return ((MondrianLevel)expr).getUniqueName();
68     }
69     else if (expr instanceof MondrianHierarchy) {
70       return ((MondrianHierarchy)expr).getUniqueName();
71     }
72     else if (expr instanceof MondrianDimension) {
73       return ((MondrianDimension)expr).getUniqueName();
74     }
75     return null;
76   }
77
78   /**
79    *
80    * parse a String
81    * currently we cannot handle anything different from
82    * - string
83    * - number
84    * - member.
85    * @see com.tonbeller.jpivot.olap.navi.ExpressionParser#parse(java.lang.String)
86    */

87   public Expression parse(String JavaDoc expr) throws InvalidSyntaxException {
88
89     // is it a String (enclose in double or single quotes ?
90
String JavaDoc trimmed = expr.trim();
91     int len = trimmed.length();
92     if (trimmed.charAt(0) == '"' && trimmed.charAt(len - 1) == '"')
93       return new StringExprImpl(trimmed.substring(1, trimmed.length() - 1));
94     if (trimmed.charAt(0) == '\'' && trimmed.charAt(len - 1) == '\'')
95       return new StringExprImpl(trimmed.substring(1, trimmed.length() - 1));
96
97     // is it a Number ?
98
Number JavaDoc number = null;
99     try {
100       number = nf.parse(trimmed);
101     } catch (ParseException JavaDoc pex) {
102       // nothing to do, should be member
103
}
104     if (number != null) {
105       if (number instanceof Double JavaDoc) {
106         return new DoubleExprImpl(number.doubleValue());
107       } else {
108         return new IntegerExprImpl(number.intValue());
109       }
110     }
111
112     MondrianModel model = (MondrianModel) getModel();
113     Query query = ((MondrianQueryAdapter)model.getQueryAdapter()).getMonQuery();
114
115     // assume member,dimension,hierarchy,level
116
OlapElement element;
117     try {
118       element = Util.lookup(query, Util.explode(trimmed));
119     } catch (Exception JavaDoc e) {
120       logger.info(e);
121       throw new InvalidSyntaxException(trimmed);
122     }
123     if (element instanceof mondrian.olap.Member) {
124       final mondrian.olap.Member monMember = (mondrian.olap.Member) element;
125       Member member = model.lookupMemberByUName(monMember.getUniqueName());
126       return member;
127     } else if (element instanceof mondrian.olap.Level) {
128       mondrian.olap.Level monLevel = (mondrian.olap.Level) element;
129       MondrianLevel level = model.lookupLevel(monLevel.getUniqueName());
130       return level;
131     } else if (element instanceof mondrian.olap.Hierarchy) {
132       mondrian.olap.Hierarchy monHier = (mondrian.olap.Hierarchy) element;
133       MondrianHierarchy hier = model.lookupHierarchy(monHier.getUniqueName());
134       return hier;
135     } else if (element instanceof mondrian.olap.Dimension) {
136       mondrian.olap.Dimension monDim = (mondrian.olap.Dimension) element;
137       MondrianDimension dim = model.lookupDimension(monDim.getUniqueName());
138       return dim;
139     }
140
141     throw new InvalidSyntaxException("could not resolve expression " + trimmed);
142   }
143
144   public Member lookupMember(String JavaDoc uniqueName) throws InvalidSyntaxException {
145     MondrianModel model = (MondrianModel) getModel();
146     return model.lookupMemberByUName(uniqueName);
147   }
148
149   public Level lookupLevel(String JavaDoc uniqueName) throws InvalidSyntaxException {
150     MondrianModel model = (MondrianModel) getModel();
151     return model.lookupLevel(uniqueName);
152   }
153
154   public Hierarchy lookupHierarchy(String JavaDoc uniqueName) throws InvalidSyntaxException {
155     MondrianModel model = (MondrianModel) getModel();
156     return model.lookupHierarchy(uniqueName);
157   }
158
159   public Dimension lookupDimension(String JavaDoc uniqueName) throws InvalidSyntaxException {
160     MondrianModel model = (MondrianModel) getModel();
161     return model.lookupDimension(uniqueName);
162   }
163
164 } // MondrianExpressionParser
165
Popular Tags