KickJava   Java API By Example, From Geeks To Geeks.

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


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.tags.javascript.IScriptReporter;
24 import org.apache.beehive.netui.tags.javascript.ScriptRequestState;
25 import org.apache.beehive.netui.tags.divpanel.DivPanel;
26 import org.apache.beehive.netui.tags.databinding.repeater.Repeater;
27 import org.apache.beehive.netui.util.Bundle;
28
29 import javax.servlet.ServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.jsp.JspException JavaDoc;
32 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
33 import javax.servlet.jsp.tagext.Tag JavaDoc;
34 import java.util.HashMap JavaDoc;
35
36 import org.apache.struts.util.ResponseUtils;
37
38 /**
39  * Used within a content page to provide content for a placeholder
40  * section defined within a template. The body content of the tag
41  * is passed to the <code>IncludeSection</code> tag in the template
42  * providing the content for that section.
43  * If the <code>name</code> attribute matches a
44  * <code>name</code> attribute on a
45  * <code>IncludeSection</code> tag in the template, the body
46  * content of this tag will be rendered.
47
48  * @jsptagref.tagdescription
49  * Sets HTML content inside placeholders defined by a
50  * {@link IncludeSection} tag.
51  *
52  * <p>The &lt;netui-template:section> tag must have a parent
53  * {@link Template} tag.
54  *
55  * <p>The &lt;netui-template:section> tag appears in content pages, which adopt a template page,
56  * set properties on the template's placeholders
57  * (using this &lt;netui-template:section> tag),
58  * and render the completed HTML in the browser.
59  *
60  * <p>For content to be placed in the placeholder, the &lt;netui-template:section> and
61  * &lt;netui-template:includeSection> tags must have matching <code>name</code> attributes.
62  *
63  * <p>For example, assume a template page defines the following content placeholder.
64  *
65  * <p><b>In the template JSP page...</b>
66  *
67  * <pre> &lt;table>
68  * &lt;tr>
69  * &lt;td colspan="3">
70  * &lt;netui-template:includeSection name="tableHeader"/>
71  * &lt;/td>
72  * &lt;/tr></pre>
73  *
74  * <p>Then a content page can set HTML content in the placeholder using the &lt;netui-template:section> tag.
75  *
76  * <p><b>In a content JSP page...</b>
77  *
78  * <pre> &lt;netui-template:section name="tableHeader">
79  * &lt;h1>HEADER TEXT&lt;/h1>
80  * &lt;/netui-template:section></pre>
81  *
82  * <p>The HTML rendered in the browser will appear as follows.
83  *
84  * <pre> &lt;table>
85  * &lt;tr>
86  * &lt;td colspan="3">
87  * &lt;h1>HEADER TEXT&lt;/h1>
88  * &lt;/td>
89  * &lt;/tr></pre>
90  *
91  * @example
92  * Assume a &lt;netui-template:includeSection> tag defines a content placeholder inside a
93  * table row
94  *
95  * <pre> &lt;tr>
96  * &lt;netui-template:includeSection name="rowPlaceholder"/>
97  * &lt;/tr></pre>
98  *
99  * <p>A content page can set content into the placeholder using the &lt;netui-template:section>
100  * tag as follows.
101  *
102  * <pre> &lt;netui-template:section name="rowPlaceHolder">
103  * &lt;td>&lt;p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
104  * sed diam nonummy nibh euismod tincidunt ut laoreet dolore
105  * magna aliquam erat volutpat. Ut wisi enim ad minim veniam,
106  * quis nostrud exerci tation ullamcorper suscipit lobortis nisl
107  * ut aliquip ex ea commodo consequat.&lt;/p>&lt;/td>
108  * &lt;/netui-template:section></pre>
109  *
110  * The HTML rendered in the browser will appear as follows.
111  *
112  * <pre> &lt;tr>
113  * &lt;td>&lt;p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit,
114  * sed diam nonummy nibh euismod tincidunt ut laoreet dolore
115  * magna aliquam erat volutpat. Ut wisi enim ad minim veniam,
116  * quis nostrud exerci tation ullamcorper suscipit lobortis nisl
117  * ut aliquip ex ea commodo consequat.&lt;/p>&lt;/td>
118  * &lt;/tr></pre>
119  *
120  * @netui:tag name="section"
121  * description="Use this tag to mark out content to replace a netui-template:includeSection within a template file."
122  */

123 public class Section extends AbstractClassicTag
124         implements TemplateConstants
125 {
126     /**
127      * The name of the section.
128      */

129     private String JavaDoc _name;
130
131     /**
132      * Is the section visible
133      */

134     private boolean _visible = true;
135
136     /**
137      * Returns the name of the Tag. This is used to
138      * identify the type of tag reporting errors.
139      * @return a constant string representing the name of the tag.
140      */

141     public String JavaDoc getTagName() {
142         return "Section";
143     }
144
145     /**
146      * Sets the name of the placeholder section defined in
147      * the template that this tag is providing content for.
148      * This name is matched against the <code>IncludeSection</code>
149      * name. If the names match, the content of this tag will be
150      * rendered within the template's section.
151      * @param name The name of an <code>IncludeSection<code> the
152      * this tag is providing content for.
153      *
154      * @jsptagref.attributedescription
155      * The name of the content to fill the placeholder.
156      * This name is matched against the &lt;netui-template:includeSection>
157      * name. If the names match, the content of this tag will be
158      * rendered within the template's placeholder.
159      *
160      * @jsptagref.databindable false
161      *
162      * @jsptagref.attributesyntaxvalue <i>string_name</i>
163      *
164      * @netui:attribute required="true" rtexprvalue="true"
165      * description="The name of the content to fill the placeholder."
166      */

167     public void setName(String JavaDoc name) {
168         _name = name;
169     }
170
171     /**
172      * Sets the visible state of the tag.
173      * @param visible <code>Boolean</code> value representing the visible state.
174      *
175      * @jsptagref.attributedescription
176      * Boolean. Determines if the section is visible.
177      *
178      * @jsptagref.databindable false
179      *
180      * @jsptagref.attributesyntaxvalue <i>boolean_literal_visible</i>
181      *
182      * @netui:attribute required="false" rtexprvalue="true" type="boolean"
183      * description="Determines if the section is visible."
184      */

185     public void setVisible(boolean visible) {
186         _visible = visible;
187     }
188
189     /**
190      * Causes the content of the section to be rendered into a buffer.
191      * @return SKIP_BODY if the visible state is <code>false</code>,
192      * otherwise EVAL_BODY_BUFFERED to cause the body content to be buffered.
193      * @throws JspException if there are errors.
194      */

195     public int doStartTag()
196             throws JspException JavaDoc {
197
198         if (!_visible)
199             return SKIP_BODY;
200
201         // If the parent is a DivPanel then this section will be inlined.
202
Tag parentTag = getParent();
203         // If the parent tag is a repeater then we must insure that the parent of that is a DivPanel
204
if (parentTag instanceof Repeater) {
205             parentTag = parentTag.getParent();
206             if (!(parentTag instanceof DivPanel)) {
207                 String JavaDoc s = Bundle.getString("Temp_SectionInRepeater");
208                 registerTagError(s,null);
209                 reportErrors();
210                 localRelease();
211                 return EVAL_PAGE;
212              }
213         }
214         if (parentTag instanceof DivPanel) {
215             InternalStringBuilder results = new InternalStringBuilder(128);
216             results.append("<div ");
217             renderTagId(results);
218             if (hasErrors()) {
219                 reportErrors();
220                 localRelease();
221                 return EVAL_PAGE;
222             }
223             results.append(">");
224             ResponseUtils.write(pageContext,results.toString());
225             return EVAL_BODY_BUFFERED;
226         }
227
228         return EVAL_BODY_BUFFERED;
229     }
230
231     /**
232      * Stores the buffered body content into the <code>TEMPLATE_SECTIONS
233      * HashMap</code>. The buffered body is
234      * accessed by the template page to obtain
235      * the content for <code>IncludeSection</code> tags.
236      * @return EVAL_PAGE to continue evaluating the page.
237      * @throws JspException on error.
238      */

239     public int doEndTag()
240             throws JspException JavaDoc {
241
242         // If the parent is a DivPanel then this section will be inlined.
243
Tag parentTag = getParent();
244         if (parentTag instanceof Repeater) {
245              parentTag = parentTag.getParent();
246         }
247          if (parentTag instanceof DivPanel) {
248             return processDivPanel();
249         }
250         assert(parentTag instanceof Template);
251         return processTemplate();
252     }
253
254     /**
255      * Resets all of the fields of the tag.
256      */

257     protected void localRelease() {
258         super.localRelease();
259         _name = null;
260         _visible = true;
261     }
262
263     /**
264      * This method will process the section in the context of a DivPanel
265      * @return returns the integer code doEndTag() will return.
266      */

267     private int processDivPanel()
268             throws JspException JavaDoc {
269
270         if (!_visible)
271             return EVAL_PAGE;
272
273         if (hasErrors()) {
274             reportErrors();
275             localRelease();
276             return EVAL_PAGE;
277         }
278
279         BodyContent JavaDoc bc = getBodyContent();
280         String JavaDoc content = (bc != null) ? bc.getString() : "";
281         ResponseUtils.write(pageContext,content);
282         ResponseUtils.write(pageContext,"</div>");
283         localRelease();
284         return EVAL_PAGE;
285     }
286
287     /**
288      * THis method will process the section in the context of the Template
289      * @return returns the integer code doEndTag() will return.
290      */

291     private int processTemplate()
292     {
293         ServletRequest JavaDoc req = pageContext.getRequest();
294         Template.TemplateContext tc = (Template.TemplateContext)
295                 req.getAttribute(TEMPLATE_SECTIONS);
296
297         if (tc.secs == null) {
298             tc.secs = new HashMap JavaDoc();
299         }
300
301         assert (tc.secs != null);
302
303         if (!_visible) {
304             // set the section so that it doesn't contain anything
305
tc.secs.put(_name,"");
306             localRelease();
307             return EVAL_PAGE;
308         }
309         if (hasErrors()) {
310             String JavaDoc s = getErrorsReport();
311             tc.secs.put(_name,s);
312             localRelease();
313             return EVAL_PAGE;
314         }
315
316         BodyContent JavaDoc bc = getBodyContent();
317         String JavaDoc content = (bc != null) ? bc.getString() : "";
318         tc.secs.put(_name,content);
319         localRelease();
320         return EVAL_PAGE;
321     }
322
323     // @TODO: this code is duplicated between section and divPanel
324
/**
325      * This method will handle creating the tagId attribute. The tagId attribute indentifies the
326      * tag in the generated HTML. There is a lookup table created in JavaScript mapping the <coe>tagId</code>
327      * to the actual name. The tagId is also run through the naming service so it can be scoped. Some tags will
328      * write that <code>tagid</code> out as the <code>id</code> attribute of the HTML tag being generated.
329      * @param buffer
330      * @return String
331      */

332     private final String JavaDoc renderTagId(InternalStringBuilder buffer)
333         throws JspException JavaDoc {
334         assert(_name != null);
335
336         // @todo: this is busted. It should be writing out inline.
337
String JavaDoc realName = rewriteName(_name);
338         String JavaDoc idScript = mapLegacyTagId(_name, realName);
339
340         // some tags will output the id attribute themselves so they don't write this out
341
renderAttribute(buffer, "id", realName);
342         return idScript;
343     }
344
345     /**
346      * This method will write append an attribute value to a InternalStringBuilder.
347      * The method assumes that the attr is not <code>null</code>. If the
348      * value is <code>null</code> the attribute will not be appended to the
349      * <code>InternalStringBuilder</code>.
350      */

351     private void renderAttribute(InternalStringBuilder buf, String JavaDoc name, String JavaDoc value)
352     {
353         assert (name != null);
354         if (value == null)
355             return;
356
357         buf.append(" ");
358         buf.append(name);
359         buf.append("=\"");
360         buf.append(value);
361         buf.append("\"");
362     }
363
364     private String JavaDoc mapLegacyTagId(String JavaDoc tagId, String JavaDoc value)
365     {
366         // @todo: this is sort of broken, it needs to be updated
367
IScriptReporter scriptReporter = getScriptReporter();
368         ScriptRequestState srs = ScriptRequestState.getScriptRequestState((HttpServletRequest JavaDoc) pageContext.getRequest());
369         String JavaDoc scriptId = srs.mapLegacyTagId(scriptReporter,tagId, value);
370         return scriptId;
371     }
372 }
373
Popular Tags