View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/taglib/JavascriptArrayTag.java,v 1.19 2006/01/13 13:38:51 hkollmann Exp $
3    * $Revision: 1.19 $
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.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  import org.dbforms.interfaces.IDataContainer;
30  import org.dbforms.interfaces.IEscaper;
31  import org.dbforms.interfaces.StaticData;
32  
33  import java.util.List;
34  import java.util.StringTokenizer;
35  
36  import javax.servlet.jsp.*;
37  
38  
39  
40  /***
41   * <p>
42   * This tag renders a javascript array with Embeded data.  Only Value is
43   * generated.
44   * </p>
45   *
46   * @author Eric Beaumier
47   */
48  public class JavascriptArrayTag extends AbstractScriptHandlerTag
49     implements IDataContainer, javax.servlet.jsp.tagext.TryCatchFinally {
50     private static Log logCat = LogFactory.getLog(JavascriptArrayTag.class
51                                                   .getName()); // logging category for this class
52     private List       embeddedData = null;
53     private String     name         = null;
54  
55     /***
56      * This method is a "hookup" for EmbeddedData - Tags which can assign the
57      * lines of data they loaded (by querying a database, or by rendering
58      * data-subelements, etc. etc.) and make the data available to this tag.
59      * [this method is defined in Interface DataContainer]
60      *
61      * @param embeddedData DOCUMENT ME!
62      */
63     public void setEmbeddedData(List embeddedData) {
64        this.embeddedData = embeddedData;
65     }
66  
67  
68     /***
69      * DOCUMENT ME!
70      *
71      * @return DOCUMENT ME!
72      */
73     public IEscaper getEscaper() {
74        return getConfig()
75                  .getEscaper();
76     }
77  
78  
79     /***
80      * DOCUMENT ME!
81      *
82      * @param name DOCUMENT ME!
83      */
84     public void setName(String name) {
85        this.name = name;
86     }
87  
88  
89  
90     /***
91      * DOCUMENT ME!
92      *
93      * @return DOCUMENT ME!
94      *
95      * @throws javax.servlet.jsp.JspException DOCUMENT ME!
96      * @throws JspException DOCUMENT ME!
97      */
98     public int doEndTag() throws javax.servlet.jsp.JspException {
99        StringBuffer tagBuf = new StringBuffer();
100 
101       if (embeddedData == null) { // no embedded data is nested in this tag
102          logCat.warn("No EmbeddedData provide for javascriptArray TagLib "
103                      + name);
104 
105          return EVAL_PAGE;
106       } else {
107          tagBuf.append("\n<script language=\"javascript\">\n");
108          tagBuf.append("   var " + name + " = new Array();\n");
109 
110          int embeddedDataSize = embeddedData.size();
111 
112          for (int i = 0; i < embeddedDataSize; i++) {
113             StaticData aKeyValuePair = (StaticData) embeddedData.get(i);
114             String       aKey = aKeyValuePair.getKey();
115             tagBuf.append("   ")
116                   .append(name)
117                   .append("[")
118                   .append(i)
119                   .append("] = new Array('")
120                   .append(aKey)
121                   .append("'");
122 
123             String          aValue = aKeyValuePair.getValue();
124 
125             StringTokenizer st = new StringTokenizer(aValue, ",");
126 
127             while (st.hasMoreTokens())
128                tagBuf.append(",'")
129                      .append(st.nextToken())
130                      .append("'");
131 
132             tagBuf.append(");\n");
133          }
134 
135          tagBuf.append("</script>\n");
136       }
137 
138       try {
139          pageContext.getOut()
140                     .write(tagBuf.toString());
141       } catch (java.io.IOException ioe) {
142          throw new JspException("IO Error: " + ioe.getMessage());
143       }
144 
145       return EVAL_PAGE;
146    }
147 
148 
149    /***
150     * DOCUMENT ME!
151     */
152    public void doFinally() {
153       name         = null;
154       embeddedData = null;
155    }
156 
157 
158    /***
159     * DOCUMENT ME!
160     *
161     * @return DOCUMENT ME!
162     *
163     * @throws javax.servlet.jsp.JspException DOCUMENT ME!
164     */
165    public int doStartTag() throws javax.servlet.jsp.JspException {
166       return EVAL_BODY_BUFFERED;
167    }
168 }