KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.jsp.JspTagException JavaDoc;
20 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
21
22 /**
23  * <p>Executes its body if the last ResultSet tag received 0 rows
24  * from the database. You must be after a ResultSet tag and inside
25  * a StatementTag or PreparedStatementTag, or an error will be generated.</p>
26  *
27  * <p>The subclass WasNotEmpty sets the "value" property to false,
28  * and therefore executes its tag body is the ResultSet contained
29  * <i>greater than</i> 0 rows.
30  *
31  * <p>JSP Tag Lib Descriptor
32  * <pre>
33  * &lt;name>wasEmpty&lt;/name>
34  * &lt;tagclass>org.apache.taglibs.dbtags.statement.WasEmptyTag&lt;/tagclass>
35  * &lt;bodycontent>JSP&lt;/bodycontent>
36  * &lt;info>
37  * Executes its body if the last ResultSet tag received 0 rows
38  * from the database. You must be after a ResultSet tag and inside
39  * a StatementTag or PreparedStatementTag, or an error will be generated.
40  * &lt;/info>
41  * </pre>
42  *
43  * @author Morgan Delagrange
44  * @see WasNotEmptyTag
45  */

46 public class WasEmptyTag extends TagSupport JavaDoc {
47
48   private boolean _value = true;
49
50   /**
51    * Sets the necessary boolean value in order to
52    * execute the tag body.
53    *
54    * @param value
55    */

56   public void setValue(boolean value) {
57     _value = value;
58   }
59
60   public int doStartTag() throws JspTagException JavaDoc {
61
62     Integer JavaDoc integer =
63       (Integer JavaDoc) pageContext.getAttribute("org.apache.taglibs.dbtags.resultset.rowcount");
64     
65     if (integer == null) {
66       throw new JspTagException JavaDoc("WasEmpty and WasNotEmpty tags must follow a ResultSet tag.");
67     }
68
69     int rowCount = integer.intValue();
70
71     boolean wasEmpty = true;
72     if (rowCount > 0) {
73       wasEmpty = false;
74     }
75
76     // evaluate the body only if wasEmpty matches the desired
77
// value (true or false)
78
if (wasEmpty == _value) {
79       return EVAL_BODY_INCLUDE;
80     } else {
81       return SKIP_BODY;
82     }
83   }
84
85   public void release() {
86     _value = true;
87   }
88 }
89
90
Popular Tags