KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/JavascriptArrayTag.java,v 1.14 2004/10/12 15:49:45 hkollmann Exp $
3  * $Revision: 1.14 $
4  * $Date: 2004/10/12 15:49:45 $
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.util.*;
30
31 import java.util.List JavaDoc;
32 import java.util.StringTokenizer JavaDoc;
33
34 import javax.servlet.jsp.*;
35
36
37
38 /**
39  * <p>
40  * This tag renders a javascript array with Embeded data. Only Value is
41  * generated.
42  * </p>
43  *
44  * @author Eric Beaumier
45  */

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

61    public void setEmbeddedData(List JavaDoc embeddedData) {
62       this.embeddedData = embeddedData;
63    }
64
65
66    /**
67     * DOCUMENT ME!
68     *
69     * @return DOCUMENT ME!
70     */

71    public IEscaper getEscaper() {
72       return getConfig()
73                 .getEscaper();
74    }
75
76
77    /**
78     * DOCUMENT ME!
79     *
80     * @param name DOCUMENT ME!
81     */

82    public void setName(String JavaDoc name) {
83       this.name = name;
84    }
85
86
87    /**
88     * DOCUMENT ME!
89     *
90     * @param t DOCUMENT ME!
91     *
92     * @throws Throwable DOCUMENT ME!
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       StringBuffer JavaDoc tagBuf = new StringBuffer JavaDoc();
109
110       if (embeddedData == null) { // no embedded data is nested in this tag
111
logCat.warn("No EmbeddedData provide for javascriptArray TagLib "
112                      + name);
113
114          return EVAL_PAGE;
115       } else {
116          tagBuf.append("\n<script language=\"javascript\">\n");
117          tagBuf.append(" var " + name + " = new Array();\n");
118
119          int embeddedDataSize = embeddedData.size();
120
121          for (int i = 0; i < embeddedDataSize; i++) {
122             KeyValuePair aKeyValuePair = (KeyValuePair) embeddedData.get(i);
123             String JavaDoc aKey = aKeyValuePair.getKey();
124             tagBuf.append(" ")
125                   .append(name)
126                   .append("[")
127                   .append(i)
128                   .append("] = new Array('")
129                   .append(aKey)
130                   .append("'");
131
132             String JavaDoc aValue = aKeyValuePair.getValue();
133
134             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(aValue, ",");
135
136             while (st.hasMoreTokens())
137                tagBuf.append(",'")
138                      .append(st.nextToken())
139                      .append("'");
140
141             tagBuf.append(");\n");
142          }
143
144          tagBuf.append("</script>\n");
145       }
146
147       try {
148          pageContext.getOut()
149                     .write(tagBuf.toString());
150       } catch (java.io.IOException JavaDoc ioe) {
151          throw new JspException("IO Error: " + ioe.getMessage());
152       }
153
154       return EVAL_PAGE;
155    }
156
157
158    /**
159     * DOCUMENT ME!
160     */

161    public void doFinally() {
162       name = null;
163       embeddedData = null;
164    }
165
166
167    /**
168     * DOCUMENT ME!
169     *
170     * @return DOCUMENT ME!
171     *
172     * @throws javax.servlet.jsp.JspException DOCUMENT ME!
173     */

174    public int doStartTag() throws javax.servlet.jsp.JspException JavaDoc {
175       return EVAL_BODY_BUFFERED;
176    }
177 }
178
Popular Tags