KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > josql > expressions > SelectItemExpression


1 /*
2  * Copyright 2004-2005 Gary Bentley
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may
5  * not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */

15 package org.josql.expressions;
16
17 import java.util.Map JavaDoc;
18 import java.util.Collection JavaDoc;
19
20 import org.josql.Query;
21 import org.josql.QueryExecutionException;
22 import org.josql.QueryParseException;
23
24 import org.josql.internal.Utilities;
25
26 public class SelectItemExpression extends Expression
27 {
28
29     public static final String JavaDoc KEY = "key";
30     public static final String JavaDoc VALUE = "value";
31
32     private Expression exp = null;
33     private boolean fixedResult = false;
34     private Class JavaDoc addItemsType = null;
35     private String JavaDoc addItemsMapType = null;
36     private String JavaDoc alias = null;
37
38     public String JavaDoc getAlias ()
39     {
40
41     return this.alias;
42
43     }
44
45     public void setAlias (String JavaDoc a)
46     {
47
48     this.alias = Utilities.stripQuotes (a);
49
50     }
51
52     public boolean hasFixedResult (Query q)
53     {
54
55     return this.fixedResult;
56
57     }
58
59     public Class JavaDoc getExpectedReturnType (Query q)
60                                     throws QueryParseException
61     {
62
63     return this.exp.getExpectedReturnType (q);
64
65     }
66
67     public void init (Query q)
68                   throws QueryParseException
69     {
70
71     this.exp.init (q);
72
73     this.fixedResult = this.exp.hasFixedResult (q);
74
75     /*
76     if (this.isAddItemsFromCollectionOrMap ())
77     {
78
79         // Get the expected return type.
80         Class expC = this.getExpectedReturnType (q);
81
82         if (expC.getName ().equals (Object.class.getName ()))
83         {
84
85         // Defer until later.
86         return;
87
88         }
89
90         if (!this.addItemsType.isAssignableFrom (expC))
91         {
92
93         throw new QueryParseException ("Expected return type from select clause column will be: " +
94                            expC.getName () +
95                            ", however expecting to add items to results from type: " +
96                            this.addItemsType.getName ());
97
98         }
99
100     }
101     */

102
103     }
104
105     public void setAddMapType (String JavaDoc t)
106     {
107
108     this.addItemsMapType = t;
109
110     }
111
112     public Collection JavaDoc getAddItems (Object JavaDoc v)
113     {
114
115     if (Map JavaDoc.class.isAssignableFrom (this.addItemsType))
116     {
117
118         boolean k = this.addItemsMapType.equals (SelectItemExpression.KEY);
119
120         Map JavaDoc m = (Map JavaDoc) v;
121
122         if (k)
123         {
124
125         return m.keySet ();
126
127         }
128
129         return m.values ();
130
131     }
132
133     if (v instanceof Collection JavaDoc)
134     {
135
136         return (Collection JavaDoc) v;
137
138     }
139
140     // This is a nasty hack but is "ok" for now, fix in a later version...
141
java.util.List JavaDoc l = new java.util.ArrayList JavaDoc ();
142     l.add (v);
143
144     return l;
145
146     }
147
148     public boolean isAddItemsFromCollectionOrMap ()
149     {
150
151     return this.addItemsType != null;
152
153     }
154
155     public void setAddItemsType (Class JavaDoc c)
156     {
157
158     this.addItemsType = c;
159
160     }
161
162     public Expression getExpression ()
163     {
164
165     return this.exp;
166
167     }
168
169     public void setExpression (Expression exp)
170     {
171
172     this.exp = exp;
173
174     }
175
176     public boolean isTrue (Object JavaDoc o,
177                Query q)
178                        throws QueryExecutionException
179     {
180
181     return this.exp.isTrue (o,
182                 q);
183
184     }
185
186     public Object JavaDoc getValue (Object JavaDoc o,
187                 Query q)
188                         throws QueryExecutionException
189     {
190
191     return this.exp.getValue (o,
192                   q);
193
194     }
195
196     public String JavaDoc toString ()
197     {
198
199     StringBuffer JavaDoc b = new StringBuffer JavaDoc ();
200
201     if (this.isAddItemsFromCollectionOrMap ())
202     {
203
204         b.append ("[*");
205
206         if (this.addItemsMapType != null)
207         {
208
209         b.append (", ");
210         b.append (this.addItemsMapType);
211
212         }
213
214         b.append ("] ");
215
216     }
217
218     b.append (this.exp);
219
220     if (this.alias != null)
221     {
222
223         b.append (" ");
224         b.append (this.alias);
225
226     }
227
228     return b.toString ();
229
230     }
231
232 }
233
Popular Tags