
package org.codehaus.xfire.util;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import org.codehaus.xfire.XFireRuntimeException;


public class Resolver {

            private File file;
            private URI uri;
            private InputStream is;
    private byte[] bytes;

            public Resolver(String path) throws IOException {
/*  32*/        this("", path);
            }

            public Resolver(String baseUriStr, String uriStr) throws IOException {
/*  41*/        try {
/*  41*/            File uriFile = new File(uriStr);
/*  42*/            uriFile = new File(uriFile.getAbsolutePath());
                    URI relative;
/*  43*/            if(uriFile.exists())
/*  45*/                relative = uriFile.toURI();
/*  48*/            else
/*  48*/                relative = new URI(uriStr);
/*  50*/            if(relative.isAbsolute()) {
/*  52*/                uri = relative;
/*  53*/                is = relative.toURL().openStream();
                    } else
/*  55*/            if(baseUriStr != null) {
/*  58*/                File baseFile = new File(baseUriStr);
                        URI base;
/*  59*/                if(baseFile.exists())
/*  60*/                    base = baseFile.toURI();
/*  62*/                else
/*  62*/                    base = new URI(baseUriStr);
/*  64*/                base = base.resolve(relative);
/*  65*/                if(base.isAbsolute()) {
/*  67*/                    is = base.toURL().openStream();
/*  68*/                    uri = base;
                        }
                    }
                }
/*  71*/        catch(URISyntaxException e) { }
/*  74*/        if(uri != null && "file".equals(uri.getScheme()))
/*  76*/            file = new File(uri);
/*  79*/        if(is == null && file != null && file.exists()) {
/*  81*/            uri = file.toURI();
/*  84*/            try {
/*  84*/                is = new FileInputStream(file);
                    }
/*  86*/            catch(FileNotFoundException e) {
/*  88*/                throw new XFireRuntimeException("File was deleted! " + uriStr, e);
                    }
                } else
/*  91*/        if(is == null) {
/*  93*/            URL url = ClassLoaderUtils.getResource(uriStr, getClass());
/*  95*/            if(url == null)
/*  99*/                try {
/*  99*/                    url = new URL(uriStr);
/* 100*/                    uri = new URI(url.toString());
/* 101*/                    is = url.openStream();
                        }
/* 103*/                catch(MalformedURLException e) { }
/* 106*/                catch(URISyntaxException e) { }
/* 112*/            else
/* 112*/                is = url.openStream();
                }
/* 116*/        if(is == null)
/* 117*/            throw new IOException("Could not find resource '" + uriStr + "' relative to '" + baseUriStr + "'");

                //HORRIBLE BUG.
                // STORE TEH byte[] for each getInputStream()

                ByteArrayOutputStream bos=new ByteArrayOutputStream();
                byte[] buffer=new byte[256];
                for (int read=is.read(buffer); read>-1;  read=is.read(buffer))
                    bos.write(buffer, 0, read);
                bos.close();
                bytes=bos.toByteArray();
            }

            public URI getURI() {
/* 123*/        return uri;
            }

            public InputStream getInputStream() {
/* 128*/        return new ByteArrayInputStream(bytes);
            }

            public boolean isFile() {
/* 133*/        return file.exists();
            }

            public File getFile() {
/* 138*/        return file;
            }
}

