From 7bea09f6eb442441908de2df48b0ed7dd75e001f Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Tue, 12 Jul 2016 11:34:40 +0200 Subject: [PATCH] SONAR-7883 Expose InputFile charset in API --- .../main/java/org/sonar/xoo/lang/CpdTokenizerSensor.java | 2 +- .../src/main/java/org/sonar/xoo/rule/HasTagSensor.java | 2 +- .../java/org/sonar/xoo/rule/MultilineIssuesSensor.java | 4 ++-- .../src/main/java/org/sonar/xoo/rule/NoSonarSensor.java | 6 +++--- .../src/main/java/org/sonar/api/batch/fs/FileSystem.java | 5 +++-- .../src/main/java/org/sonar/api/batch/fs/InputFile.java | 7 +++++++ .../sonar/api/batch/fs/internal/DefaultInputFileTest.java | 5 ++++- 7 files changed, 21 insertions(+), 10 deletions(-) diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/CpdTokenizerSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/CpdTokenizerSensor.java index aa2c69faa57..ed06b22574a 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/CpdTokenizerSensor.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/lang/CpdTokenizerSensor.java @@ -42,7 +42,7 @@ public class CpdTokenizerSensor implements Sensor { NewCpdTokens newCpdTokens = context.newCpdTokens().onFile(inputFile); try { StringBuilder sb = new StringBuilder(); - for (String line : FileUtils.readLines(inputFile.file(), context.fileSystem().encoding())) { + for (String line : FileUtils.readLines(inputFile.file(), inputFile.charset())) { int startOffset = 0; int endOffset = 0; for (int i = 0; i < line.length(); i++) { diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/HasTagSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/HasTagSensor.java index 9f030e6db69..afea04da892 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/HasTagSensor.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/HasTagSensor.java @@ -58,7 +58,7 @@ public class HasTagSensor extends AbstractXooRuleSensor { } try { int[] lineCounter = {1}; - Files.lines(inputFile.path(), context.fileSystem().encoding()).forEachOrdered(lineStr -> { + Files.lines(inputFile.path(), inputFile.charset()).forEachOrdered(lineStr -> { int startIndex = -1; while ((startIndex = lineStr.indexOf(tag, startIndex + 1)) != -1) { NewIssue newIssue = context.newIssue(); diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/MultilineIssuesSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/MultilineIssuesSensor.java index babb207377e..ef63c48adf3 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/MultilineIssuesSensor.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/MultilineIssuesSensor.java @@ -84,7 +84,7 @@ public class MultilineIssuesSensor implements Sensor { Map> endFlowsPositions) { int currentLine = 0; try { - for (String lineStr : Files.readAllLines(file.path(), context.fileSystem().encoding())) { + for (String lineStr : Files.readAllLines(file.path(), file.charset())) { currentLine++; Matcher m = START_FLOW_PATTERN.matcher(lineStr); @@ -159,7 +159,7 @@ public class MultilineIssuesSensor implements Sensor { Map endPositions) { int currentLine = 0; try { - for (String lineStr : Files.readAllLines(file.path(), context.fileSystem().encoding())) { + for (String lineStr : Files.readAllLines(file.path(), file.charset())) { currentLine++; Matcher m = START_ISSUE_PATTERN.matcher(lineStr); diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/NoSonarSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/NoSonarSensor.java index d57e2f1eb93..93e4d169070 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/NoSonarSensor.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/NoSonarSensor.java @@ -50,15 +50,15 @@ public class NoSonarSensor implements Sensor { @Override public void execute(SensorContext context) { for (InputFile inputFile : context.fileSystem().inputFiles(context.fileSystem().predicates().hasLanguage(Xoo.KEY))) { - processFile(inputFile, context); + processFile(inputFile); } } - private void processFile(InputFile inputFile, SensorContext context) { + private void processFile(InputFile inputFile) { try { Set noSonarLines = new HashSet<>(); int[] lineCounter = {1}; - try (Stream stream = Files.lines(inputFile.path(), context.fileSystem().encoding())) { + try (Stream stream = Files.lines(inputFile.path(), inputFile.charset())) { stream.forEachOrdered(lineStr -> { if (lineStr.contains("//NOSONAR")) { noSonarLines.add(lineCounter[0]); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/FileSystem.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/FileSystem.java index af020cdbc03..0e8bf735df6 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/FileSystem.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/FileSystem.java @@ -60,8 +60,9 @@ public interface FileSystem { File baseDir(); /** - * Default encoding of input files. If it's not defined, then - * the platform default encoding is returned + * Default encoding of files in this project. If it's not defined, then + * the platform default encoding is returned. + * When reading an {@link InputFile} it is preferable to use {@link InputFile#charset()} */ Charset encoding(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java index 722abe9e5ff..dafc0c8ad79 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java @@ -20,6 +20,7 @@ package org.sonar.api.batch.fs; import java.io.File; +import java.nio.charset.Charset; import java.nio.file.Path; import javax.annotation.CheckForNull; import org.sonar.api.batch.fs.internal.DefaultInputFile; @@ -156,4 +157,10 @@ public interface InputFile extends InputPath { * @since 5.2 */ TextRange selectLine(int line); + + /** + * Charset to be used to decode this specific file. + * @since 6.0 + */ + Charset charset(); } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java index 6b06eabe4b9..da9ead4cba7 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java @@ -20,6 +20,7 @@ package org.sonar.api.batch.fs.internal; import java.io.File; +import java.nio.charset.StandardCharsets; import java.nio.file.Path; import org.junit.Rule; import org.junit.Test; @@ -43,7 +44,8 @@ public class DefaultInputFileTest { .setLines(42) .setLanguage("php") .setStatus(InputFile.Status.ADDED) - .setType(InputFile.Type.TEST); + .setType(InputFile.Type.TEST) + .setCharset(StandardCharsets.ISO_8859_1); assertThat(inputFile.relativePath()).isEqualTo("src/Foo.php"); assertThat(new File(inputFile.relativePath())).isRelative(); @@ -53,6 +55,7 @@ public class DefaultInputFileTest { assertThat(inputFile.status()).isEqualTo(InputFile.Status.ADDED); assertThat(inputFile.type()).isEqualTo(InputFile.Type.TEST); assertThat(inputFile.lines()).isEqualTo(42); + assertThat(inputFile.charset()).isEqualTo(StandardCharsets.ISO_8859_1); } @Test -- 2.39.5