KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > test > olap > TestExpressionParser


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.test.olap;
14
15 import com.tonbeller.jpivot.olap.model.Dimension;
16 import com.tonbeller.jpivot.olap.model.Displayable;
17 import com.tonbeller.jpivot.olap.model.Expression;
18 import com.tonbeller.jpivot.olap.model.Hierarchy;
19 import com.tonbeller.jpivot.olap.model.Level;
20 import com.tonbeller.jpivot.olap.model.Member;
21 import com.tonbeller.jpivot.olap.navi.ExpressionParser;
22
23 public class TestExpressionParser extends TestExtensionSupport implements ExpressionParser {
24
25   public Expression parse(String JavaDoc name) throws InvalidSyntaxException {
26     Dimension[] dims = model().getDimensions();
27     for (int i = 0; i < dims.length; i++) {
28       Dimension d = dims[i];
29       if (name.equals(d.getLabel()))
30         return d;
31       Expression e = findHierarchies(name, d.getHierarchies());
32       if (e != null)
33         return e;
34     }
35     // not a dim, hierarchy, level. create member!
36
TestMember tm = new TestMember();
37     tm.setLabel(name);
38     return tm;
39   }
40   
41   private Expression findHierarchies(String JavaDoc name, Hierarchy[] hiers) {
42     for (int i = 0; i < hiers.length; i++) {
43       Hierarchy h = hiers[i];
44       if (name.equals(h.getLabel()))
45         return h;
46       Expression e = findLevels(name, h.getLevels());
47       if (e != null)
48         return e;
49     }
50     return null;
51   }
52   
53   private Expression findLevels(String JavaDoc name, Level[] levels) {
54     for (int i = 0; i < levels.length; i++) {
55       Level l = levels[i];
56       if (name.equals(l.getLabel()))
57         return l;
58     }
59     return null;
60   }
61
62   public String JavaDoc unparse(Expression expr) {
63     return ((Displayable) expr).getLabel();
64   }
65
66   public Member lookupMember(String JavaDoc uniqueName) throws InvalidSyntaxException {
67     Object JavaDoc obj = parse(uniqueName);
68     if (obj instanceof Member)
69       return (Member)obj;
70     return null;
71   }
72
73   public Level lookupLevel(String JavaDoc uniqueName) throws InvalidSyntaxException {
74     Object JavaDoc obj = parse(uniqueName);
75     if (obj instanceof Level)
76       return (Level)obj;
77     return null;
78   }
79
80   public Hierarchy lookupHierarchy(String JavaDoc uniqueName) throws InvalidSyntaxException {
81     Object JavaDoc obj = parse(uniqueName);
82     if (obj instanceof Hierarchy)
83       return (Hierarchy)obj;
84     return null;
85   }
86
87   public Dimension lookupDimension(String JavaDoc uniqueName) throws InvalidSyntaxException {
88     Object JavaDoc obj = parse(uniqueName);
89     if (obj instanceof Dimension)
90       return (Dimension)obj;
91     return null;
92   }
93
94 }
Popular Tags