View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/DbDataContainerLabelTag.java,v 1.26 2006/01/13 13:38:50 hkollmann Exp $
3    * $Revision: 1.26 $
4    * $Date: 2006/01/13 13:38:50 $
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.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  import org.dbforms.config.ResultSetVector;
30  import org.dbforms.interfaces.IDataContainer;
31  import org.dbforms.interfaces.StaticData;
32  
33  import org.dbforms.util.Util;
34  
35  import java.util.List;
36  
37  import javax.servlet.jsp.JspException;
38  
39  
40  
41  /***
42   * this tag renders a dabase-datadriven LABEL, which is a passive element (it
43   * can't be changed by the user) - it is predestinated for use with read-only
44   * data (i.e. primary keys you don't want the user to change, etc) so far it
45   * is equivalent to DbLabelTag. But this tag may have a body containing any
46   * kind of EmbeddedData - tag. i put this feature into a seperate class for
47   * performance reasons (we do not want the overhead of pushing and poping the
48   * jsp writer to and off the stack
49   *
50   * @author Joachim Peer
51   */
52  public class DbDataContainerLabelTag extends AbstractDbBaseHandlerTag
53     implements IDataContainer, javax.servlet.jsp.tagext.TryCatchFinally {
54     // logging category for this class
55     private List   embeddedData = null;
56     private static Log    logCat = LogFactory.getLog(DbDataContainerLabelTag.class);
57     private String strict = "false";
58  
59     /***
60      * This method is a "hookup" for EmbeddedData - Tags which can assign the
61      * lines of data they loaded (by querying a database, or by rendering
62      * data-subelements, etc. etc.) and make the data available to this tag.
63      * [this method is defined in Interface DataContainer]
64      *
65      * @param embeddedData DOCUMENT ME!
66      */
67     public void setEmbeddedData(List embeddedData) {
68        this.embeddedData = embeddedData;
69     }
70  
71  
72     /***
73      * DOCUMENT ME!
74      *
75      * @param string
76      */
77     public void setStrict(String string) {
78        strict = string;
79     }
80  
81  
82     /***
83      * DOCUMENT ME!
84      *
85      * @return
86      */
87     public String getStrict() {
88        return strict;
89     }
90  
91  
92  
93     /***
94      * DOCUMENT ME!
95      *
96      * @return DOCUMENT ME!
97      *
98      * @throws javax.servlet.jsp.JspException DOCUMENT ME!
99      * @throws JspException DOCUMENT ME!
100     */
101    public int doEndTag() throws javax.servlet.jsp.JspException {
102       try {
103          String fieldValue = "";
104 
105          if (!Util.getTrue(strict)) {
106             fieldValue = this.getFormattedFieldValue();
107          }
108 
109          String compareValue = this.getFieldValue();
110 
111          // "fieldValue" is the variable actually printed out
112          if (!ResultSetVector.isNull(getParentForm().getResultSetVector())) {
113             if (embeddedData != null) { //  embedded data is nested in this tag
114 
115                int    embeddedDataSize  = embeddedData.size();
116                int    i                 = 0;
117                String embeddedDataValue = null;
118 
119                while (i < embeddedDataSize) {
120                   StaticData aKeyValuePair = (StaticData) embeddedData.get(i);
121 
122                   if (aKeyValuePair.getKey()
123                                          .equals(compareValue)) {
124                      embeddedDataValue = aKeyValuePair.getValue();
125 
126                      break;
127                   }
128 
129                   i++;
130                }
131 
132                if (embeddedDataValue != null) {
133                   fieldValue = embeddedDataValue;
134 
135                   // we'll print out embedded value associated with the current value
136                }
137             }
138          }
139 
140          // PG, 2001-12-14
141          // If maxlength was input, trim display
142          String size = null;
143 
144          if (((size = this.getMaxlength()) != null)
145                    && (size.trim()
146                                  .length() > 0)) {
147             //convert to int
148             int count = Integer.parseInt(size);
149 
150             // Trim and add trim indicator (...)
151             if (count < fieldValue.length()) {
152                fieldValue = fieldValue.substring(0, count);
153                fieldValue += "...";
154             }
155          }
156 
157          // SM 2003-08-05
158          // if styleClass is present, render a SPAN with text included
159          String s = prepareStyles();
160 
161          if (Util.isNull(s)) {
162             pageContext.getOut()
163                        .write(fieldValue);
164          } else {
165             pageContext.getOut()
166                        .write("<span " + s + ">" + fieldValue + "</span>");
167          }
168       } catch (java.io.IOException ioe) {
169          logCat.error(ioe);
170          throw new JspException("IO Error: " + ioe.getMessage());
171       } catch (Exception e) {
172          logCat.error(e);
173          throw new JspException("Error: " + e.getMessage());
174       }
175 
176       return EVAL_PAGE;
177    }
178 
179 
180    /***
181     * DOCUMENT ME!
182     */
183    public void doFinally() {
184       embeddedData = null;
185       super.doFinally();
186    }
187 }