// 
// Decompiled by Procyon v0.6.0
// 

package org.jline.builtins;

import java.nio.file.FileSystem;
import java.util.Map;
import java.nio.file.FileSystems;
import java.util.HashMap;
import java.net.URI;
import java.nio.file.Paths;
import java.net.URL;
import java.net.URISyntaxException;
import java.io.IOException;
import java.nio.file.Path;

public class ClasspathResourceUtil
{
    public static Path getResourcePath(final String name) throws IOException, URISyntaxException {
        return getResourcePath(name, ClasspathResourceUtil.class.getClassLoader());
    }
    
    public static Path getResourcePath(final String name, final Class<?> clazz) throws IOException, URISyntaxException {
        final URL resource = clazz.getResource(name);
        if (resource == null) {
            throw new IOException("Resource not found: " + name);
        }
        return getResourcePath(resource);
    }
    
    public static Path getResourcePath(final String name, final ClassLoader classLoader) throws IOException, URISyntaxException {
        final URL resource = classLoader.getResource(name);
        if (resource == null) {
            throw new IOException("Resource not found: " + name);
        }
        return getResourcePath(resource);
    }
    
    public static Path getResourcePath(final URL resource) throws IOException, URISyntaxException {
        final URI uri = resource.toURI();
        final String scheme = uri.getScheme();
        if (scheme.equals("file")) {
            return Paths.get(uri);
        }
        if (!scheme.equals("jar")) {
            throw new IllegalArgumentException("Cannot convert to Path: " + uri);
        }
        final String s = uri.toString();
        final int separator = s.indexOf("!/");
        final String entryName = s.substring(separator + 2);
        final URI fileURI = URI.create(s.substring(0, separator));
        final FileSystem fs = FileSystems.newFileSystem(fileURI, new HashMap<String, Object>());
        return fs.getPath(entryName, new String[0]);
    }
}
