View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/SetCustomFormatterTag.java,v 1.11 2006/01/13 13:38:51 hkollmann Exp $
3    * $Revision: 1.11 $
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 org.dbforms.interfaces.ICustomFormat;
27  
28  import java.util.HashMap;
29  import java.util.Locale;
30  
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.jsp.JspException;
33  import javax.servlet.jsp.PageContext;
34  
35  
36  import org.dbforms.util.MessageResources;
37  import org.dbforms.util.ReflectionUtil;
38  import org.dbforms.util.Util;
39  
40  /***
41   * DOCUMENT ME!
42   *
43   * @author Neal Katz &lt;db:setCustomFormater name="foo" class="" arg=""
44   *         reset="" &gt; arg and reset are optional
45   */
46  public class SetCustomFormatterTag extends AbstractScriptHandlerTag
47     implements javax.servlet.jsp.tagext.TryCatchFinally {
48     
49     private static final String sessionKey = "dbforms.org.tag.CustomFormatterTag";
50     
51     Object              arg       = null;
52     String              className = null;
53     String              name      = null;
54  
55  
56     public static String sprintf(String customFormatter, PageContext pageContext, String s) {
57  		if (!Util.isNull(customFormatter)) {
58  			HashMap hm = (HashMap) pageContext
59  					.getAttribute(SetCustomFormatterTag.sessionKey);
60  			if (hm != null) {
61  				Object obj = hm.get(customFormatter);
62  				if (obj instanceof ICustomFormat) {
63  					ICustomFormat cf = (ICustomFormat) obj;
64  					Locale locale = MessageResources
65  							.getLocale((HttpServletRequest) pageContext
66  									.getRequest());
67  					cf.setLocale(locale);
68  					s = cf.sprintf(s);
69  				}
70  			}
71  		}
72  		return s;
73  	   
74     }
75     
76     /***
77      * optional argument passed to CustomFormatter instance init()
78      *
79      * @param obj
80      */
81     public void setArg(Object obj) {
82        this.arg = obj; 
83     }
84  
85  
86     /***
87      * classname of a class implementing the CustomFormatter Interface
88      *
89      * @param className
90      */
91     public void setClassName(String className) {
92        this.className = className;
93     }
94  
95  
96     /***
97      * name to use, other tags will use this as the value for the
98      * customFormatter attribute
99      *
100     * @param name
101     */
102    public void setName(String name) {
103       this.name = name;
104    }
105 
106    /***
107     * DOCUMENT ME!
108     *
109     * @return DOCUMENT ME!
110     */
111    public int doEndTag() throws JspException {
112       if ((name != null) && (name.length() > 0)) {
113          HashMap     hm = (HashMap) pageContext.getAttribute(sessionKey);
114          if (hm == null) {
115             hm = new HashMap();
116             pageContext.setAttribute(sessionKey, hm);
117          }
118          try {
119             ICustomFormat cf = (ICustomFormat) hm.get(name);
120             if (cf == null) {
121                cf = (ICustomFormat) ReflectionUtil.newInstance(className);
122                cf.setArg(arg.toString());
123                hm.put(name, cf);
124             }
125          } catch (Exception e) {
126             throw new JspException(e.getLocalizedMessage());
127          }
128       }
129       return EVAL_PAGE;
130    }
131 
132 
133    /***
134     * DOCUMENT ME!
135     */
136    public void doFinally() {
137       this.arg       = null;
138       this.className = null;
139       this.name      = null;
140       super.doFinally();
141    }
142 
143 
144    /***
145     * DOCUMENT ME!
146     *
147     * @return DOCUMENT ME!
148     *
149     * @throws javax.servlet.jsp.JspException DOCUMENT ME!
150     */
151    public int doStartTag() throws javax.servlet.jsp.JspException {
152       return SKIP_BODY;
153    }
154 }