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 org.apache.commons.logging.Log;
009: import org.apache.commons.logging.LogFactory;
010: import org.xml.sax.Attributes;
011: import org.xml.sax.ContentHandler;
012: import org.xml.sax.SAXException;
013: import org.xml.sax.SAXParseException;
014: import org.xml.sax.helpers.DefaultHandler;
015:
016: /**
017: * The top level SAX event handler that receives event from the SAX parser
018: * when parsing a deployment descriptor. The top level element in a deployment
019: * descriptor can contain any number of children all of which can conditionally
020: * be wrapped in a processIf tag allowing for conditional parsing of one to many
021: * elements. This handler performs the switching between a filtering handler
022: * and a handler that routes events to the appropriate handlers of specific
023: * subsection of the descriptor tree.
024: *
025: * @author Mark Boyd {@link <a href="mailto:mark.boyd@engineer.com">mark.boyd@engineer.com</a>}
026: * @version $Revision: 36690 $
027: */
028: public class ComponentTagHandler extends DefaultHandler {
029: private static final Log log = LogFactory
030: .getLog(ComponentTagHandler.class);
031: private ParsingContext ctx = null;
032: private RoutingHandler routingHandler = null;
033: private ProcessIfTagHandler processTagHandler = null;
034:
035: private ContentHandler currentHandler = null;
036:
037: ComponentTagHandler(ParsingContext ctx, PathRouter[] routers) {
038: routingHandler = new RoutingHandler(ctx, routers);
039: processTagHandler = new ProcessIfTagHandler(ctx, routingHandler);
040: currentHandler = routingHandler;
041: this .ctx = ctx;
042: }
043:
044: /////// ErrorHandler interface methods
045:
046: public void error(SAXParseException exception) throws SAXException {
047: if (log.isDebugEnabled())
048: log
049: .debug(
050: "A non-fatal parsing error occurred while parsing "
051: + CarResources.DEPLOYMENT_DESCRIPTOR
052: + " in "
053: + ctx.getJarFile().getName() + ".",
054: exception);
055: }
056:
057: public void fatalError(SAXParseException exception)
058: throws SAXException {
059: throw new RuntimeException(exception.toString());
060: }
061:
062: public void warning(SAXParseException exception)
063: throws SAXException {
064: if (log.isDebugEnabled())
065: log.debug("A parsing warning occurred while parsing "
066: + CarResources.DEPLOYMENT_DESCRIPTOR + " in "
067: + ctx.getJarFile().getName() + ".", exception);
068: }
069:
070: /////// ContentHandler methods of interest
071:
072: public void startElement(java.lang.String namespaceURI,
073: java.lang.String localName, java.lang.String qName,
074: Attributes atts) throws SAXException {
075: // see if we are entering a process tag and if so wrap the routing with
076: // a process handler for conditional filtering
077: if (qName.equals(DescriptorHandler.PROCESS_TAG_NAME)
078: && ctx.getPath().equals(DescriptorHandler.COMPONENT)) {
079: processTagHandler.setAttributes(atts);
080: currentHandler = processTagHandler;
081: } else
082: currentHandler.startElement(namespaceURI, localName, qName,
083: atts);
084: }
085:
086: public void endElement(java.lang.String namespaceURI,
087: java.lang.String localName, java.lang.String qName)
088: throws SAXException {
089: // see if we are leaving a process tag and if so switch back to routing
090: // since the process tag does not contribute to the traversal path the
091: // path should be just the outermost component element
092: if (qName.equals(DescriptorHandler.PROCESS_TAG_NAME)
093: && ctx.getPath().equals(DescriptorHandler.COMPONENT)) {
094: currentHandler = routingHandler;
095: } else
096: currentHandler.endElement(namespaceURI, localName, qName);
097: }
098:
099: public void characters(char[] ch, int start, int length)
100: throws SAXException {
101: currentHandler.characters(ch, start, length);
102: }
103: }
|