diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/internal/util/ShortFilenameUtil.java b/pmd-core/src/main/java/net/sourceforge/pmd/internal/util/ShortFilenameUtil.java new file mode 100644 index 0000000000..cc081da613 --- /dev/null +++ b/pmd-core/src/main/java/net/sourceforge/pmd/internal/util/ShortFilenameUtil.java @@ -0,0 +1,56 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.internal.util; + +import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; + +import net.sourceforge.pmd.PMDConfiguration; +import net.sourceforge.pmd.cli.PMDParameters; + +public final class ShortFilenameUtil { + + private ShortFilenameUtil() { + } + + /** + * Determines the filename that should be used in the report depending on the + * option "shortnames". If the option is enabled, then the filename in the report + * is without the directory prefix of the directories, that have been analyzed. + * If the option "shortnames" is not enabled, then the inputFileName is returned as-is. + * + * @param inputPathPrefixes + * @param inputFileName + * @return + * + * @see PMDConfiguration#isReportShortNames() + * @see PMDParameters#isShortnames() + */ + public static String determineFileName(List inputPathPrefixes, String inputFileName) { + for (final String prefix : inputPathPrefixes) { + final Path prefPath = Paths.get(prefix).toAbsolutePath(); + final String prefPathString = prefPath.toString(); + + if (inputFileName.startsWith(prefPathString)) { + if (prefPath.toFile().isDirectory()) { + return trimAnyPathSep(inputFileName.substring(prefPathString.length())); + } else { + if (inputFileName.indexOf(File.separatorChar) == -1) { + return inputFileName; + } + return trimAnyPathSep(inputFileName.substring(prefPathString.lastIndexOf(File.separatorChar))); + } + } + } + + return inputFileName; + } + + private static String trimAnyPathSep(String name) { + return name != null && name.charAt(0) == File.separatorChar ? name.substring(1) : name; + } +} diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/AbstractRenderer.java b/pmd-core/src/main/java/net/sourceforge/pmd/renderers/AbstractRenderer.java index 71420e8a78..6279575266 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/renderers/AbstractRenderer.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/renderers/AbstractRenderer.java @@ -4,11 +4,8 @@ package net.sourceforge.pmd.renderers; -import java.io.File; import java.io.IOException; import java.io.Writer; -import java.nio.file.Path; -import java.nio.file.Paths; import java.util.Collections; import java.util.List; @@ -16,6 +13,7 @@ import org.apache.commons.io.IOUtils; import net.sourceforge.pmd.PMDConfiguration; import net.sourceforge.pmd.cli.PMDParameters; +import net.sourceforge.pmd.internal.util.ShortFilenameUtil; import net.sourceforge.pmd.properties.AbstractPropertySource; /** @@ -88,27 +86,7 @@ public abstract class AbstractRenderer extends AbstractPropertySource implements * @see PMDParameters#isShortnames() */ protected String determineFileName(String inputFileName) { - for (final String prefix : inputPathPrefixes) { - final Path prefPath = Paths.get(prefix).toAbsolutePath(); - final String prefPathString = prefPath.toString(); - - if (inputFileName.startsWith(prefPathString)) { - if (prefPath.toFile().isDirectory()) { - return trimAnyPathSep(inputFileName.substring(prefPathString.length())); - } else { - if (inputFileName.indexOf(File.separatorChar) == -1) { - return inputFileName; - } - return trimAnyPathSep(inputFileName.substring(prefPathString.lastIndexOf(File.separatorChar))); - } - } - } - - return inputFileName; - } - - private String trimAnyPathSep(String name) { - return name != null && name.charAt(0) == File.separatorChar ? name.substring(1) : name; + return ShortFilenameUtil.determineFileName(inputPathPrefixes, inputFileName); } @Override diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/util/datasource/FileDataSource.java b/pmd-core/src/main/java/net/sourceforge/pmd/util/datasource/FileDataSource.java index b4bb31aa01..8f2bca6082 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/util/datasource/FileDataSource.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/util/datasource/FileDataSource.java @@ -8,16 +8,15 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; + +import net.sourceforge.pmd.internal.util.ShortFilenameUtil; /** * DataSource implementation to read data from a file. */ public class FileDataSource implements DataSource { - - private static final String FILE_SEPARATOR = System.getProperty("file.separator"); - private final File file; /** @@ -34,29 +33,16 @@ public class FileDataSource implements DataSource { } @Override - public String getNiceFileName(boolean shortNames, String inputFileName) { - return glomName(shortNames, inputFileName, file); + public String getNiceFileName(boolean shortNames, String inputPaths) { + return glomName(shortNames, inputPaths, file); } - private String glomName(boolean shortNames, String inputFileName, File file) { + private String glomName(boolean shortNames, String inputPaths, File file) { if (shortNames) { - if (inputFileName != null) { - for (final String prefix : inputFileName.split(",")) { - final Path prefPath = Paths.get(prefix).toAbsolutePath(); - final String prefPathString = prefPath.toString(); - final String absoluteFilePath = file.getAbsolutePath(); - - if (absoluteFilePath.startsWith(prefPathString)) { - if (prefPath.toFile().isDirectory()) { - return trimAnyPathSep(absoluteFilePath.substring(prefPathString.length())); - } else { - if (inputFileName.indexOf(FILE_SEPARATOR.charAt(0)) == -1) { - return inputFileName; - } - return trimAnyPathSep(absoluteFilePath.substring(prefPathString.lastIndexOf(FILE_SEPARATOR))); - } - } - } + if (inputPaths != null) { + List inputPathPrefixes = Arrays.asList(inputPaths.split(",")); + final String absoluteFilePath = file.getAbsolutePath(); + return ShortFilenameUtil.determineFileName(inputPathPrefixes, absoluteFilePath); } else { // if the 'master' file is not specified, just use the file name return file.getName(); @@ -70,11 +56,6 @@ public class FileDataSource implements DataSource { } } - private String trimAnyPathSep(String name) { - - return name.startsWith(FILE_SEPARATOR) ? name.substring(1) : name; - } - @Override public String toString() { return new StringBuilder(FileDataSource.class.getSimpleName())