001: /*
002: * The Apache Software License, Version 1.1
003: *
004: * Copyright (c) 1999 The Apache Software Foundation. All rights
005: * reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution, if
020: * any, must include the following acknowlegement:
021: * "This product includes software developed by the
022: * Apache Software Foundation (http://www.apache.org/)."
023: * Alternately, this acknowlegement may appear in the software itself,
024: * if and wherever such third-party acknowlegements normally appear.
025: *
026: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
027: * Foundation" must not be used to endorse or promote products derived
028: * from this software without prior written permission. For written
029: * permission, please contact apache@apache.org.
030: *
031: * 5. Products derived from this software may not be called "Apache"
032: * nor may "Apache" appear in their names without prior written
033: * permission of the Apache Group.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of the Apache Software Foundation. For more
051: * information on the Apache Software Foundation, please see
052: * <http://www.apache.org/>.
053: *
054: */
055:
056: package com.sun.portal.providers.jsp.jasper3.jasper.compiler;
057:
058: import java.util.Hashtable;
059: import java.util.Enumeration;
060:
061: import com.sun.portal.providers.jsp.jasper3.jasper.JasperException;
062: import com.sun.portal.providers.jsp.jasper3.jasper.Constants;
063: import com.sun.portal.providers.jsp.jasper3.jasper.JspCompilationContext;
064:
065: /**
066: * Generator for <jsp:plugin>
067: *
068: * @author Rajiv Mordani [mode@eng.sun.com]
069: */
070: public class PluginGenerator extends GeneratorBase implements
071: ServiceMethodPhase {
072: Hashtable attrs;
073: Hashtable param;
074: String fallback;
075:
076: String ieClassId;
077: Mark start;
078:
079: public PluginGenerator(Mark start, Hashtable attrs,
080: Hashtable param, String fallback) {
081: this .attrs = attrs;
082: this .param = param;
083: this .fallback = fallback;
084: this .start = start;
085: }
086:
087: public void init(JspCompilationContext ctxt) throws JasperException {
088: ieClassId = ctxt.getOptions().getIeClassId();
089: }
090:
091: public void generate(ServletWriter writer, Class phase)
092: throws JasperException {
093: String type = getAttribute("type");
094: String code = getAttribute("code");
095: String codebase = getAttribute("codebase");
096: String archive = getAttribute("archive");
097: String jreversion = getAttribute("jreversion");
098: String name = getAttribute("name");
099: String nspluginurl = getAttribute("nspluginurl");
100: String iepluginurl = getAttribute("iepluginurl");
101:
102: if (type == null)
103: throw new CompileException(start, Constants
104: .getString("jsp.error.plugin.notype"));
105: if (code == null)
106: throw new CompileException(start, Constants
107: .getString("jsp.error.plugin.nocode"));
108:
109: writer.popIndent();
110: writer.println("/*Code generated for plugin*/");
111: writer.println("{"); // Work around multi-plugin-tag bug
112: writer.indent();
113: writer.print("out.println (\"<OBJECT classid=\\\"");
114: if (ieClassId == null)
115: writer.print(Constants.IE_PLUGIN_CLASSID);
116: else
117: writer.print(ieClassId);
118:
119: writer.print("\\\"");
120: generateCommon(writer);
121: writer.print(" codebase=\\\"");
122: if (iepluginurl == null)
123: writer.print(Constants.IE_PLUGIN_URL);
124: else
125: writer.print(iepluginurl);
126:
127: writer.print("\\\"");
128: writer.print(">\");");
129: // Done to avoid spaces being generated between the closing angular
130: // bracket of the tag.
131: writer.println();
132: writer.indent();
133: writer.print("out.println (\"<PARAM name=\\\"java_code\\\"");
134: writer.print(" value=\\\"");
135: writer.print(code);
136: writer.print("\\\"");
137: writer.print(">\");");
138: writer.println();
139:
140: if (codebase != null) {
141: writer.indent();
142: writer
143: .print("out.println (\"<PARAM name=\\\"java_codebase\\\"");
144: writer.print(" value=\\\"");
145: writer.print(codebase);
146: writer.print("\\\"");
147: writer.print(">\");");
148: writer.println();
149: }
150:
151: if (archive != null) {
152: writer.indent();
153: writer
154: .print("out.println (\"<PARAM name=\\\"java_archive\\\"");
155: writer.print(" value=\\\"");
156: writer.print(archive);
157: writer.print("\\\"");
158: writer.print(">\");");
159: writer.println();
160: }
161:
162: writer.indent();
163: writer.print("out.println (\"<PARAM name=\\\"type\\\"");
164: writer.print(" value=\\\"");
165: if (type.equals("applet"))
166: writer.print("application/x-java-applet;");
167: else if (type.equals("bean"))
168: writer.print("application/x-java-bean;");
169: if (jreversion != null) {
170: writer.print("version=");
171: writer.print(jreversion);
172: }
173: writer.print("\\\"");
174: writer.print(">\");");
175: writer.println();
176:
177: // Evaluate params and if expression get the appropriate code for
178: // the expression
179:
180: Enumeration e = null;
181: String value[] = null;
182: String key = null;
183:
184: writer.indent();
185: writer.print("String _jspxString = null;");
186: writer.println();
187:
188: if (param != null) {
189: e = param.keys();
190: while (e.hasMoreElements()) {
191: key = (String) e.nextElement();
192: value = (String[]) param.get(key);
193: writer.indent();
194: //Check to see if the param is an expression, if so
195: //evaluate that and put the value.
196: String temp = null;
197: if (JspUtil.isExpression(value[0])) {
198: temp = JspUtil.getExpr(value[0]);
199: //value[0] = JspUtil.getExpr(value[0]);
200: writer.print("_jspxString =" + temp + ";");
201: } else {
202: writer.print("_jspxString = \"" + value[0] + "\";");
203: }
204: writer.println();
205: writer.indent();
206: writer.print("out.println (\"<PARAM name=\\\"");
207: if (key.equalsIgnoreCase("object"))
208: writer.print("java_object");
209: else if (key.equalsIgnoreCase("type"))
210: writer.print("java_type");
211: else
212: writer.print(key);
213: writer.print("\\\"");
214: writer.print(" value=\\\"\"");
215: writer.print("+ _jspxString + ");
216: writer.print("\"\\\"");
217: writer.print(">\");");
218: writer.println();
219: }
220: }
221:
222: writer.indent();
223: writer.print("String [][] _jspxNSString = null;");
224: writer.println();
225: writer.indent();
226: writer.print("int i = 0;");
227: writer.println();
228: if (param != null) {
229: e = param.keys();
230: writer.indent();
231: writer.indent();
232: writer.print("_jspxNSString = new String [");
233: Integer temp = new Integer(param.size());
234: writer.print(temp.toString());
235: writer.print("][2];");
236: writer.println();
237: writer.indent();
238: key = null;
239: value = null;
240: while (e.hasMoreElements()) {
241: key = (String) e.nextElement();
242: value = (String[]) param.get(key);
243: writer.print("_jspxNSString [i][0] = \"");
244: if (key.equalsIgnoreCase("object"))
245: key = "java_object";
246: else if (key.equalsIgnoreCase("type"))
247: key = "java_type";
248: writer.print(key);
249: writer.print("\";");
250: writer.println();
251: writer.indent();
252:
253: //value = (String[]) param.get (key);
254: if (JspUtil.isExpression(value[0])) {
255: value[0] = JspUtil.getExpr(value[0]);
256: writer.print("_jspxNSString[i][1] =" + value[0]
257: + ";");
258: } else {
259: writer.print("_jspxNSString[i][1] = \"" + value[0]
260: + "\";");
261: }
262: writer.println();
263: writer.indent();
264: writer.print("i++;");
265: writer.println();
266: }
267: }
268:
269: writer.println("out.println (\"<COMMENT>\");");
270: writer.indent();
271: writer.print("out.print (\"<EMBED type=\\\"");
272: if (type.equals("applet"))
273: writer.print("application/x-java-applet");
274: else if (type.equals("bean"))
275: writer.print("application/x-java-bean");
276: if (jreversion != null) {
277: writer.print(";version=");
278: writer.print(jreversion);
279: }
280: writer.print("\\\" ");
281: generateCommon(writer);
282: writer.print("pluginspage=\\\"");
283: if (nspluginurl == null)
284: writer.print(Constants.NS_PLUGIN_URL);
285: else
286: writer.print(nspluginurl);
287:
288: writer.print("\\\" ");
289:
290: writer.print("java_code=\\\"");
291: writer.print(code);
292: writer.print("\\\" ");
293: if (codebase != null) {
294: writer.print("java_codebase=\\\"");
295: writer.print(codebase);
296: writer.print("\\\" ");
297: }
298:
299: if (archive != null) {
300: writer.print("java_archive=\\\"");
301: writer.print(archive);
302: writer.print("\\\" ");
303: }
304:
305: writer.print("\");");
306: writer.println();
307:
308: writer.indent();
309: writer.print("if (_jspxNSString != null) {");
310: writer.println();
311: writer.pushIndent();
312: writer.indent();
313: writer.print("for (i = 0; i < _jspxNSString.length; i++) {");
314: writer.println();
315: writer.pushIndent();
316: writer.indent();
317: writer.print("out.println (");
318: writer.print(" _jspxNSString [i][0] + ");
319: writer.print("\"=\\\"\"");
320: writer.print(" + _jspxNSString[i][1]");
321: writer.print(" + \"\\\"\"");
322: writer.print(");");
323: writer.println();
324: writer.popIndent();
325: writer.println("}");
326: writer.popIndent();
327: writer.println("}");
328: writer.println("out.println (\">\");");
329: writer.println("out.println (\"<NOEMBED>\");");
330: writer.println("out.println (\"</COMMENT>\");");
331:
332: if (fallback != null) {
333: fallback = writer.quoteString(fallback);
334: writer.indent();
335: writer.print("out.println (");
336: writer.print(fallback);
337: writer.print(");");
338: writer.println();
339: }
340: writer.println("out.println (\"</NOEMBED></EMBED>\");");
341: writer.println("out.println (\"</OBJECT>\");");
342: writer.pushIndent();
343: writer.println("}"); // Work around multi-plugin-tag bug
344: }
345:
346: public void generateCommon(ServletWriter writer) {
347: String align = getAttribute("align");
348: String width = getAttribute("width");
349: String height = getAttribute("height");
350: String hspace = getAttribute("hspace");
351: String vspace = getAttribute("vspace");
352:
353: if (width != null) {
354: writer.print(" width=\\\"");
355: writer.print(width);
356: writer.print("\\\" ");
357: }
358:
359: if (height != null) {
360: writer.print(" height=\\\"");
361: writer.print(height);
362: writer.print("\\\" ");
363: }
364:
365: if (hspace != null) {
366: writer.print(" hspace=\\\"");
367: writer.print(hspace);
368: writer.print("\\\" ");
369: }
370:
371: if (vspace != null) {
372: writer.print(" vspace=\\\"");
373: writer.print(vspace);
374: writer.print("\\\" ");
375: }
376:
377: if (align != null) {
378: writer.print(" align=\\\"");
379: writer.print(align);
380: writer.print("\\\" ");
381: }
382:
383: }
384:
385: public String getAttribute(String name) {
386: return (attrs != null) ? (String) attrs.get(name) : null;
387: }
388: }
|