Skip to content

Commit

Permalink
use recursive file finder to lookup for files
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorge Costa authored and Jorge Costa committed Dec 6, 2015
1 parent fb8cf6e commit e4f717b
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 114 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Sonar C++ Plugin (Community)
* Copyright (C) 2010 Neticoa SAS France
* [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.plugins.cxx.utils;

import static java.nio.file.FileVisitResult.CONTINUE;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class CxxFileFinder extends SimpleFileVisitor<Path> {

private final PathMatcher matcher;
private final boolean recursive;
private final String baseDir;
private List<Path> matchedPaths = new ArrayList<Path>();

CxxFileFinder(String pattern, String baseDir, boolean recursive) {
matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
this.recursive = recursive;
this.baseDir = baseDir.toLowerCase();
}

void match(Path file) {
Path name = file.getFileName();

if (name != null && matcher.matches(name)) {
matchedPaths.add(file);
}
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
if (recursive) {
match(file);
} else {
String parentPath = file.getParent().toString().toLowerCase();
if (parentPath.equals(this.baseDir)) {
match(file);
}
}

return CONTINUE;
}

@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
CxxUtils.LOG.warn("File access Failed '{}' : ", file, exc.getMessage());
return CONTINUE;
}

public Collection<Path> getMatchedPaths() {
return matchedPaths;
}

public static Collection<Path> FindFiles(String baseDir, String pattern, boolean recursive) throws IOException {
CxxFileFinder finder = new CxxFileFinder(pattern, baseDir, recursive);
Files.walkFileTree(Paths.get(baseDir), finder);
return finder.getMatchedPaths();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@
package org.sonar.plugins.cxx.utils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.tools.ant.DirectoryScanner;
import org.apache.commons.io.FilenameUtils;
Expand Down Expand Up @@ -151,8 +156,8 @@ protected String getStringProperty(String name, String def) {
}

public static List<File> getReports(Settings conf,
String baseDirPath1,
String baseDirPath2,
String reactorBaseDir,
String moduleBaseDir,
String reportPathPropertyKey) {

String reportPath = conf.getString(reportPathPropertyKey);
Expand All @@ -165,41 +170,9 @@ public static List<File> getReports(Settings conf,
reports.add(singleFile);
} else {
CxxUtils.LOG.debug("Using pattern '{}' to find reports", reportPath);
String baseDirPath = baseDirPath1;
DirectoryScannerData scanner = GetDirectoryScannerForReport(baseDirPath, reportPath);

String[] relPaths = new String[0];
try {
scanner.scan();
relPaths = scanner.getIncludedFiles();
CxxUtils.LOG.debug("Number of Report Files Found '{}'", relPaths.length);
} catch (IllegalStateException e) {
CxxUtils.LOG.error("Invalid report baseDir '{}'", baseDirPath);
}

if (relPaths.length < 1 && !baseDirPath2.isEmpty()) {
scanner = GetDirectoryScannerForReport(baseDirPath2, reportPath);
try {
scanner.scan();
relPaths = scanner.getIncludedFiles();
} catch (IllegalStateException e) {
CxxUtils.LOG.error("Using module base failed to find Path '{}'", e.getMessage());
}
}

for (String relPath : relPaths) {
CxxUtils.LOG.debug("Process report '{}'", relPath);
String path = CxxUtils.normalizePath(new File(scanner.getBaseDir(), relPath).getAbsolutePath());
try {
File reportFile = new File(path);
if (reportFile.exists()) {
reports.add(reportFile);
} else {
CxxUtils.LOG.error("Can't read report '{}'", path);
}
} catch (SecurityException e) {
CxxUtils.LOG.error("Read access to report '{}' denied", path);
}
CxxUtils.GetReportForBaseDirAndPattern(reactorBaseDir, reportPath, reports);
if (reports.isEmpty() && !moduleBaseDir.isEmpty()) {
CxxUtils.GetReportForBaseDirAndPattern(moduleBaseDir, reportPath, reports);
}
if (reports.isEmpty()) {
CxxUtils.LOG.warn("Cannot find a report for '{}={}'", reportPathPropertyKey, reportPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,36 @@

package org.sonar.plugins.cxx.utils;

import java.io.File;
import org.apache.tools.ant.DirectoryScanner;

public class DirectoryScannerData extends DirectoryScanner {

DirectoryScannerData() {
public class CxxSearchPathData {
private String pattern = "";
private String basePath = "";
private boolean recursive;

CxxSearchPathData() {
this.recursive = false;
}

public void setBaseDir(String base) {
this.setBasedir(new File(base));
this.basePath = base;
}

public void setInclude(String include) {
String[] incls = new String[1];
incls[0] = include;
this.setIncludes(incls);
public void setPattern(String pattern) {
this.pattern = pattern;
}

public File getBaseDir() {
return this.basedir;
public String getBaseDir() {
return this.basePath;
}

public String [] getIncludes() {
return this.includes;
public String getPattern() {
return this.pattern;
}

public void setRecursive() {
this.recursive = true;
}

public boolean isRecursive() {
return this.recursive;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
package org.sonar.plugins.cxx.utils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.FilenameUtils;
Expand Down Expand Up @@ -74,59 +78,60 @@ public static String normalizePath(String filename) {
* @param reportPath
* @return
*/
public static DirectoryScannerData GetDirectoryScannerForReport(String rootDirPath, String reportPath) {
public static CxxSearchPathData GetDirectoryScannerForReport(String rootDirPath, String reportPath) {
File singleFile = new File(reportPath);
DirectoryScannerData scanner = new DirectoryScannerData();
CxxSearchPathData scanner = new CxxSearchPathData();

CxxUtils.LOG.debug("Unprocessed root directory '{}'", rootDirPath);
CxxUtils.LOG.debug("Unprocessed report file '{}'", reportPath);

if (singleFile.isAbsolute()) {
String baseNormalize = FilenameUtils.normalize(rootDirPath);
String delimeter = Pattern.quote(System.getProperty("file.separator"));
String reportNormalize = FilenameUtils.normalize(reportPath);

if (reportNormalize.contains(baseNormalize)) {
reportPath = reportPath.replace(baseNormalize, "");
if (reportPath.startsWith("/") || reportPath.startsWith("\\")) {
reportPath = reportPath.substring(1);
}
scanner.setBaseDir(FilenameUtils.normalize(baseNormalize));
scanner.setInclude(reportPath);
String[] elementsOfPath = reportNormalize.split(delimeter);
String pattern = elementsOfPath[elementsOfPath.length - 1];
String root = reportNormalize.replace(pattern, "");
if (root.endsWith(File.separator)) {
scanner.setBaseDir(root.substring(0, root.length()-1));
} else {
Pattern p = Pattern.compile("^([A-Za-z]):");
Matcher m = p.matcher(reportPath);
if (m.find()) {
File[] roots = File.listRoots();
for(int i = 0; i < roots.length ; i++) {
if (roots[i].toString().toLowerCase().startsWith(m.group().toLowerCase())) {
scanner.setBaseDir(roots[i].toString().substring(0, 2));
}
}
scanner.setInclude(reportPath.substring(3));
} else {
scanner.setBaseDir(FilenameUtils.normalize("/"));
scanner.setInclude(reportPath.replace(scanner.getBaseDir().toString(), "").substring(1));
}

scanner.setBaseDir(root);
}

scanner.setPattern(pattern);
} else {
if (reportPath.startsWith("**")) {
scanner.setBaseDir(FilenameUtils.normalize(rootDirPath));
scanner.setInclude(reportPath);
scanner.setPattern(reportPath);
scanner.setRecursive();
} else {
File file1 = new File(rootDirPath);
File file2 = new File(file1, reportPath);
scanner.setBaseDir(FilenameUtils.normalize(file2.getParent()));
scanner.setInclude(file2.getName());
scanner.setPattern(file2.getName());
}
}

CxxUtils.LOG.debug("Processed root directory '{}'", scanner.getBaseDir());
CxxUtils.LOG.debug("Processed report file '{}'", scanner.getIncludes()[0]);
CxxUtils.LOG.debug("Processed report file '{}'", scanner.getPattern());

return scanner;
}

public static void GetReportForBaseDirAndPattern(String baseDirPath, String reportPath, List<File> reports) {
try {
CxxSearchPathData scanner = GetDirectoryScannerForReport(baseDirPath, reportPath);
Collection<Path> reportPaths = CxxFileFinder.FindFiles(scanner.getBaseDir(), scanner.getPattern(), scanner.isRecursive());

for (Path path : reportPaths) {
CxxUtils.LOG.debug("add report '{}'", path.toAbsolutePath().toString());
reports.add(new File(path.toAbsolutePath().toString()));
}
} catch (IOException ex) {
CxxUtils.LOG.warn("Cannot find a report for '{}={}'", baseDirPath, reportPath);
CxxUtils.LOG.warn("Exception '{}'", ex.getMessage());
}
}

/**
* @return returns case sensitive full path
*/
Expand Down
Loading

0 comments on commit e4f717b

Please sign in to comment.