aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2017-08-02 13:51:17 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2017-08-04 14:58:16 +0200
commit343758acd64fd2318a35c5f55c6511cf8b2f6467 (patch)
treee43a20a33651b7aa3e9896d0acc8054ebba9d46d /sonar-plugin-api
parentbcae3c079bef995afef0ffc4dbe774fbdf0bf9ed (diff)
downloadsonarqube-343758acd64fd2318a35c5f55c6511cf8b2f6467.tar.gz
sonarqube-343758acd64fd2318a35c5f55c6511cf8b2f6467.zip
Don't change PathResolver methods to static
This is an API breaking change
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/URIPredicate.java8
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/PathResolver.java31
2 files changed, 29 insertions, 10 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/URIPredicate.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/URIPredicate.java
index edf241d7207..8d1df915802 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/URIPredicate.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/URIPredicate.java
@@ -24,10 +24,10 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
+import java.util.Optional;
import org.sonar.api.batch.fs.FileSystem.Index;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.scan.filesystem.PathResolver;
-import org.sonar.api.utils.PathUtils;
/**
* @since 6.6
@@ -50,11 +50,11 @@ class URIPredicate extends AbstractFilePredicate {
@Override
public Iterable<InputFile> get(Index index) {
Path path = Paths.get(uri);
- String relative = PathUtils.sanitize(PathResolver.relativePath(baseDir, path));
- if (relative == null) {
+ Optional<String> relative = PathResolver.relativize(baseDir, path);
+ if (!relative.isPresent()) {
return Collections.emptyList();
}
- InputFile f = index.inputFile(relative);
+ InputFile f = index.inputFile(relative.get());
return f != null ? Arrays.asList(f) : Collections.<InputFile>emptyList();
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/PathResolver.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/PathResolver.java
index 692fd5427da..cd90998741a 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/PathResolver.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/PathResolver.java
@@ -24,6 +24,7 @@ import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
+import java.util.Optional;
import javax.annotation.CheckForNull;
import javax.annotation.concurrent.Immutable;
import org.apache.commons.io.FilenameUtils;
@@ -39,10 +40,7 @@ import static java.util.stream.Collectors.joining;
@Immutable
public class PathResolver {
- /**
- * Static since 6.6
- */
- public static File relativeFile(File dir, String path) {
+ public File relativeFile(File dir, String path) {
return dir.toPath().resolve(path).normalize().toFile();
}
@@ -79,11 +77,10 @@ public class PathResolver {
* <li>null is returned if file is not a child of dir
* <li>the resulting path is converted to use Unix separators
* </ul>
- * Static since 6.6
* @since 6.0
*/
@CheckForNull
- public static String relativePath(Path dir, Path file) {
+ public String relativePath(Path dir, Path file) {
Path baseDir = dir.normalize();
Path path = file.normalize();
if (!path.startsWith(baseDir)) {
@@ -97,6 +94,28 @@ public class PathResolver {
}
}
+ /**
+ * Similar to {@link Path#relativize(Path)} except that:
+ * <ul>
+ * <li>Empty is returned if file is not a child of dir
+ * <li>the resulting path is converted to use Unix separators
+ * </ul>
+ * @since 6.6
+ */
+ public static Optional<String> relativize(Path dir, Path file) {
+ Path baseDir = dir.normalize();
+ Path path = file.normalize();
+ if (!path.startsWith(baseDir)) {
+ return Optional.empty();
+ }
+ try {
+ Path relativized = baseDir.relativize(path);
+ return Optional.of(FilenameUtils.separatorsToUnix(relativized.toString()));
+ } catch (IllegalArgumentException e) {
+ return Optional.empty();
+ }
+ }
+
@CheckForNull
public String relativePath(File dir, File file) {
return relativePath(dir.toPath(), file.toPath());