001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsp.java;
030:
031: import com.caucho.jsp.JspParseException;
032: import com.caucho.vfs.WriteStream;
033: import com.caucho.xml.QName;
034:
035: import java.io.IOException;
036: import java.util.ArrayList;
037:
038: /**
039: * Represents a Java scriptlet.
040: */
041: public class JspPlugin extends JspNode {
042: private static final QName NAME = new QName("name");
043: private static final QName TYPE = new QName("type");
044: private static final QName CODE = new QName("code");
045: private static final QName CODEBASE = new QName("codebase");
046: private static final QName IEPLUGINURL = new QName("iepluginurl");
047: private static final QName JREVERSION = new QName("jreversion");
048: private static final QName NSPLUGINURL = new QName("nspluginurl");
049: private static final QName WIDTH = new QName("width");
050: private static final QName HEIGHT = new QName("height");
051: private static final QName ALIGN = new QName("align");
052: private static final QName VSPACE = new QName("vspace");
053: private static final QName HSPACE = new QName("hspace");
054: private static final QName ARCHIVE = new QName("archive");
055:
056: static final String IE_CLSID = "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93";
057: static final String IE_URL = "http://java.sun.com/products/plugin/1.2.2/jinstall-1_2_2-win.cab#Version=1,2,2,0";
058: static final String NS_URL = "http://java.sun.com/products/plugin/";
059:
060: private String _name;
061: private String _type;
062: private String _code;
063:
064: private String _jreversion;
065: private String _nspluginurl;
066: private String _iepluginurl;
067:
068: private String _width;
069: private String _height;
070: private String _archive;
071:
072: private ArrayList<String> _attrNames = new ArrayList<String>();
073: private ArrayList<String> _attrValues = new ArrayList<String>();
074:
075: private JspParams _params;
076:
077: private JspFallback _fallback;
078:
079: /**
080: * Adds an attribute.
081: */
082: public void addAttribute(QName name, String value)
083: throws JspParseException {
084: if (NAME.equals(name)) {
085: _name = value;
086:
087: checkForbidExpression("name", value);
088: } else if (TYPE.equals(name)) {
089: _type = value;
090:
091: checkForbidExpression("type", value);
092: } else if (CODE.equals(name)) {
093: _code = value;
094:
095: checkForbidExpression("code", value);
096: } else if (IEPLUGINURL.equals(name)) {
097: _iepluginurl = value;
098:
099: checkForbidExpression("ispluginurl", value);
100: } else if (JREVERSION.equals(name)) {
101: _jreversion = value;
102:
103: checkForbidExpression("jreversion", value);
104: } else if (NSPLUGINURL.equals(name)) {
105: _nspluginurl = value;
106:
107: checkForbidExpression("nspluginurl", value);
108: } else if (WIDTH.equals(name)) {
109: } else if (HEIGHT.equals(name)) {
110: } else if (ALIGN.equals(name)) {
111: checkForbidExpression("align", value);
112: } else if (VSPACE.equals(name)) {
113: checkForbidExpression("vspace", value);
114: } else if (HSPACE.equals(name)) {
115: checkForbidExpression("hspace", value);
116: } else if (ARCHIVE.equals(name)) {
117: checkForbidExpression("archive", value);
118: } else if (CODEBASE.equals(name)) {
119: checkForbidExpression("codebase", value);
120: } else
121: super .addAttribute(name, value);
122:
123: _attrNames.add(name.getName());
124: _attrValues.add(value);
125: }
126:
127: private void checkForbidExpression(String attribute, String value)
128: throws JspParseException {
129: if (hasELAttribute(value))
130: throw error(L.l("'{0}' may not have EL expression '{1}'",
131: attribute, value));
132: else if (hasRuntimeAttribute(value))
133: throw error(L.l("'{0}' may not have RT expression '{1}'",
134: attribute, value));
135: }
136:
137: /**
138: * Adds a child.
139: */
140: public void addChild(JspNode node) throws JspParseException {
141: if (node instanceof JspParams) {
142: _params = (JspParams) node;
143: } else if (node instanceof JspFallback) {
144: _fallback = (JspFallback) node;
145: } else if (node instanceof JspBody) {
146: } else {
147: super .addChild(node);
148: }
149: }
150:
151: /**
152: * Adds a child.
153: */
154: public void addChildEnd(JspNode node) throws JspParseException {
155: if (node instanceof JspBody) {
156: JspBody body = (JspBody) node;
157:
158: for (JspNode child : body.getChildren())
159: addChild(child);
160: } else
161: super .addChildEnd(node);
162: }
163:
164: /**
165: * Called after the attributes complete.
166: */
167: public void endElement() throws JspParseException {
168: if (_code == null)
169: throw error(L.l("<jsp:plugin> expects a `code' attribute."));
170:
171: if (_type == null)
172: throw error(L.l("<jsp:plugin> expects a `type' attribute."));
173: else if (!_type.equals("applet") && !_type.equals("bean"))
174: throw error(L
175: .l("`type' attribute of <jsp:plugin> must either be `applet' or `bean'."));
176:
177: if (_iepluginurl == null)
178: _iepluginurl = IE_URL;
179:
180: if (_nspluginurl == null)
181: _nspluginurl = NS_URL;
182: }
183:
184: /**
185: * Generates the XML text representation for the tag validation.
186: *
187: * @param os write stream to the generated XML.
188: */
189: public void printXml(WriteStream os) throws IOException {
190: os.print("<jsp:plugin/>");
191: }
192:
193: /**
194: * Generates the code for the scriptlet
195: *
196: * @param out the output writer for the generated java.
197: */
198: public void generate(JspJavaWriter out) throws Exception {
199: String type = null;
200: if (_type.equals("applet"))
201: type = "application/x-java-applet";
202: else if (_type.equals("bean"))
203: type = "application/x-java-bean";
204:
205: if (_jreversion != null)
206: type = type + ";version=" + _jreversion;
207:
208: printText(out, "<object classid=\"" + IE_CLSID + "\"");
209: printText(out, " codebase=\"" + _iepluginurl + "\"");
210:
211: generateObjectParams(out, false);
212:
213: printText(out, "<param name=\"type\" value=\"" + type + "\">\n");
214:
215: printText(out, "<comment>");
216:
217: printText(out, "<embed type=\"" + type + "\"");
218: printText(out, " codebase=\"" + _nspluginurl + "\"");
219:
220: generateObjectParams(out, true);
221:
222: printText(out, ">\n<noembed></comment>");
223:
224: if (_fallback != null)
225: _fallback.generateFallback(out);
226:
227: printText(out, "</noembed></embed></object>\n");
228: }
229:
230: /**
231: * Generates the parameters for the jsp:plugin object.
232: */
233: protected void generateObjectParams(JspJavaWriter out,
234: boolean isEmbed) throws Exception {
235: for (int i = 0; i < _attrNames.size(); i++) {
236: String name = _attrNames.get(i);
237: String value = _attrValues.get(i);
238:
239: if (name.equals("type") || name.equals("jreversion")
240: || name.equals("iepluginurl")
241: || name.equals("nspluginurl")
242: || name.equals("code") || name.equals("archive")
243: || name.equals("codebase") || name.equals("object"))
244: continue;
245:
246: printText(out, " " + name + "=\"");
247:
248: out.println("out.print("
249: + generateValue(String.class, value) + ");");
250:
251: printText(out, "\"");
252: }
253:
254: if (!isEmbed)
255: printText(out, ">\n");
256:
257: for (int i = 0; i < _attrNames.size(); i++) {
258: String name = _attrNames.get(i);
259: String value = _attrValues.get(i);
260:
261: if (name.equals("archive"))
262: name = "java_archive";
263: else if (name.equals("codebase"))
264: name = "java_codebase";
265: else if (name.equals("code"))
266: name = "java_code";
267: else if (name.equals("object"))
268: name = "java_object";
269: else
270: continue;
271:
272: if (isEmbed) {
273: printText(out, " " + name + "=\"");
274:
275: if (hasRuntimeAttribute(value))
276: out.println("out.print("
277: + getRuntimeAttribute(value) + ");");
278: else
279: printText(out, value);
280:
281: printText(out, "\"");
282: } else {
283: printText(out, "<param name=\"" + name + "\" value=\"");
284:
285: if (hasRuntimeAttribute(value))
286: out.println("out.print("
287: + getRuntimeAttribute(value) + ");");
288: else
289: printText(out, value);
290:
291: printText(out, "\">\n");
292: }
293: }
294:
295: if (_params == null)
296: return;
297:
298: ArrayList<JspParam> paramList = _params.getParams();
299:
300: for (int i = 0; i < paramList.size(); i++) {
301: JspParam param = paramList.get(i);
302:
303: String name = param.getName();
304: String value = param.getValue();
305:
306: if (isEmbed)
307: printText(out, " " + name + "=\"");
308: else
309: printText(out, "<param name=\"" + name + "\" value=\"");
310:
311: if (hasRuntimeAttribute(value)) {
312: out.println("out.print(" + getRuntimeAttribute(value)
313: + ");");
314: } else
315: printText(out, value);
316:
317: if (isEmbed)
318: printText(out, "\"");
319: else
320: printText(out, "\">\n");
321: }
322: }
323:
324: private void printText(JspJavaWriter out, String text)
325: throws Exception {
326: out.print("out.print(\"");
327: out.printJavaString(text);
328: out.println("\");");
329: }
330: }
|