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++) {
}
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();
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);
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);
@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]);
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();
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;
* @since 5.2
*/
TextRange selectLine(int line);
+
+ /**
+ * Charset to be used to decode this specific file.
+ * @since 6.0
+ */
+ Charset charset();
}
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;
.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();
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