KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/TextFormatTag.java,v 1.5 2004/10/21 20:39:23 hkollmann Exp $
3  * $Revision: 1.5 $
4  * $Date: 2004/10/21 20:39:23 $
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.apache.commons.beanutils.PropertyUtils;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import org.dbforms.config.Field;
31 import org.dbforms.config.FieldValue;
32 import org.dbforms.config.ResultSetVector;
33
34 import org.dbforms.util.Util;
35
36 import javax.servlet.jsp.JspException JavaDoc;
37
38
39
40 /**
41  * Special class for TextFormatting
42  *
43  * @author hkk
44  */

45 public class TextFormatTag extends DbBaseHandlerTag
46    implements javax.servlet.jsp.tagext.TryCatchFinally JavaDoc {
47    private static Log logCat = LogFactory.getLog(TextFormatTag.class);
48    private Object JavaDoc fieldObject; // Holds the object to retrieve.
49
private String JavaDoc contextVar;
50    private String JavaDoc pattern;
51    private String JavaDoc type;
52    private String JavaDoc value;
53
54    /**
55     * DOCUMENT ME!
56     *
57     * @param contextVar DOCUMENT ME!
58     */

59    public void setContextVar(String JavaDoc contextVar) {
60       this.contextVar = contextVar;
61    }
62
63
64    /**
65     * DOCUMENT ME!
66     *
67     * @return DOCUMENT ME!
68     */

69    public String JavaDoc getContextVar() {
70       return contextVar;
71    }
72
73
74    /**
75     * DOCUMENT ME!
76     *
77     * @return
78     */

79    public Object JavaDoc getFieldObject() {
80       return fieldObject;
81    }
82
83
84    /**
85     * DOCUMENT ME!
86     *
87     * @param pattern The pattern to set.
88     */

89    public void setPattern(String JavaDoc pattern) {
90       this.pattern = pattern;
91    }
92
93
94    /**
95     * DOCUMENT ME!
96     *
97     * @return Returns the pattern.
98     */

99    public String JavaDoc getPattern() {
100       return pattern;
101    }
102
103
104    /**
105     * DOCUMENT ME!
106     *
107     * @param type The type to set.
108     */

109    public void setType(String JavaDoc type) {
110       this.type = type;
111    }
112
113
114    /**
115     * DOCUMENT ME!
116     *
117     * @return Returns the type.
118     */

119    public String JavaDoc getType() {
120       return type;
121    }
122
123
124    /**
125     * DOCUMENT ME!
126     *
127     * @param value The value to set.
128     */

129    public void setValue(String JavaDoc value) {
130       this.value = value;
131    }
132
133
134    /**
135     * DOCUMENT ME!
136     *
137     * @return Returns the value.
138     */

139    public String JavaDoc getValue() {
140       return value;
141    }
142
143
144    /**
145     * @see javax.servlet.jsp.tagext.TryCatchFinally#doCatch(java.lang.Throwable)
146     */

147    public void doCatch(Throwable JavaDoc t) throws Throwable JavaDoc {
148       throw t;
149    }
150
151
152    /**
153     * Description of the Method
154     *
155     * @return Description of the Return Value
156     *
157     * @exception javax.servlet.jsp.JspException Description of the Exception
158     * @throws JspException DOCUMENT ME!
159     */

160    public int doEndTag() throws javax.servlet.jsp.JspException JavaDoc {
161       if (Util.isNull(getContextVar()) && Util.isNull(getValue())) {
162          throw new JspException JavaDoc("either var or value must be setted!");
163       }
164
165       Field field = new Field();
166
167       if (!Util.isNull(getValue())) {
168          if (Util.isNull(getType())) {
169             throw new JspException JavaDoc("value setted - type must be setted too!");
170          }
171
172          field.setFieldType(getType());
173
174          FieldValue fv = new FieldValue(field, getValue());
175          fv.setPattern(getPattern());
176          fieldObject = fv.getFieldValueAsObject();
177       } else {
178          String JavaDoc search = getContextVar();
179          int pos = search.indexOf(".");
180
181          ResultSetVector rsv = null;
182
183          if (getParentForm() != null) {
184             rsv = getParentForm()
185                      .getResultSetVector();
186          }
187
188          if (pos == -1) {
189             if (rsv != null) {
190                fieldObject = rsv.getAttribute(search);
191             }
192
193             if (fieldObject == null) {
194                // simple type, 'search' is an object in the session
195
fieldObject = pageContext.findAttribute(search);
196             }
197          } else {
198             try {
199                // complex, 'search' is really a bean
200
String JavaDoc search_bean = search.substring(0, pos);
201                search = search.substring(pos + 1);
202
203                Object JavaDoc bean = null;
204
205                if (rsv != null) {
206                   bean = rsv.getAttribute(search_bean);
207                }
208
209                if (bean == null) {
210                   // simple type, 'search' is an object in the session
211
bean = pageContext.findAttribute(search_bean);
212                }
213
214                if (bean != null) {
215                   logCat.debug("calling PropertyUtils.getProperty "
216                                + search_bean + " " + search);
217                   fieldObject = PropertyUtils.getProperty(bean, search);
218                }
219             } catch (Exception JavaDoc e) {
220                throw new JspException JavaDoc(e.getMessage());
221             }
222          }
223
224          if (fieldObject == null) {
225             throw new JspException JavaDoc("object not found in context!");
226          }
227
228          field.setTypeByObject(fieldObject);
229       }
230
231       this.setField(field);
232
233       String JavaDoc fieldValue = getFormattedFieldValue();
234       fieldValue = escapeHTML(fieldValue);
235
236       try {
237          pageContext.getOut()
238                     .write(fieldValue);
239       } catch (java.io.IOException JavaDoc ioe) {
240          // better to KNOW what happended !
241
throw new JspException JavaDoc("IO Error: " + ioe.getMessage());
242       }
243
244       return EVAL_PAGE;
245    }
246
247
248    /**
249     * DOCUMENT ME!
250     */

251    public void doFinally() {
252       pattern = null;
253       type = null;
254       value = null;
255       contextVar = null;
256       super.doFinally();
257    }
258
259
260    /**
261     * DOCUMENT ME!
262     *
263     * @param fieldObject DOCUMENT ME!
264     */

265    protected void setFieldObject(Object JavaDoc fieldObject) {
266       this.fieldObject = fieldObject;
267    }
268 }
269
Popular Tags