KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > tag > common > sql > DateParamTagSupport


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.standard.tag.common.sql;
17
18 import javax.servlet.jsp.JspException JavaDoc;
19 import javax.servlet.jsp.JspTagException JavaDoc;
20 import javax.servlet.jsp.jstl.sql.SQLExecutionTag;
21 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
22
23 import org.apache.taglibs.standard.resources.Resources;
24
25
26 /**
27  * <p>Tag handler for &lt;Param&gt; in JSTL, used to set
28  * parameter values for a SQL statement.</p>
29  *
30  * @author Justyna Horwat
31  */

32
33 public abstract class DateParamTagSupport extends TagSupport JavaDoc {
34
35     //*********************************************************************
36
// Private constants
37

38     private static final String JavaDoc TIMESTAMP_TYPE = "timestamp";
39     private static final String JavaDoc TIME_TYPE = "time";
40     private static final String JavaDoc DATE_TYPE = "date";
41     
42
43     //*********************************************************************
44
// Protected state
45

46     protected String JavaDoc type;
47     protected java.util.Date JavaDoc value;
48
49
50     //*********************************************************************
51
// Constructor
52

53     public DateParamTagSupport() {
54         super();
55         init();
56     }
57
58     private void init() {
59         value = null;
60         type = null;
61     }
62
63
64     //*********************************************************************
65
// Tag logic
66

67     public int doEndTag() throws JspException JavaDoc {
68     SQLExecutionTag parent = (SQLExecutionTag)
69         findAncestorWithClass(this, SQLExecutionTag.class);
70     if (parent == null) {
71         throw new JspTagException JavaDoc(
72                 Resources.getMessage("SQL_PARAM_OUTSIDE_PARENT"));
73     }
74
75         if (value != null) {
76             convertValue();
77         }
78
79     parent.addSQLParameter(value);
80     return EVAL_PAGE;
81     }
82
83
84     //*********************************************************************
85
// Private utility methods
86

87     private void convertValue() throws JspException JavaDoc {
88
89     if ((type == null) || (type.equalsIgnoreCase(TIMESTAMP_TYPE))) {
90         if (!(value instanceof java.sql.Timestamp JavaDoc)) {
91         value = new java.sql.Timestamp JavaDoc(value.getTime());
92         }
93     } else if (type.equalsIgnoreCase(TIME_TYPE)) {
94         if (!(value instanceof java.sql.Time JavaDoc)) {
95         value = new java.sql.Time JavaDoc(value.getTime());
96         }
97     } else if (type.equalsIgnoreCase(DATE_TYPE)) {
98         if (!(value instanceof java.sql.Date JavaDoc)) {
99         value = new java.sql.Date JavaDoc(value.getTime());
100         }
101     } else {
102         throw new JspException JavaDoc(
103                 Resources.getMessage("SQL_DATE_PARAM_INVALID_TYPE", type));
104     }
105     }
106 }
107
Popular Tags