KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > taglib > StyleTag


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/StyleTag.java,v 1.11 2004/08/18 12:26:08 hkollmann Exp $
3  * $Revision: 1.11 $
4  * $Date: 2004/08/18 12:26:08 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23
24 package org.dbforms.taglib;
25
26 import java.util.Hashtable JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.jsp.JspException JavaDoc;
31
32
33
34 /**
35  * Renders an dbforms style tag
36  *
37  * @author Joe Peer
38  */

39 public class StyleTag extends TagSupportWithScriptHandler
40    implements javax.servlet.jsp.tagext.TryCatchFinally JavaDoc {
41    private Hashtable JavaDoc params;
42    private String JavaDoc paramList;
43    private String JavaDoc part;
44
45    // --------------------- properties ------------------------------------------------------------------
46
private String JavaDoc template;
47
48    //private String templateBegin, templateEnd;
49
private String JavaDoc templateBase;
50    private String JavaDoc templateBaseDir;
51
52    /**
53     * DOCUMENT ME!
54     *
55     * @param pageContext DOCUMENT ME!
56     */

57    public void setPageContext(final javax.servlet.jsp.PageContext JavaDoc 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 JavaDoc 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 JavaDoc getParamList() {
86       return paramList;
87    }
88
89
90    /**
91     * DOCUMENT ME!
92     *
93     * @param part DOCUMENT ME!
94     */

95    public void setPart(String JavaDoc part) {
96       this.part = part;
97    }
98
99
100    /**
101     * DOCUMENT ME!
102     *
103     * @return DOCUMENT ME!
104     */

105    public String JavaDoc getPart() {
106       return part;
107    }
108
109
110    /**
111     * DOCUMENT ME!
112     *
113     * @param template DOCUMENT ME!
114     */

115    public void setTemplate(String JavaDoc template) {
116       this.template = template;
117       this.templateBaseDir = templateBase + "/" + template + "/";
118
119       //this.templateBegin = templateBaseDir + template + "_begin.jsp";
120
//this.templateEnd = templateBaseDir + template + "_end.jsp";
121
}
122
123
124    /**
125     * DOCUMENT ME!
126     *
127     * @return DOCUMENT ME!
128     */

129    public String JavaDoc 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 JavaDoc {
142       return SKIP_BODY; // gets only rendered 1 time
143
}
144
145
146    /**
147     * DOCUMENT ME!
148     *
149     * @param t DOCUMENT ME!
150     *
151     * @throws Throwable DOCUMENT ME!
152     */

153    public void doCatch(Throwable JavaDoc t) throws Throwable JavaDoc {
154       throw t;
155    }
156
157
158    /**
159     * DOCUMENT ME!
160     *
161     * @return DOCUMENT ME!
162     *
163     * @throws JspException DOCUMENT ME!
164     */

165    public int doEndTag() throws JspException JavaDoc {
166       try {
167          if (bodyContent != null) {
168             bodyContent.writeOut(bodyContent.getEnclosingWriter());
169          }
170
171          HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pageContext
172                                       .getRequest();
173
174          if (params != null) {
175             request.setAttribute("styleparams", params);
176          }
177
178          request.setAttribute("baseDir", templateBaseDir);
179
180          pageContext.include(templateBaseDir + template + "_" + part + ".jsp");
181       } catch (Exception JavaDoc ioe) {
182          throw new JspException JavaDoc("Problem 2 including template end - "
183                                 + ioe.toString());
184       }
185
186       return EVAL_PAGE;
187    }
188
189
190    /**
191     * DOCUMENT ME!
192     */

193    public void doFinally() {
194       params = null;
195       templateBase = null;
196       templateBaseDir = null;
197       template = null;
198       paramList = null;
199       part = null;
200    }
201
202
203    /**
204
205                               */

206    public int doStartTag() throws JspException JavaDoc {
207       /*
208                       try {
209                               if(params!=null) pageContext.getRequest().setAttribute("styleparams", params);
210                               pageContext.getRequest().setAttribute("baseDir", templateBaseDir);
211
212
213                 //pageContext.forward(templateBegin);
214
215                 HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
216                 HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
217
218                 request.getRequestDispatcher(templateBegin).include(request, response);
219
220                 //pageContext.getOut().flush();
221                       } catch(IOException ioe) {
222                               throw new JspException("Problem 1including template begin - "+ioe.toString());
223                       } catch(ServletException se) {}
224       */

225       return EVAL_BODY_BUFFERED;
226    }
227
228
229    /**
230     * this method splits a string of the form "param1 = 'foo1', param2 = foo2,
231     * param3=foo3" into a hashtable containing following key/value pairs: {
232     * ("param1"/"foo1"), ("param2"/"foo2"), ("param3"/"foo3") } #fixme:
233     * primitive algorithm! breaks if params contains komma - signs. fix it
234     *
235     * @param s DOCUMENT ME!
236     *
237     * @return DOCUMENT ME!
238     */

239    private Hashtable JavaDoc parseParams(String JavaDoc s) {
240       Hashtable JavaDoc result = new Hashtable JavaDoc();
241
242       // break into main (key/value)- tokens
243
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(s, ",");
244
245       while (st.hasMoreTokens()) {
246          String JavaDoc token = st.nextToken(); // a key-value pair in its orignal string-shape
247

248          int equalSignIndex = token.indexOf('=');
249
250          // peeling out the key
251
String JavaDoc key = token.substring(0, equalSignIndex)
252                            .trim();
253
254          // peeling out the value (which may or not be embedded in single quotes)
255
String JavaDoc value = token.substring(equalSignIndex + 1)
256                              .trim();
257
258          if ((value.charAt(0) == '\'')
259                    && (value.charAt(value.length() - 1) == '\'')) // get out of any single quotes
260
{
261             value = value.substring(1, value.length() - 1);
262          }
263
264          result.put(key, value);
265       }
266
267       return result;
268    }
269 }
270
Popular Tags