KickJava   Java API By Example, From Geeks To Geeks.

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


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

17 package org.apache.taglibs.dbtags.resultset;
18
19 import java.sql.ResultSet JavaDoc;
20 import java.sql.ResultSetMetaData JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.util.Locale JavaDoc;
23
24 import javax.servlet.jsp.JspTagException JavaDoc;
25 import javax.servlet.jsp.PageContext JavaDoc;
26 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
27
28 /**
29  * Base class for all the getter tags in the resultset package.
30  *
31  * @author Morgan Delagrange
32  * @author Marius Scurtescu
33  */

34 public class BaseGetterTag extends TagSupport JavaDoc {
35
36   // subclasses should use getPosition to access
37
// the column via getString(int) etc.
38
private int _position = -1;
39   private String JavaDoc _name = null;
40
41   protected String JavaDoc _attributeName = null;
42   protected String JavaDoc _scope = "page";
43   protected ResultSetTag _tag = null;
44   protected ResultSetMetaData JavaDoc _metaData = null;
45   protected String JavaDoc _locale = null;
46
47   public void setLocale (String JavaDoc strLocale)
48   {
49     _locale = strLocale;
50   }
51
52   /**
53    * Sets the column number of the result set.
54    *
55    * @param position column index
56    */

57   public void setPosition(int position) {
58     _position = position;
59   }
60
61   /**
62    * Gets the column number for the result set.
63    *
64    * @return column position (1,2,3,etc.)
65    */

66   public int getPosition() throws JspTagException JavaDoc{
67     // if the column was set according to its name,
68
// figure out its position
69
if (_position == -1) {
70       _position = getColumnNumber(_name);
71     }
72     return _position;
73   }
74
75   /**
76    * Sets the column number of the result set.
77    *
78    * @param position column index
79    */

80   public void setPosition(String JavaDoc strPosition) throws JspTagException JavaDoc
81   {
82     try {
83       _position = Integer.parseInt (strPosition);
84     } catch (NumberFormatException JavaDoc ex) {
85       throw new JspTagException JavaDoc ("The 'position' attribute must be an int: " + ex.getMessage());
86     }
87   }
88
89   /**
90    * Sets the column name if the result set.
91    *
92    * @param strName The column name.
93    */

94   public void setColName (String JavaDoc strName)
95   {
96     _name = strName;
97   }
98
99   /**
100    * Name of the attribute.
101    *
102    * @param attributeName
103    * attribute name
104    */

105   public void setTo(String JavaDoc attributeName) {
106     _attributeName = attributeName;
107   }
108
109   /**
110    * Scope of the attribute.
111    *
112    * @param scope scope (page | request | session | application)
113    */

114   public void setScope(String JavaDoc scope) {
115     _scope = scope;
116   }
117
118   /**
119    * Set an attribute to the specified scope.
120    *
121    * @param name name of the attribute
122    * @param object the attribute
123    * @param scope attribute scope (page | request | session | application)
124    */

125   protected void setAttribute(String JavaDoc name, Object JavaDoc object, String JavaDoc scope) {
126     if (scope.equals("request")) {
127       pageContext.setAttribute(name, object, PageContext.REQUEST_SCOPE);
128     } else if (scope.equals("application")) {
129       pageContext.setAttribute(name, object, PageContext.APPLICATION_SCOPE);
130     } else if (scope.equals("session")) {
131       pageContext.setAttribute(name, object, PageContext.SESSION_SCOPE);
132     } else {
133       pageContext.setAttribute(name, object);
134     }
135   }
136
137   /**
138    * Get the ResultSet object from the enclosing resultset tag
139    *
140    * @return ResultSet of the resultset tag
141    */

142   protected ResultSet JavaDoc getResultSet() throws JspTagException JavaDoc {
143     if (_tag == null) {
144       _tag = getResultSetTag();
145     }
146     return _tag.getResultSet();
147   }
148
149   /**
150    * Get the MetaData object for this result set
151    *
152    * @return Meta data object
153    * @exception JspTagException
154    */

155   protected ResultSetMetaData JavaDoc getMetaData() throws JspTagException JavaDoc {
156     if (_metaData == null) {
157
158       if (_tag == null) {
159         _tag = getResultSetTag();
160       }
161
162       try {
163         _metaData = _tag.getResultSet().getMetaData();
164       } catch (SQLException JavaDoc e) {
165         throw new JspTagException JavaDoc(e.toString());
166       }
167     }
168     return _metaData;
169   }
170
171   /**
172    * Get the parent result set tag
173    *
174    * @return ResultSetTag
175    * @exception JspTagException
176    */

177   protected ResultSetTag getResultSetTag() throws JspTagException JavaDoc{
178     try {
179       ResultSetTag rsetTag =
180       (ResultSetTag) findAncestorWithClass(this, Class.forName("org.apache.taglibs.dbtags.resultset.ResultSetTag"));
181       return rsetTag;
182     } catch (ClassNotFoundException JavaDoc e) {
183       throw new JspTagException JavaDoc(e.toString());
184     }
185   }
186
187   // get the position for a named column
188
// needed for the metadata methods
189
private int getColumnNumber (String JavaDoc strName)
190   throws JspTagException JavaDoc {
191
192     ResultSetMetaData JavaDoc meta = getMetaData();
193
194     try {
195       int cntColumn = meta.getColumnCount ();
196
197       for (int i = 1; i <= cntColumn; i++) {
198         if (strName.equalsIgnoreCase (meta.getColumnName (i))) {
199           return i;
200         }
201       }
202     } catch (SQLException JavaDoc e) {
203       throw new JspTagException JavaDoc(e.toString());
204     }
205
206     throw new JspTagException JavaDoc("Could not find column named " + strName);
207
208   }
209
210
211   /**
212    * Create a Locale for the formatting of dates, times,
213    * and numbers.
214    *
215    * @param strLocale
216    * @return the indicated Locale
217    */

218   protected Locale JavaDoc getLocale (String JavaDoc strLocale)
219   {
220     if (strLocale == null)
221       return Locale.getDefault ();
222
223     int pos1 = strLocale.indexOf ("_");
224
225     if (pos1 == -1)
226       return new Locale JavaDoc (strLocale, "");
227
228     String JavaDoc strLanguage = strLocale.substring (0, pos1);
229     int pos2 = strLocale.indexOf ("_", pos1 + 1);
230
231     if (pos2 == -1)
232       return new Locale JavaDoc (strLanguage, strLocale.substring (pos1 + 1));
233
234     return new Locale JavaDoc (strLanguage, strLocale.substring (pos1 + 1, pos2), strLocale.substring (pos2 + 1));
235   }
236
237
238   /**
239    * If I understand the new JSP spec correctly, release()
240    * is NOT called between invocations of a cached taglib,
241    * so I guess we do it manually or write a new method.
242    *
243    * @return EVAL_PAGE constant
244    */

245   public int doEndTag() {
246     release();
247     return EVAL_PAGE;
248   }
249
250   public void release() {
251     _position = -1;
252     _attributeName = null;
253     _name = null;
254     _scope = "page";
255     _tag = null;
256     _metaData = null;
257     _locale = null;
258   }
259 }
260
Popular Tags