Example usage for java.net URL toURI

List of usage examples for java.net URL toURI

Introduction

In this page you can find the example usage for java.net URL toURI.

Prototype

public URI toURI() throws URISyntaxException 

Source Link

Document

Returns a java.net.URI equivalent to this URL.

Usage

From source file:com.aerofs.baseline.config.TestConfiguration.java

private static <T extends Configuration> T loadConfiguration(Object typedClass, String filename)
        throws IOException, URISyntaxException {
    URL resourceURL = Resources.getResource(filename);

    try (FileInputStream in = new FileInputStream(new File(resourceURL.toURI()))) {
        return Configuration.loadYAMLConfigurationFromStream(typedClass.getClass(), in);
    }/*w w w.j a v  a 2s. c  o  m*/
}

From source file:cppsensor.utils.UnitTestProject.java

public static File getFile(String relPath) {
    URL url = UnitTestProject.class.getResource(File.separator + relPath);
    File file = null;/*from   w  ww . j  a  v  a 2s . c o m*/
    try {
        file = new File(url.toURI());
    } catch (URISyntaxException e) {
        log.error("Failed to locate resource " + url, e);
    }

    return file;
}

From source file:com.boundary.sdk.metric.MeasurementTest.java

public static Measurement read(String resource) throws URISyntaxException {
    Measurement instance = new Measurement();

    ClassLoader classLoader = instance.getClass().getClassLoader();
    URL url = classLoader.getResource(resource);
    File file = new File(url.toURI());

    ObjectMapper mapper = new ObjectMapper();

    try {//  w ww  .ja  va2  s. c o  m
        instance = mapper.readValue(file, Measurement.class);
    } catch (JsonParseException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return instance;
}

From source file:com.networknt.light.server.handler.loader.Loader.java

public static File getFileFromResourceFolder(String folder) {
    URL url = Thread.currentThread().getContextClassLoader().getResource(folder);
    File file = null;//from  w  w w .  j ava2  s. c o m
    try {
        file = new File(url.toURI());
    } catch (URISyntaxException e) {
        file = new File(url.getPath());
    } finally {
        return file;
    }
}

From source file:eu.dasish.annotation.backend.dao.impl.JdbcResourceDaoTest.java

public static String getNormalisedSql() throws FileNotFoundException, URISyntaxException {
    // remove the unsupported sql for the test
    final URL sqlUrl = JdbcResourceDaoTest.class.getResource("/sql/DashishAnnotatorCreate.sql");
    String sqlString = new Scanner(new File(sqlUrl.toURI()), "UTF8").useDelimiter("\\Z").next();
    for (String unknownToken : new String[] { "SET client_encoding", "CREATE DATABASE", "\\\\connect",
            "SET default_with_oids",
            //"ALTER SEQUENCE",
            //ALTER TABLE ONLY",
            //"ADD CONSTRAINT",
            //"CREATE INDEX", 
            // "ALTER TABLE ONLY [a-z]* ALTER COLUMN",
            // "ALTER TABLE ONLY [^A]* ADD CONSTRAINT"
    }) {/*from   w  w  w.  j a va 2 s . c o m*/
        sqlString = sqlString.replaceAll(unknownToken, "-- " + unknownToken);
    }

    sqlString = sqlString.replaceAll("bytea", "blob");
    sqlString = sqlString.replaceAll("SERIAL NOT NULL", "INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY");
    sqlString = sqlString.replaceAll("\\(current_timestamp AT TIME ZONE 'UTC'\\)",
            "current_timestamp AT TIME ZONE INTERVAL '00:00' HOUR TO MINUTE");
    return sqlString;
}

From source file:org.jboss.jdf.stacks.StacksFactory.java

public static Stacks create(final StacksConfiguration stacksConfig) throws IOException {
    InputStream inputStream = null;
    HttpResponse response = null;/*from  w  ww .ja  v  a2  s .c o m*/
    DefaultHttpClient client = null;
    final URL url = stacksConfig.getUrl();
    try {
        if (url.getProtocol().startsWith("file")) {
            inputStream = new FileInputStream(new File(url.toURI()));
        } else {
            client = new DefaultHttpClient();
            configureProxy(client, stacksConfig);
            final HttpGet method = new HttpGet(url.toURI());
            response = client.execute(method);
            final HttpEntity entity = response.getEntity();
            if (entity != null) {
                inputStream = entity.getContent();
            }
        }
        return new Parser().parse(inputStream);
    } catch (URISyntaxException e) {
        // TODO cleanup
        throw new IllegalStateException(e);
    } finally {
        HttpClientUtils.closeQuietly(response);
        HttpClientUtils.closeQuietly(client);
        Parser.safeClose(inputStream);
    }
}

From source file:com.orange.ngsi2.Utils.java

/**
 * Load a resource as an UTF-8 string/*from   ww  w  .  ja  va 2  s.c o  m*/
 * @param name name of the resource
 * @return the content of the resource
 */
public static String loadResource(String name) {
    try {
        URL url = Utils.class.getClassLoader().getResource(name);
        if (url == null) {
            return "";
        }
        return new String(Files.readAllBytes(Paths.get(url.toURI())), "UTF-8");
    } catch (Exception e) {
        return "";
    }
}

From source file:com.ansorgit.plugins.bash.BashTestUtils.java

private static String computeBasePath() {
    String configuredDir = StringUtils.stripToNull(System.getenv("BASHSUPPORT_TESTDATA"));
    if (configuredDir != null) {
        File dir = new File(configuredDir);
        if (dir.isDirectory() && dir.exists()) {
            return dir.getAbsolutePath();
        }/*w w w .j  a  v a  2  s  .  c  o m*/
    }

    //try to find out from the current classloader
    URL url = BashTestUtils.class.getClassLoader().getResource("log4j.xml");
    if (url != null) {
        try {
            File resourceFile = new File(url.toURI());
            //we need to cut the out dir and the other resource paths
            File basePath = resourceFile.getParentFile().getParentFile().getParentFile().getParentFile();
            if (basePath != null && basePath.isDirectory()) {
                return basePath + File.separator + "testData";
            }
        } catch (Exception e) {
            //ignore, use fallback below
        }
    }

    return null;
}

From source file:es.molabs.io.utils.FileHelper.java

public static URL[] getFiles(URL path, boolean recursive, String... extensions) throws IOException {
    URL[] urls = null;//from ww w  . j a v a2  s  .c o m

    try {
        Collection<File> fileCollection = FileUtils.listFiles(new File(path.toURI()), extensions, recursive);
        urls = new URL[fileCollection.size()];

        Iterator<File> iterator = fileCollection.iterator();
        int index = 0;
        while (iterator.hasNext()) {
            File file = iterator.next();

            urls[index++] = file.toURI().toURL();
        }
    } catch (URISyntaxException USe) {
        throw new IOException(USe);
    }

    return urls;
}

From source file:com.qubole.quark.server.EndToEndTest.java

public static void setupTables(String dbUrl, String filename)
        throws ClassNotFoundException, SQLException, IOException, URISyntaxException {

    Class.forName("org.h2.Driver");
    Properties props = new Properties();
    props.setProperty("user", "sa");
    props.setProperty("password", "");

    Connection connection = DriverManager.getConnection(dbUrl, props);

    Statement stmt = connection.createStatement();
    java.net.URL url = EndToEndTest.class.getResource("/" + filename);
    java.nio.file.Path resPath = java.nio.file.Paths.get(url.toURI());
    String sql = new String(java.nio.file.Files.readAllBytes(resPath), "UTF8");

    stmt.execute(sql);//from  www .  j av  a 2 s.c  om
}