KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > el > SqlDateParamTag


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jstl.el;
31
32 import com.caucho.el.Expr;
33 import com.caucho.util.L10N;
34
35 import javax.el.ELContext;
36 import javax.servlet.jsp.JspException JavaDoc;
37 import javax.servlet.jsp.jstl.sql.SQLExecutionTag;
38 import javax.servlet.jsp.tagext.Tag JavaDoc;
39 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
40
41 /**
42  * Looks up an i18n message from a bundle and prints it.
43  */

44 public class SqlDateParamTag extends TagSupport JavaDoc {
45   private static L10N L = new L10N(SqlDateParamTag.class);
46   
47   private Expr _valueExpr;
48   private Expr _typeExpr;
49
50   /**
51    * Sets the value
52    *
53    * @param value the JSP-EL expression for the value.
54    */

55   public void setValue(Expr value)
56   {
57     _valueExpr = value;
58   }
59
60   /**
61    * Sets the type
62    *
63    * @param type the JSP-EL expression for the type.
64    */

65   public void setType(Expr type)
66   {
67     _typeExpr = type;
68   }
69
70   /**
71    * Process the tag.
72    */

73   public int doStartTag()
74     throws JspException JavaDoc
75   {
76     ELContext env = pageContext.getELContext();
77     
78     Object JavaDoc value = _valueExpr.evalObject(env);
79
80     long time = 0;
81
82     Object JavaDoc result = null;
83
84     if (value == null) {
85     }
86     else if (value instanceof Number JavaDoc)
87       time = ((Number JavaDoc) value).longValue();
88     else if (value instanceof java.util.Date JavaDoc)
89       time = ((java.util.Date JavaDoc) value).getTime();
90     else if (value instanceof java.sql.Date JavaDoc)
91       time = ((java.sql.Date JavaDoc) value).getTime();
92     else
93       throw new JspException JavaDoc(L.l("sql:dateParam requires at date at `{0}'", value));
94
95     if (value == null)
96       result = null;
97     else if (_typeExpr == null)
98       result = new java.sql.Timestamp JavaDoc(time);
99     else {
100       String JavaDoc type = _typeExpr.evalString(env);
101
102       if (type.equals("time"))
103     result = new java.sql.Time JavaDoc(time);
104       else if (type.equals("date"))
105     result = new java.sql.Date JavaDoc(time);
106       else
107     result = new java.sql.Timestamp JavaDoc(time);
108     }
109
110     Tag parent = getParent();
111     for (;
112      parent != null && ! (parent instanceof SQLExecutionTag);
113      parent = parent.getParent()) {
114     }
115
116     if (parent == null)
117       throw new JspException JavaDoc(L.l("sql:dateParam requires sql:query parent."));
118
119     SQLExecutionTag tag = (SQLExecutionTag) parent;
120
121     tag.addSQLParameter(result);
122     
123     return SKIP_BODY;
124   }
125 }
126
Popular Tags