KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/DbDataContainerLabelTag.java,v 1.22 2004/08/24 00:32:35 javajunkie Exp $
3  * $Revision: 1.22 $
4  * $Date: 2004/08/24 00:32:35 $
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
31 import org.dbforms.util.KeyValuePair;
32 import org.dbforms.util.Util;
33
34 import java.util.List JavaDoc;
35
36 import javax.servlet.jsp.JspException JavaDoc;
37
38
39
40 /**
41  * this tag renders a dabase-datadriven LABEL, which is a passive element (it
42  * can't be changed by the user) - it is predestinated for use with read-only
43  * data (i.e. primary keys you don't want the user to change, etc) so far it
44  * is equivalent to DbLabelTag. But this tag may have a body containing any
45  * kind of EmbeddedData - tag. i put this feature into a seperate class for
46  * performance reasons (we do not want the overhead of pushing and poping the
47  * jsp writer to and off the stack
48  *
49  * @author Joachim Peer
50  */

51 public class DbDataContainerLabelTag extends DbBaseHandlerTag
52    implements DataContainer, javax.servlet.jsp.tagext.TryCatchFinally JavaDoc {
53    // logging category for this class
54
private List JavaDoc embeddedData = null;
55    private static Log logCat = LogFactory.getLog(DbDataContainerLabelTag.class);
56    private String JavaDoc strict = "false";
57
58    /**
59     * This method is a "hookup" for EmbeddedData - Tags which can assign the
60     * lines of data they loaded (by querying a database, or by rendering
61     * data-subelements, etc. etc.) and make the data available to this tag.
62     * [this method is defined in Interface DataContainer]
63     *
64     * @param embeddedData DOCUMENT ME!
65     */

66    public void setEmbeddedData(List JavaDoc embeddedData) {
67       this.embeddedData = embeddedData;
68    }
69
70
71    /**
72     * DOCUMENT ME!
73     *
74     * @param string
75     */

76    public void setStrict(String JavaDoc string) {
77       strict = string;
78    }
79
80
81    /**
82     * DOCUMENT ME!
83     *
84     * @return
85     */

86    public String JavaDoc getStrict() {
87       return strict;
88    }
89
90
91    /**
92     * @see javax.servlet.jsp.tagext.TryCatchFinally#doCatch(java.lang.Throwable)
93     */

94    public void doCatch(Throwable JavaDoc t) throws Throwable JavaDoc {
95       throw t;
96    }
97
98
99    /**
100     * DOCUMENT ME!
101     *
102     * @return DOCUMENT ME!
103     *
104     * @throws javax.servlet.jsp.JspException DOCUMENT ME!
105     * @throws JspException DOCUMENT ME!
106     */

107    public int doEndTag() throws javax.servlet.jsp.JspException JavaDoc {
108       try {
109          String JavaDoc fieldValue = "";
110
111          if (!Util.getTrue(strict)) {
112             fieldValue = this.getFormattedFieldValue();
113          }
114
115          String JavaDoc compareValue = this.getFieldValue();
116
117          // "fieldValue" is the variable actually printed out
118
if (!ResultSetVector.isNull(getParentForm().getResultSetVector())) {
119             if (embeddedData != null) { // embedded data is nested in this tag
120

121                int embeddedDataSize = embeddedData.size();
122                int i = 0;
123                String JavaDoc embeddedDataValue = null;
124
125                while (i < embeddedDataSize) {
126                   KeyValuePair aKeyValuePair = (KeyValuePair) embeddedData.get(i);
127
128                   if (aKeyValuePair.getKey()
129                                          .equals(compareValue)) {
130                      embeddedDataValue = aKeyValuePair.getValue();
131
132                      break;
133                   }
134
135                   i++;
136                }
137
138                if (embeddedDataValue != null) {
139                   fieldValue = embeddedDataValue;
140
141                   // we'll print out embedded value associated with the current value
142
}
143             }
144          }
145
146          // PG, 2001-12-14
147
// If maxlength was input, trim display
148
String JavaDoc size = null;
149
150          if (((size = this.getMaxlength()) != null)
151                    && (size.trim()
152                                  .length() > 0)) {
153             //convert to int
154
int count = Integer.parseInt(size);
155
156             // Trim and add trim indicator (...)
157
if (count < fieldValue.length()) {
158                fieldValue = fieldValue.substring(0, count);
159                fieldValue += "...";
160             }
161          }
162
163          // SM 2003-08-05
164
// if styleClass is present, render a SPAN with text included
165
String JavaDoc s = prepareStyles();
166
167          if (Util.isNull(s)) {
168             pageContext.getOut()
169                        .write(fieldValue);
170          } else {
171             pageContext.getOut()
172                        .write("<span " + s + ">" + fieldValue + "</span>");
173          }
174       } catch (java.io.IOException JavaDoc ioe) {
175          logCat.error(ioe);
176          throw new JspException JavaDoc("IO Error: " + ioe.getMessage());
177       } catch (Exception JavaDoc e) {
178          logCat.error(e);
179          throw new JspException JavaDoc("Error: " + e.getMessage());
180       }
181
182       return EVAL_PAGE;
183    }
184
185
186    /**
187     * DOCUMENT ME!
188     */

189    public void doFinally() {
190       embeddedData = null;
191       super.doFinally();
192    }
193 }
194
Popular Tags