/* Copyright 2001 The JA-SIG Collaborative.  All rights reserved.
*  See license distributed with this file and
*  available online at http://www.uportal.org/license.html
*/

package org.jasig.portal.utils;

import java.io.InputStream;

import org.jasig.portal.PortalSessionManager;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;

/**
 * Provides a means to resolve uPortal DTDs
 * @author Peter Kharchenko, pkharchenko@unicon.net
 * @author Ken Weiner, kweiner@unicon.net
 * @author Dave Wallace, dwallace@udel.edu modifications 
 * @version $Revision$
 */
public class DTDResolver implements EntityResolver
{
  private String dtdPath = "dtd";
  private String dtdName = null;

  /**
   * Constructor for DTDResolver
   */
  public DTDResolver () {
  }

  /**
   * Constructor for DTDResolver
   * @param dtdName the name of the dtd
   */
  public DTDResolver (String dtdName) {
    this.dtdName = dtdName;
  }

  /**
   * Sets up a new input source based on the dtd specified in the xml document
   * @param publicId the public ID
   * @param systemId the system ID
   * @return an input source based on the dtd specified in the xml document
   *               or null if we don't have a dtd that matches systemId or publicId
   */
  public InputSource resolveEntity (String publicId, String systemId) {
    InputStream inStream = null;

    // Check for a match on the systemId
    if (systemId != null) {
      if (dtdName != null && systemId.indexOf(dtdName) != -1)
        inStream = PortalSessionManager.getResourceAsStream(dtdPath + "/" + dtdName);
      else if (systemId.trim().equalsIgnoreCase("http://my.netscape.com/publish/formats/rss-0.91.dtd"))
        inStream = PortalSessionManager.getResourceAsStream(dtdPath + "/rss-0.91.dtd");
         
      if ( null != inStream )
          return new InputSource(inStream);
    }
    
    // Check for a match on the public id
    if ( publicId != null ) {
        if ( publicId.trim().equalsIgnoreCase("-//Netscape Communications//DTD RSS 0.91//EN"))
            inStream = PortalSessionManager.getResourceAsStream(dtdPath + "/rss-0.91.dtd");
            
        if ( null != inStream )
            return new InputSource(inStream);
    }
        
    // Return null to let the parser handle this entity 
    return null;
  }
}

@@ -542,17 +542,16 @@
 ications
- 
 %0A * @ver
@@ -625,32 +625,45 @@
 ver%0A%7B%0A  private 
+static final 
 String dtdPath =
@@ -670,16 +670,114 @@
  %22dtd%22;%0A
+  private static final String channelPublishingDtd = dtdPath + %22/channelPublishingDocument.dtd%22;%0A%0A
   privat
@@ -1593,16 +1593,18 @@
 ) != -1)
+ %7B
 %0A       
@@ -1687,16 +1687,18 @@
 );%0A     
+ %7D
  else if
@@ -1787,16 +1787,18 @@
 1.dtd%22))
+ %7B
 %0A       
@@ -1880,27 +1880,26 @@
 td%22);%0A      
-   
+%7D%0A
 %0A      if ( 
@@ -1908,32 +1908,34 @@
 ll != inStream )
+ %7B
 %0A          retur
@@ -1963,26 +1963,30 @@
 tream);%0A
+      %7D%0A
     %7D%0A
-    
 %0A    // 
@@ -2144,16 +2144,18 @@
 1//EN%22))
+ %7B
 %0A       
@@ -2243,28 +2243,202 @@
 %22);%0A        
-    
+%7D else if (publicId.trim().equalsIgnoreCase(%22-//uPortal//Channel Publishing/EN%22)) %7B%0A        %09inStream = PortalSessionManager.getResourceAsStream(channelPublishingDtd);%0A        %7D%0A
 %0A        if 
@@ -2457,16 +2457,18 @@
 Stream )
+ %7B
 %0A       
@@ -2514,18 +2514,20 @@
     
-%7D%0A
     
+%7D%0A
     
+%7D%0A
 %0A   
@@ -2578,17 +2578,16 @@
 s entity
- 
 %0A    ret

Resolve the Channel Publishing Public ID