package org.csstudio.opibuilder.scriptUtil;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

import org.csstudio.opibuilder.util.ResourceUtil;
import org.eclipse.core.runtime.IPath;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class CssEntityResolver implements EntityResolver {

    @Override
    public InputSource resolveEntity(String publicId, String systemId)
            throws SAXException, IOException {
        try {
           URI uri = new URI(systemId);
           File file = new File(uri);
           if(!file.exists()) {
               IPath path = ResourceUtil.getPathFromString(file.getPath());
               InputStream inputStream = ResourceUtil.pathToInputStream(path);
               if(inputStream != null) {
                   return new InputSource(inputStream);
               }
           }
       } catch (Exception e) {
        // Entity may not be found and this may throw exception. This is normal and FileUtil will revert to xi:fallback
       }

       return null;
    }

}

@@ -471,28 +471,16 @@
 ystemId)
-%0A           
  throws 
@@ -532,16 +532,17 @@
         
+ 
 URI uri 
@@ -562,16 +562,17 @@
 temId);%0A
+ 
         
@@ -612,18 +612,20 @@
         
+ 
 if
+ 
 (!file.e
@@ -631,24 +631,25 @@
 exists()) %7B%0A
+ 
             
@@ -712,16 +712,17 @@
 ath());%0A
+ 
         
@@ -792,16 +792,18 @@
 (path);%0A
+%0A 
         
@@ -811,16 +811,17 @@
       if
+ 
 (inputSt
@@ -859,45 +859,148 @@
     
-return new InputSource(inputStream);%0A
+ InputSource source = new InputSource(systemId);%0A                    source.setByteStream(inputStream);%0A                    return source;%0A 
     
@@ -1008,24 +1008,25 @@
           %7D%0A
+ 
            %7D
@@ -1022,24 +1022,25 @@
           %7D%0A
+ 
        %7D cat
@@ -1054,24 +1054,28 @@
 eption e) %7B%0A
+    
         // E
@@ -1189,19 +1189,20 @@
 %0A       
+ 
 %7D%0A
-%0A
+ 
        r

Fix for XInclude -- resolving entities with workspace relative path