/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.ode.bpel.compiler;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Basic implementation of the {@link ResourceFinder} interface. Resolves
 * URIs relative to a base URI specified at the time of construction.
 *  
 * @author Maciej Szefler - m s z e f l e r @ g m a i l . c o m
 *
 */
public class DefaultResourceFinder implements ResourceFinder {
    private static final Log __log = LogFactory.getLog(DefaultResourceFinder.class); 
    
    private File _suDir;

    /**
     * Default constructor: resolve relative URIs against current working directory.
     */
    public DefaultResourceFinder() {
        _suDir = new File("");
    }

    /**
     * Constructor: resolve relative URIs against specified directory.
     * @param suDir base path for relative URIs.
     */
    public DefaultResourceFinder(File suDir) {
        _suDir = suDir;
    }


    public InputStream openResource(URI uri) throws MalformedURLException, IOException {
        URI suURI = _suDir.toURI();
        
        // Note that if we get an absolute URI, the relativize operation will simply 
        // return the absolute URI. 
        URI relative = suURI.relativize(uri);
        if (relative.isAbsolute() && !relative.getScheme().equals("urn")) {
           __log.fatal("openResource: invalid scheme (should be urn:)  " + uri);
           return null;
        }
        
        File f = new File(suURI.getPath(),relative.getPath());
        if (!f.exists()) {
            __log.debug("fileNotFound: " + f);
            return null;
        }
        
        return new FileInputStream(f);
    }

}

@@ -1247,19 +1247,17 @@
 tion.%0A *
-  
 %0A
+
  * @auth
@@ -1466,22 +1466,17 @@
 .class);
- %0A    
+%0A
 %0A    pri
@@ -2010,24 +2010,25 @@
 oURI();%0A
+%0A
         
 %0A       
@@ -2011,32 +2011,355 @@
 URI();%0A%0A        
+if (uri.isAbsolute() && uri.getScheme().equals(%22file%22)) %7B%0A            try %7B%0A                return uri.toURL().openStream();%0A            %7D catch (Exception except) %7B%0A                __log.fatal(%22openResource: unable to open file URL %22 + uri + %22; %22 + except.toString());%0A                return null;%0A            %7D%0A        %7D%0A
 %0A        // Note
@@ -2427,17 +2427,16 @@
 l simply
- 
 %0A       
@@ -2463,17 +2463,16 @@
 ute URI.
- 
 %0A       
@@ -2697,33 +2697,25 @@
 ;%0A        %7D%0A
-        
 %0A
+
         File
@@ -2874,24 +2874,16 @@
       %7D%0A
-        
 %0A       

Fix ODE-111: Support <xsd:include> with relative schemaLocation