KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > josql > functions > FormattingFunctions


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.functions;
16
17 import java.util.List JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Date JavaDoc;
20
21 import java.text.SimpleDateFormat JavaDoc;
22 import java.text.DecimalFormat JavaDoc;
23
24 import com.gentlyweb.utils.TimeDuration;
25 import com.gentlyweb.utils.Timing;
26 import com.gentlyweb.utils.Getter;
27
28 import org.josql.Query;
29 import org.josql.QueryExecutionException;
30
31 public class FormattingFunctions extends AbstractFunctionHandler
32 {
33
34     public static final String JavaDoc HANDLER_ID = "_internal_formatting";
35
36     public static String JavaDoc DEFAULT_DATE_FORMAT_SPEC = "dd/MMM/yyyy";
37     public static String JavaDoc DEFAULT_DATE_TIME_FORMAT_SPEC = FormattingFunctions.DEFAULT_DATE_FORMAT_SPEC +
38                                                          ", hh:mm:ss";
39     public static String JavaDoc DEFAULT_DECIMAL_FORMAT_SPEC = "###,###,###.##";
40     
41     private SimpleDateFormat JavaDoc defSDF = new SimpleDateFormat JavaDoc (FormattingFunctions.DEFAULT_DATE_FORMAT_SPEC);
42     private SimpleDateFormat JavaDoc defSDTF = new SimpleDateFormat JavaDoc (FormattingFunctions.DEFAULT_DATE_TIME_FORMAT_SPEC);
43
44     public String JavaDoc formatTimeDuration (Object JavaDoc o)
45                                   throws QueryExecutionException
46     {
47
48     if (o instanceof Number JavaDoc)
49     {
50
51         return TimeDuration.getInstance (((Number JavaDoc) o).longValue ()).format ();
52
53     }
54
55     if (o instanceof Date JavaDoc)
56     {
57
58         return TimeDuration.getInstance ((Date JavaDoc) o).format ();
59
60     }
61
62     if (o instanceof TimeDuration)
63     {
64
65         return TimeDuration.getInstance ((TimeDuration) o).format ();
66
67     }
68
69     if (o instanceof Timing)
70     {
71
72         return TimeDuration.getInstance ((Timing) o).format ();
73
74     }
75
76     throw new QueryExecutionException ("Type: " +
77                        o.getClass ().getName () +
78                        " not supported.");
79
80     }
81
82     public void setDefaultDateFormatSpec (String JavaDoc spec)
83     {
84
85     this.defSDF = new SimpleDateFormat JavaDoc (spec);
86
87     }
88
89     public String JavaDoc formatDate (Object JavaDoc o)
90                           throws QueryExecutionException
91     {
92
93     if (o == null)
94     {
95
96         throw new QueryExecutionException ("Cannot format a null date.");
97
98     }
99
100     Date JavaDoc d = null;
101
102     if (o instanceof Date JavaDoc)
103     {
104
105         d = (Date JavaDoc) o;
106
107     }
108
109     if (o instanceof Number JavaDoc)
110     {
111
112         d = new Date JavaDoc (((Number JavaDoc) o).longValue ());
113
114     }
115
116     // If this is a string try and parse the string first, basically convert
117
// from one format to another.
118
if (o instanceof String JavaDoc)
119     {
120
121         d = ((ConversionFunctions) this.q.getFunctionHandler (ConversionFunctions.HANDLER_ID)).toDate ((String JavaDoc) o);
122
123     }
124
125     if (d == null)
126     {
127
128         throw new QueryExecutionException ("Type: " +
129                            o.getClass ().getName () +
130                            " not supported.");
131
132     }
133
134     return this.defSDF.format (d);
135
136     }
137
138     public String JavaDoc formatDateTime (Object JavaDoc o)
139                               throws QueryExecutionException
140     {
141
142     if (o == null)
143     {
144
145         throw new QueryExecutionException ("Cannot format a null date.");
146
147     }
148
149     Date JavaDoc d = null;
150
151     if (o instanceof Date JavaDoc)
152     {
153
154         d = (Date JavaDoc) o;
155
156     }
157
158     if (o instanceof Number JavaDoc)
159     {
160
161         d = new Date JavaDoc (((Number JavaDoc) o).longValue ());
162
163     }
164
165     // If this is a string try and parse the string first, basically convert
166
// from one format to another.
167
if (o instanceof String JavaDoc)
168     {
169
170         d = ((ConversionFunctions) this.q.getFunctionHandler (ConversionFunctions.HANDLER_ID)).toDate ((String JavaDoc) o);
171
172     }
173
174     if (d == null)
175     {
176
177         throw new QueryExecutionException ("Type: " +
178                            o.getClass ().getName () +
179                            " not supported.");
180
181     }
182
183     return this.defSDTF.format (d);
184
185     }
186
187     public String JavaDoc formatDate (Query q,
188                   Object JavaDoc o,
189                               Getter g,
190                   String JavaDoc spec,
191                   String JavaDoc saveValueName)
192                           throws QueryExecutionException
193     {
194
195     if (g != null)
196     {
197
198         try
199         {
200
201         o = g.getValue (o);
202
203         } catch (Exception JavaDoc e) {
204
205         throw new QueryExecutionException ("Unable to get value from accessor: " +
206                            g,
207                            e);
208
209         }
210
211     }
212
213     if (o == null)
214     {
215
216         return null + "";
217
218     }
219
220     Date JavaDoc d = null;
221
222     if (o instanceof Date JavaDoc)
223     {
224
225         d = (Date JavaDoc) o;
226
227     }
228
229     if (o instanceof Long JavaDoc)
230     {
231
232         d = new Date JavaDoc (((Long JavaDoc) o).longValue ());
233
234     }
235
236     Object JavaDoc so = null;
237
238     if (saveValueName != null)
239     {
240
241         so = q.getSaveValue (saveValueName);
242
243     }
244
245     SimpleDateFormat JavaDoc df = null;
246     
247     if (so != null)
248     {
249
250         df = (SimpleDateFormat JavaDoc) so;
251
252     } else {
253         
254         if (spec == null)
255         {
256
257         spec = FormattingFunctions.DEFAULT_DATE_FORMAT_SPEC;
258
259         }
260
261         df = new SimpleDateFormat JavaDoc (spec);
262         
263     }
264
265     return df.format (d);
266
267     }
268
269     public String JavaDoc formatNumber (Object JavaDoc n)
270                             throws QueryExecutionException
271     {
272
273     return this.formatNumber (this.q,
274                   n,
275                   null,
276                   null);
277
278     }
279
280     public String JavaDoc formatNumber (Query q,
281                 Object JavaDoc o,
282                 String JavaDoc spec,
283                 String JavaDoc saveValueName)
284                             throws QueryExecutionException
285     {
286
287     if (!(o instanceof Number JavaDoc))
288     {
289
290         if (o == null)
291         {
292
293         return "NaN (null)";
294
295         }
296
297         return "NaN (" + o.getClass ().getName () + ")";
298
299     }
300
301     if (o == null)
302     {
303
304         return "0";
305
306     }
307
308     Object JavaDoc so = null;
309
310     if (saveValueName != null)
311     {
312
313         so = q.getSaveValue (saveValueName);
314
315     }
316
317     Number JavaDoc n = (Number JavaDoc) o;
318
319     DecimalFormat JavaDoc df = null;
320     
321     if (so != null)
322     {
323
324         if (!(so instanceof DecimalFormat JavaDoc))
325         {
326
327         throw new QueryExecutionException ("Expected save value: \"" +
328                            saveValueName +
329                            "\" object to be of type: " +
330                            DecimalFormat JavaDoc.class.getName () +
331                            ", is: " +
332                            so.getClass ().getName ());
333
334         }
335
336         df = (DecimalFormat JavaDoc) so;
337
338     } else {
339         
340         if (spec == null)
341         {
342
343         spec = FormattingFunctions.DEFAULT_DECIMAL_FORMAT_SPEC;
344
345         }
346
347         df = new DecimalFormat JavaDoc (spec);
348         
349     }
350
351     return df.format (n.doubleValue ());
352
353     }
354
355     public String JavaDoc formatNumber (Query q,
356                 Object JavaDoc o,
357                 Getter g,
358                 String JavaDoc spec,
359                     String JavaDoc saveValueName)
360                             throws QueryExecutionException
361     {
362
363     try
364     {
365
366         o = g.getValue (o);
367
368     } catch (Exception JavaDoc e) {
369
370         throw new QueryExecutionException ("Unable to get value from accessor: " +
371                            g,
372                            e);
373
374     }
375
376     return this.formatNumber (q,
377                   o,
378                   spec,
379                   saveValueName);
380     
381     }
382
383 }
384
Popular Tags