KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > databinding > repeater > RepeaterComponent


1 /*
2  * Copyright 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  * $Header:$
17  */

18 package org.apache.beehive.netui.tags.databinding.repeater;
19
20 import java.io.IOException JavaDoc;
21 import javax.servlet.jsp.JspException JavaDoc;
22 import javax.servlet.jsp.JspWriter JavaDoc;
23 import javax.servlet.jsp.tagext.Tag JavaDoc;
24 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
25
26 import org.apache.beehive.netui.tags.AbstractClassicTag;
27 import org.apache.beehive.netui.util.Bundle;
28 import org.apache.beehive.netui.util.logging.Logger;
29
30 /**
31  * <p>
32  * The base class for tags that are part of the {@link Repeater} tag set and participate in the structured nature
33  * of {@link Repeater} rendering. This class provides typed access to the {@link Repeater} tag and
34  * enforces the basic JSP tag parenting requirements of tags that can only be nested within the
35  * {@link Repeater} tag.
36  * </p>
37  */

38 public abstract class RepeaterComponent
39     extends AbstractClassicTag {
40
41     private static final Logger LOGGER = Logger.getInstance(RepeaterComponent.class);
42
43     private Repeater _repeater = null;
44
45     /**
46      * <p>
47      * Starts a tag's lifecycle. This method performs several operations before
48      * invoking the @see renderStartTag(int) method. In order, these stages are:
49      * <ol>
50      * <li>@see verifyAttributes()</li>
51      * <li>@see renderStartTag(int)</li>
52      * </ol>
53      * <br/>
54      * Any errors that occur before calling @see renderStartTag(int) are reported
55      * in the page.
56      * </p>
57      * @return the value returned from calling @see renderStartTag(int), which can be
58      * any value that can be returned from the @see javax.servlet.jsp.tagext.TagSupport
59      * class. If an error occurs, the tag returns SKIP_BODY.
60      */

61     public int doStartTag()
62         throws JspException JavaDoc {
63         
64         int ret = SKIP_BODY;
65         try {
66             Tag JavaDoc parent = getParent();
67             Class JavaDoc validContainer = Repeater.class;
68
69             if((validContainer != null && parent == null) ||
70                     validContainer != null && !validContainer.isAssignableFrom(parent.getClass())) {
71                 if(LOGGER.isErrorEnabled()) {
72                     LOGGER.error("A tag of type \"" + getClass().getName() + "\" must be nested within a tag of type \"" + Repeater.class.getName() + "\"");
73                 }
74
75                 String JavaDoc msg = Bundle.getString("Tags_RepeaterComponent_invalidParent", new Object JavaDoc[]{getClass().getName(), Repeater.class.getName()});
76                 registerTagError(msg, null);
77             }
78
79             if(LOGGER.isDebugEnabled())
80                 LOGGER.debug("verifyStructure: hasErrors=" + hasErrors());
81
82             if(hasErrors()) {
83                 reportErrors();
84                 return SKIP_BODY;
85             }
86
87             /* the repeater property needs to be populated *before* verifyAttributes() is called */
88             _repeater = (Repeater)getParent();
89
90             verifyAttributes();
91
92             _repeater.registerChildTag(this);
93
94             if(LOGGER.isDebugEnabled())
95                 LOGGER.debug("verifyAttributes: hasErrors=" + hasErrors());
96
97             if(hasErrors()) {
98                 reportErrors();
99                 return SKIP_BODY;
100             }
101
102             if(shouldRender())
103                 ret = EVAL_BODY_BUFFERED;
104             else
105                 ret = SKIP_BODY;
106         }
107         catch(Exception JavaDoc e) {
108             String JavaDoc msg = Bundle.getString("Tags_RepeaterComponent_startTagError", new Object JavaDoc[]{getTagName(), e});
109
110             if(LOGGER.isErrorEnabled())
111                 LOGGER.error("An error occurred rendering the startTag of the tag \"" + getTagName() + "\". Cause: " + e, e);
112
113             registerTagError(msg, e);
114             reportErrors();
115             ret = SKIP_BODY;
116         }
117
118         return ret;
119     }
120
121     /**
122      * Default implementation of this JSP lifecycle method.
123      *
124      * @return SKIP_BODY
125      */

126     public int doAfterBody()
127             throws JspException JavaDoc {
128         assert _repeater != null;
129         _repeater.addContent(bodyContent.getString());
130         return SKIP_BODY;
131     }
132
133     /**
134      * Ends a tag's lifecycle. This call is a wrapper around the @see renderEndTag(int) call
135      * that allows a tag directly contained in a repeating tag to act based on the state
136      * of the parent.
137      *
138      * @return EVAL_PAGE
139      * @throws JspException if an error that occurred that could not be reported to the page
140      */

141     public int doEndTag()
142             throws JspException JavaDoc {
143         /*
144            note, this does not report errors because the <repeater> tag itself does
145            the error reporting
146          */

147         if(hasErrors()) {
148             /* bug: this doesn't report errors */
149             localRelease();
150             return EVAL_PAGE;
151         }
152
153         int ret = EVAL_PAGE;
154         try {
155             int state = _repeater.getRenderState();
156             ret = renderEndTag(state);
157         }
158         catch(Exception JavaDoc e) {
159             String JavaDoc msg = Bundle.getString("Tags_RepeaterComponent_endTagError", new Object JavaDoc[]{getTagName(), e.toString()});
160             registerTagError(msg, e);
161             reportErrors();
162             ret = EVAL_PAGE;
163         }
164
165         localRelease();
166         return ret;
167     }
168
169     /**
170      * Get the {@link Repeater} parent of this tag.
171      *
172      * @return the {@link Repeater} parent of this tag
173      */

174     protected final Repeater getRepeater() {
175         return _repeater;
176     }
177
178     /**
179      * Reset all of the fields of this tag.
180      */

181     protected void localRelease() {
182         super.localRelease();
183         _repeater = null;
184     }
185
186     protected abstract boolean shouldRender();
187
188     protected void verifyAttributes()
189             throws JspException JavaDoc {
190     }
191
192     protected int renderEndTag(int state)
193             throws JspException JavaDoc {
194         return EVAL_PAGE;
195     }
196 }
197
Popular Tags