diff --git a/pmd-ant/src/main/java/net/sourceforge/pmd/ant/Formatter.java b/pmd-ant/src/main/java/net/sourceforge/pmd/ant/Formatter.java
index 8201ef9878..cbdea6ac17 100644
--- a/pmd-ant/src/main/java/net/sourceforge/pmd/ant/Formatter.java
+++ b/pmd-ant/src/main/java/net/sourceforge/pmd/ant/Formatter.java
@@ -29,10 +29,10 @@ import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.annotation.InternalApi;
import net.sourceforge.pmd.internal.util.IOUtil;
import net.sourceforge.pmd.lang.document.TextFile;
-import net.sourceforge.pmd.reporting.FileNameRenderer;
import net.sourceforge.pmd.renderers.Renderer;
import net.sourceforge.pmd.renderers.RendererFactory;
import net.sourceforge.pmd.reporting.FileAnalysisListener;
+import net.sourceforge.pmd.reporting.FileNameRenderer;
import net.sourceforge.pmd.reporting.GlobalAnalysisListener;
import net.sourceforge.pmd.reporting.ListenerInitializer;
diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/PmdAnalysis.java b/pmd-core/src/main/java/net/sourceforge/pmd/PmdAnalysis.java
index 249c089df0..6b4af459f1 100644
--- a/pmd-core/src/main/java/net/sourceforge/pmd/PmdAnalysis.java
+++ b/pmd-core/src/main/java/net/sourceforge/pmd/PmdAnalysis.java
@@ -6,10 +6,8 @@ package net.sourceforge.pmd;
import static net.sourceforge.pmd.util.CollectionUtil.listOf;
-import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
-import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
@@ -19,7 +17,6 @@ import java.util.Map;
import java.util.Objects;
import java.util.Set;
-import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.event.Level;
@@ -44,7 +41,6 @@ import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersion;
import net.sourceforge.pmd.lang.LanguageVersionDiscoverer;
import net.sourceforge.pmd.lang.document.FileCollector;
-import net.sourceforge.pmd.lang.document.FileId;
import net.sourceforge.pmd.lang.document.TextFile;
import net.sourceforge.pmd.renderers.Renderer;
import net.sourceforge.pmd.reporting.ConfigurableFileNameRenderer;
@@ -566,58 +562,4 @@ public final class PmdAnalysis implements AutoCloseable {
}
}
- /**
- * Return the textfile's display name. Takes the shortest path we
- * can construct from the relativize roots.
- *
- *
package private for test only
- */
- public static String getDisplayName(FileId file, List relativizeRoots) {
- String best = file.toAbsolutePath();
- for (Path root : relativizeRoots) {
- if (isFileSystemRoot(root)) {
- // Absolutize the path. Since the relativize roots are
- // sorted by ascending length, this should be the first in the list
- // (so another root can override it).
- best = file.toAbsolutePath();
- continue;
- }
-
- String relative = relativizePath(root.toAbsolutePath().toString(), file.toAbsolutePath());
- if (countSegments(relative) < countSegments(best)) {
- best = relative;
- }
- }
- return best;
- }
-
- private static int countSegments(String best) {
- return StringUtils.countMatches(best, File.separatorChar);
- }
-
- private static String relativizePath(String base, String other) {
- String[] baseSegments = base.split("[/\\\\]");
- String[] otherSegments = other.split("[/\\\\]");
- int prefixLength = 0;
- int maxi = Math.min(baseSegments.length, otherSegments.length);
- while (prefixLength < maxi && baseSegments[prefixLength].equals(otherSegments[prefixLength])) {
- prefixLength++;
- }
-
- if (prefixLength == 0) {
- return other;
- }
-
- List relative = new ArrayList<>();
- for (int i = prefixLength; i < baseSegments.length; i++) {
- relative.add("..");
- }
- relative.addAll(Arrays.asList(otherSegments).subList(prefixLength, otherSegments.length));
- return String.join(File.separator, relative);
- }
-
- /** Return whether the path is the root path (/). */
- private static boolean isFileSystemRoot(Path root) {
- return root.isAbsolute() && root.getNameCount() == 0;
- }
}
diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/reporting/ConfigurableFileNameRenderer.java b/pmd-core/src/main/java/net/sourceforge/pmd/reporting/ConfigurableFileNameRenderer.java
index 87fb306440..b40b019b7b 100644
--- a/pmd-core/src/main/java/net/sourceforge/pmd/reporting/ConfigurableFileNameRenderer.java
+++ b/pmd-core/src/main/java/net/sourceforge/pmd/reporting/ConfigurableFileNameRenderer.java
@@ -4,13 +4,16 @@
package net.sourceforge.pmd.reporting;
+import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
-import net.sourceforge.pmd.PmdAnalysis;
+import org.apache.commons.lang3.StringUtils;
+
import net.sourceforge.pmd.lang.document.FileId;
import net.sourceforge.pmd.lang.document.TextFile;
@@ -48,8 +51,63 @@ public class ConfigurableFileNameRenderer implements FileNameRenderer {
private String getLocalDisplayName(FileId file) {
if (!relativizeRootPaths.isEmpty()) {
- return PmdAnalysis.getDisplayName(file, relativizeRootPaths);
+ return getDisplayName(file, relativizeRootPaths);
}
return file.getOriginalPath();
}
+
+ private static int countSegments(String best) {
+ return StringUtils.countMatches(best, File.separatorChar);
+ }
+
+ static String relativizePath(String base, String other) {
+ String[] baseSegments = base.split("[/\\\\]");
+ String[] otherSegments = other.split("[/\\\\]");
+ int prefixLength = 0;
+ int maxi = Math.min(baseSegments.length, otherSegments.length);
+ while (prefixLength < maxi && baseSegments[prefixLength].equals(otherSegments[prefixLength])) {
+ prefixLength++;
+ }
+
+ if (prefixLength == 0) {
+ return other;
+ }
+
+ List relative = new ArrayList<>();
+ for (int i = prefixLength; i < baseSegments.length; i++) {
+ relative.add("..");
+ }
+ relative.addAll(Arrays.asList(otherSegments).subList(prefixLength, otherSegments.length));
+ return String.join(File.separator, relative);
+ }
+
+ /** Return whether the path is the root path (/). */
+ private static boolean isFileSystemRoot(Path root) {
+ return root.isAbsolute() && root.getNameCount() == 0;
+ }
+
+ /**
+ * Return the textfile's display name. Takes the shortest path we
+ * can construct from the relativize roots.
+ *
+ * package private for test only
+ */
+ static String getDisplayName(FileId file, List relativizeRoots) {
+ String best = file.toAbsolutePath();
+ for (Path root : relativizeRoots) {
+ if (isFileSystemRoot(root)) {
+ // Absolutize the path. Since the relativize roots are
+ // sorted by ascending length, this should be the first in the list
+ // (so another root can override it).
+ best = file.toAbsolutePath();
+ continue;
+ }
+
+ String relative = relativizePath(root.toAbsolutePath().toString(), file.toAbsolutePath());
+ if (countSegments(relative) < countSegments(best)) {
+ best = relative;
+ }
+ }
+ return best;
+ }
}
diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/lang/document/FileCollectorTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/lang/document/FileCollectorTest.java
index a6b47cb18e..234e4ecd60 100644
--- a/pmd-core/src/test/java/net/sourceforge/pmd/lang/document/FileCollectorTest.java
+++ b/pmd-core/src/test/java/net/sourceforge/pmd/lang/document/FileCollectorTest.java
@@ -12,13 +12,11 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
-import java.nio.file.Paths;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
-import net.sourceforge.pmd.PmdAnalysis;
import net.sourceforge.pmd.lang.DummyLanguageModule;
import net.sourceforge.pmd.lang.Language;
import net.sourceforge.pmd.lang.LanguageRegistry;
@@ -93,11 +91,6 @@ class FileCollectorTest {
}
- @Test
- void testRelativize() {
- String displayName = PmdAnalysis.getDisplayName(FileId.forPath(Paths.get("a", "b", "c")), listOf(Paths.get("a")));
- assertEquals(displayName, Paths.get("b", "c").toString());
- }
private Path newFile(Path root, String path) throws IOException {
Path resolved = root.resolve(path);
diff --git a/pmd-core/src/test/java/net/sourceforge/pmd/reporting/ConfigurableFileNameRendererTest.java b/pmd-core/src/test/java/net/sourceforge/pmd/reporting/ConfigurableFileNameRendererTest.java
new file mode 100644
index 0000000000..a33c32d340
--- /dev/null
+++ b/pmd-core/src/test/java/net/sourceforge/pmd/reporting/ConfigurableFileNameRendererTest.java
@@ -0,0 +1,39 @@
+/*
+ * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
+ */
+
+package net.sourceforge.pmd.reporting;
+
+import static net.sourceforge.pmd.reporting.ConfigurableFileNameRenderer.getDisplayName;
+import static net.sourceforge.pmd.util.CollectionUtil.listOf;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.junit.jupiter.api.Test;
+
+import net.sourceforge.pmd.lang.document.FileId;
+
+/**
+ * @author Clément Fournier
+ */
+public class ConfigurableFileNameRendererTest {
+
+ @Test
+ void testRelativize() {
+ FileId file = FileId.forPath(Paths.get("a", "b", "c"));
+ String displayName = getDisplayName(file, listOf(Paths.get("a")));
+ assertEquals(displayName, Paths.get("b", "c").toString());
+ }
+
+ @Test
+ void testRelativizeWithRoot() {
+ Path path = Paths.get("a", "b", "c");
+ FileId file = FileId.forPath(path);
+ String displayName = getDisplayName(file, listOf(Paths.get("/")));
+ assertEquals(path.toAbsolutePath().toString(),
+ displayName);
+ }
+
+}