1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.dbforms.taglib;
25
26 import java.util.Hashtable;
27 import java.util.StringTokenizer;
28
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.jsp.JspException;
31
32
33
34 /***
35 * Renders an dbforms style tag
36 *
37 * @author Joe Peer
38 */
39 public class StyleTag extends AbstractScriptHandlerTag
40 implements javax.servlet.jsp.tagext.TryCatchFinally {
41 private Hashtable params;
42 private String paramList;
43 private String part;
44
45
46 private String template;
47
48
49 private String templateBase;
50 private String templateBaseDir;
51
52 /***
53 * DOCUMENT ME!
54 *
55 * @param pageContext DOCUMENT ME!
56 */
57 public void setPageContext(final javax.servlet.jsp.PageContext pageContext) {
58 super.setPageContext(pageContext);
59
60 templateBase = pageContext.getServletContext()
61 .getInitParameter("templateBase");
62
63 if (templateBase == null) {
64 templateBase = "templates";
65 }
66 }
67
68
69 /***
70 * DOCUMENT ME!
71 *
72 * @param paramList DOCUMENT ME!
73 */
74 public void setParamList(String paramList) {
75 this.paramList = paramList;
76 this.params = parseParams(paramList);
77 }
78
79
80 /***
81 * DOCUMENT ME!
82 *
83 * @return DOCUMENT ME!
84 */
85 public String getParamList() {
86 return paramList;
87 }
88
89
90 /***
91 * DOCUMENT ME!
92 *
93 * @param part DOCUMENT ME!
94 */
95 public void setPart(String part) {
96 this.part = part;
97 }
98
99
100 /***
101 * DOCUMENT ME!
102 *
103 * @return DOCUMENT ME!
104 */
105 public String getPart() {
106 return part;
107 }
108
109
110 /***
111 * DOCUMENT ME!
112 *
113 * @param template DOCUMENT ME!
114 */
115 public void setTemplate(String template) {
116 this.template = template;
117 this.templateBaseDir = templateBase + "/" + template + "/";
118
119
120
121 }
122
123
124 /***
125 * DOCUMENT ME!
126 *
127 * @return DOCUMENT ME!
128 */
129 public String getTemplate() {
130 return template;
131 }
132
133
134 /***
135 * DOCUMENT ME!
136 *
137 * @return DOCUMENT ME!
138 *
139 * @throws JspException DOCUMENT ME!
140 */
141 public int doAfterBody() throws JspException {
142 return SKIP_BODY;
143 }
144
145
146 /***
147 * DOCUMENT ME!
148 *
149 * @return DOCUMENT ME!
150 *
151 * @throws JspException DOCUMENT ME!
152 */
153 public int doEndTag() throws JspException {
154 try {
155 if (bodyContent != null) {
156 bodyContent.writeOut(bodyContent.getEnclosingWriter());
157 }
158
159 HttpServletRequest request = (HttpServletRequest) pageContext
160 .getRequest();
161
162 if (params != null) {
163 request.setAttribute("styleparams", params);
164 }
165
166 request.setAttribute("baseDir", templateBaseDir);
167
168 pageContext.include(templateBaseDir + template + "_" + part + ".jsp");
169 } catch (Exception ioe) {
170 throw new JspException("Problem 2 including template end - "
171 + ioe.toString());
172 }
173
174 return EVAL_PAGE;
175 }
176
177
178 /***
179 * DOCUMENT ME!
180 */
181 public void doFinally() {
182 params = null;
183 templateBase = null;
184 templateBaseDir = null;
185 template = null;
186 paramList = null;
187 part = null;
188 }
189
190
191 /***
192
193 */
194 public int doStartTag() throws JspException {
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213 return EVAL_BODY_BUFFERED;
214 }
215
216
217 /***
218 * this method splits a string of the form "param1 = 'foo1', param2 = foo2,
219 * param3=foo3" into a hashtable containing following key/value pairs: {
220 * ("param1"/"foo1"), ("param2"/"foo2"), ("param3"/"foo3") } #fixme:
221 * primitive algorithm! breaks if params contains komma - signs. fix it
222 *
223 * @param s DOCUMENT ME!
224 *
225 * @return DOCUMENT ME!
226 */
227 private Hashtable parseParams(String s) {
228 Hashtable result = new Hashtable();
229
230
231 StringTokenizer st = new StringTokenizer(s, ",");
232
233 while (st.hasMoreTokens()) {
234 String token = st.nextToken();
235
236 int equalSignIndex = token.indexOf('=');
237
238
239 String key = token.substring(0, equalSignIndex)
240 .trim();
241
242
243 String value = token.substring(equalSignIndex + 1)
244 .trim();
245
246 if ((value.charAt(0) == '\'')
247 && (value.charAt(value.length() - 1) == '\''))
248 {
249 value = value.substring(1, value.length() - 1);
250 }
251
252 result.put(key, value);
253 }
254
255 return result;
256 }
257 }