001: /* Copyright 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 java.util.Date;
009: import java.util.Map;
010: import javax.xml.parsers.DocumentBuilderFactory;
011:
012: import org.jasig.portal.ChannelRuntimeProperties;
013: import org.jasig.portal.ChannelStaticData;
014: import org.jasig.portal.PortalEvent;
015: import org.jasig.portal.channels.support.TitledChannelRuntimeProperties;
016: import org.w3c.dom.Document;
017: import org.w3c.dom.Element;
018:
019: /**
020: * An example channel that includes the current time in its dynamically generated title.
021: * @since uPortal 2.5.1
022: * @version $Revision$ $Date$
023: */
024: public final class CTimeTitle extends CAbstractXslt {
025:
026: /**
027: * "xslUri" is the name of the ChannelStaticData attribute that we will read and,
028: * if set to a non-null value, we will use its value as our XSLT URI. If this
029: * ChannelStaticData attribute is not set, we will fall back on our default.
030: */
031: public static final String XSL_PARAM_KEY = "xslUri";
032:
033: /**
034: * By default, we use the XSLT 'TimeTitle.xsl' which will be found
035: * in the stylesheets subdirectory corresponding to the package of
036: * this CTimeTitle channel.
037: */
038: public static final String DEFAULT_XSL_URI = "CTimeTitle/CTimeTitle.xsl";
039:
040: protected final Document getXml() throws Exception {
041: /*
042: * Here we build a Document conveying the current time.
043: */
044:
045: Document doc = DocumentBuilderFactory.newInstance()
046: .newDocumentBuilder().newDocument();
047:
048: Element missingPropsElem = doc.createElement("time");
049: String currentDateTime = new Date().toString();
050: missingPropsElem.setTextContent(currentDateTime);
051:
052: doc.appendChild(missingPropsElem);
053:
054: return doc;
055: }
056:
057: protected final String getXsltUri() throws Exception {
058: try {
059: ChannelStaticData staticData = getStaticData();
060: String xsltUri = staticData.getParameter(XSL_PARAM_KEY);
061:
062: if (xsltUri != null) {
063: return xsltUri;
064: }
065:
066: // if xsltUri was null we will fall back on returning our default.
067:
068: } catch (RuntimeException rte) {
069: log
070: .error(
071: "Error checking ChannelStaticData attribute ["
072: + XSL_PARAM_KEY
073: + "] for alternate XSLT; falling back on default value: ["
074: + DEFAULT_XSL_URI + "]", rte);
075: }
076:
077: // return our default value
078: return DEFAULT_XSL_URI;
079: }
080:
081: protected final Map getStylesheetParams() throws Exception {
082: // no parameters
083: return null;
084: }
085:
086: public final void receiveEvent(PortalEvent ev) {
087: // do nothing - handles no events
088: }
089:
090: public final ChannelRuntimeProperties getRuntimeProperties() {
091: // this channel returns ChannelRuntimeProperties that specify the
092: // dynamic channel title to be the current time.
093:
094: log.trace("CTimeTitle.getRuntimeProperties()");
095:
096: String currentTime = new Date().toString();
097:
098: return new TitledChannelRuntimeProperties(currentTime);
099:
100: }
101:
102: }
|