Example usage for java.nio.file Path startsWith

List of usage examples for java.nio.file Path startsWith

Introduction

In this page you can find the example usage for java.nio.file Path startsWith.

Prototype

default boolean startsWith(String other) 

Source Link

Document

Tests if this path starts with a Path , constructed by converting the given path string, in exactly the manner specified by the #startsWith(Path) startsWith(Path) method.

Usage

From source file:Main.java

public static void main(String[] args) {

    Path path01 = Paths.get("/tutorial/Java/JavaFX/Topic.txt");
    Path path02 = Paths.get("C:/tutorial/Java/JavaFX/Topic.txt");

    boolean sw = path01.startsWith("/tutorial/Java");
    System.out.println(sw);/*w w w  .j a  v  a 2 s.  c  om*/

}

From source file:Main.java

public static void main(String[] args) {
    Path path = Paths.get("C:", "tutorial/Java/JavaFX", "Topic.txt");

    Path path1 = Paths.get("C:", "tutorial/Java/JavaFX", "Topic.txt");

    System.out.println(path.startsWith(path1));

}

From source file:services.PathPermissionService.java

public static boolean canAccessPath(Project project, Path path) {
    Path root = PathService.getRootImageDirectory();
    try {//from ww  w.  j a v  a  2  s. co m
        if (path.startsWith(root))
            return true;
    } catch (Exception e) {
        return false;
    }
    return false;
}

From source file:com.github.blindpirate.gogradle.util.StringUtils.java

public static boolean pathStartsWith(String a, String b) {
    Path aPath = Paths.get(a);
    Path bPath = Paths.get(b);

    return aPath.startsWith(bPath);
}

From source file:org.dataconservancy.dcs.util.UriUtility.java

/**
 * Create a URI string for a file, ensuring that it has 3 slashes to meet File URL specifications
 * @param file The file to check.  This doesn't have to be an actual existing file
 * @param basedir The directory to make the file URI relative to.  Can be null.  If not null, the basedir must be
 *                in the path of the file parameter, or an exception will be thrown
 * @return A string representing the URI to the file on the local disk.
 * @throws URISyntaxException if there is an error in the URI syntax
 *//*from   w w  w. j av  a 2  s  .c  om*/
public static URI makeFileUriString(File file, File basedir) throws URISyntaxException {
    if (basedir == null) {
        basedir = new File(".");
    }

    Path relativePath = file.toPath();

    if (relativePath.startsWith(basedir.toPath())) {
        relativePath = basedir.toPath().relativize(file.toPath());
    }

    String path = FilenameUtils.separatorsToUnix(relativePath.toString());

    // Remove leading slashes from the path
    path = path.replaceFirst("^\\/*", "");

    return new URI("file", null, "///" + path, null);
}

From source file:org.dataconservancy.packaging.impl.UriUtility.java

/**
 * Create a URI string for a file, ensuring that it has 3 slashes to meet File URL specifications
 *
 * @param file The file to check. This doesn't have to be an actual existing file
 * @param basedir The directory to make the file URI relative to. Can be null. If not null, the basedir must be in
 *        the path of the file parameter, or an exception will be thrown
 * @return A string representing the URI to the file on the local disk.
 * @throws URISyntaxException if there is an error in the URI syntax
 *///from   w w  w  . j  a v  a  2 s.  c om
public static URI makeFileUriString(final File file, final File basedir) throws URISyntaxException {
    final File dir = basedir == null ? new File(".") : basedir;

    Path relativePath = file.toPath();

    if (relativePath.startsWith(dir.toPath())) {
        relativePath = dir.toPath().relativize(file.toPath());
    }

    String path = FilenameUtils.separatorsToUnix(relativePath.toString());

    // Remove leading slashes from the path
    path = path.replaceFirst("^\\/*", "");

    return new URI("file", null, "///" + path, null);
}

From source file:org.dataconservancy.dcs.util.UriUtility.java

/**
* Create a URI string for a file in a BagIt bag,
* @param file The file to check.  This doesn't have to be an actual existing file
* @param basedir The directory to make the file URI relative to.  Can be null.  If not null, the basedir must be
*                in the path of the file parameter, or an exception will be thrown
* @return A string representing the URI to the file on the local disk.
* @throws URISyntaxException if there is an error in the URI syntax
*//*from w  w  w .j  a  v  a  2  s. com*/
public static URI makeBagUriString(File file, File basedir) throws URISyntaxException {
    if (basedir == null) {
        basedir = new File(".");
    }

    Path relativePath = file.toPath();

    if (relativePath.startsWith(basedir.toPath())) {
        relativePath = basedir.toPath().relativize(file.toPath());
    }

    String path = FilenameUtils.separatorsToUnix(relativePath.toString());
    if (relativePath.getNameCount() > 1) {

        Path uriAuthority = relativePath.getName(0);
        Path uriPath = relativePath.subpath(1, relativePath.getNameCount());
        path = FilenameUtils.separatorsToUnix(uriPath.toString());
        if (!uriPath.isAbsolute()) {
            path = "/" + path;
        }

        return new URI(BAG_URI_SCHEME, uriAuthority.toString(), path, null, null);
    }

    return new URI(BAG_URI_SCHEME, path, null, null, null);
}

From source file:org.dataconservancy.packaging.impl.UriUtility.java

/**
 * Create a URI string for a file in a BagIt bag,
 *
 * @param file The file to check. This doesn't have to be an actual existing file
 * @param basedir The directory to make the file URI relative to. Can be null. If not null, the basedir must be in
 *        the path of the file parameter, or an exception will be thrown
 * @return A string representing the URI to the file on the local disk.
 * @throws URISyntaxException if there is an error in the URI syntax
 *///from  w w  w  . ja  v a  2  s.c  o  m
public static URI makeBagUriString(final File file, final File basedir) throws URISyntaxException {
    final File dir = basedir == null ? new File(".") : basedir;

    Path relativePath = file.toPath();

    if (relativePath.startsWith(dir.toPath())) {
        relativePath = dir.toPath().relativize(file.toPath());
    }

    String path = FilenameUtils.separatorsToUnix(relativePath.toString());
    if (relativePath.getNameCount() > 1) {

        final Path uriAuthority = relativePath.getName(0);
        final Path uriPath = relativePath.subpath(1, relativePath.getNameCount());
        path = FilenameUtils.separatorsToUnix(uriPath.toString());
        if (!uriPath.isAbsolute()) {
            path = "/" + path;
        }

        return new URI(BAG_URI_SCHEME, uriAuthority.toString(), path, null, null);
    }

    return new URI(BAG_URI_SCHEME, path, null, null, null);
}

From source file:io.liveoak.testtools.AbstractTestCase.java

@AfterClass
public static void tearDownMongo() throws IOException {
    if (mongoLauncher != null) {
        mongoLauncher.stopMongo();// w ww  . j  av  a 2  s . co  m

        // wait for it to stop
        long start = System.currentTimeMillis();
        while (mongoLauncher.serverRunning(mongoHost, mongoPort, (e) -> {
            if (System.currentTimeMillis() - start > 120000)
                throw new RuntimeException(e);
        })) {

            if (System.currentTimeMillis() - start > 120000) {
                throw new RuntimeException("mongod process still seems to be running (2m timeout)");
            }
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                throw new RuntimeException("Interrupted!");
            }
        }

        // now delete the data dir except log file
        Files.walkFileTree(new File(mongoLauncher.getDbPath()).toPath(), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (!file.startsWith(mongoLauncher.getLogPath())) {
                    Files.delete(file);
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                try {
                    Files.delete(dir);
                } catch (DirectoryNotEmptyException ignored) {
                }
                return FileVisitResult.CONTINUE;
            }
        });

        mongoLauncher = null;
    }
}

From source file:org.codice.ddf.admin.configurator.impl.ConfigValidator.java

private static void protectHomeDir(Path target) {
    String ddfHomeProp = System.getProperty("ddf.home");
    validateString(ddfHomeProp, "No value set for system property ddf.home");

    Path ddfHomePath = Paths.get(ddfHomeProp);
    if (!target.startsWith(ddfHomePath)) {
        throw new IllegalArgumentException(String.format("File [%s] is not beneath ddf home directory [%s]",
                target.toString(), ddfHomePath.toString()));
    }//  ww  w.  ja va2  s  . c o  m

    if (target.getParent().equals(ddfHomePath)) {
        throw new IllegalArgumentException("Invalid attempt to edit file in ddf.home directory");
    }
}