KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > module > sitemesh > taglib > AbstractTag


1 /*
2  * Title: AbstractTag
3  * Description:
4  *
5  * This software is published under the terms of the OpenSymphony Software
6  * License version 1.1, of which a copy has been included with this
7  * distribution in the LICENSE.txt file.
8  */

9
10 package com.opensymphony.module.sitemesh.taglib;
11
12 import com.opensymphony.module.sitemesh.Page;
13 import com.opensymphony.module.sitemesh.RequestConstants;
14 import com.opensymphony.module.sitemesh.util.OutputConverter;
15
16 import javax.servlet.jsp.JspException JavaDoc;
17 import javax.servlet.jsp.PageContext JavaDoc;
18 import javax.servlet.jsp.tagext.Tag JavaDoc;
19 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
20 import java.io.Writer JavaDoc;
21
22 /**
23  * Convenience implementation of Tag containing generice methods required
24  * by all (or most) taglibs.
25  *
26  * @author <a HREF="mailto:joe@truemesh.com">Joe Walnes</a>
27  * @version $Revision: 1.4 $
28  */

29 public abstract class AbstractTag extends BodyTagSupport JavaDoc implements RequestConstants {
30     protected PageContext JavaDoc pageContext;
31     protected Tag JavaDoc parent;
32
33     /** To be implemented by all empty tags. */
34     public abstract int doEndTag() throws JspException JavaDoc;
35
36     /** Returns SKIP_BODY. */
37     public int doStartTag() {
38         return SKIP_BODY;
39     }
40
41     public void release() {
42     }
43
44     public Tag JavaDoc getParent() {
45         return parent;
46     }
47
48     public void setParent(Tag JavaDoc parent) {
49         this.parent = parent;
50     }
51
52     public void setPageContext(PageContext JavaDoc pageContext) {
53         this.pageContext = pageContext;
54     }
55
56     /**
57      * Return the Page object from the PAGE scope. If this is found in REQUEST scope
58      * instead, it will be moved into PAGE scope - to handle multi-level includes.
59      */

60     protected Page getPage() {
61         Page p = (Page)pageContext.getAttribute(PAGE, PageContext.PAGE_SCOPE);
62
63         if (p == null) {
64             p = (Page)pageContext.getAttribute(PAGE, PageContext.REQUEST_SCOPE);
65             if (p == null) {
66                 pageContext.removeAttribute(PAGE, PageContext.PAGE_SCOPE);
67             }
68             else {
69                 pageContext.setAttribute(PAGE, p, PageContext.PAGE_SCOPE);
70             }
71             pageContext.removeAttribute(PAGE, PageContext.REQUEST_SCOPE);
72         }
73         return p;
74     }
75
76     /** Log exception generated by taglib. */
77     protected static void trace(Exception JavaDoc e) {
78         e.printStackTrace();
79     }
80
81     /**
82      * Get the outputWriter. This method should be used in preference to
83      * <code>pageContext.getOut()</code>, as some charset conversions may need
84      * to happen in some servers.
85      * @return the writer for use in the tag
86      */

87     protected Writer JavaDoc getOut() {
88         return OutputConverter.getWriter(pageContext.getOut());
89     }
90 }
Popular Tags