KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > template > IncludeSection


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.template;
19
20 import org.apache.beehive.netui.util.internal.InternalStringBuilder;
21
22 import org.apache.beehive.netui.tags.AbstractClassicTag;
23 import org.apache.beehive.netui.util.Bundle;
24 import org.apache.beehive.netui.util.logging.Logger;
25
26 import javax.servlet.RequestDispatcher JavaDoc;
27 import javax.servlet.ServletException JavaDoc;
28 import javax.servlet.ServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31 import javax.servlet.jsp.JspException JavaDoc;
32 import javax.servlet.jsp.JspWriter JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.Writer JavaDoc;
35 import java.net.MalformedURLException JavaDoc;
36 import java.net.URL JavaDoc;
37
38 /**
39  * Used within a template JSP page to define a placeholder for section content.
40  * Within a template, one or more sections are defined within the overall
41  * structure of the page. Each section has a unique name identifying it.
42  * The content page, through the <code>Section</code> tag, provides content
43  * that is included into the defined sections.
44  * <p>
45  * All content found within the body of the <code>IncludeSection</code>
46  * is ignored.
47
48  * @jsptagref.tagdescription
49  * Defines a content placeholder within a template.
50  * Each placeholder must have a unique name identifying it.
51  * Different content pages adopt the template page, set properties
52  * on its placeholders (using the {@link Section} tag), and render the
53  * completed HTML in the browser.
54  *
55  * <p>For example, a template page can use the &lt;netui-template:includeSection> tag to
56  * define a content placeholder.
57  *
58  * <p><b>In the template JSP page...</b>
59  *
60  * <pre> &lt;table>
61  * &lt;tr>
62  * &lt;td colspan="3">
63  * &lt;netui-template:includeSection name="tableHeader"/>
64  * &lt;/td>
65  * &lt;/tr></pre>
66  *
67  * <p>Then a content page can set HTML content in the placeholder using the {@link Section} tag.
68  *
69  * <p><b>In a content JSP page...</b>
70  *
71  * <pre> &lt;netui-template:section name="tableHeader">
72  * &lt;h1>HEADER TEXT&lt;/h1>
73  * &lt;/netui-template:section></pre>
74  *
75  * <p>The HTML rendered in the browser will appear as follows.
76  *
77  * <pre> &lt;table>
78  * &lt;tr>
79  * &lt;td colspan="3">
80  * &lt;h1>HEADER TEXT&lt;/h1>
81  * &lt;/td>
82  * &lt;/tr></pre>
83  *
84  * <p>If the content page does not define content to be placed in the placeholder, then
85  * the <code>defaultPage</code> attribute will be used. The
86  * <code>defaultPage</code> attribute points at a stand-alone JSP page. The entire contents of the page
87  * will be placed in the placeholder, after any Java elements, such as scriptlets have been resolved.
88  *
89  * @example
90  * In this sample a &lt;netui-template:includeSection> tag defines a place holder for a
91  * table row
92  *
93  * <pre> &lt;tr>
94  * &lt;netui-template:includeSection name="rowPlaceholder" defaultPage="defaultPage.jsp"/>
95  * &lt;/tr></pre>
96  *
97  * <p>If there is no content page that sets content into this placeholder using a &lt;netui-template:section>
98  * tag, then the entire contents of the defaultPage.jsp will be used.
99  * Assume that the defaultPage.jsp appears as follows.
100  *
101  * <pre> &lt;p>&lt;%= 1 + 1 %>&lt;/p></pre>
102  *
103  * Then the HTML rendered in the browser will appear as follows. Note that the Java scriptlet
104  * <code>&lt;%= 1 + 1 %></code> has been resolved to the value <code>2</code>.
105  *
106  * <pre> &lt;tr>
107  * &lt;p>2&lt;/p>
108  * &lt;/tr></pre>
109  *
110  * @netui:tag name="includeSection"
111  * description="Include this tag in a template file to mark out content that will be used in another JSP page."
112  */

113 public class IncludeSection extends AbstractClassicTag
114         implements TemplateConstants
115 {
116     private static final Logger logger = Logger.getInstance(IncludeSection.class);
117
118     /**
119      * The name of the section.
120      */

121     private String JavaDoc _name;
122
123     /**
124      * The name of a JSP that will act as a default page
125      */

126     private String JavaDoc _default;
127
128     /**
129      * Returns the name of the Tag. This is used to
130      * identify the type of tag reporting errors.
131      */

132     public String JavaDoc getTagName() {
133         return "IncludeSection";
134     }
135
136     /**
137      * Sets the name of the section. This name must be unique within
138      * the template page.
139      * @param name The name of the defined section within the template.
140      * This name must be unique within the template.
141      *
142      * @jsptagref.attributedescription
143      * The name of the section. This name must be unique within the template page.
144      *
145      * @jsptagref.databindable false
146      *
147      * @jsptagref.attributesyntaxvalue <i>string_name</i>
148      *
149      * @netui:attribute required="true" rtexprvalue="true"
150      * description="The name of the section. This name must be unique within the template page."
151      */

152     public void setName(String JavaDoc name) {
153         _name = name;
154     }
155
156     /**
157      * Sets a default JSP page to provide content for the section if
158      * the content page does not define the content.
159      * @param defaultPage a URL identifying a JSP or HTML page
160      * providing default content to the defined section.
161      *
162      * @jsptagref.attributedescription
163      * A default JSP page to provide content for the placeholder if
164      * the content page fails to define the content.
165      *
166      * @jsptagref.databindable false
167      *
168      * @jsptagref.attributesyntaxvalue <i>string_defaultPage</i>
169      *
170      * @netui:attribute required="false" rtexprvalue="true"
171      * description="A default JSP page to provide content for the placeholder if
172      * the content page fails to define the content."
173      */

174     public void setDefaultPage(String JavaDoc defaultPage) {
175         _default = defaultPage;
176     }
177
178     /**
179      * Renders the content of the section into the template. Errors
180      * are reported inline within the template in development
181      * mode. If no sections are defined an error is reported. If
182      * a section is not defined and no default URL is provided an
183      * error is reported.
184      * @return SKIP_BODY to skip any content found in the tag.
185      * @throws JspException on Errors.
186      */

187     public int doStartTag()
188             throws JspException JavaDoc {
189         ServletRequest JavaDoc req = pageContext.getRequest();
190         Template.TemplateContext tc = (Template.TemplateContext)
191                 req.getAttribute(TEMPLATE_SECTIONS);
192         if (tc == null) {
193
194             String JavaDoc s = Bundle.getString("Tags_TemplateContextMissing");
195             // report the error. If this returns a value then we throw an
196
// exception
197
logger.warn(stripBold(s));
198             registerTagError(s,null);
199             reportErrors();
200             localRelease();
201             return SKIP_BODY;
202         }
203         if (tc.secs == null) {
204             if (_default != null) {
205                 return callDefault(req);
206             }
207             String JavaDoc s = Bundle.getString("Tags_TemplateSectionMissing",
208                     new Object JavaDoc[]{_name});
209             // report the error. If this returns a value then we throw an
210
// exception
211
logger.warn(stripBold(s));
212             registerTagError(s,null);
213             reportErrors();
214             localRelease();
215             return SKIP_BODY;
216         }
217
218         String JavaDoc val = (String JavaDoc) tc.secs.get(_name);
219         if (val == null) {
220
221             if (_default == null) {
222                 String JavaDoc s = Bundle.getString("Tags_TemplateSectionMissing",
223                         new Object JavaDoc[]{_name});
224                 logger.warn(stripBold(s));
225
226                 // report the error. If this returns a value then we throw an
227
// exception
228
registerTagError(s,null);
229                 reportErrors();
230                 localRelease();
231                 return SKIP_BODY;
232             }
233             return callDefault(req);
234
235         }
236
237         try {
238             Writer JavaDoc out = pageContext.getOut();
239             out.write(val);
240         }
241         catch (IOException JavaDoc e) {
242             String JavaDoc reason = Bundle.getString("TempExcp_WritingContent");
243             String JavaDoc s = Bundle.getString("TempExcp_Except",
244                     new Object JavaDoc[]{"IOException",
245                                  reason});
246             logger.error(s);
247             throw new JspException JavaDoc(s,e);
248         }
249         // continue to evalue the page...(This should be a template)
250
localRelease();
251         return SKIP_BODY;
252     }
253
254     private int callDefault(ServletRequest JavaDoc req)
255             throws JspException JavaDoc {
256         if (!defaultExists()) {
257             String JavaDoc s = Bundle.getString("TempExcp_MissingDefaultPage",
258                     new Object JavaDoc[]{_default});
259             logger.error(s);
260             registerTagError(s,null);
261             reportErrors();
262             localRelease();
263             return SKIP_BODY;
264         }
265
266         try {
267             HttpServletResponse JavaDoc resp = (HttpServletResponse JavaDoc)
268                     pageContext.getResponse();
269
270             RequestDispatcher JavaDoc rd = req.getRequestDispatcher(_default);
271             // This is now causing an IOException in Weblogic. This code was added because Tomcat didn't work
272
// without it. I'm going to catch and ignore the IOException because this really should affect
273
// things.
274
try {
275                 JspWriter JavaDoc out = pageContext.getOut();
276                 out.flush();
277             }
278             catch (IOException JavaDoc ignore) {}
279             rd.include(req,resp);
280             localRelease();
281             return SKIP_BODY;
282         }
283         catch (IOException JavaDoc e) {
284             String JavaDoc s = Bundle.getString("TempExcp_ExceptIncludeDefault",
285                     new Object JavaDoc[]{"IOException",
286                                  _default});
287             logger.error(s,e);
288             throw new JspException JavaDoc(s,e);
289         }
290         catch (ServletException JavaDoc se) {
291             String JavaDoc s = Bundle.getString("TempExcp_ExceptIncludeDefault",
292                     new Object JavaDoc[]{"ServletException",
293                                  _default});
294             logger.error(s,se);
295             throw new JspException JavaDoc(s,se);
296         }
297     }
298
299     private boolean defaultExists()
300         throws JspException JavaDoc
301     {
302         HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) pageContext.getRequest();
303         String JavaDoc realURI = Template.getRealURI(req,_default);
304         try {
305             URL JavaDoc uri = pageContext.getServletContext().getResource(realURI);
306             return (uri != null);
307         }
308         catch (MalformedURLException JavaDoc e) {
309            String JavaDoc s = Bundle.getString("TempExcp_ExceptIncludeDefault",
310                     new Object JavaDoc[]{"MalformedURLException",
311                                  _default});
312             logger.error(s,e);
313             throw new JspException JavaDoc(s,e);
314         }
315     }
316
317     /**
318      * Resets all of the fields of the tag.
319      */

320     protected void localRelease() {
321         super.localRelease();
322         _name = null;
323         _default = null;
324     }
325
326     /**
327      * This will strip any html out of a warning
328      */

329     static String JavaDoc stripBold(String JavaDoc in) {
330         String JavaDoc boldStart = "<b>";
331         String JavaDoc boldEnd = "</b>";
332         int pos = in.indexOf(boldStart);
333         if (pos == -1)
334             return in;
335         InternalStringBuilder sb = new InternalStringBuilder(in.substring(0,pos));
336         int fill = pos+boldStart.length();
337         pos = in.indexOf(boldEnd,fill);
338         if (pos == -1)
339             return in;
340         sb.append(in.substring(fill,pos));
341         pos += boldEnd.length();
342         sb.append(in.substring(pos));
343         return sb.toString();
344     }
345 }
346
Popular Tags