Source Code Cross Referenced for FormattingFunctions.java in  » Development » JoSQL » org » josql » functions » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Development » JoSQL » org.josql.functions 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004-2007 Gary Bentley 
003:         * 
004:         * Licensed under the Apache License, Version 2.0 (the "License"); you may 
005:         * not use this file except in compliance with the License. 
006:         * You may obtain a copy of the License at 
007:         *    http://www.apache.org/licenses/LICENSE-2.0 
008:         *
009:         * Unless required by applicable law or agreed to in writing, software 
010:         * distributed under the License is distributed on an "AS IS" BASIS, 
011:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
012:         * See the License for the specific language governing permissions and 
013:         * limitations under the License.
014:         */
015:        package org.josql.functions;
016:
017:        import java.util.Date;
018:
019:        import java.text.SimpleDateFormat;
020:        import java.text.DecimalFormat;
021:
022:        import com.gentlyweb.utils.TimeDuration;
023:        import com.gentlyweb.utils.Timing;
024:        import com.gentlyweb.utils.Getter;
025:
026:        import org.josql.Query;
027:        import org.josql.QueryExecutionException;
028:
029:        public class FormattingFunctions extends AbstractFunctionHandler {
030:
031:            public static final String HANDLER_ID = "_internal_formatting";
032:
033:            public static String DEFAULT_DATE_FORMAT_SPEC = "dd/MMM/yyyy";
034:            public static String DEFAULT_DATE_TIME_FORMAT_SPEC = FormattingFunctions.DEFAULT_DATE_FORMAT_SPEC
035:                    + ", hh:mm:ss";
036:            public static String DEFAULT_DECIMAL_FORMAT_SPEC = "###,###,###.##";
037:
038:            private SimpleDateFormat defSDF = new SimpleDateFormat(
039:                    FormattingFunctions.DEFAULT_DATE_FORMAT_SPEC);
040:            private SimpleDateFormat defSDTF = new SimpleDateFormat(
041:                    FormattingFunctions.DEFAULT_DATE_TIME_FORMAT_SPEC);
042:
043:            public String formatTimeDuration(Object o)
044:                    throws QueryExecutionException {
045:
046:                if (o instanceof  Number) {
047:
048:                    return TimeDuration.getInstance(((Number) o).longValue())
049:                            .format();
050:
051:                }
052:
053:                if (o instanceof  Date) {
054:
055:                    return TimeDuration.getInstance((Date) o).format();
056:
057:                }
058:
059:                if (o instanceof  TimeDuration) {
060:
061:                    return TimeDuration.getInstance((TimeDuration) o).format();
062:
063:                }
064:
065:                if (o instanceof  Timing) {
066:
067:                    return TimeDuration.getInstance((Timing) o).format();
068:
069:                }
070:
071:                throw new QueryExecutionException("Type: "
072:                        + o.getClass().getName() + " not supported.");
073:
074:            }
075:
076:            public void setDefaultDateFormatSpec(String spec) {
077:
078:                this .defSDF = new SimpleDateFormat(spec);
079:
080:            }
081:
082:            public String formatDate(Object o) throws QueryExecutionException {
083:
084:                if (o == null) {
085:
086:                    throw new QueryExecutionException(
087:                            "Cannot format a null date.");
088:
089:                }
090:
091:                Date d = null;
092:
093:                if (o instanceof  Date) {
094:
095:                    d = (Date) o;
096:
097:                }
098:
099:                if (o instanceof  Number) {
100:
101:                    d = new Date(((Number) o).longValue());
102:
103:                }
104:
105:                // If this is a string try and parse the string first, basically convert
106:                // from one format to another.
107:                if (o instanceof  String) {
108:
109:                    d = ((ConversionFunctions) this .q
110:                            .getFunctionHandler(ConversionFunctions.HANDLER_ID))
111:                            .toDate((String) o);
112:
113:                }
114:
115:                if (d == null) {
116:
117:                    throw new QueryExecutionException("Type: "
118:                            + o.getClass().getName() + " not supported.");
119:
120:                }
121:
122:                return this .defSDF.format(d);
123:
124:            }
125:
126:            public String formatDateTime(Object o)
127:                    throws QueryExecutionException {
128:
129:                if (o == null) {
130:
131:                    throw new QueryExecutionException(
132:                            "Cannot format a null date.");
133:
134:                }
135:
136:                Date d = null;
137:
138:                if (o instanceof  Date) {
139:
140:                    d = (Date) o;
141:
142:                }
143:
144:                if (o instanceof  Number) {
145:
146:                    d = new Date(((Number) o).longValue());
147:
148:                }
149:
150:                // If this is a string try and parse the string first, basically convert
151:                // from one format to another.
152:                if (o instanceof  String) {
153:
154:                    d = ((ConversionFunctions) this .q
155:                            .getFunctionHandler(ConversionFunctions.HANDLER_ID))
156:                            .toDate((String) o);
157:
158:                }
159:
160:                if (d == null) {
161:
162:                    throw new QueryExecutionException("Type: "
163:                            + o.getClass().getName() + " not supported.");
164:
165:                }
166:
167:                return this .defSDTF.format(d);
168:
169:            }
170:
171:            public String formatDate(Query q, Object o, Getter g, String spec,
172:                    String saveValueName) throws QueryExecutionException {
173:
174:                if (g != null) {
175:
176:                    try {
177:
178:                        o = g.getValue(o);
179:
180:                    } catch (Exception e) {
181:
182:                        throw new QueryExecutionException(
183:                                "Unable to get value from accessor: " + g, e);
184:
185:                    }
186:
187:                }
188:
189:                if (o == null) {
190:
191:                    return null + "";
192:
193:                }
194:
195:                Date d = null;
196:
197:                if (o instanceof  Date) {
198:
199:                    d = (Date) o;
200:
201:                }
202:
203:                if (o instanceof  Long) {
204:
205:                    d = new Date(((Long) o).longValue());
206:
207:                }
208:
209:                Object so = null;
210:
211:                if (saveValueName != null) {
212:
213:                    so = q.getSaveValue(saveValueName);
214:
215:                }
216:
217:                SimpleDateFormat df = null;
218:
219:                if (so != null) {
220:
221:                    df = (SimpleDateFormat) so;
222:
223:                } else {
224:
225:                    if (spec == null) {
226:
227:                        spec = FormattingFunctions.DEFAULT_DATE_FORMAT_SPEC;
228:
229:                    }
230:
231:                    df = new SimpleDateFormat(spec);
232:
233:                }
234:
235:                return df.format(d);
236:
237:            }
238:
239:            public String formatNumber(Object n) throws QueryExecutionException {
240:
241:                return this .formatNumber(this .q, n, null, null);
242:
243:            }
244:
245:            public String formatNumber(Query q, Object o, String spec,
246:                    String saveValueName) throws QueryExecutionException {
247:
248:                if (!(o instanceof  Number)) {
249:
250:                    if (o == null) {
251:
252:                        return "NaN (null)";
253:
254:                    }
255:
256:                    return "NaN (" + o.getClass().getName() + ")";
257:
258:                }
259:
260:                if (o == null) {
261:
262:                    return "0";
263:
264:                }
265:
266:                Object so = null;
267:
268:                if (saveValueName != null) {
269:
270:                    so = q.getSaveValue(saveValueName);
271:
272:                }
273:
274:                Number n = (Number) o;
275:
276:                DecimalFormat df = null;
277:
278:                if (so != null) {
279:
280:                    if (!(so instanceof  DecimalFormat)) {
281:
282:                        throw new QueryExecutionException(
283:                                "Expected save value: \"" + saveValueName
284:                                        + "\" object to be of type: "
285:                                        + DecimalFormat.class.getName()
286:                                        + ", is: " + so.getClass().getName());
287:
288:                    }
289:
290:                    df = (DecimalFormat) so;
291:
292:                } else {
293:
294:                    if (spec == null) {
295:
296:                        spec = FormattingFunctions.DEFAULT_DECIMAL_FORMAT_SPEC;
297:
298:                    }
299:
300:                    df = new DecimalFormat(spec);
301:
302:                }
303:
304:                return df.format(n.doubleValue());
305:
306:            }
307:
308:            public String formatNumber(Query q, Object o, Getter g,
309:                    String spec, String saveValueName)
310:                    throws QueryExecutionException {
311:
312:                try {
313:
314:                    o = g.getValue(o);
315:
316:                } catch (Exception e) {
317:
318:                    throw new QueryExecutionException(
319:                            "Unable to get value from accessor: " + g, e);
320:
321:                }
322:
323:                return this.formatNumber(q, o, spec, saveValueName);
324:
325:            }
326:
327:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.