View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/StyleTag.java,v 1.14 2006/01/13 13:38:51 hkollmann Exp $
3    * $Revision: 1.14 $
4    * $Date: 2006/01/13 13:38:51 $
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;
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     // --------------------- properties ------------------------------------------------------------------
46     private String template;
47  
48     //private String templateBegin, templateEnd;
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       //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 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; // gets only rendered 1 time
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                       try {
197                               if(params!=null) pageContext.getRequest().setAttribute("styleparams", params);
198                               pageContext.getRequest().setAttribute("baseDir", templateBaseDir);
199 
200 
201                 //pageContext.forward(templateBegin);
202 
203                 HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
204                 HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
205 
206                 request.getRequestDispatcher(templateBegin).include(request, response);
207 
208                 //pageContext.getOut().flush();
209                       } catch(IOException ioe) {
210                               throw new JspException("Problem 1including template begin - "+ioe.toString());
211                       } catch(ServletException se) {}
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       // break into main (key/value)- tokens
231       StringTokenizer st = new StringTokenizer(s, ",");
232 
233       while (st.hasMoreTokens()) {
234          String token = st.nextToken(); // a key-value pair in its orignal string-shape
235 
236          int    equalSignIndex = token.indexOf('=');
237 
238          // peeling out the key
239          String key = token.substring(0, equalSignIndex)
240                            .trim();
241 
242          // peeling out the value (which may or not be embedded in single quotes)
243          String value = token.substring(equalSignIndex + 1)
244                              .trim();
245 
246          if ((value.charAt(0) == '\'')
247                    && (value.charAt(value.length() - 1) == '\'')) // get out of any single quotes
248           {
249             value = value.substring(1, value.length() - 1);
250          }
251 
252          result.put(key, value);
253       }
254 
255       return result;
256    }
257 }