[doc] Replace IOUtils with IOUtil

This commit is contained in:
Andreas Dangel
2022-05-13 11:39:22 +02:00
parent a1922c5956
commit bef7eed0ec
8 changed files with 40 additions and 37 deletions

View File

@ -37,8 +37,7 @@ import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.io.IOUtils;
import net.sourceforge.pmd.util.IOUtil;
/**
* Checks links to local pages for non-existing link-targets.
@ -298,7 +297,7 @@ public class DeadLinksChecker {
private String fileToString(Path mdFile) {
try (InputStream inputStream = Files.newInputStream(mdFile)) {
return IOUtils.toString(inputStream, StandardCharsets.UTF_8);
return IOUtil.readToString(inputStream, StandardCharsets.UTF_8);
} catch (IOException ex) {
throw new RuntimeException("error reading " + mdFile, ex);
}

View File

@ -10,16 +10,16 @@ import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import org.apache.commons.io.FilenameUtils;
import net.sourceforge.pmd.RuleSet;
import net.sourceforge.pmd.RuleSetLoader;
import net.sourceforge.pmd.util.IOUtil;
public final class GenerateRuleDocsCmd {
private GenerateRuleDocsCmd() {
@ -51,7 +51,7 @@ public final class GenerateRuleDocsCmd {
try {
List<String> additionalRulesets = new ArrayList<>();
Pattern rulesetPattern = Pattern.compile("^.+" + Pattern.quote(File.separator) + "pmd-\\w+"
+ Pattern.quote(FilenameUtils.normalize("/src/main/resources/rulesets/"))
+ Pattern.quote(IOUtil.normalizePath(File.separator + Paths.get("src", "main", "resources", "rulesets").toString() + File.separator))
+ "\\w+" + Pattern.quote(File.separator) + "\\w+.xml$");
Files.walkFileTree(basePath, new SimpleFileVisitor<Path>() {
@Override

View File

@ -30,8 +30,8 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.apache.commons.text.StringEscapeUtils;
import net.sourceforge.pmd.Rule;
@ -43,6 +43,7 @@ import net.sourceforge.pmd.lang.rule.RuleReference;
import net.sourceforge.pmd.lang.rule.XPathRule;
import net.sourceforge.pmd.properties.MultiValuePropertyDescriptor;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.util.IOUtil;
public class RuleDocGenerator {
private static final Logger LOG = Logger.getLogger(RuleDocGenerator.class.getName());
@ -131,7 +132,7 @@ public class RuleDocGenerator {
}
private Path getAbsoluteOutputPath(String filename) {
return root.resolve(FilenameUtils.normalize(filename));
return root.resolve(IOUtil.normalizePath(filename));
}
private Map<Language, List<RuleSet>> sortRulesets(List<RuleSet> rulesets) {
@ -608,17 +609,13 @@ public class RuleDocGenerator {
// is replaced by a correct path.
for (List<RuleSet> rulesets : sortedRulesets.values()) {
for (RuleSet ruleset : rulesets) {
// Note: the path is normalized to unix path separators, so that the editme link
// uses forward slashes
String rulesetFilename = FilenameUtils.normalize(StringUtils.chomp(ruleset.getFileName()), true);
String rulesetFilename = normalizeForwardSlashes(StringUtils.chomp(ruleset.getFileName()));
allRulesets.put(ruleset.getFileName(), rulesetFilename);
for (Rule rule : ruleset.getRules()) {
String ruleClass = rule.getRuleClass();
String relativeSourceFilename = ruleClass.replaceAll("\\.", Matcher.quoteReplacement(File.separator))
+ ".java";
// Note: the path is normalized to unix path separators, so that the editme link
// uses forward slashes
allRules.put(ruleClass, FilenameUtils.normalize(relativeSourceFilename, true));
allRules.put(ruleClass, normalizeForwardSlashes(relativeSourceFilename));
}
}
}
@ -640,9 +637,7 @@ public class RuleDocGenerator {
}
if (foundRuleClass != null) {
Path foundPath = root.relativize(file);
// Note: the path is normalized to unix path separators, so that the editme link
// uses forward slashes
allRules.put(foundRuleClass, FilenameUtils.normalize(foundPath.toString(), true));
allRules.put(foundRuleClass, normalizeForwardSlashes(foundPath.toString()));
}
String foundRuleset = null;
@ -654,9 +649,7 @@ public class RuleDocGenerator {
}
if (foundRuleset != null) {
Path foundPath = root.relativize(file);
// Note: the path is normalized to unix path separators, so that the editme link
// uses forward slashes
allRulesets.put(foundRuleset, FilenameUtils.normalize(foundPath.toString(), true));
allRulesets.put(foundRuleset, normalizeForwardSlashes(foundPath.toString()));
}
}
return FileVisitResult.CONTINUE;
@ -666,4 +659,14 @@ public class RuleDocGenerator {
throw new RuntimeException(e);
}
}
private static String normalizeForwardSlashes(String path) {
String normalized = IOUtil.normalizePath(path);
if (SystemUtils.IS_OS_WINDOWS) {
// Note: windows path separators are changed to forward slashes,
// so that the editme link works
normalized = normalized.replaceAll(Pattern.quote(File.separator), "/");
}
return normalized;
}
}

View File

@ -4,12 +4,14 @@
package net.sourceforge.pmd.docs;
import org.apache.commons.io.FilenameUtils;
import java.io.File;
import org.apache.commons.lang3.StringUtils;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleSet;
import net.sourceforge.pmd.lang.rule.RuleReference;
import net.sourceforge.pmd.util.IOUtil;
public final class RuleSetUtils {
@ -28,7 +30,7 @@ public final class RuleSetUtils {
}
public static String getRuleSetFilename(String rulesetFileName) {
return FilenameUtils.getBaseName(StringUtils.chomp(rulesetFileName));
return IOUtil.getFilenameBase(StringUtils.chomp(rulesetFileName));
}
/**
@ -50,8 +52,8 @@ public final class RuleSetUtils {
}
public static String getRuleSetClasspath(RuleSet ruleset) {
final String RESOURCES_PATH = "/resources/";
String filename = FilenameUtils.normalize(StringUtils.chomp(ruleset.getFileName()), true);
final String RESOURCES_PATH = File.separator + "resources" + File.separator;
String filename = IOUtil.normalizePath(StringUtils.chomp(ruleset.getFileName()));
int startIndex = filename.lastIndexOf(RESOURCES_PATH);
if (startIndex > -1) {
return filename.substring(startIndex + RESOURCES_PATH.length());

View File

@ -10,7 +10,6 @@ import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import net.sourceforge.pmd.PMD;
@ -49,7 +48,7 @@ public class MockedFileWriter implements FileWriter {
}
public static String normalizeLineSeparators(String s) {
return s.replaceAll(Pattern.quote(IOUtils.LINE_SEPARATOR_WINDOWS), IOUtils.LINE_SEPARATOR_UNIX)
.replaceAll(Pattern.quote(IOUtils.LINE_SEPARATOR_UNIX), PMD.EOL);
return s.replaceAll(Pattern.quote("\r\n"), "\n")
.replaceAll(Pattern.quote("\n"), PMD.EOL);
}
}

View File

@ -11,11 +11,10 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@ -24,6 +23,7 @@ import org.junit.rules.TemporaryFolder;
import net.sourceforge.pmd.RuleSet;
import net.sourceforge.pmd.RuleSetLoader;
import net.sourceforge.pmd.docs.MockedFileWriter.FileEntry;
import net.sourceforge.pmd.util.IOUtil;
public class RuleDocGeneratorTest {
@ -52,7 +52,7 @@ public class RuleDocGeneratorTest {
private static String loadResource(String name) throws IOException {
return MockedFileWriter.normalizeLineSeparators(
IOUtils.toString(RuleDocGeneratorTest.class.getResourceAsStream(name), StandardCharsets.UTF_8));
IOUtil.readToString(RuleDocGeneratorTest.class.getResourceAsStream(name), StandardCharsets.UTF_8));
}
@Test
@ -69,15 +69,15 @@ public class RuleDocGeneratorTest {
assertEquals(3, writer.getData().size());
FileEntry languageIndex = writer.getData().get(0);
assertTrue(FilenameUtils.normalize(languageIndex.getFilename(), true).endsWith("docs/pages/pmd/rules/java.md"));
assertTrue(IOUtil.normalizePath(languageIndex.getFilename()).endsWith(Paths.get("docs", "pages", "pmd", "rules", "java.md").toString()));
assertEquals(loadResource("/expected/java.md"), languageIndex.getContent());
FileEntry ruleSetIndex = writer.getData().get(1);
assertTrue(FilenameUtils.normalize(ruleSetIndex.getFilename(), true).endsWith("docs/pages/pmd/rules/java/sample.md"));
assertTrue(IOUtil.normalizePath(ruleSetIndex.getFilename()).endsWith(Paths.get("docs", "pages", "pmd", "rules", "java", "sample.md").toString()));
assertEquals(loadResource("/expected/sample.md"), ruleSetIndex.getContent());
FileEntry sidebar = writer.getData().get(2);
assertTrue(FilenameUtils.normalize(sidebar.getFilename(), true).endsWith("docs/_data/sidebars/pmd_sidebar.yml"));
assertTrue(IOUtil.normalizePath(sidebar.getFilename()).endsWith(Paths.get("docs", "_data", "sidebars", "pmd_sidebar.yml").toString()));
assertEquals(loadResource("/expected/pmd_sidebar.yml"), sidebar.getContent());
}
}

View File

@ -12,19 +12,19 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.io.FilenameUtils;
import org.junit.Test;
import net.sourceforge.pmd.RuleSetFactory;
import net.sourceforge.pmd.RuleSetNotFoundException;
import net.sourceforge.pmd.RulesetsFactoryUtils;
import net.sourceforge.pmd.util.IOUtil;
public class RuleSetResolverTest {
private static List<String> excludedRulesets = new ArrayList<>();
static {
excludedRulesets.add(FilenameUtils.normalize("pmd-test/src/main/resources/rulesets/dummy/basic.xml"));
excludedRulesets.add(IOUtil.normalizePath("pmd-test/src/main/resources/rulesets/dummy/basic.xml"));
}
@Test

View File

@ -15,7 +15,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.SystemUtils;
import org.junit.Before;
import org.junit.Test;
@ -28,6 +27,7 @@ import net.sourceforge.pmd.RuleSet;
import net.sourceforge.pmd.RulesetsFactoryUtils;
import net.sourceforge.pmd.lang.Language;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.util.IOUtil;
public class SidebarGeneratorTest {
private MockedFileWriter writer = new MockedFileWriter();
@ -56,7 +56,7 @@ public class SidebarGeneratorTest {
String yaml = new Yaml(options).dump(result);
String expected = MockedFileWriter.normalizeLineSeparators(
IOUtils.toString(SidebarGeneratorTest.class.getResourceAsStream("sidebar.yml"), StandardCharsets.UTF_8));
IOUtil.readToString(SidebarGeneratorTest.class.getResourceAsStream("sidebar.yml"), StandardCharsets.UTF_8));
assertEquals(expected, yaml);
}
}