001: /* Copyright 2001, 2005 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005:
006: package org.jasig.portal.channels;
007:
008: import javax.xml.parsers.DocumentBuilderFactory;
009: import javax.xml.parsers.ParserConfigurationException;
010:
011: import org.jasig.portal.ChannelCacheKey;
012: import org.jasig.portal.GeneralRenderingException;
013: import org.jasig.portal.ICacheable;
014: import org.jasig.portal.PortalException;
015: import org.jasig.portal.i18n.LocaleManager;
016: import org.jasig.portal.utils.ResourceLoader;
017: import org.jasig.portal.utils.XSLT;
018: import org.w3c.dom.Document;
019: import org.w3c.dom.Element;
020: import org.xml.sax.ContentHandler;
021:
022: /** <p>Displays an applet. To pass in applet parameters, construct
023: * channel parameters whose keys start with the string "APPLET."</p>
024: * <p>For example, the key/value pair
025: * <code>APPLET.data=foo</code>
026: * as a channel parameter is translated to an applet parameter as
027: * <code>data=foo</code></p>
028: * <p><i>This code was adapted from uPortal 1.0's
029: * <code>org.jasig.portal.channels.CApplet</code></i></p>
030: * @author Ken Weiner, kweiner@unicon.net
031: * @version $Revision: 36683 $
032: */
033: public class CApplet extends BaseChannel implements ICacheable {
034: private static final String sslLocation = "CApplet/CApplet.ssl";
035:
036: /**
037: * Output channel content to the portal
038: * @param out a sax document handler
039: */
040: public void renderXML(ContentHandler out) throws PortalException {
041: Document doc = null;
042: try {
043: doc = DocumentBuilderFactory.newInstance()
044: .newDocumentBuilder().newDocument();
045: } catch (ParserConfigurationException pce) {
046: log.error("Error obtaining a Document", pce);
047: throw new GeneralRenderingException(pce);
048: }
049:
050: // Create XML doc
051: Element appletE = doc.createElement("applet");
052: appletE.setAttribute("code", staticData.getParameter("code"));
053: appletE.setAttribute("codebase", staticData
054: .getParameter("codeBase"));
055: appletE.setAttribute("width", staticData.getParameter("width"));
056: appletE.setAttribute("height", staticData
057: .getParameter("height"));
058: appletE.setAttribute("align", "top");
059: appletE.setAttribute("border", "0");
060: appletE.setAttribute("archive", staticData
061: .getParameter("archive"));
062:
063: // Take all parameters whose names start with "APPLET." and pass them
064: // to the applet (after stripping "APPLET.")
065: java.util.Enumeration allKeys = staticData.keys();
066: while (allKeys.hasMoreElements()) {
067: String p = (String) allKeys.nextElement();
068: if (p.startsWith("APPLET.")) {
069: Element paramE = doc.createElement("param");
070: paramE
071: .setAttribute("name", p.substring(7) /*skip "APPLET."*/);
072: paramE.setAttribute("value", (String) staticData
073: .getParameter(p));
074: appletE.appendChild(paramE);
075: }
076: }
077:
078: doc.appendChild(appletE);
079:
080: XSLT xslt = XSLT.getTransformer(this , runtimeData.getLocales());
081: xslt.setXML(doc);
082: xslt.setXSL(sslLocation, "main", runtimeData.getBrowserInfo());
083: xslt.setTarget(out);
084: xslt.transform();
085: }
086:
087: // ICachable methods...
088:
089: public ChannelCacheKey generateKey() {
090: ChannelCacheKey key = new ChannelCacheKey();
091: key.setKey(getKey());
092: key.setKeyScope(ChannelCacheKey.SYSTEM_KEY_SCOPE);
093: key.setKeyValidity(null);
094: return key;
095: }
096:
097: public boolean isCacheValid(Object validity) {
098: return true;
099: }
100:
101: private String getKey() {
102: StringBuffer sbKey = new StringBuffer(1024);
103: sbKey.append("org.jasig.portal.channels.CApplet").append(": ");
104: sbKey.append("xslUri:");
105: try {
106: String sslUrl = ResourceLoader.getResourceAsURLString(this
107: .getClass(), sslLocation);
108: sbKey.append(
109: XSLT.getStylesheetURI(sslUrl, runtimeData
110: .getBrowserInfo())).append(", ");
111: } catch (PortalException pe) {
112: sbKey.append("Not available, ");
113: }
114: sbKey.append("staticData:").append(staticData.toString());
115: sbKey.append("locales:").append(
116: LocaleManager.stringValueOf(runtimeData.getLocales()));
117:
118: return sbKey.toString();
119: }
120: }
|