KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > views > jsp > vui > AbstractVUITag


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.views.jsp.vui;
6
7 import com.opensymphony.webwork.components.Include;
8 import com.opensymphony.webwork.config.Configuration;
9 import com.opensymphony.webwork.util.ContainUtil;
10 import com.opensymphony.webwork.views.jsp.ParamTag;
11 import com.opensymphony.webwork.views.jsp.WebWorkBodyTagSupport;
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14
15 import javax.servlet.http.HttpServletRequest JavaDoc;
16 import javax.servlet.http.HttpServletResponse JavaDoc;
17 import javax.servlet.jsp.JspException JavaDoc;
18 import javax.servlet.jsp.JspTagException JavaDoc;
19 import javax.servlet.jsp.JspWriter JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
22
23
24 /**
25  * Abstract VUI tag
26  *
27  * @author Jeff Haynie (jhaynie@vocalocity.net)
28  * @version $Revision: 1.9 $
29  */

30 public abstract class AbstractVUITag extends WebWorkBodyTagSupport implements ParamTag.Parametric {
31     //~ Static fields/initializers /////////////////////////////////////////////
32

33     // Attributes ----------------------------------------------------
34
private static Log log = LogFactory.getLog(AbstractVUITag.class);
35
36     //~ Instance fields ////////////////////////////////////////////////////////
37

38     protected Map JavaDoc params = new HashMap JavaDoc();
39     protected String JavaDoc templateFooterAttr;
40     protected String JavaDoc templateHeaderAttr;
41     protected String JavaDoc theme;
42     protected String JavaDoc themeAttr;
43
44     //~ Methods ////////////////////////////////////////////////////////////////
45

46     public abstract String JavaDoc getFooterTemplate();
47
48     // Public --------------------------------------------------------
49
public abstract String JavaDoc getHeaderTemplate();
50
51     public String JavaDoc getBrowserUserAgent() {
52         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext.getRequest();
53         String JavaDoc ua = request.getHeader("User-Agent");
54
55         if (ua == null) {
56             ua = request.getHeader("user-agent");
57         }
58
59         return ((ua == null) ? "" : ua);
60     }
61
62     public Map JavaDoc getParameters() {
63         return params;
64     }
65
66     public void setTemplateFooter(String JavaDoc aName) {
67         templateFooterAttr = aName;
68     }
69
70     public void setTemplateHeader(String JavaDoc aName) {
71         templateHeaderAttr = aName;
72     }
73
74     public void setTheme(String JavaDoc aName) {
75         themeAttr = aName;
76     }
77
78     public String JavaDoc getTheme() {
79         // If theme set is not explicitly given,
80
// try to find attribute which states the theme set to use
81
if ((theme == null) || (theme == "")) {
82             theme = (String JavaDoc) pageContext.findAttribute("theme");
83         }
84
85         // Default template set
86
if ((theme == null) || (theme == "")) {
87             theme = Configuration.getString("webwork.ui.theme");
88
89             if (!theme.endsWith("/")) {
90                 theme += "/";
91             }
92         }
93
94         return theme;
95     }
96
97     public void addParameter(String JavaDoc name, Object JavaDoc value) {
98         addParameterInternal(name, value);
99     }
100
101     public int doAfterBody() throws JspException JavaDoc {
102         if (bodyContent != null) {
103             try {
104                 JspWriter JavaDoc out = getPreviousOut();
105                 out.print(bodyContent.getString());
106                 bodyContent.clearBody();
107             } catch (Exception JavaDoc ex) {
108                 ex.printStackTrace();
109                 throw new JspTagException JavaDoc("Exception:: " + toString(ex));
110             }
111
112             return (EVAL_BODY_BUFFERED);
113         }
114
115         return (SKIP_BODY);
116     }
117
118     public int doEndTag() throws JspException JavaDoc {
119         if (themeAttr != null) {
120             theme = (String JavaDoc) findValue(themeAttr);
121         }
122
123         getStack().push(this);
124
125         try {
126             // footer
127
String JavaDoc template = templateFooterAttr;
128
129             if (template == null) {
130                 template = getFooterTemplate();
131             }
132
133             Include.include(getTemplateDirectory() + template, pageContext.getOut(),
134                     pageContext.getRequest(), (HttpServletResponse JavaDoc) pageContext.getResponse());
135         } catch (Exception JavaDoc ex) {
136             ex.printStackTrace();
137             throw new JspTagException JavaDoc("Exception including footer: " + toString(ex));
138         } finally {
139             getStack().pop();
140             params = new HashMap JavaDoc();
141         }
142
143         return EVAL_PAGE;
144     }
145
146     // FieldTag overrides ------------------------------------------
147
public int doStartTag() {
148         if (themeAttr != null) {
149             theme = (String JavaDoc) findValue(themeAttr);
150         }
151
152         initializeAttributes();
153
154         getStack().push(this);
155
156         try {
157             // header
158
String JavaDoc template = templateHeaderAttr;
159
160             if (template == null) {
161                 template = getHeaderTemplate();
162             }
163
164             Include.include(getTemplateDirectory() + template, pageContext.getOut(),
165                     pageContext.getRequest(), (HttpServletResponse JavaDoc) pageContext.getResponse());
166         } catch (Exception JavaDoc ex) {
167             ex.printStackTrace();
168
169             return SKIP_BODY;
170         } finally {
171             getStack().pop();
172         }
173
174         return EVAL_BODY_INCLUDE;
175     }
176
177     public boolean memberOf(Object JavaDoc obj1, Object JavaDoc obj2) {
178         return ContainUtil.contains(obj1, obj2);
179     }
180
181     protected void getSetParameter(String JavaDoc a, String JavaDoc n) {
182         if (a != null) {
183             Object JavaDoc value = findValue(a);
184
185             if (value != null) {
186                 addParameterInternal(n, value);
187             }
188         }
189     }
190
191     protected abstract void initializeAttributes();
192
193     /**
194      * get the template directory for a specific voice browser
195      */

196     protected String JavaDoc getTemplateDirectory() {
197         String JavaDoc ua = getBrowserUserAgent();
198
199         return BrowserSupport.getBrowserTemplateDirectory(ua);
200     }
201
202     private void addParameterInternal(String JavaDoc name, Object JavaDoc value) {
203         params.put(name, value);
204     }
205 }
206
Popular Tags