01: /* Copyright 2002 The JA-SIG Collaborative. All rights reserved.
02: * See license distributed with this file and
03: * available online at http://www.uportal.org/license.html
04: */
05:
06: package org.jasig.portal.car;
07:
08: import org.xml.sax.Attributes;
09: import org.xml.sax.SAXException;
10: import org.xml.sax.helpers.DefaultHandler;
11:
12: /**
13: * Handles routing events to a registered set of handlers that each
14: * handles different portions of the descriptors xml tree. For
15: * example, a services block and all events for its top level services
16: * tag and any nested elements would be routed to the handler for
17: * services. This object also keeps track of the full traversal path
18: * from an element back to the containing document object.
19: *
20: * @author Mark Boyd {@link <a href="mailto:mark.boyd@engineer.com">mark.boyd@engineer.com</a>}
21: * @version $Revision: 36690 $
22: */
23: public class RoutingHandler extends DefaultHandler {
24: private ParsingContext ctx = null;
25: private PathRouter currentRouter = null;
26: private PathRouter[] routers = null;
27:
28: RoutingHandler(ParsingContext ctx, PathRouter[] routers) {
29: this .routers = routers;
30: this .ctx = ctx;
31: }
32:
33: /////// ContentHandler methods of interest
34:
35: public void startElement(java.lang.String namespaceURI,
36: java.lang.String localName, java.lang.String qName,
37: Attributes atts) throws SAXException {
38: // add to the path as we traverse the xml tree.
39: ctx.getPath().append(qName);
40:
41: // if no one is handling this portion of the sub tree see if anyone.
42: // should.
43: if (currentRouter == null)
44: for (int i = 0; currentRouter == null && i < routers.length; i++)
45: if (routers[i].looksFor(ctx.getPath()))
46: currentRouter = routers[i];
47:
48: // if anyone is handling, pass the event on to them.
49: if (currentRouter != null)
50: currentRouter.handler.startElement(namespaceURI, localName,
51: qName, atts);
52: }
53:
54: public void endElement(java.lang.String namespaceURI,
55: java.lang.String localName, java.lang.String qName)
56: throws SAXException {
57: // if anyone is handling, pass the event on to them.
58: if (currentRouter != null) {
59: currentRouter.handler.endElement(namespaceURI, localName,
60: qName);
61:
62: // now see if we are leaving the sub-tree handled by current
63: // router's handler.
64: if (currentRouter.looksFor(ctx.getPath()))
65: currentRouter = null;
66: }
67:
68: // drop the last item from the list. should be same as localName.
69: ctx.getPath().removeLast();
70: }
71:
72: public void characters(char[] ch, int start, int length)
73: throws SAXException {
74: if (currentRouter != null)
75: currentRouter.handler.characters(ch, start, length);
76: }
77: }
|