@@ -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++) { |
@@ -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(); |
@@ -84,7 +84,7 @@ public class MultilineIssuesSensor implements Sensor { | |||
Map<Integer, Table<Integer, Integer, TextPointer>> 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<Integer, TextPointer> 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); |
@@ -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<Integer> noSonarLines = new HashSet<>(); | |||
int[] lineCounter = {1}; | |||
try (Stream<String> stream = Files.lines(inputFile.path(), context.fileSystem().encoding())) { | |||
try (Stream<String> stream = Files.lines(inputFile.path(), inputFile.charset())) { | |||
stream.forEachOrdered(lineStr -> { | |||
if (lineStr.contains("//NOSONAR")) { | |||
noSonarLines.add(lineCounter[0]); |
@@ -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(); | |||
@@ -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(); | |||
} |
@@ -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 |