001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/jsf/tags/sakai_2-4-1/widgets/src/java/org/sakaiproject/jsf/renderer/AppletRenderer.java $
003: * $Id: AppletRenderer.java 22110 2007-03-02 23:40:17Z ray@media.berkeley.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2003, 2004 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.jsf.renderer;
021:
022: import java.io.IOException;
023: import javax.faces.component.UIComponent;
024: import javax.faces.component.UIOutput;
025: import javax.faces.context.FacesContext;
026: import javax.faces.context.ResponseWriter;
027: import javax.faces.render.Renderer;
028:
029: import org.sakaiproject.jsf.util.RendererUtil;
030: import java.util.Map;
031: import java.util.HashMap;
032: import java.util.Iterator;
033: import java.util.StringTokenizer;
034:
035: /**
036: * <p>Description: </p>
037: * <p>Render an applet in a browser-indepemdent way
038: *
039: * <p>Copyright: Copyright (c) 2005</p>
040: * <p>Organization: Sakai Project</p>
041: * @author Ed Smiley
042: * @version $Id: AppletRenderer.java 22110 2007-03-02 23:40:17Z ray@media.berkeley.edu $
043: */
044:
045: public class AppletRenderer extends Renderer {
046: private static final String IE_CODEBASE = "http://java.sun.com/products/plugin/1.4/jinstall-14-win32.cab#Version=1,4,0,mn";
047: private static final String NS_PLUGINSPAGE = "http://java.sun.com/products/plugin/1.4/plugin-install.html";
048: private static final String VERSION = "1.4";
049: private static final String DEFAULT_WIDTH = "200";
050: private static final String DEFAULT_HEIGHT = "200";
051: private static final String DEFAULT_VSPACE = "0";
052: private static final String DEFAULT_HSPACE = "0";
053:
054: public boolean supportsComponentType(UIComponent component) {
055: return (component instanceof UIOutput);
056: }
057:
058: /**
059: * @todo apply attributes
060: * <p>Faces render output method .</p>
061: * <p>Method Generator: org.sakaiproject.jsf.devtool.RenderMaker</p>
062: *
063: * @param context <code>FacesContext</code> for the current request
064: * @param component <code>UIComponent</code> being rendered
065: *
066: * @throws IOException if an input/output error occurs
067: */
068: public void encodeBegin(FacesContext context, UIComponent component)
069: throws IOException {
070: if (!component.isRendered()) {
071: return;
072: }
073:
074: String width = (String) RendererUtil.getDefaultedAttribute(
075: context, component, "width", DEFAULT_WIDTH);
076: String height = (String) RendererUtil.getDefaultedAttribute(
077: context, component, "height", DEFAULT_HEIGHT);
078: String vspace = (String) RendererUtil.getDefaultedAttribute(
079: context, component, "vspace", DEFAULT_VSPACE);
080: String hspace = (String) RendererUtil.getDefaultedAttribute(
081: context, component, "hspace", DEFAULT_HSPACE);
082:
083: String javaClass = (String) RendererUtil.getAttribute(context,
084: component, "javaClass");
085: String javaArchive = (String) RendererUtil.getAttribute(
086: context, component, "javaArchive");
087: String codebase = (String) RendererUtil.getAttribute(context,
088: component, "codebase");
089: String paramList = (String) RendererUtil.getAttribute(context,
090: component, "paramList");
091:
092: String name = (String) RendererUtil.getAttribute(context,
093: component, "name");
094: Map appParamMap = makeAppletParameters(paramList);
095:
096: if (null != codebase)
097: appParamMap.put("codebase", codebase);
098: if (null != javaArchive)
099: appParamMap.put("archive", javaArchive);
100: //if(null!=name) appParamMap.put("id", name);
101: appParamMap.put("code", javaClass);
102:
103: ResponseWriter writer = context.getResponseWriter();
104: renderApplet(width, height, vspace, hspace, codebase,
105: javaClass, javaArchive, name, appParamMap, writer);
106: }
107:
108: /**
109: * Accept a comma separated list of name value pairs.
110: * @todo quotations to escape out "," and "=".
111: * Usage: <sakai:applet ... paramList="param1=value1,param2=value2,..."...
112: * @param paramList
113: * @return
114: */
115: private Map makeAppletParameters(String paramList) {
116: Map appParamMap = new HashMap();
117: if (paramList != null) {
118: StringTokenizer paramsTokenizer = new StringTokenizer(
119: paramList, ",", false);
120: while (paramsTokenizer.hasMoreTokens()) {
121: StringTokenizer nameValueTokenizer = new StringTokenizer(
122: paramsTokenizer.nextToken(), "=", false);
123: if (nameValueTokenizer.countTokens() < 2)
124: continue;
125: String name = nameValueTokenizer.nextToken();
126: String value = nameValueTokenizer.nextToken();
127: appParamMap.put(name, value);
128: }
129: }
130: return appParamMap;
131: }
132:
133: /**
134: * Render applet in browser independent way.
135: * @param width value of the corresponding tag attribute
136: * @param height value of the corresponding tag attribute
137: * @param vspace value of the corresponding tag attribute
138: * @param hspace value of the corresponding tag attribute
139: * @param codebase value of the corresponding tag attribute
140: * @param javaClass value of the corresponding tag attribute
141: * @param javaArchive value of the corresponding tag attribute
142: * @param version value of the corresponding tag attribute
143: * @param appParamMap comma separated list of name value pairs from paramList
144: * tag attribute
145: *
146: * @param writer ResponseWriter
147: * @throws IOException
148: */
149: // <OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
150: // width="200" height="200" align="baseline"
151: // codebase="http://java.sun.com/products/plugin/1.4/jinstall-14-win32.cab#Version=1,4,0,mn">
152: // <PARAM NAME="code" VALUE="XYZApp.class">
153: // <PARAM NAME="codebase" VALUE="html/">
154: // <PARAM NAME="type" VALUE="application/x-java-applet;jpi-version=1.4">
155: // <PARAM NAME="model" VALUE="models/HyaluronicAcid.xyz">
156: // <PARAM NAME="scriptable" VALUE="true">
157: // <COMMENT>
158: // <EMBED type="application/x-java-applet;jpi-version=1.4" width="200"
159: // height="200" align="baseline" code="XYZApp.class"
160: // codebase="html/" model="models/HyaluronicAcid.xyz"
161: // pluginspage="http://java.sun.com/j2se/1.4/download.html">
162: // <NOEMBED>
163: // No Java 2 SDK, Standard Edition v 1.4 support for APPLET!!
164: // </NOEMBED>
165: // </EMBED>
166: // </COMMENT>
167: //</OBJECT>
168: private void renderApplet(String width, String height,
169: String vspace, String hspace, String codebase,
170: String javaClass, String javaArchive, String name,
171: Map appParamMap, ResponseWriter writer) throws IOException {
172: writer.write("");
173: writer.write("<OBJECT ");
174: writer
175: .write(" classid=\"clsid:8AD9C840-044E-11D1-B3E9-00805F499D93\" ");
176: writer.write(" width=\"" + width + "\" ");
177: writer.write(" height=\"" + height + "\" ");
178: writer.write(" hspace=\"" + hspace + "\" ");
179: writer.write(" vspace=\"" + vspace + "\" ");
180: writer.write(" codebase=\"" + IE_CODEBASE + "\"");
181: writer.write(" archive=\"" + javaArchive + "\"");
182: writer.write(" id=\"" + name + "\"");
183: writer.write(">");
184: // writer.write(" <PARAM name=\"java_code\" value=\"" + javaClass + "\">");
185: // writer.write(" <PARAM name=\"java_archive\" value=\"" + javaArchive +
186: // ">");
187: writer
188: .write(" <PARAM name=\"type\" value=\"application/x-java-applet;version="
189: + VERSION + "\">");
190: renderObjectParams(writer, appParamMap);
191: writer.write("<COMMENT>");
192: writer.write("<EMBED ");
193: writer.write(" type=\"application/x-java-applet;version="
194: + VERSION + "\" ");
195: writer.write(" width=\"" + width + "\" ");
196: writer.write(" height=\"" + height + "\" ");
197: writer.write(" hspace=\"" + hspace + "\" ");
198: writer.write(" vspace=\"" + vspace + "\" ");
199: writer.write(" pluginspage=\"" + NS_PLUGINSPAGE + "\" ");
200: // writer.write(" java_code=\"" + javaClass + "\"");
201: // writer.write(" java_archive=\"" + javaArchive + "");
202: // if the name is not null duplicate it to the id param
203: if (null != name) {
204: appParamMap.put("id", name);
205: appParamMap.put("name", name);
206: }
207: renderEmbedParams(writer, appParamMap);
208: writer.write("/>");
209: writer.write("");
210: writer.write("<NOEMBED>");
211: writer.write(" <font color=\"red\">");
212: writer
213: .write(" Note: Requires Java Plug-in " + VERSION
214: + ".");
215: writer.write(" </b></font><br/>");
216: writer.write("</NOEMBED>");
217: writer.write("</COMMENT>");
218: writer.write("</OBJECT>");
219: }
220:
221: /**
222: * Render Map of Embed tag params,
223: * @param writer
224: * @param appParamMap
225: * @throws IOException
226: */
227: private void renderEmbedParams(ResponseWriter writer,
228: Map appParamMap) throws IOException {
229: Iterator paramIter = appParamMap.keySet().iterator();
230: while (paramIter.hasNext()) {
231: String name = (String) paramIter.next();
232: String value = (String) appParamMap.get(name);
233: writer.write(" " + name + "=\"" + value + "\" ");
234: }
235: }
236:
237: /**
238: * Render Map of Object PARAMs.
239: * @param writer
240: * @param appParamMap
241: * @throws IOException
242: */
243: private void renderObjectParams(ResponseWriter writer,
244: Map appParamMap) throws IOException {
245: Iterator iter = appParamMap.keySet().iterator();
246: while (iter.hasNext()) {
247: String name = (String) iter.next();
248: String value = (String) appParamMap.get(name);
249: writer.write(" <PARAM name=\"" + name + "\" value=\""
250: + value + "\">");
251: }
252: }
253: }
|