View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/TextFormatTag.java,v 1.7 2006/01/13 13:38:51 hkollmann Exp $
3    * $Revision: 1.7 $
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.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;
37  
38  
39  
40  /***
41   * Special class for TextFormatting
42   *
43   * @author hkk
44   */
45  public class TextFormatTag extends AbstractDbBaseHandlerTag
46     implements javax.servlet.jsp.tagext.TryCatchFinally {
47     private static Log logCat      = LogFactory.getLog(TextFormatTag.class);
48     private Object     fieldObject; // Holds the object to retrieve.
49     private String contextVar;
50     private String type;
51     private String value;
52  
53     /***
54      * DOCUMENT ME!
55      *
56      * @param contextVar DOCUMENT ME!
57      */
58     public void setContextVar(String contextVar) {
59        this.contextVar = contextVar;
60     }
61  
62  
63     /***
64      * DOCUMENT ME!
65      *
66      * @return DOCUMENT ME!
67      */
68     public String getContextVar() {
69        return contextVar;
70     }
71  
72  
73     /***
74      * DOCUMENT ME!
75      *
76      * @return
77      */
78     public Object getFieldObject() {
79        return fieldObject;
80     }
81  
82  
83     /***
84      * DOCUMENT ME!
85      *
86      * @param type The type to set.
87      */
88     public void setType(String type) {
89        this.type = type;
90     }
91  
92  
93     /***
94      * DOCUMENT ME!
95      *
96      * @return Returns the type.
97      */
98     public String getType() {
99        return type;
100    }
101 
102 
103    /***
104     * DOCUMENT ME!
105     *
106     * @param value The value to set.
107     */
108    public void setValue(String value) {
109       this.value = value;
110    }
111 
112 
113    /***
114     * DOCUMENT ME!
115     *
116     * @return Returns the value.
117     */
118    public String getValue() {
119       return value;
120    }
121 
122    /***
123     * Description of the Method
124     *
125     * @return Description of the Return Value
126     *
127     * @exception javax.servlet.jsp.JspException Description of the Exception
128     * @throws JspException DOCUMENT ME!
129     */
130    public int doEndTag() throws javax.servlet.jsp.JspException {
131       if (Util.isNull(getContextVar()) && Util.isNull(getValue())) {
132          throw new JspException("either var or value must be setted!");
133       }
134 
135       Field field = new Field();
136 
137       if (!Util.isNull(getValue())) {
138          if (Util.isNull(getType())) {
139             throw new JspException("value setted - type must be setted  too!");
140          }
141 
142          field.setFieldType(getType());
143 
144          FieldValue fv = new FieldValue(field, getValue());
145          fv.setPattern(getPattern());
146          fieldObject = fv.getFieldValueAsObject();
147       } else {
148          String          search = getContextVar();
149          int             pos = search.indexOf(".");
150 
151          ResultSetVector rsv = null;
152 
153          if (getParentForm() != null) {
154             rsv = getParentForm()
155                      .getResultSetVector();
156          }
157 
158          if (pos == -1) {
159             if (rsv != null) {
160                fieldObject = rsv.getAttribute(search);
161             }
162 
163             if (fieldObject == null) {
164                // simple type, 'search' is an object in the session
165                fieldObject = pageContext.findAttribute(search);
166             }
167          } else {
168             try {
169                // complex, 'search' is really a bean
170                String search_bean = search.substring(0, pos);
171                search = search.substring(pos + 1);
172 
173                Object bean = null;
174 
175                if (rsv != null) {
176                   bean = rsv.getAttribute(search_bean);
177                }
178 
179                if (bean == null) {
180                   // simple type, 'search' is an object in the session
181                   bean = pageContext.findAttribute(search_bean);
182                }
183 
184                if (bean != null) {
185                   logCat.debug("calling PropertyUtils.getProperty "
186                                + search_bean + " " + search);
187                   fieldObject = PropertyUtils.getProperty(bean, search);
188                }
189             } catch (Exception e) {
190                throw new JspException(e.getMessage());
191             }
192          }
193 
194          if (fieldObject == null) {
195             throw new JspException("object not found in context!");
196          }
197 
198          field.setTypeByObject(fieldObject);
199       }
200 
201       this.setField(field);
202 
203       String fieldValue = getFormattedFieldValue();
204       fieldValue = escapeHTML(fieldValue);
205 
206       try {
207          pageContext.getOut()
208                     .write(fieldValue);
209       } catch (java.io.IOException ioe) {
210          // better to KNOW what happended !
211          throw new JspException("IO Error: " + ioe.getMessage());
212       }
213 
214       return EVAL_PAGE;
215    }
216 
217 
218    /***
219     * DOCUMENT ME!
220     */
221    public void doFinally() {
222       type       = null;
223       value      = null;
224       contextVar = null;
225       super.doFinally();
226    }
227 
228 
229    /***
230     * DOCUMENT ME!
231     *
232     * @param fieldObject DOCUMENT ME!
233     */
234    protected void setFieldObject(Object fieldObject) {
235       this.fieldObject = fieldObject;
236    }
237 }