KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > josql > contrib > JoSQLJSPQueryTag


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

15 package org.josql.contrib;
16
17 import java.util.List JavaDoc;
18
19 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
20 import javax.servlet.jsp.tagext.Tag JavaDoc;
21
22 import javax.servlet.jsp.JspException JavaDoc;
23
24 import org.josql.Query;
25 import org.josql.QueryResults;
26
27 /**
28  * Allows a JoSQL Query to be used in a JSP.
29  * <p>
30  * Example:
31  * <pre>
32  * &lt;%@ page import="java.util.Arrays" %>
33  * &lt;%@ page import="java.io.File" %>
34  *
35  * &lt;%@ tablib prefix="josql" uri="josqltaglib" %>
36  *
37  * &lt;josql:query inputList='&lt;%= Arrays.asList (new File ("/home/me/").listFiles ()) %>'
38  * results="queryResults">
39  * SELECT *
40  * FROM java.io.File
41  * WHERE name = '.bashrc'
42  * &lt;/josql:query>
43  * </pre>
44  * <p>
45  * The body of the tag is taken as the statement to execute.
46  * <p>
47  * Note: this class deliberately does NOT extend {@link Query} since doing so would then
48  * prevent the query from being released via the {@link #release()} method.
49  * <p>
50  * The following <b>attributes</b> are supported:
51  * <ul>
52  * <li><b>inputList</b> - specifies the name of an attribute that holds the List of objects
53  * to execute the JoSQL statement against, or is the List of objects. The attribute
54  * allows request-time expressions to be used, i.e. the "rtexprvalue" in the .tld file
55  * is set to <code>true</code>.</li>
56  * <li><b>results</b> - specifies the name of the attribute to hold the results (instance of:
57  * {@link QueryResults}).</li>
58  * </ul>
59  */

60 public class JoSQLJSPQueryTag extends BodyTagSupport JavaDoc
61 {
62
63     private Object JavaDoc inputList = null;
64     private String JavaDoc results = null;
65     private Query query = null;
66
67     /**
68      * Set the name of the attribute that should hold the results.
69      *
70      * @param r The name.
71      */

72     public void setResults (String JavaDoc r)
73     {
74
75     this.results = r;
76
77     }
78
79     /**
80      * Set the input list, i.e. the list of objects to execute the JoSQL
81      * statement against. Can be either a "java.lang.String" which will
82      * indicate the name of an attribute to use, or a "java.util.List"
83      * which will be the list of objects of use.
84      * The list of objects is not gained until the {@link #doEndTag()} method
85      * is called.
86      *
87      * @param l The name or list of objects to execute the statement against.
88      */

89     public void setInputList (Object JavaDoc l)
90     {
91
92     this.inputList = l;
93
94     }
95
96     /**
97      * When called will parse the tag body into a JoSQL statement.
98      *
99      * @return {@link Tag#SKIP_BODY} is returned.
100      * @throws JspException If the tag body cannot be parsed into a JoSQL statement.
101      */

102     public int doAfterBody ()
103                         throws JspException JavaDoc
104     {
105
106     this.query = new Query ();
107
108     String JavaDoc st = this.getBodyContent ().getString ();
109
110     try
111     {
112
113         this.query.parse (st);
114
115     } catch (Exception JavaDoc e) {
116
117         throw new JspException JavaDoc ("Unable to parse statement: " +
118                     st,
119                     e);
120
121     }
122
123     return Tag.SKIP_BODY;
124
125     }
126
127     /**
128      * Execute the statement parsed in {@link #doAfterBody()}.
129      * The list of objects to execute the statement against is first retrieved
130      * using the value specified in {@link #setInputList(Object)}. The list must
131      * not be null and must be a list otherwise an exception is thrown.
132      * <p>
133      * Once executed the results are set as an attribute specified by:
134      * {@link #setResults(String)}.
135      *
136      * @return {@link Tag#EVAL_PAGE} always.
137      * @throws JspException If the list is null, not a list or the statement cannot
138      * be executed against the list of objects.
139      */

140     public int doEndTag ()
141                      throws JspException JavaDoc
142     {
143
144     List JavaDoc l = null;
145
146     if (this.inputList instanceof String JavaDoc)
147     {
148
149         String JavaDoc ln = (String JavaDoc) this.inputList;
150
151         // Get the list.
152
Object JavaDoc o = this.pageContext.findAttribute (ln);
153
154         if (o == null)
155         {
156
157         throw new JspException JavaDoc ("No list with name: " +
158                     ln +
159                     " can be found");
160
161         }
162
163         if (!(o instanceof List JavaDoc))
164         {
165
166         throw new JspException JavaDoc ("Attribute: " +
167                     ln +
168                     " is not an instance of: " +
169                     List JavaDoc.class.getName () +
170                     ", is: " +
171                     o.getClass ().getName ());
172
173         }
174
175         l = (List JavaDoc) o;
176
177     }
178
179     if (this.inputList instanceof List JavaDoc)
180     {
181
182         l = (List JavaDoc) this.inputList;
183
184     }
185
186     if (l == null)
187     {
188
189         throw new JspException JavaDoc ("No input list specified.");
190
191     }
192
193     try
194     {
195
196         QueryResults qr = this.query.execute (l);
197
198         // Set the query results as an attribute.
199
this.pageContext.setAttribute (this.results,
200                        qr);
201
202     } catch (Exception JavaDoc e) {
203
204         throw new JspException JavaDoc ("Unable to execute query with list: " +
205                     this.inputList,
206                     e);
207
208     }
209
210     return Tag.EVAL_PAGE;
211
212     }
213
214     /**
215      * Release the objects we have references to and then call: super.release ().
216      */

217     public void release ()
218     {
219
220     this.inputList = null;
221     this.results = null;
222     this.query = null;
223     
224     super.release ();
225
226     }
227
228 }
229
Popular Tags