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.IChannel;
015: import org.jasig.portal.PortalException;
016: import org.jasig.portal.i18n.LocaleManager;
017: import org.jasig.portal.utils.ResourceLoader;
018: import org.jasig.portal.utils.XSLT;
019: import org.w3c.dom.Document;
020: import org.w3c.dom.Element;
021: import org.xml.sax.ContentHandler;
022:
023: /** <p>A simple channel which renders an image along with an optional
024: * caption and subcaption.</p>
025: * <p>Channel parameters:</p>
026: * <table>
027: * <tr><th>Name</th><th>Description</th><th>Example</th><th>Required</th></tr>
028: * <tr><td>img-uri</td><td>The URI of the image to display</td><td>http://webcam.its.hawaii.edu/uhmwebcam/image01.jpg</td><td>yes</td></tr>
029: * <tr><td>img-width</td><td>The width of the image to display</td><td>320</td><td>no</td></tr>
030: * <tr><td>img-height</td><td>The height of the image to display</td><td>240</td><td>no</td></tr>
031: * <tr><td>img-border</td><td>The border of the image to display</td><td>0</td><td>no</td></tr>
032: * <tr><td>img-link</td><td>A URI to be used as an href for the image</td><td>http://www.hawaii.edu/visitor/#webcams</td><td>no</td></tr>
033: * <tr><td>caption</td><td>A caption of the image to display</td><td>Almost Live Shot of Hamilton Library Front Entrance</td><td>no</td></tr>
034: * <tr><td>subcaption</td><td>The subcaption of the image to display</td><td>Updated Once per Minute During Daylight Hours</td><td>no</td></tr>
035: * <tr><td>alt-text</td><td>Text to include as the 'alt' attribute of the img tag</td><td>Almost live shot of Hamilton library front enterance</td><td>no, but highly recommended in support of non-visual browsers</td></tr>
036: * </table>
037: * @author Ken Weiner, kweiner@unicon.net
038: * @version $Revision: 36835 $
039: */
040: public class CImage extends BaseChannel implements ICacheable, IChannel {
041: public static final String ALT_TEXT_CHANNEL_PARAM_NAME = "alt-text";
042:
043: private static final String sslLocation = "CImage/CImage.ssl";
044:
045: /**
046: * Output channel content to the portal
047: * @param out a sax content handler
048: * @throws org.jasig.portal.PortalException
049: */
050: public void renderXML(ContentHandler out) throws PortalException {
051: // Get the static data
052: String sImageUri = staticData.getParameter("img-uri");
053: String sImageWidth = staticData.getParameter("img-width");
054: String sImageHeight = staticData.getParameter("img-height");
055: String sImageBorder = staticData.getParameter("img-border");
056: String sImageLink = staticData.getParameter("img-link");
057: String sCaption = staticData.getParameter("caption");
058: String sSubCaption = staticData.getParameter("subcaption");
059: String sAltText = staticData
060: .getParameter(ALT_TEXT_CHANNEL_PARAM_NAME);
061:
062: Document doc = null;
063: try {
064: doc = DocumentBuilderFactory.newInstance()
065: .newDocumentBuilder().newDocument();
066: } catch (ParserConfigurationException pce) {
067: log.error("Error getting a Document", pce);
068: throw new GeneralRenderingException(pce);
069: }
070:
071: // Create XML doc
072: Element contentE = doc.createElement("content");
073:
074: // Add image tag src, width, height, border, and link
075: Element imageE = doc.createElement("image");
076: imageE.setAttribute("src", sImageUri);
077: if (exists(sImageWidth))
078: imageE.setAttribute("width", sImageWidth);
079: if (exists(sImageWidth))
080: imageE.setAttribute("height", sImageHeight);
081: if (exists(sImageWidth))
082: imageE.setAttribute("border", sImageBorder);
083: if (exists(sImageWidth))
084: imageE.setAttribute("link", sImageLink);
085: if (exists(sAltText)) {
086: imageE.setAttribute("alt-text", sAltText);
087: }
088: contentE.appendChild(imageE);
089:
090: // Add a caption if it is specified
091: if (exists(sCaption)) {
092: Element captionE = doc.createElement("caption");
093: captionE.appendChild(doc.createTextNode(sCaption));
094: contentE.appendChild(captionE);
095: }
096:
097: // Add a subcaption if it is specified
098: if (exists(sSubCaption)) {
099: Element subcaptionE = doc.createElement("subcaption");
100: subcaptionE.appendChild(doc.createTextNode(sSubCaption));
101: contentE.appendChild(subcaptionE);
102: }
103:
104: doc.appendChild(contentE);
105:
106: XSLT xslt = XSLT.getTransformer(this , runtimeData.getLocales());
107: xslt.setXML(doc);
108: xslt.setXSL(sslLocation, runtimeData.getBrowserInfo());
109: xslt.setTarget(out);
110: xslt.setStylesheetParameter("baseActionURL", runtimeData
111: .getBaseActionURL());
112: xslt.transform();
113: }
114:
115: private static boolean exists(String s) {
116: return (s != null && s.length() > 0);
117: }
118:
119: // ICachable methods...
120:
121: public ChannelCacheKey generateKey() {
122: ChannelCacheKey key = new ChannelCacheKey();
123: key.setKey(getKey());
124: key.setKeyScope(ChannelCacheKey.SYSTEM_KEY_SCOPE);
125: key.setKeyValidity(null);
126: return key;
127: }
128:
129: public boolean isCacheValid(Object validity) {
130: return true;
131: }
132:
133: private String getKey() {
134: StringBuffer sbKey = new StringBuffer(1024);
135: sbKey.append("org.jasig.portal.channels.CImage").append(": ");
136: sbKey.append("xslUri:");
137: try {
138: String sslUrl = ResourceLoader.getResourceAsURLString(this
139: .getClass(), sslLocation);
140: sbKey.append(
141: XSLT.getStylesheetURI(sslUrl, runtimeData
142: .getBrowserInfo())).append(", ");
143: } catch (PortalException pe) {
144: sbKey.append("Not available, ");
145: }
146: sbKey.append("staticData:").append(staticData.toString());
147: sbKey.append("locales:").append(
148: LocaleManager.stringValueOf(runtimeData.getLocales()));
149:
150: return sbKey.toString();
151: }
152: }
|