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:com.google.gwt.resources.converter.Css2Gss.java

private static String convertFile(File resource, Set<String> simpleBooleanConditions, Set<URL> scopeFiles)
        throws MalformedURLException, UnableToCompleteException {
    Predicate<String> simpleConditionPredicate;

    if (simpleBooleanConditions != null) {
        simpleConditionPredicate = Predicates.in(simpleBooleanConditions);
    } else {/* w  ww  . ja v a2s  . c o  m*/
        simpleConditionPredicate = Predicates.alwaysFalse();
    }

    return new Css2Gss(resource.toURI().toURL(), false, simpleConditionPredicate, scopeFiles).toGss();
}

From source file:brooklyn.util.ResourceUtils.java

public static URL tidy(URL url) {
    // File class has helpful methods for URIs but not URLs. So we convert.
    URI in;//from   w w  w .  j  a v a 2  s.  c o m
    try {
        in = url.toURI();
    } catch (URISyntaxException e) {
        throw Exceptions.propagate(e);
    }
    URI out;

    Matcher matcher = pattern.matcher(in.toString());
    if (matcher.matches()) {
        // home-relative
        File home = new File(Os.home());
        File file = new File(home, matcher.group(1));
        out = file.toURI();
    } else if (in.getScheme().equals("file:")) {
        // some other file, so canonicalize
        File file = new File(in);
        out = file.toURI();
    } else {
        // some other scheme, so no-op
        out = in;
    }

    URL urlOut;
    try {
        urlOut = out.toURL();
    } catch (MalformedURLException e) {
        throw Exceptions.propagate(e);
    }
    if (!urlOut.equals(url) && log.isDebugEnabled()) {
        log.debug("quietly changing " + url + " to " + urlOut);
    }
    return urlOut;
}

From source file:com.hazelcast.stabilizer.Utils.java

public static byte[] zip(List<File> roots) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Deque<File> queue = new LinkedList<File>();
    ZipOutputStream zout = new ZipOutputStream(out);

    Set<String> names = new HashSet<String>();

    try {//from  w  w  w .  ja v  a 2  s  . c o  m
        for (File root : roots) {
            URI base = root.isDirectory() ? root.toURI() : root.getParentFile().toURI();
            queue.push(root);
            while (!queue.isEmpty()) {
                File file = queue.pop();
                if (file.getName().equals(".DS_Store")) {
                    continue;
                }

                //                    log.finest("Zipping: " + file.getAbsolutePath());

                if (file.isDirectory()) {
                    String name = base.relativize(file.toURI()).getPath();
                    name = name.endsWith("/") ? name : name + "/";

                    if (names.add(name)) {
                        zout.putNextEntry(new ZipEntry(name));
                    }

                    for (File kid : file.listFiles()) {
                        queue.push(kid);
                    }
                } else {
                    String name = base.relativize(file.toURI()).getPath();
                    zout.putNextEntry(new ZipEntry(name));
                    copy(file, zout);
                    zout.closeEntry();
                }
            }
        }
    } finally {
        zout.close();
    }

    return out.toByteArray();
}

From source file:de.alpharogroup.lang.PropertiesUtils.java

/**
 * Load a Properties-object from the given File-object.
 * /*  w w w .  j  a  v a 2  s.c  o  m*/
 * @param propertiesFile
 *            the properties file
 * @return the properties or null if the file is not found.
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static Properties loadProperties(final File propertiesFile) throws IOException {
    Properties properties = null;
    InputStream is = null;
    if (propertiesFile.exists()) {
        is = propertiesFile.toURI().toURL().openStream();
        if (is != null) {
            properties = new Properties();
            properties.load(is);
        }
    } else {
        throw new FileNotFoundException(propertiesFile.getName() + " not found.");
    }
    return properties;
}

From source file:Main.java

public static void deepCopyDocument(Document ttml, File target) throws IOException {
    try {//from w  ww  .  ja v  a 2 s.  c o  m
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        XPathExpression expr = xpath.compile("//*/@backgroundImage");
        NodeList nl = (NodeList) expr.evaluate(ttml, XPathConstants.NODESET);
        for (int i = 0; i < nl.getLength(); i++) {
            Node backgroundImage = nl.item(i);
            URI backgroundImageUri = URI.create(backgroundImage.getNodeValue());
            if (!backgroundImageUri.isAbsolute()) {
                copyLarge(new URI(ttml.getDocumentURI()).resolve(backgroundImageUri).toURL().openStream(),
                        new File(target.toURI().resolve(backgroundImageUri).toURL().getFile()));
            }
        }
        copyLarge(new URI(ttml.getDocumentURI()).toURL().openStream(), target);

    } catch (XPathExpressionException e) {
        throw new IOException(e);
    } catch (URISyntaxException e) {
        throw new IOException(e);
    }
}

From source file:dk.dma.ais.abnormal.analyzer.AbnormalAnalyzerAppModule.java

/** Return URL for local file /data/geomask.xml if it exists and is readable; otherwise return default embedded geomask resource. */
private static URL getGeomaskResource() {
    URL geomaskResource = null;/*  w  ww .  j  a va  2 s  .  c o  m*/

    File customGeomaskFile = new File("/data/geomask.xml");
    if (customGeomaskFile.exists() && customGeomaskFile.isFile() && customGeomaskFile.canRead()) {
        try {
            geomaskResource = customGeomaskFile.toURI().toURL();
        } catch (MalformedURLException e) {
            LOG.error("Cannot load geomask.xml from file: " + customGeomaskFile.getAbsolutePath(), e);
        }
    }

    if (geomaskResource == null) {
        geomaskResource = ClassLoader.class.getResource("/geomask.xml");
    }

    return geomaskResource;
}

From source file:net.rim.ejde.internal.util.WorkspaceDependencyUtils.java

/**
*
*//*from   ww  w . ja v a 2s .com*/
static public IClasspathEntry[] toClasspathEntries(File[] jarFiles) {
    // check the bounds
    if (null == jarFiles || 0 == jarFiles.length) {
        return new IClasspathEntry[] {};
    }

    /*
     * business rule; enforcement for the jdw files who's import section is tagged as '<none>'
     */
    if (1 == jarFiles.length && NONE_CLASSPATH_STRING.equalsIgnoreCase(jarFiles[0].getName())) {
        return new IClasspathEntry[] {};
    }

    String importJarPath;
    IAccessRule[] accessRules = null;
    IPath importJarLocation;
    IClasspathAttribute[] classpathAttributes = {};
    String jdeDocsLocation = null;
    File docFile;
    IClasspathAttribute javadocClasspathAttribute;
    IClasspathEntry entry;

    List<IClasspathEntry> classpathEntries = new ArrayList<IClasspathEntry>();
    // jdeDocsLocation = RimCore.MetaContext.getLegacyJDEDocs();

    if (StringUtils.isNotBlank(jdeDocsLocation)) {
        docFile = new File(jdeDocsLocation);
        javadocClasspathAttribute = JavaCore.newClasspathAttribute(
                IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, docFile.toURI().toString());
        classpathAttributes = new IClasspathAttribute[] { javadocClasspathAttribute };
    }

    for (File importJar : jarFiles) {
        // filter out "<none>" and "net_rim_api.jar"
        if (importJar.getName().equalsIgnoreCase(NONE_CLASSPATH_STRING)
                || importJar.getName().equalsIgnoreCase(NET_RIM_API_JAR)) {
            continue;
        }
        importJarPath = EnvVarUtils.replaceRIAEnvVars(importJar.getPath());
        importJarLocation = new Path(importJarPath);
        IPath sourceJarPath = ImportUtils.getSourceJarPath(importJarLocation);
        if (sourceJarPath == null || sourceJarPath.isEmpty() || !sourceJarPath.toFile().exists()) {
            entry = JavaCore.newLibraryEntry(importJarLocation, null, null, accessRules, classpathAttributes,
                    false);
        } else {
            entry = JavaCore.newLibraryEntry(importJarLocation, sourceJarPath, null, accessRules,
                    classpathAttributes, false);
        }
        classpathEntries.add(entry);
    }
    return classpathEntries.toArray(new IClasspathEntry[classpathEntries.size()]);
}

From source file:com.net2plan.gui.GUINet2Plan.java

private static void loadJavadocLib() {
    File javadocFile = new File(SystemUtils.getCurrentDir() + SystemUtils.getDirectorySeparator() + "doc"
            + SystemUtils.getDirectorySeparator() + "javadoc" + SystemUtils.getDirectorySeparator() + "api"
            + SystemUtils.getDirectorySeparator() + "index.html");

    try {//from ww w.j a v a 2  s.  co m
        HTMLUtils.browse(javadocFile.toURI());
    } catch (Throwable ex) {
        ErrorHandling.showErrorDialog(
                "Unable to show the Library API Javadoc. Please, visit http://www.net2plan.com for the latest version",
                "Error showing Library API Javadoc");
    }
}

From source file:io.janusproject.Boot.java

/** Parse the command line.
 *
 * @param args - the CLI arguments given to the program.
 * @param propertyFiles - files that may be filled with the filenames given on the CLI.
 * @return the arguments that are not recognized as CLI options.
 *//*from  ww w.j a v  a  2 s  .c  o m*/
public static String[] parseCommandLine(String[] args, List<URL> propertyFiles) {
    CommandLineParser parser = new GnuParser();
    try {
        CommandLine cmd = parser.parse(getOptions(), args);
        parseCommandForInfoOptions(cmd);
        parseCommandLineForSystemProperties(cmd);
        parseCommandLineForVerbosity(cmd);
        // Retreive the list of the property files given on CLI
        if (cmd.hasOption('f')) {
            for (String rawFilename : cmd.getOptionValues('f')) {
                if (rawFilename == null || "".equals(rawFilename)) { //$NON-NLS-1$
                    showHelp();
                }
                File file = new File(rawFilename);
                if (!file.canRead()) {
                    //CHECKSTYLE:OFF
                    System.err.println(Locale.getString("INVALID_PROPERTY_FILENAME", //$NON-NLS-1$
                            rawFilename));
                    //CHECKSTYLE:ON
                    System.exit(ERROR_EXIT_CODE);
                }
                propertyFiles.add(file.toURI().toURL());
            }
        }
        return cmd.getArgs();
    } catch (IOException | ParseException e) {
        //CHECKSTYLE:OFF
        e.printStackTrace();
        //CHECKSTYLE:ON
        showHelp();
        // Only to avoid compilation errors
        throw new Error();
    }
}

From source file:com.kegare.caveworld.util.CaveUtils.java

public static boolean archiveDirZip(final File dir, final File dest) {
    final Path dirPath = dir.toPath();
    final String parent = dir.getName();
    Map<String, String> env = Maps.newHashMap();
    env.put("create", "true");
    URI uri = dest.toURI();

    try {/*from   w  w  w . j  av  a2 s  .  c o m*/
        uri = new URI("jar:" + uri.getScheme(), uri.getPath(), null);
    } catch (Exception e) {
        return false;
    }

    try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
        Files.createDirectory(zipfs.getPath(parent));

        for (File file : dir.listFiles()) {
            if (file.isDirectory()) {
                Files.walkFileTree(file.toPath(), new SimpleFileVisitor<Path>() {
                    @Override
                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                        Files.copy(file, zipfs.getPath(parent, dirPath.relativize(file).toString()),
                                StandardCopyOption.REPLACE_EXISTING);

                        return FileVisitResult.CONTINUE;
                    }

                    @Override
                    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                            throws IOException {
                        Files.createDirectory(zipfs.getPath(parent, dirPath.relativize(dir).toString()));

                        return FileVisitResult.CONTINUE;
                    }
                });
            } else {
                Files.copy(file.toPath(), zipfs.getPath(parent, file.getName()),
                        StandardCopyOption.REPLACE_EXISTING);
            }
        }

        return true;
    } catch (Exception e) {
        e.printStackTrace();

        return false;
    }
}