KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > dbtags > resultset > BaseDateTimeGetterTag


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

16 package org.apache.taglibs.dbtags.resultset;
17
18 import java.sql.ResultSet JavaDoc;
19 import java.sql.SQLException JavaDoc;
20 import java.sql.Types JavaDoc;
21 import java.text.DateFormat JavaDoc;
22 import java.text.SimpleDateFormat JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Locale JavaDoc;
25
26 import javax.servlet.jsp.JspTagException JavaDoc;
27
28 /**
29  * Base tag for getDate, getTime, and getTimestamp
30  *
31  * @author Morgan Delagrange
32  * @author Marius Scurtescu
33  */

34 public class BaseDateTimeGetterTag extends BaseGetterTag {
35
36   protected static HashMap JavaDoc _mapDateTimeStyle = new HashMap JavaDoc (4);
37   protected String JavaDoc _format = null;
38
39   static
40   {
41     _mapDateTimeStyle.put ("FULL", new Integer JavaDoc (DateFormat.FULL));
42     _mapDateTimeStyle.put ("LONG", new Integer JavaDoc (DateFormat.LONG));
43     _mapDateTimeStyle.put ("MEDIUM", new Integer JavaDoc (DateFormat.MEDIUM));
44     _mapDateTimeStyle.put ("SHORT", new Integer JavaDoc (DateFormat.SHORT));
45   }
46
47   public void setFormat (String JavaDoc strFormat) {
48     _format = strFormat;
49   }
50
51   /**
52    * Returns a formatted Date as a String, or null if the
53    * database row was null
54    *
55    * @param position position of the column in the ResultSet
56    * @param nType one of the following java.sql.Types: DATE, TIME, TIMESTAMP
57    * @return a formatted String, or null
58    * @exception JspTagException
59    */

60   protected String JavaDoc getDateAsString(int position, int nType) throws JspTagException JavaDoc {
61
62     ResultSet JavaDoc rset = getResultSet();
63
64     String JavaDoc string = null;
65     try {
66
67       switch (nType) {
68         case Types.DATE:
69           {
70             java.sql.Date JavaDoc date = rset.getDate (position);
71
72             if (date != null) {
73               if (_format == null)
74                 return date.toString ();
75
76               DateFormat JavaDoc fmt = getDateFormat (nType, _format, getLocale (_locale));
77
78               string = fmt.format (date);
79             }
80             break;
81           }
82         case Types.TIME:
83           {
84             java.sql.Time JavaDoc time = rset.getTime (position);
85
86             if (time != null) {
87               if (_format == null)
88                 return time.toString ();
89
90               DateFormat JavaDoc fmt = getDateFormat (nType, _format, getLocale (_locale));
91
92               string = fmt.format (time);
93             }
94             break;
95           }
96         case Types.TIMESTAMP:
97           {
98             java.sql.Timestamp JavaDoc times = rset.getTimestamp (position);
99
100             if (times != null) {
101               if (_format == null)
102                 return times.toString ();
103
104               DateFormat JavaDoc fmt = getDateFormat (nType, _format, getLocale (_locale));
105
106               string = fmt.format (times);
107             }
108             break;
109           }
110         default:
111           throw new JspTagException JavaDoc();
112       }
113     } catch (SQLException JavaDoc e) {
114       throw new JspTagException JavaDoc(e.toString());
115     }
116
117     return string;
118
119   }
120
121   private DateFormat JavaDoc getDateFormat (int nType, String JavaDoc strFormat, Locale JavaDoc loc) throws JspTagException JavaDoc
122   {
123     switch (nType) {
124       case Types.DATE:
125         {
126           if (!_mapDateTimeStyle.containsKey (strFormat))
127             return new SimpleDateFormat JavaDoc (strFormat, loc);
128
129           Integer JavaDoc iDateStyle = (Integer JavaDoc)_mapDateTimeStyle.get (strFormat);
130           return DateFormat.getDateInstance (iDateStyle.intValue(), loc);
131         }
132
133       case Types.TIME:
134         {
135           if (!_mapDateTimeStyle.containsKey (strFormat))
136             return new SimpleDateFormat JavaDoc (strFormat, loc);
137
138           Integer JavaDoc iTimeStyle = (Integer JavaDoc)_mapDateTimeStyle.get (strFormat);
139           return DateFormat.getTimeInstance (iTimeStyle.intValue(), loc);
140         }
141
142       case Types.TIMESTAMP:
143         {
144           Integer JavaDoc iDateStyle, iTimeStyle;
145           int pos = strFormat.indexOf (",");
146
147           if (pos == -1) {
148             if (!_mapDateTimeStyle.containsKey (strFormat))
149               return new SimpleDateFormat JavaDoc (strFormat, loc);
150
151             iDateStyle = iTimeStyle = (Integer JavaDoc)_mapDateTimeStyle.get (strFormat);
152           } else {
153             String JavaDoc strDateStyle = strFormat.substring (0, pos);
154             String JavaDoc strTimeStyle = strFormat.substring (pos + 1);
155
156             if (!_mapDateTimeStyle.containsKey (strDateStyle) || !_mapDateTimeStyle.containsKey (strTimeStyle))
157               return new SimpleDateFormat JavaDoc (strFormat, loc);
158
159             iDateStyle = (Integer JavaDoc)_mapDateTimeStyle.get (strDateStyle);
160             iTimeStyle = (Integer JavaDoc)_mapDateTimeStyle.get (strTimeStyle);
161           }
162
163           return DateFormat.getDateTimeInstance (iDateStyle.intValue (), iTimeStyle.intValue (), loc);
164         }
165
166       default:
167         throw new JspTagException JavaDoc ("Not a Date/Time JDBC type: " + nType);
168     }
169   }
170
171
172 }
173
Popular Tags