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;
17 import javax.servlet.jsp.PageContext;
18 import javax.servlet.jsp.tagext.Tag;
19 import javax.servlet.jsp.tagext.BodyTagSupport;
20 import java.io.Writer;
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 implements RequestConstants {
30 protected PageContext pageContext;
31 protected Tag parent;
32
33 /** To be implemented by all empty tags. */
34 public abstract int doEndTag() throws JspException;
35
36 /** Returns SKIP_BODY. */
37 public int doStartTag() {
38 return SKIP_BODY;
39 }
40
41 public void release() {
42 }
43
44 public Tag getParent() {
45 return parent;
46 }
47
48 public void setParent(Tag parent) {
49 this.parent = parent;
50 }
51
52 public void setPageContext(PageContext 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 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 getOut() {
88 return OutputConverter.getWriter(pageContext.getOut());
89 }
90 }