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 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 <db:setCustomFormater name="foo" class="" arg=""
44 * reset="" > 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 }