KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > java > JspPlugin


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source 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, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jsp.java;
30
31 import com.caucho.jsp.JspParseException;
32 import com.caucho.vfs.WriteStream;
33 import com.caucho.xml.QName;
34
35 import java.io.IOException JavaDoc;
36 import java.util.ArrayList JavaDoc;
37
38 /**
39  * Represents a Java scriptlet.
40  */

41 public class JspPlugin extends JspNode {
42   private static final QName NAME = new QName("name");
43   private static final QName TYPE = new QName("type");
44   private static final QName CODE = new QName("code");
45   private static final QName CODEBASE = new QName("codebase");
46   private static final QName IEPLUGINURL = new QName("iepluginurl");
47   private static final QName JREVERSION = new QName("jreversion");
48   private static final QName NSPLUGINURL = new QName("nspluginurl");
49   private static final QName WIDTH = new QName("width");
50   private static final QName HEIGHT = new QName("height");
51   private static final QName ALIGN = new QName("align");
52   private static final QName VSPACE = new QName("vspace");
53   private static final QName HSPACE = new QName("hspace");
54   private static final QName ARCHIVE = new QName("archive");
55   
56   static final String JavaDoc IE_CLSID = "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93";
57   static final String JavaDoc IE_URL = "http://java.sun.com/products/plugin/1.2.2/jinstall-1_2_2-win.cab#Version=1,2,2,0";
58   static final String JavaDoc NS_URL = "http://java.sun.com/products/plugin/";
59   
60   private String JavaDoc _name;
61   private String JavaDoc _type;
62   private String JavaDoc _code;
63
64   private String JavaDoc _jreversion;
65   private String JavaDoc _nspluginurl;
66   private String JavaDoc _iepluginurl;
67
68   private String JavaDoc _width;
69   private String JavaDoc _height;
70   private String JavaDoc _archive;
71
72   private ArrayList JavaDoc<String JavaDoc> _attrNames = new ArrayList JavaDoc<String JavaDoc>();
73   private ArrayList JavaDoc<String JavaDoc> _attrValues = new ArrayList JavaDoc<String JavaDoc>();
74
75   private JspParams _params;
76
77   private JspFallback _fallback;
78   
79   /**
80    * Adds an attribute.
81    */

82   public void addAttribute(QName name, String JavaDoc value)
83     throws JspParseException
84   {
85     if (NAME.equals(name)) {
86       _name = value;
87       
88       checkForbidExpression("name", value);
89     }
90     else if (TYPE.equals(name)) {
91       _type = value;
92
93       checkForbidExpression("type", value);
94     }
95     else if (CODE.equals(name)) {
96       _code = value;
97
98       checkForbidExpression("code", value);
99     }
100     else if (IEPLUGINURL.equals(name)) {
101       _iepluginurl = value;
102       
103       checkForbidExpression("ispluginurl", value);
104     }
105     else if (JREVERSION.equals(name)) {
106       _jreversion = value;
107     
108       checkForbidExpression("jreversion", value);
109     }
110     else if (NSPLUGINURL.equals(name)) {
111       _nspluginurl = value;
112     
113       checkForbidExpression("nspluginurl", value);
114     }
115     else if (WIDTH.equals(name)) {
116     }
117     else if (HEIGHT.equals(name)) {
118     }
119     else if (ALIGN.equals(name)) {
120       checkForbidExpression("align", value);
121     }
122     else if (VSPACE.equals(name)) {
123       checkForbidExpression("vspace", value);
124     }
125     else if (HSPACE.equals(name)) {
126       checkForbidExpression("hspace", value);
127     }
128     else if (ARCHIVE.equals(name)) {
129       checkForbidExpression("archive", value);
130     }
131     else if (CODEBASE.equals(name)) {
132       checkForbidExpression("codebase", value);
133     }
134     else
135       super.addAttribute(name, value);
136
137     _attrNames.add(name.getName());
138     _attrValues.add(value);
139   }
140
141   private void checkForbidExpression(String JavaDoc attribute, String JavaDoc value)
142     throws JspParseException
143   {
144     if (hasELAttribute(value))
145       throw error(L.l("'{0}' may not have EL expression '{1}'",
146               attribute, value));
147     else if (hasRuntimeAttribute(value))
148       throw error(L.l("'{0}' may not have RT expression '{1}'",
149               attribute, value));
150   }
151
152   /**
153    * Adds a child.
154    */

155   public void addChild(JspNode node)
156     throws JspParseException
157   {
158     if (node instanceof JspParams) {
159       _params = (JspParams) node;
160     }
161     else if (node instanceof JspFallback) {
162       _fallback = (JspFallback) node;
163     }
164     else if (node instanceof JspBody) {
165     }
166     else {
167       super.addChild(node);
168     }
169   }
170
171   /**
172    * Adds a child.
173    */

174   public void addChildEnd(JspNode node)
175     throws JspParseException
176   {
177     if (node instanceof JspBody) {
178       JspBody body = (JspBody) node;
179
180       for (JspNode child : body.getChildren())
181     addChild(child);
182     }
183     else
184       super.addChildEnd(node);
185   }
186
187   /**
188    * Called after the attributes complete.
189    */

190   public void endElement()
191     throws JspParseException
192   {
193     if (_code == null)
194       throw error(L.l("<jsp:plugin> expects a `code' attribute."));
195     
196     if (_type == null)
197       throw error(L.l("<jsp:plugin> expects a `type' attribute."));
198     else if (! _type.equals("applet") && ! _type.equals("bean"))
199       throw error(L.l("`type' attribute of <jsp:plugin> must either be `applet' or `bean'."));
200     
201     if (_iepluginurl == null)
202       _iepluginurl = IE_URL;
203     
204     if (_nspluginurl == null)
205       _nspluginurl = NS_URL;
206   }
207
208   /**
209    * Generates the XML text representation for the tag validation.
210    *
211    * @param os write stream to the generated XML.
212    */

213   public void printXml(WriteStream os)
214     throws IOException JavaDoc
215   {
216     os.print("<jsp:plugin/>");
217   }
218
219   /**
220    * Generates the code for the scriptlet
221    *
222    * @param out the output writer for the generated java.
223    */

224   public void generate(JspJavaWriter out)
225     throws Exception JavaDoc
226   {
227     String JavaDoc type = null;
228     if (_type.equals("applet"))
229       type = "application/x-java-applet";
230     else if (_type.equals("bean"))
231       type = "application/x-java-bean";
232
233     if (_jreversion != null)
234       type = type + ";version=" + _jreversion;
235     
236     printText(out, "<object classid=\"" + IE_CLSID + "\"");
237     printText(out, " codebase=\"" + _iepluginurl + "\"");
238
239     generateObjectParams(out, false);
240
241     printText(out, "<param name=\"type\" value=\"" + type + "\">\n");
242
243     printText(out, "<comment>");
244     
245     printText(out, "<embed type=\"" + type + "\"");
246     printText(out, " codebase=\"" + _nspluginurl + "\"");
247     
248     generateObjectParams(out, true);
249     
250     printText(out, ">\n<noembed></comment>");
251
252     if (_fallback != null)
253       _fallback.generateFallback(out);
254
255     printText(out, "</noembed></embed></object>\n");
256   }
257
258   /**
259    * Generates the parameters for the jsp:plugin object.
260    */

261   protected void generateObjectParams(JspJavaWriter out, boolean isEmbed)
262     throws Exception JavaDoc
263   {
264     for (int i = 0; i < _attrNames.size(); i++) {
265       String JavaDoc name = _attrNames.get(i);
266       String JavaDoc value = _attrValues.get(i);
267       
268       if (name.equals("type") || name.equals("jreversion") ||
269           name.equals("iepluginurl") || name.equals("nspluginurl") ||
270           name.equals("code") || name.equals("archive") ||
271           name.equals("codebase") || name.equals("object"))
272         continue;
273
274       printText(out, " " + name + "=\"");
275
276       out.println("out.print(" + generateValue(String JavaDoc.class, value) + ");");
277       
278       printText(out, "\"");
279     }
280     
281     if (! isEmbed)
282       printText(out, ">\n");
283
284     for (int i = 0; i < _attrNames.size(); i++) {
285       String JavaDoc name = _attrNames.get(i);
286       String JavaDoc value = _attrValues.get(i);
287       
288       if (name.equals("archive"))
289         name = "java_archive";
290       else if (name.equals("codebase"))
291         name = "java_codebase";
292       else if (name.equals("code"))
293         name = "java_code";
294       else if (name.equals("object"))
295         name = "java_object";
296       else
297         continue;
298
299       if (isEmbed) {
300         printText(out, " " + name + "=\"");
301     
302     if (hasRuntimeAttribute(value))
303       out.println("out.print(" + getRuntimeAttribute(value) + ");");
304     else
305       printText(out, value);
306
307     printText(out, "\"");
308       }
309       else {
310         printText(out, "<param name=\"" + name + "\" value=\"");
311     
312     if (hasRuntimeAttribute(value))
313       out.println("out.print(" + getRuntimeAttribute(value) + ");");
314     else
315       printText(out, value);
316
317     printText(out, "\">\n");
318       }
319     }
320
321     if (_params == null)
322       return;
323     
324     ArrayList JavaDoc<JspParam> paramList = _params.getParams();
325
326     for (int i = 0; i < paramList.size(); i++) {
327       JspParam param = paramList.get(i);
328
329       String JavaDoc name = param.getName();
330       String JavaDoc value = param.getValue();
331
332       if (isEmbed)
333         printText(out, " " + name + "=\"");
334       else
335         printText(out, "<param name=\"" + name + "\" value=\"");
336
337       if (hasRuntimeAttribute(value)) {
338         out.println("out.print(" + getRuntimeAttribute(value) + ");");
339       }
340       else
341         printText(out, value);
342
343       if (isEmbed)
344         printText(out, "\"");
345       else
346         printText(out, "\">\n");
347     }
348   }
349
350   private void printText(JspJavaWriter out, String JavaDoc text)
351     throws Exception JavaDoc
352   {
353     out.print("out.print(\"");
354     out.printJavaString(text);
355     out.println("\");");
356   }
357 }
358
359
Popular Tags