001: /* Copyright 2002 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.car;
007:
008: import java.io.ByteArrayInputStream;
009:
010: import org.jasig.portal.ChannelDefinition;
011: import org.apache.commons.logging.Log;
012: import org.apache.commons.logging.LogFactory;
013: import org.jasig.portal.tools.chanpub.ChannelPublisher;
014: import org.xml.sax.Attributes;
015: import org.xml.sax.SAXException;
016: import org.xml.sax.helpers.DefaultHandler;
017:
018: /**
019: * Buffers a single channel definition located in a CAR
020: * channel-definition block and delegates to the chanpub
021: * ChannelPublisher to publish the channel. Also strips our groups
022: * and categories definitions since groups and categories can change
023: * from site to site. If not specified then ChannelPublisher will
024: * place in the "Auto-Published" category and grant access to
025: * admins. Then admins can determine in what category it should be
026: * placed and to whom it should be granted.
027: *
028: * @author Mark Boyd {@link <a href="mailto:mark.boyd@engineer.com">mark.boyd@engineer.com</a>}
029: * @version $Revision: 36690 $
030: */
031: public class DefaultChanPubInnerHandler extends DefaultHandler {
032: private static final Log log = LogFactory
033: .getLog(DefaultChanPubInnerHandler.class);
034: private ParsingContext ctx = null;
035: private StringBuffer buffer = new StringBuffer();
036: private StringBuffer charBuf = new StringBuffer();
037:
038: public DefaultChanPubInnerHandler(ParsingContext ctx) {
039: this .ctx = ctx;
040: buffer.append("<!DOCTYPE channel-definition "
041: + "SYSTEM \"channelDefinition.dtd\">");
042: }
043:
044: public void startElement(String namespaceURI, String localName,
045: String qName, Attributes atts) throws SAXException {
046: // channel definition elements currently have textual content or
047: // other elements and don't have attributes so don't cache attribs.
048: buffer.append('<');
049: buffer.append(qName);
050: buffer.append('>');
051: charBuf = new StringBuffer();
052: }
053:
054: public void characters(char[] ch, int start, int length)
055: throws SAXException {
056: charBuf.append(ch, start, length);
057: }
058:
059: public void endElement(String namespaceURI, String localName,
060: String qName) throws SAXException {
061: // capture the closing tag info
062: buffer.append(charBuf.toString());
063: buffer.append("</");
064: buffer.append(qName);
065: buffer.append('>');
066: // clean out the buffer at end also to properly handle nested elements
067: charBuf = new StringBuffer();
068:
069: // now see if this is the last piece of info for this chan-def
070: if (qName.equals(DescriptorHandler.CHANDEF_TAG_NAME)
071: && ctx.getPath().equals(DescriptorHandler.CHANDEFS)) {
072: // leaving ext block so push the channel definition into the
073: // channel publisher to be published.
074: try {
075: byte[] bytes = buffer.toString().getBytes();
076:
077: if (log.isInfoEnabled())
078: log
079: .info("CAR channel definition '"
080: + buffer.toString()
081: + "' ready to publish.");
082:
083: final ByteArrayInputStream is = new ByteArrayInputStream(
084: bytes);
085: final ChannelPublisher publisher = ChannelPublisher
086: .getChannelArchiveInstance();
087:
088: ChannelDefinition chanDef = null;
089:
090: chanDef = publisher.publishChannel(is);
091:
092: if (chanDef != null && log.isInfoEnabled())
093: log.info(" Successfully published channel "
094: + chanDef.getTitle() + " with fname "
095: + chanDef.getFName());
096: } catch (Exception e) {
097: if (log.isInfoEnabled())
098: log
099: .info(
100: "A problem occurred during auto publishing.",
101: e);
102: }
103: }
104: }
105: }
|