001: /* Copyright 2001 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.utils;
007:
008: import java.io.FileInputStream;
009: import java.io.FileNotFoundException;
010: import java.io.InputStream;
011: import java.net.MalformedURLException;
012: import java.net.URISyntaxException;
013: import java.net.URL;
014:
015: import org.apache.commons.logging.Log;
016: import org.apache.commons.logging.LogFactory;
017: import org.jasig.portal.ChannelRegistryManager;
018: import org.jasig.portal.PortalSessionManager;
019: import org.xml.sax.EntityResolver;
020: import org.xml.sax.InputSource;
021:
022: /**
023: * Provides a means to resolve uPortal DTDs
024: * @author Peter Kharchenko, pkharchenko@unicon.net
025: * @author Ken Weiner, kweiner@unicon.net
026: * @author Dave Wallace, dwallace@udel.edu modifications
027: * @version $Revision: 36727 $
028: */
029: public class DTDResolver implements EntityResolver {
030:
031: private static final Log log = LogFactory.getLog(DTDResolver.class);
032:
033: private static final String dtdPath = "dtd";
034:
035: private static class PublicId {
036: public String publicId;
037: public String dtdFile;
038:
039: public PublicId(final String publicId, final String dtdFile) {
040: this .publicId = publicId;
041: this .dtdFile = dtdPath + "/" + dtdFile;
042: }
043: }
044:
045: private static final PublicId[] publicIds = new PublicId[] {
046: new PublicId(
047: "-//Netscape Communications//DTD RSS 0.91//EN",
048: "rss-0.91.dtd"),
049: new PublicId("-//uPortal//Tables/EN", "tables.dtd"),
050: new PublicId("-//uPortal//PersonDirs/EN", "PersonDirs.dtd"),
051: new PublicId("-//uPortal//Channel Publishing/EN",
052: "channelPublishingDocument.dtd"),
053: new PublicId("-//uPortal//Data/EN", "data.dtd"),
054: new PublicId("-//uPortal//PAGSGroupStore/EN",
055: "PAGSGroupStore.dtd"),
056: new PublicId("-//uPortal//LDAPGroupStore/EN",
057: "LDAPGroupStore.dtd") };
058: private String dtdName = null;
059:
060: /**
061: * Constructor for DTDResolver
062: */
063: public DTDResolver() {
064: }
065:
066: /**
067: * Constructor for DTDResolver
068: * @param dtdName the name of the dtd
069: */
070: public DTDResolver(String dtdName) {
071: this .dtdName = dtdName;
072: }
073:
074: /**
075: * Sets up a new input source based on the dtd specified in the xml document
076: * @param publicId the public ID
077: * @param systemId the system ID
078: * @return an input source based on the dtd specified in the xml document
079: * or null if we don't have a dtd that matches systemId or publicId
080: */
081: public InputSource resolveEntity(String publicId, String systemId) {
082: InputStream inStream = null;
083:
084: // Check for a match on the systemId
085: if (systemId != null) {
086: if (dtdName != null && systemId.indexOf(dtdName) != -1) {
087: inStream = getResourceAsStream(dtdPath + "/" + dtdName);
088: } else if (systemId
089: .trim()
090: .equalsIgnoreCase(
091: "http://my.netscape.com/publish/formats/rss-0.91.dtd")) {
092: inStream = getResourceAsStream(dtdPath
093: + "/rss-0.91.dtd");
094: }
095:
096: if (null != inStream) {
097: return new InputSource(inStream);
098: }
099: }
100:
101: // Check for a match on the public id
102: if (publicId != null) {
103: publicId = publicId.trim();
104: for (int i = 0; i < publicIds.length; i++) {
105: if (publicId.equalsIgnoreCase(publicIds[i].publicId)) {
106: inStream = getResourceAsStream(publicIds[i].dtdFile);
107: if (null != inStream) {
108: return new InputSource(inStream);
109: }
110: break;
111: }
112: }
113: }
114:
115: // Return null to let the parser handle this entity
116: return null;
117: }
118:
119: public InputStream getResourceAsStream(String resource) {
120: if (PortalSessionManager.isServletContext()) {
121: return PortalSessionManager.getResourceAsStream(resource);
122: } else {
123: try {
124: final URL root = DTDResolver.class.getResource("/");
125: return new FileInputStream(root.toURI().getPath()
126: + "../../" + resource);
127: } catch (FileNotFoundException e) {
128: log.error("Unable to find path to dtd " + resource, e);
129: } catch (URISyntaxException e) {
130: log.error("Unable to find path to dtd " + resource, e);
131: }
132: return null;
133: }
134: }
135: }
|