Example usage for java.nio.file Path normalize

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

Introduction

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

Prototype

Path normalize();

Source Link

Document

Returns a path that is this path with redundant name elements eliminated.

Usage

From source file:io.gravitee.maven.plugins.json.schema.generator.util.ClassFinder.java

/**
 * Find class Paths from the given root Path that match the given list of globs.
 *
 * @param root  the root Path from which start searching
 * @param globs the glob list to taking into account during path matching
 * @return a list of Paths that match the given list of globs from the root Path
 * @throws IOException if an I/O occurs//from   w w  w. j a  va  2s  . com
 */
private static List<Path> findClassPaths(Path root, Globs globs) throws IOException {
    List<Path> matchedPaths = new ArrayList<>();
    Files.walkFileTree(root.normalize(), new GlobsMatchingClassFileVisitor(globs, matchedPaths));
    return matchedPaths;
}

From source file:org.wso2.carbon.logging.service.provider.FileLogProvider.java

/**
 * Tests if the provided path is inside the base directory path.
 *
 * @param baseDirPath absolute {@link Path} of the base directory in which we want to check whether the given path
 *                    is inside//from www.  j a v  a 2 s. c o  m
 * @param path        relative {@link Path} to be tested
 * @return {@code true} if the given path is inside the base directory path, otherwise {@code false}
 */
private static boolean isPathInsideBaseDirectory(Path baseDirPath, Path path) {
    Path resolvedPath = baseDirPath.resolve(path.normalize()).normalize();
    return resolvedPath.startsWith(baseDirPath.normalize());
}

From source file:org.sakuli.actions.screenbased.ScreenshotActions.java

/**
 * Transfers a {@link BufferedImage} to an picture and saves it
 *
 * @param pictureFile Path to the final image
 * @return {@link Path} ot the screenshot.
 *///from  w w w .ja  v a  2  s .  c  o  m
static Path createPictureFromBufferedImage(Path pictureFile, String screenshotFormat,
        BufferedImage bufferedImage) throws IOException {
    Path absPath = pictureFile.normalize().toAbsolutePath();
    //check Folder
    if (!Files.exists(absPath.getParent())) {
        Files.createDirectory(absPath.getParent());
    }

    if (Files.exists(absPath)) {
        LOGGER.info("overwrite screenshot '{}'", absPath);
        Files.delete(absPath);
    }

    Files.createFile(pictureFile);
    OutputStream outputStream = Files.newOutputStream(pictureFile);

    if (!allowedScreenshotFormats.contains(screenshotFormat)) {
        throw new IOException("Format '" + screenshotFormat + "' is not supported! Use "
                + allowedScreenshotFormats.toString());
    }
    //write image
    ImageIO.write(bufferedImage, screenshotFormat, outputStream);
    LOGGER.info("screen shot saved to file \"" + pictureFile.toFile().getAbsolutePath() + "\"");
    return pictureFile;
}

From source file:se.kth.climate.fast.netcdfparquet.Main.java

private static File[] findFiles(String[] fileNames) {
    ArrayList<File> files = new ArrayList<>();
    for (String fileName : fileNames) {
        if (fileName.contains("*")) {
            String[] parts = fileName.split("\\*");
            if (parts.length > 2) {
                LOG.error("Only a single file wildcard ist supported! (in {} -> {})", fileName,
                        Arrays.toString(parts));
                System.exit(1);//from   w  w w .ja  va 2 s  . c  o  m
            }
            Path filePath = FileSystems.getDefault().getPath(parts[0]);
            String filePrefix = filePath.getName(filePath.getNameCount() - 1).toString();
            Path directoryPath = filePath.getParent();
            if (directoryPath == null) {
                directoryPath = FileSystems.getDefault().getPath(".");
            }
            directoryPath = directoryPath.normalize();
            File dir = directoryPath.toFile();
            if (dir.exists() && dir.isDirectory() && dir.canRead()) {

                FileFilter fileFilter;
                if (parts.length == 1) {
                    fileFilter = new WildcardFileFilter(filePrefix + "*");
                } else {
                    fileFilter = new WildcardFileFilter(filePrefix + "*" + parts[1]);
                }
                File[] matches = dir.listFiles(fileFilter);
                for (File f : matches) {
                    files.add(f);
                }
            } else {
                LOG.error("Can't access {} properly!", dir);
                System.exit(1);
            }
        } else {
            files.add(new File(fileName));
        }
    }
    return files.toArray(new File[0]);
}

From source file:org.sonar.java.AbstractJavaClasspath.java

private static Path resolvePath(Path baseDir, String fileName) {
    Path filePath = Paths.get(fileName);
    if (!filePath.isAbsolute()) {
        filePath = baseDir.resolve(fileName);
    }//from w w w  .j a  v a 2  s. c om
    return filePath.normalize();
}

From source file:org.apache.nifi.toolkit.tls.standalone.TlsToolkitStandaloneCommandLine.java

protected static String calculateDefaultOutputDirectory(Path currentPath) {
    Path currentAbsolutePath = currentPath.toAbsolutePath();
    Path parent = currentAbsolutePath.getParent();
    if (parent == currentAbsolutePath.getRoot()) {
        return parent.toString();
    } else {/*from   w  ww  .j a  v a  2 s.  c om*/
        Path currentNormalizedPath = currentAbsolutePath.normalize();
        return "../" + currentNormalizedPath.getFileName().toString();
    }
}

From source file:schemacrawler.test.utility.TestUtility.java

private static Path buildDirectory() throws Exception {
    final StackTraceElement ste = currentMethodStackTraceElement();
    final Class<?> callingClass = Class.forName(ste.getClassName());
    final Path codePath = Paths.get(callingClass.getProtectionDomain().getCodeSource().getLocation().toURI())
            .normalize().toAbsolutePath();
    final boolean isInTarget = codePath.toString().contains("target");
    if (!isInTarget) {
        throw new RuntimeException("Not in build directory, " + codePath);
    }/*from  w  ww. j a  v a  2  s.c  om*/
    final Path directory = codePath.resolve("..");
    return directory.normalize().toAbsolutePath();
}

From source file:org.sonar.scanner.scan.ProjectReactorBuilder.java

protected static File resolvePath(File baseDir, String path) {
    Path filePath = Paths.get(path);
    if (!filePath.isAbsolute()) {
        filePath = baseDir.toPath().resolve(path);
    }/*from w ww . ja  v  a  2  s  . c om*/
    return filePath.normalize().toFile();
}

From source file:org.wso2.carbon.logging.service.util.LoggingUtil.java

/**
 * Tests if the provided path is inside the base directory path.
 *
 * @param baseDirPath absolute {@link Path} of the base directory in which we want to check whether the given path
 *                    is inside/*w  w w.  ja  va2s.  c o  m*/
 * @param path        {@link Path} to be tested
 * @return {@code true} if the given path is inside the base directory path, otherwise {@code false}
 */
private static boolean isPathInsideBaseDirectory(Path baseDirPath, Path path) {
    Path resolvedPath = baseDirPath.resolve(path).normalize();
    return resolvedPath.startsWith(baseDirPath.normalize());
}

From source file:io.gravitee.maven.plugins.json.schema.generator.util.ClassFinder.java

/**
 * Find class names from the given root Path that matching the given list of globs.
 * <p/>/*w w  w  .  jav a  2 s.c  o  m*/
 * Class names are built following the given rule:
 * - Given the root Path: /root/path/
 * - Given the Class Path: /root/path/the/path/to/the/class/Class.class
 * - Then associated Class name would be: the.path.to.the.class.Class
 *
 * @param root  the root Path from which start searching
 * @param globs the glob list to taking into account during path matching
 * @return a list of Paths that match the given list of globs from the root Path
 * @throws IOException if an I/O occurs
 */
public static List<String> findClassNames(Path root, Globs globs) throws IOException {
    Validate.notNull(root, "Unable to handle null root path");
    Validate.isTrue(root.toFile().isDirectory(), "Unable to handle non existing or non directory root path");
    Validate.notNull(globs, "Unable to handle null globs");

    List<String> matchedClassNames = new ArrayList<>();
    matchedClassNames.addAll(findClassPaths(root, globs).stream()
            .map(path -> ClassUtils.convertClassPathToClassName(path.toString(), root.normalize().toString()))
            .collect(Collectors.toList()));
    return matchedClassNames;
}