KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > olap > Id


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/olap/Id.java#26 $
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) 1998-2002 Kana Software, Inc.
7 // Copyright (C) 2001-2006 Julian Hyde and others
8 // All Rights Reserved.
9 // You must accept the terms of that agreement to use this software.
10 //
11 // jhyde, 21 January, 1999
12 */

13
14 package mondrian.olap;
15 import mondrian.olap.type.Type;
16 import mondrian.mdx.MdxVisitor;
17
18 import java.io.PrintWriter JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collections JavaDoc;
22
23 /**
24  * Multi-part identifier.
25  */

26 public class Id
27     extends ExpBase
28     implements Cloneable JavaDoc {
29
30     private final List JavaDoc<Segment> segments;
31
32     /**
33      * Creates an identifier containing a single part.
34      *
35      * @param segment Segment, consisting of a name and quoting style
36      */

37     public Id(Segment segment) {
38         segments = Collections.singletonList(segment);
39     }
40
41     private Id(List JavaDoc<Segment> segments) {
42         this.segments = segments;
43     }
44
45     public Id clone() {
46         // This is immutable, so no need to clone.
47
return this;
48     }
49
50     public int getCategory() {
51         return Category.Unknown;
52     }
53
54     public Type getType() {
55         // Can't give the type until we have resolved.
56
throw new UnsupportedOperationException JavaDoc();
57     }
58
59     public String JavaDoc toString() {
60         return Util.quoteMdxIdentifier(toStringArray());
61     }
62
63     public String JavaDoc[] toStringArray() {
64         String JavaDoc[] names = new String JavaDoc[segments.size()];
65         int k = 0;
66         for (Segment segment : segments) {
67             names[k++] = segment.name;
68         }
69         return names;
70     }
71
72     public String JavaDoc getElement(int i) {
73         return segments.get(i).name;
74     }
75
76     /**
77      * Returns a new Identifier consisting of this one with another segment
78      * appended. Does not modify this Identifier.
79      *
80      * @param segment Name of segment
81      * @return New identifier
82      */

83     public Id append(Segment segment) {
84         List JavaDoc<Segment> newSegments = new ArrayList JavaDoc<Segment>(segments);
85         newSegments.add(segment);
86         return new Id(newSegments);
87     }
88
89     public Exp accept(Validator validator) {
90         if (segments.size() == 1) {
91             final Segment s = segments.get(0);
92             if (s.quoting == Quoting.UNQUOTED &&
93                 validator.getFunTable().isReserved(s.name)) {
94                 return Literal.createSymbol(s.name.toUpperCase());
95             }
96         }
97         final String JavaDoc[] names = toStringArray();
98         final Exp element = Util.lookup(validator.getQuery(), names, true);
99         if (element == null) {
100             return null;
101         }
102         return element.accept(validator);
103     }
104
105     public Object JavaDoc accept(MdxVisitor visitor) {
106         return visitor.visit(this);
107     }
108
109     public void unparse(PrintWriter JavaDoc pw) {
110         int k = 0;
111         for (Segment s : segments) {
112             if (k++ > 0) {
113                 pw.print(".");
114             }
115             switch (s.quoting) {
116             case UNQUOTED:
117                 pw.print(s.name);
118                 break;
119             case KEY:
120                 pw.print("&[" + Util.mdxEncodeString(s.name) + "]");
121                 break;
122             case QUOTED:
123                 pw.print("[" + Util.mdxEncodeString(s.name) + "]");
124                 break;
125             }
126         }
127     }
128
129     /**
130      * Component in a compound identifier. It is described by its name and how
131      * the name is quoted.
132      *
133      * <p>For example, the identifier
134      * <code>[Store].USA.[New Mexico].&[45]</code> has four segments:<ul>
135      * <li>"Store", {@link mondrian.olap.Id.Quoting#QUOTED}</li>
136      * <li>"USA", {@link mondrian.olap.Id.Quoting#UNQUOTED}</li>
137      * <li>"New Mexico", {@link mondrian.olap.Id.Quoting#QUOTED}</li>
138      * <li>"45", {@link mondrian.olap.Id.Quoting#KEY}</li>
139      * </ul>
140      */

141     public static class Segment {
142         public final String JavaDoc name;
143         public final Quoting quoting;
144
145         public Segment(String JavaDoc name, Quoting quoting) {
146             this.name = name;
147             this.quoting = quoting;
148         }
149     }
150
151     public enum Quoting {
152
153         /**
154          * Unquoted identifier, for example "Measures".
155          */

156         UNQUOTED,
157
158         /**
159          * Quoted identifier, for example "[Measures]".
160          */

161         QUOTED,
162
163         /**
164          * Identifier quoted with an ampersand to indicate a key value, for example
165          * the second segment in "[Employees].&[89]".
166          */

167         KEY
168     }
169 }
170
171 // End Id.java
172
Popular Tags