Example usage for java.io File toURI

List of usage examples for java.io File toURI

Introduction

In this page you can find the example usage for java.io File toURI.

Prototype

public URI toURI() 

Source Link

Document

Constructs a file: URI that represents this abstract pathname.

Usage

From source file:info.novatec.testit.livingdoc.util.ClassUtils.java

public static ClassLoader toClassLoaderWithDefaultParent(Collection<String> classPaths)
        throws MalformedURLException {
    Set<URL> dependencies = new HashSet<URL>();
    for (String classPath : classPaths) {
        File dependency = new File(classPath);
        dependencies.add(dependency.toURI().toURL());
    }//w  w  w  . j a  v a2 s. c  o  m
    ClassLoader classLoader = URLClassLoader.newInstance(dependencies.toArray(new URL[dependencies.size()]));
    return classLoader;
}

From source file:info.novatec.testit.livingdoc.util.ClassUtils.java

public static ClassLoader toClassLoader(Collection<String> classPaths, ClassLoader parent)
        throws MalformedURLException {
    Set<URL> dependencies = new HashSet<URL>();
    for (String classPath : classPaths) {
        File dependency = new File(classPath);
        dependencies.add(dependency.toURI().toURL());
    }//from www  .  ja  v  a 2s .  com
    ClassLoader classLoader = URLClassLoader.newInstance(dependencies.toArray(new URL[dependencies.size()]),
            parent);
    return classLoader;
}

From source file:com.aionemu.gameserver.cache.HTMLCache.java

public static String getRelativePath(File base, File file) {
    return file.toURI().getPath().substring(base.toURI().getPath().length());
}

From source file:com.sonicle.webtop.core.app.util.LogbackHelper.java

public static URL findConfigFileURLFromSystemProperties(ClassLoader classLoader) {
    String logbackConfigFile = OptionHelper.getSystemProperty(ContextInitializer.CONFIG_FILE_PROPERTY);
    if (logbackConfigFile != null) {
        URL result = null;/*from   w  w  w .ja v a 2s .  c o m*/
        try {
            result = new URL(logbackConfigFile);
            return result;
        } catch (MalformedURLException e) {
            // so, resource is not a URL:
            // attempt to get the resource from the class path
            result = Loader.getResource(logbackConfigFile, classLoader);
            if (result != null) {
                return result;
            }
            File f = new File(logbackConfigFile);
            if (f.exists() && f.isFile()) {
                try {
                    result = f.toURI().toURL();
                } catch (MalformedURLException e1) {
                }
            }
        }
    }
    return null;
}

From source file:de.betterform.connector.file.FileURIResolver.java

/**
 * Returns a plain file listing as a document.
 *
 * @param directory the directory to list.
 * @return a plain file listing as a document.
 *//*from w ww . j  av  a 2  s .c o m*/
public static Document buildDirectoryListing(File directory) {
    Document dirList = DOMUtil.newDocument(false, false);
    Element root = dirList.createElement("dir");
    root.setAttribute("path", directory.toURI().toString());
    root.setAttribute("parentDir", directory.getParentFile().toURI().toString());

    File[] fileList = directory.listFiles();
    File file;
    Element element;
    for (int i = 0; i < fileList.length; i++) {
        file = fileList[i];

        if (file.isDirectory()) {
            element = dirList.createElement("dir");
        } else {
            element = dirList.createElement("file");
        }

        element.setAttribute("name", file.getName());
        element.setAttribute("path", file.toURI().toString());
        root.appendChild(element);
    }

    dirList.appendChild(root);
    return dirList;
}

From source file:com.ibm.wala.cast.js.nodejs.NodejsRequiredSourceModule.java

public static NodejsRequiredSourceModule make(File rootDir, File file) throws IOException {
    String className = convertFileToClassName(rootDir, file);
    SourceFileModule sourceFileModule = CAstCallGraphUtil.makeSourceModule(file.toURI().toURL(),
            file.getName());//from  www .  j  a v  a 2  s.c  om
    return new NodejsRequiredSourceModule(className, file, sourceFileModule);
}

From source file:com.diversityarrays.util.ClassPathExtender.java

public static void addDirectoryJarsToClassPath(Log logger, Consumer<File> jarChecker, File... dirs) {
    if (dirs == null || dirs.length <= 0) {
        if (logger != null) {
            logger.info("No directories provided for class path");
        }//from   w  ww  . j  a  va2s  .  c o m
        return;
    }

    ClassLoader ccl = Thread.currentThread().getContextClassLoader();
    if (ccl instanceof java.net.URLClassLoader) {
        try {
            Method m = java.net.URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
            m.setAccessible(true);

            Set<URL> currentUrls = new HashSet<URL>(Arrays.asList(((URLClassLoader) ccl).getURLs()));
            if (VERBOSE) {
                info(logger, "=== Current URLS in ClassLoader: " + currentUrls.size());
                for (URL u : currentUrls) {
                    info(logger, "\t" + u.toString());
                }
            }

            for (File dir : dirs) {
                if (dir.isDirectory()) {
                    for (File f : dir.listFiles(JAR_OR_PROPERTIES)) {
                        try {
                            URL u = f.toURI().toURL();
                            if (!currentUrls.contains(u)) {
                                m.invoke(ccl, u);
                                if (VERBOSE) {
                                    info(logger, "[Added " + u + "] to CLASSPATH");
                                }
                                if (jarChecker != null) {
                                    jarChecker.accept(f);
                                }
                            }
                        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
                                | MalformedURLException e) {
                            warn(logger,
                                    "%Unable to add " + f.getPath() + " to CLASSPATH (" + e.getMessage() + ")");
                        }
                    }
                }
            }
        } catch (NoSuchMethodException e) {
            warn(logger, "%No method: " + java.net.URLClassLoader.class.getName() + ".addURL(URL)");
        }

    } else {
        warn(logger, "%currentThread.contextClassLoader is not an instance of "
                + java.net.URLClassLoader.class.getName());
    }
}

From source file:com.magnet.plugin.common.helpers.URLHelper.java

/**
 * Utility method to get a URL instance from either a file path or a url string
 *
 * @param source path or url string to the file
 * @return URL instance or null if source is invalid
 *//*from   w w  w. j a v a  2s  .com*/
private static URL getURL(String source) {
    URL url = null;
    try {
        url = new URL(source);
    } catch (MalformedURLException e) {
        File file = new File(source);
        if (file.exists()) {
            try {
                url = file.toURI().toURL();
            } catch (Exception ex) {
                //
            }
        }
    }
    return url;

}

From source file:com.asakusafw.testdriver.DirectIoUtil.java

private static <T> DataModelSourceFactory load0(DataModelDefinition<T> definition,
        BinaryStreamFormat<? super T> format, File source) throws IOException, InterruptedException {
    String path = source.toURI().toString();
    try (InputStream stream = new FileInputStream(source);
            ModelInput<? super T> input = format.createInput(definition.getModelClass(), path, stream)) {
        return collect(definition, input);
    }//ww w.  j  a v a  2 s.c o m
}

From source file:com.doculibre.constellio.utils.persistence.ConstellioPersistenceContext.java

private static void generatePersistenceFile() {
    //Usefull for unit testing
    String basePersistenceFileName = System.getProperty("base-persistence-file");
    if (basePersistenceFileName == null) {
        basePersistenceFileName = "persistence_derby.xml";
    }/*from  w w w  .  j a  v  a  2s. com*/

    File classesDir = ClasspathUtils.getClassesDir();
    File metaInfDir = new File(classesDir, "META-INF");
    File persistenceFile = new File(metaInfDir, "persistence.xml");
    File persistenceBaseFile = new File(metaInfDir, basePersistenceFileName);
    if (persistenceFile.exists()) {
        persistenceFile.delete();
    }

    // FIXME Using text files rather than Dom4J because of empty xmlns attribute generated...
    try {
        String persistenceBaseText = FileUtils.readFileToString(persistenceBaseFile);
        StringBuffer sbJarFile = new StringBuffer("\n");
        File pluginsDir = PluginFactory.getPluginsDir();
        for (String availablePluginName : ConstellioSpringUtils.getAvailablePluginNames()) {
            if (PluginFactory.isValidPlugin(availablePluginName)) {
                File pluginDir = new File(pluginsDir, availablePluginName);
                File pluginJarFile = new File(pluginDir, availablePluginName + ".jar");
                String pluginJarURI = pluginJarFile.toURI().toString();
                sbJarFile.append("\n");
                sbJarFile.append("<jar-file>" + pluginJarURI + "</jar-file>");
            }
        }

        File webInfDir = ClasspathUtils.getWebinfDir();
        File libDir = new File(webInfDir, "lib");
        File[] contellioJarFiles = libDir.listFiles(new FileFilter() {
            @Override
            public boolean accept(File pathname) {
                boolean accept;
                if (pathname.isDirectory()) {
                    accept = false;
                } else {
                    List<String> availablePluginNames = ConstellioSpringUtils.getAvailablePluginNames();
                    String jarNameWoutExtension = FilenameUtils.removeExtension(pathname.getName());
                    accept = availablePluginNames.contains(jarNameWoutExtension);
                }
                return accept;
            }
        });
        for (File constellioJarFile : contellioJarFiles) {
            URI constellioJarFileURI = constellioJarFile.toURI();
            sbJarFile.append("\n");
            sbJarFile.append("<jar-file>" + constellioJarFileURI + "</jar-file>");
        }

        String persistenceText = persistenceBaseText.replaceAll("</provider>", "</provider>" + sbJarFile);
        FileUtils.writeStringToFile(persistenceFile, persistenceText);
        //            persistenceFile.deleteOnExit();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}