1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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());
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) {
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 }