Example usage for org.apache.commons.io FilenameUtils normalize

List of usage examples for org.apache.commons.io FilenameUtils normalize

Introduction

In this page you can find the example usage for org.apache.commons.io FilenameUtils normalize.

Prototype

public static String normalize(String filename) 

Source Link

Document

Normalizes a path, removing double and single dot path steps.

Usage

From source file:org.sonar.api.resources.DefaultProjectFileSystem.java

/**
 * getRelativePath("c:/foo/src/my/package/Hello.java", ["c:/bar", "c:/foo/src"]) is "my/package/Hello.java".
 * <p>/*from w  w  w.j  a v a 2  s  .com*/
 * Relative path is composed of slashes. Windows backslaches are replaced by /
 * </p>
 *
 * @return null if file is not in dir (including recursive subdirectories)
 */
public static String getRelativePath(java.io.File file, List<java.io.File> dirs) {
    List<String> stack = new ArrayList<String>();
    String path = FilenameUtils.normalize(file.getAbsolutePath());
    java.io.File cursor = new java.io.File(path);
    while (cursor != null) {
        if (containsFile(dirs, cursor)) {
            return StringUtils.join(stack, "/");
        }
        stack.add(0, cursor.getName());
        cursor = cursor.getParentFile();
    }
    return null;
}

From source file:org.sonar.api.scan.filesystem.PathResolver.java

public RelativePath relativePath(Collection<File> dirs, File file) {
    List<String> stack = Lists.newArrayList();
    String path = FilenameUtils.normalize(file.getAbsolutePath());
    File cursor = new File(path);
    while (cursor != null) {
        File parentDir = parentDir(dirs, cursor);
        if (parentDir != null) {
            return new RelativePath(parentDir, Joiner.on("/").join(stack));
        }/*from  w  ww  .ja v  a2 s .c  o  m*/
        stack.add(0, cursor.getName());
        cursor = cursor.getParentFile();
    }
    return null;
}

From source file:org.sonar.api.scan.filesystem.PathResolver.java

public String relativePath(File dir, File file) {
    List<String> stack = Lists.newArrayList();
    String path = FilenameUtils.normalize(file.getAbsolutePath());
    File cursor = new File(path);
    while (cursor != null) {
        if (containsFile(dir, cursor)) {
            return Joiner.on("/").join(stack);
        }// w  w  w .j  av  a  2  s .c  o  m
        stack.add(0, cursor.getName());
        cursor = cursor.getParentFile();
    }
    return null;
}

From source file:org.sonar.cxx.sensors.coverage.TestwellCtcTxtParser.java

private void parseFileUnit(final Map<String, CoverageMeasures> coverageData, Matcher headerMatcher) {
    LOG.debug("Parsing file section...");

    String normalFilename;/*www .  ja  va  2  s.c o m*/
    String filename = headerMatcher.group(1);
    if (new File(filename).isAbsolute()) {
        normalFilename = FilenameUtils.normalize(filename);
    } else {
        normalFilename = FilenameUtils.normalize("./" + filename);
    }
    File file = new File(normalFilename);
    addLines(file, coverageData);
}

From source file:org.sonar.cxx.sensors.utils.CxxReportSensor.java

/**
 * resolveFilename normalizes the report full path
 *
 * @param baseDir of the project//from  w w  w. j av  a  2s .co m
 * @param filename of the report
 * @return String
 */
@Nullable
public static String resolveFilename(final String baseDir, @Nullable final String filename) {

    if (filename != null) {
        // Normalization can return null if path is null, is invalid,
        // or is a path with back-ticks outside known directory structure
        String normalizedPath = FilenameUtils.normalize(filename);
        if ((normalizedPath != null) && (new File(normalizedPath).isAbsolute())) {
            return normalizedPath;
        }

        // Prefix with absolute module base directory, attempt normalization again -- can still get null here
        normalizedPath = FilenameUtils.normalize(baseDir + File.separator + filename);
        if (normalizedPath != null) {
            return normalizedPath;
        }
    }
    return null;
}

From source file:org.sonar.cxx.sensors.valgrind.ValgrindFrame.java

/**
 * Constructs a stack frame with given attributes. Its perfectly valid if some
 * of them are empty or don't carry meaningful information.
 *///from w  w w. j ava 2  s. c o m
public ValgrindFrame(@Nullable String ip, @Nullable String obj, @Nullable String fn, @Nullable String dir,
        @Nullable String file, @Nullable String line) {
    this.ip = (ip != null) ? ip : "???";
    this.obj = (obj != null) ? obj : "";
    this.fn = (fn != null) ? fn : "???";
    this.dir = (dir != null) ? FilenameUtils.normalize(dir) : "";
    this.file = (file != null) ? file : "";
    this.line = (line != null) ? line : "";
}

From source file:org.sonar.cxx.sensors.valgrind.ValgrindStack.java

/**
 * Returns the last frame (counted from the bottom of the stack) of a function
 * which is in 'our' code/*from ww  w  . j  a  v  a  2s .c  om*/
 *
 * @param basedir
 * @return ValgrindFrame frame or null
 */
@Nullable
public ValgrindFrame getLastOwnFrame(String basedir) {
    String workdir = FilenameUtils.normalize(basedir);
    for (ValgrindFrame frame : frames) {
        if (isInside(frame.getDir(), workdir)) {
            return frame;
        }
    }
    return null;
}

From source file:org.sonar.NAME.plugin.utils.NAMEReportSensor.java

public static List<File> getReports(Settings conf, String baseDirPath, String reportPathPropertyKey,
        String defaultReportPath) {
    String reportPath = conf.getString(reportPathPropertyKey);
    if (reportPath == null) {
        reportPath = defaultReportPath;//from  www  .  j  a  v  a  2 s.c o  m
    }
    reportPath = FilenameUtils.normalize(reportPath);

    NAMEUtils.LOG.debug("Using pattern '{}' to find reports", reportPath);

    DirectoryScanner scanner = new DirectoryScanner();
    String[] includes = new String[1];
    includes[0] = reportPath;
    scanner.setIncludes(includes);
    scanner.setBasedir(new File(baseDirPath));
    scanner.scan();
    String[] relPaths = scanner.getIncludedFiles();

    List<File> reports = new ArrayList<File>();
    for (String relPath : relPaths) {
        String path = NAMEUtils.normalizePath(new File(baseDirPath, relPath).getAbsolutePath());
        reports.add(new File(path));
    }

    return reports;
}

From source file:org.sonar.plugins.cxx.utils.CxxReportSensor.java

public static List<File> getReports(Settings settings, final File moduleBaseDir, String reportPathPropertyKey) {

    List<File> reports = new ArrayList<>();

    List<String> includes = Arrays.asList(settings.getStringArray(reportPathPropertyKey));
    if (!includes.isEmpty()) {
        includes = Lists.transform(includes, new Function<String, String>() {
            @Override/*w w w  . j av a2s .  co  m*/
            public String apply(@Nullable String path) {
                if (path == null) {
                    return null;
                }
                path = FilenameUtils.normalize(path);
                if (new File(path).isAbsolute()) {
                    return path;
                }
                return FilenameUtils.normalize(moduleBaseDir.getAbsolutePath() + File.separator + path);
            }
        });

        CxxUtils.LOG.debug("Normalized report includes to '{}'", includes);

        DirectoryScanner directoryScanner = new DirectoryScanner();
        directoryScanner.setIncludes(includes.toArray(new String[includes.size()]));
        directoryScanner.scan();

        for (String found : directoryScanner.getIncludedFiles()) {
            CxxUtils.LOG.debug("Adding report '{}'", found);
            reports.add(new File(found));
        }

        if (reports.isEmpty()) {
            CxxUtils.LOG.warn("Cannot find a report for '{}'", reportPathPropertyKey);
        }
    } else {
        CxxUtils.LOG.error("Undefined report path value for key '{}'", reportPathPropertyKey);
    }

    return reports;
}

From source file:org.sonar.plugins.delphi.utils.DirectoryFileFilter.java

public static String getRelativePath(File file, List<File> dirs) {
    List<String> stack = new ArrayList<>();
    String path = FilenameUtils.normalize(file.getAbsolutePath());
    File cursor = new File(path);
    while (cursor != null) {
        if (containsFile(dirs, cursor)) {
            return StringUtils.join(stack, "/");
        }/*w  w w  .  j  av  a2  s . co  m*/
        stack.add(0, cursor.getName());
        cursor = cursor.getParentFile();
    }
    return null;
}