KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > jsp > tags > ScreenTag


1 package org.apache.turbine.services.jsp.tags;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.IOException JavaDoc;
20 import javax.servlet.jsp.JspException JavaDoc;
21 import javax.servlet.jsp.PageContext JavaDoc;
22 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
23 import org.apache.turbine.modules.ScreenLoader;
24 import org.apache.turbine.services.TurbineServices;
25 import org.apache.turbine.services.jsp.JspService;
26 import org.apache.turbine.services.template.TemplateService;
27 import org.apache.turbine.util.Log;
28 import org.apache.turbine.util.RunData;
29 import org.apache.turbine.util.template.TemplateInfo;
30
31 /**
32  * Supporting class for the screen tag.
33  * Sends the content between the screen's body or head tags to the
34  * output stream. The decision to output the head or body contents
35  * is decided by the section parameter. Example usage:
36  * <x:screen section="head" />
37  *
38  * @author <a HREF="mailto:john.mcnally@clearink.com">John D. McNally</a>
39  * @version $Id: ScreenTag.java,v 1.1.2.2 2004/05/20 03:03:07 seade Exp $
40  */

41 public class ScreenTag extends TagSupport JavaDoc
42 {
43     /**
44      * section attribute. Valid values are head or body.
45      */

46     private String JavaDoc section;
47     /**
48      * contextRelevant attribute. Meant to allow the Screen
49      * java code to be skipped in the event the content is static.
50      * Not working and have not decided whether to make it work or
51      * remove the functionality.
52      */

53     private boolean staticContent;
54     /**
55      * Key used to prevent the templates Screen module from executing
56      * multiple times while evaluating the layout.
57      */

58     private static final String JavaDoc FLAG = "_screen_executed_";
59
60     /**
61      * Key used to access the section parameter in the request.
62      */

63     static final String JavaDoc SECTION_KEY = "_layout_section_";
64     /**
65      * One of the valid values for the section parameter
66      */

67     static final String JavaDoc BODY = "body";
68     /**
69      * One of the valid values for the section parameter
70      */

71     static final String JavaDoc HEAD = "head";
72     
73     /**
74      * setter for the section parameter
75      */

76     public void setSection(String JavaDoc section)
77     {
78         pageContext.setAttribute(SECTION_KEY, section,
79             PageContext.REQUEST_SCOPE);
80         this.section = section;
81     }
82     
83     /**
84      * setter for the staticContent parameter
85      */

86     public void setStaticContent(boolean staticContent)
87     {
88         this.staticContent = staticContent;
89     }
90
91     /**
92      * Method called when the tag is encountered to send attributes to the
93      * output stream
94      *
95      * @return SKIP_BODY, as it is intended to be a single tag.
96      */

97     public int doStartTag() throws JspException JavaDoc
98     {
99         RunData data = (RunData)pageContext
100                 .getAttribute(JspService.RUNDATA, PageContext.REQUEST_SCOPE);
101         
102         String JavaDoc module = null;
103         String JavaDoc template = null;
104         Boolean JavaDoc flag = (Boolean JavaDoc)pageContext
105                 .getAttribute(FLAG, PageContext.REQUEST_SCOPE);
106         JspService jsp = (JspService) TurbineServices.getInstance()
107             .getService(JspService.SERVICE_NAME);
108         try
109         {
110             pageContext.getOut().flush();
111             template = data.getTemplateInfo().getScreenTemplate();
112             
113             if ( staticContent || (flag != null && flag.booleanValue()) )
114             {
115                 jsp.handleRequest(data, "/screens/" + template, false);
116             }
117             else
118             {
119                 module = ((TemplateService)TurbineServices.getInstance().getService(
120                 TemplateService.SERVICE_NAME)).getScreenName(template);
121                 ScreenLoader.getInstance().exec(data, module);
122                 pageContext.setAttribute(FLAG,
123                     Boolean.TRUE, PageContext.REQUEST_SCOPE);
124             }
125         }
126         catch (Exception JavaDoc e)
127         {
128             String JavaDoc message = "Error processing screen template:" +
129                 template + " using module: " + module;
130             Log.error(message, e);
131             try
132             {
133                 data.getOut().print("Error processing screen template: "
134                     + template + " using module: " + module);
135             }
136             catch(java.io.IOException JavaDoc ioe) {}
137         }
138         return SKIP_BODY;
139     }
140 }
141
Popular Tags