01: /* Copyright 2005 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: package org.jasig.portal.channels.adminnav;
06:
07: import java.io.InputStream;
08: import org.jasig.portal.utils.DTDResolver;
09: import org.apache.commons.logging.Log;
10: import org.apache.commons.logging.LogFactory;
11: import org.xml.sax.ContentHandler;
12: import org.xml.sax.InputSource;
13: import org.xml.sax.SAXException;
14: import org.xml.sax.XMLReader;
15: import org.xml.sax.helpers.XMLReaderFactory;
16:
17: /**
18: * Loads links defined in the passed in file path having the XML format
19: * specified in javadocs for XMLLinksHandler.
20: *
21: * @author mboyd@sungardsct.com
22: * @since 2.6
23: */
24: public class XMLLinksFileLoader {
25: private static Log LOG = LogFactory
26: .getLog(XMLLinksFileLoader.class);
27:
28: /**
29: * Loads into the passed in ILinkRegistrar the links specified in the passed
30: * in file which must conform to the structure of the XMLLinksHandler.
31: */
32: public XMLLinksFileLoader(String linksFile,
33: ILinkRegistrar registrar, ILabelResolver resolver) {
34: XMLReader parser = null;
35: try {
36: parser = XMLReaderFactory.createXMLReader();
37: parser.setEntityResolver(new DTDResolver("adminNav.dtd"));
38: } catch (SAXException e) {
39: throw new RuntimeException(
40: "Unable to create an XML reader.", e);
41: }
42:
43: InputStream stream = this .getClass().getResourceAsStream(
44: linksFile);
45:
46: if (stream == null) {
47: throw new RuntimeException("No links configuration "
48: + "file " + linksFile + " found. No statically "
49: + "defined links are available for the list.");
50: }
51:
52: InputSource source = new InputSource(stream);
53: ContentHandler ch = (ContentHandler) new XMLLinksHandler(
54: registrar, resolver);
55: parser.setContentHandler(ch);
56:
57: try {
58: parser.parse(source);
59: } catch (Exception se) {
60: throw new RuntimeException("Unable to load links from "
61: + linksFile + ".", se);
62: }
63: }
64: }
|