/*
 * Copyright (c) 2002-2003 by OpenSymphony
 * All rights reserved.
 */
package com.opensymphony.workflow.loader;

import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.InputStream;

import java.net.URL;


/**
 * @author Hani Suleiman (hani@formicary.net)
 * Date: Sep 14, 2003
 * Time: 4:25:40 PM
 */
public class DTDEntityResolver implements EntityResolver {
    //~ Methods ////////////////////////////////////////////////////////////////

    public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
        if (systemId == null) {
            return null;
        }

        URL url = new URL(systemId);
        String file = url.getFile();

        if ((file != null) && (file.indexOf('/') > -1)) {
            file = file.substring(file.lastIndexOf('/') + 1);
        }

        if ("www.opensymphony.com".equals(url.getHost()) && systemId.endsWith(".dtd")) {
            InputStream is = getClass().getResourceAsStream("/META-INF/" + file);

            if (is == null) {
                is = getClass().getResourceAsStream("/" + file);
            }

            if (is != null) {
                return new InputSource(is);
            }
        }

        return null;
    }
}

@@ -268,16 +268,55 @@
 tream;%0A%0A
+import java.net.MalformedURLException;%0A
 import j
@@ -733,32 +733,50 @@
 ull;%0A        %7D%0A%0A
+        try %7B%0A    
         URL url 
@@ -804,16 +804,20 @@
         
+    
 String f
@@ -834,24 +834,28 @@
 getFile();%0A%0A
+    
         if (
@@ -912,16 +912,20 @@
         
+ 
+   
 file = f
@@ -966,32 +966,36 @@
 ) + 1);%0A        
+    
 %7D%0A%0A        if (%22
@@ -985,24 +985,28 @@
  %7D%0A%0A        
+ 
+   
 if (%22www.ope
@@ -1086,16 +1086,20 @@
         
+    
 InputStr
@@ -1169,24 +1169,28 @@
             
+  
+  
 if (is == nu
@@ -1187,32 +1187,36 @@
  (is == null) %7B%0A
+    
                 
@@ -1255,18 +1255,514 @@
 eam(
-%22/%22 + file
+'/' + file);%0A                %7D%0A%0A                if (is != null) %7B%0A                    return new InputSource(is);%0A                %7D%0A            %7D%0A        %7D%0A        //modified by mbussetti - 15 nov 2004%0A        //if the systemId isn't an URL, it is searched in the usual classpath%0A         catch (MalformedURLException e) %7B%0A            InputStream is = getClass().getResourceAsStream(%22/META-INF/%22 + systemId);%0A%0A            if (is == null) %7B%0A                is = getClass().getResourceAsStream('/' + systemId
 );%0A 

Allow non-URL resources