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: package org.jasig.portal.channels.adminnav;
006:
007: import java.util.HashMap;
008:
009: import org.xml.sax.Attributes;
010: import org.xml.sax.SAXException;
011: import org.xml.sax.helpers.DefaultHandler;
012:
013: /**
014: * Handler for receiving SAX events from parsing a set of links having the
015: * following structure.
016: *
017: * <pre>
018: * <links [bundleResolver="bundleBase"]>
019: * <link labelId="id-unique-within-resolver"
020: * channelFunctionalName="functional-name-of-channel">
021: * [<parameter name="some-Name" value="some-value" />
022: * <parameter ..../>
023: * ]
024: * <link...>...</link>
025: * </links>
026: * </pre>
027: *
028: * If an instance of ILabelResolver is not passed into this class and there
029: * is no bundleResolver declared by the links element then an
030: * IllegalStateException will be thrown. Similarly, if a null
031: * ILinkRegistrar instance is passed in an IllegalStateException will be
032: * thrown.
033: *
034: * @author mboyd@sungardsct.com
035: * @since 2.6
036: */
037: public class XMLLinksHandler extends DefaultHandler {
038: private String labelId = null;
039: private String fname = null;
040: private HashMap parameters = new HashMap();
041: private ILabelResolver globalResolver = null;
042: private ILabelResolver localResolver = null;
043: private ILinkRegistrar registrar = null;
044:
045: /**
046: * Default constructor which obtains an instance of ILinkRegistrar from
047: * AdminNavChannel's static getLinkRegistrar() method.
048: *
049: */
050: public XMLLinksHandler() {
051: this (AdminNavChannel.getLinkRegistrar());
052: }
053:
054: /**
055: * Constructor accepting an instance of ILinkRegistrar into which all link
056: * definitions will be added.
057: *
058: */
059: public XMLLinksHandler(ILinkRegistrar registrar) {
060: this (registrar, null);
061: }
062:
063: /**
064: * Constructor accepting an instance of ILinkRegistrar into which all link
065: * definitions will be added. Also accepts an ILabelResolver instance
066: * which will be used to resolve labels for all links unless a
067: * bundleResolver is specified on the outermost links element.
068: *
069: */
070: public XMLLinksHandler(ILinkRegistrar registrar,
071: ILabelResolver resolver) {
072: if (registrar == null)
073: throw new IllegalStateException("Link Registrar can "
074: + "not be null.");
075: this .registrar = registrar;
076: this .globalResolver = resolver;
077: }
078:
079: public void startElement(String nsURI, String localName,
080: String qName, Attributes atts) throws SAXException {
081: if (qName.equals("links")) {
082: String bundleBase = atts.getValue("bundleResolver");
083: if (bundleBase != null && !bundleBase.equals(""))
084: localResolver = new ResourceBundleResolver(bundleBase);
085: } else if (qName.equals("link")) {
086: fname = atts.getValue("channelFunctionalName");
087: labelId = atts.getValue("labelId");
088: } else if (qName.equals("parameter")) {
089: String name = atts.getValue("name");
090: String value = atts.getValue("value");
091: parameters.put(name, value);
092: }
093: }
094:
095: public void endElement(String uri, String localName, String qName)
096: throws SAXException {
097: if (qName.equals("link")) {
098: if (localResolver == null && globalResolver == null)
099: throw new IllegalStateException(
100: "No resolver available "
101: + "for link label resolution.");
102: if (localResolver != null)
103: registrar.addLink(fname, labelId, localResolver,
104: parameters);
105: else
106: registrar.addLink(fname, labelId, globalResolver,
107: parameters);
108: parameters.clear();
109: }
110: if (qName.equals("links")) {
111: localResolver = null;
112: }
113: }
114: }
|