Merge branch 'replace-commons-io' into pmd7-replace-commons-io

This commit is contained in:
Andreas Dangel
2022-05-13 17:14:09 +02:00
4 changed files with 34 additions and 18 deletions

View File

@ -326,7 +326,7 @@ public final class IOUtil {
charBuffer.flip();
encoder.encode(charBuffer, byteBuffer, eof);
byteBuffer.flip();
charBuffer.flip();
charBuffer.compact();
}
if (byteBuffer.hasRemaining()) {

View File

@ -193,6 +193,21 @@ public class IOUtilTest {
}
}
@Test
public void testInputStreamFromReader2() throws IOException {
int size = 8192 + 8192 + 10;
char[] data = new char[size];
for (int i = 0; i < size; i++) {
data[i] = 'A';
}
data[8192] = 'ä'; // block size border - in UTF-8 these are two bytes. Decoding needs to take the bytes
// from previous block and new block
try (InputStream inputStream = IOUtil.fromReader(new StringReader(new String(data)))) {
byte[] bytes = IOUtil.toByteArray(inputStream);
Assert.assertEquals(new String(data), new String(bytes, StandardCharsets.UTF_8));
}
}
@Test
public void testCopyStream() throws IOException {
int size = 8192 + 8192 + 10;

View File

@ -29,7 +29,6 @@ import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -609,13 +608,13 @@ public class RuleDocGenerator {
// is replaced by a correct path.
for (List<RuleSet> rulesets : sortedRulesets.values()) {
for (RuleSet ruleset : rulesets) {
String rulesetFilename = normalizeForwardSlashes(StringUtils.chomp(ruleset.getFileName()));
String rulesetFilename = RuleSetUtils.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";
allRules.put(ruleClass, normalizeForwardSlashes(relativeSourceFilename));
allRules.put(ruleClass, RuleSetUtils.normalizeForwardSlashes(relativeSourceFilename));
}
}
}
@ -637,7 +636,7 @@ public class RuleDocGenerator {
}
if (foundRuleClass != null) {
Path foundPath = root.relativize(file);
allRules.put(foundRuleClass, normalizeForwardSlashes(foundPath.toString()));
allRules.put(foundRuleClass, RuleSetUtils.normalizeForwardSlashes(foundPath.toString()));
}
String foundRuleset = null;
@ -649,7 +648,7 @@ public class RuleDocGenerator {
}
if (foundRuleset != null) {
Path foundPath = root.relativize(file);
allRulesets.put(foundRuleset, normalizeForwardSlashes(foundPath.toString()));
allRulesets.put(foundRuleset, RuleSetUtils.normalizeForwardSlashes(foundPath.toString()));
}
}
return FileVisitResult.CONTINUE;
@ -659,14 +658,4 @@ 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

@ -5,8 +5,10 @@
package net.sourceforge.pmd.docs;
import java.io.File;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleSet;
@ -52,8 +54,8 @@ public final class RuleSetUtils {
}
public static String getRuleSetClasspath(RuleSet ruleset) {
final String RESOURCES_PATH = File.separator + "resources" + File.separator;
String filename = IOUtil.normalizePath(StringUtils.chomp(ruleset.getFileName()));
final String RESOURCES_PATH = "/resources/";
String filename = normalizeForwardSlashes(StringUtils.chomp(ruleset.getFileName()));
int startIndex = filename.lastIndexOf(RESOURCES_PATH);
if (startIndex > -1) {
return filename.substring(startIndex + RESOURCES_PATH.length());
@ -62,6 +64,16 @@ public final class RuleSetUtils {
}
}
public 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;
}
/**
* Recursively resolves rule references until the last reference.
* The last reference is returned.