Receive Charset of Document in the constructor

This commit is contained in:
gonzalo
2018-01-08 08:56:40 -03:00
parent de09472421
commit 40ad978d8f
3 changed files with 26 additions and 23 deletions

View File

@ -4,15 +4,16 @@
package net.sourceforge.pmd.util.document;
import static java.util.Objects.requireNonNull;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
@ -35,10 +36,11 @@ public class DocumentFile implements Document, Closeable {
private int currentPosition = 0;
private final Path temporaryPath = Files.createTempFile("pmd-", ".tmp");
private final Writer writer = Files.newBufferedWriter(temporaryPath, StandardCharsets.UTF_8);
private final Writer writer;
public DocumentFile(final File file) throws IOException {
reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8);
public DocumentFile(final File file, final Charset charset) throws IOException {
reader = Files.newBufferedReader(requireNonNull(file).toPath(), requireNonNull(charset));
writer = Files.newBufferedWriter(temporaryPath, charset);
this.filePath = file.toPath();
mapLinesToOffsets();
}

View File

@ -38,7 +38,7 @@ public class DocumentFileTest {
public void insertAtStartOfTheFileShouldSucceed() throws IOException {
writeContentToTemporaryFile("static void main(String[] args) {}");
try (DocumentFile documentFile = new DocumentFile(temporaryFile)) {
try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) {
documentFile.insert(0, 0, "public ");
}
@ -85,7 +85,7 @@ public class DocumentFileTest {
public void insertVariousTokensIntoTheFileShouldSucceed() throws IOException {
writeContentToTemporaryFile("static void main(String[] args) {}");
try (DocumentFile documentFile = new DocumentFile(temporaryFile)) {
try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) {
documentFile.insert(0, 0, "public ");
documentFile.insert(0, 17, "final ");
}
@ -101,7 +101,7 @@ public class DocumentFileTest {
final String code = "public static void main(String[] args)";
writeContentToTemporaryFile(code);
try (DocumentFile documentFile = new DocumentFile(temporaryFile)) {
try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) {
documentFile.insert(0, code.length(), "{}");
}
@ -116,7 +116,7 @@ public class DocumentFileTest {
final String code = "public static void main(final String[] args) {}";
writeContentToTemporaryFile(code);
try (DocumentFile documentFile = new DocumentFile(temporaryFile)) {
try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) {
documentFile.delete(new RegionByLineImp(0, 0, 24, 30));
}
@ -131,7 +131,7 @@ public class DocumentFileTest {
final String code = "static void main(final String[] args) {}";
writeContentToTemporaryFile(code);
try (DocumentFile documentFile = new DocumentFile(temporaryFile)) {
try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) {
documentFile.insert(0, 0, "public ");
documentFile.delete(new RegionByLineImp(0, 0, 17, 23));
}
@ -147,7 +147,7 @@ public class DocumentFileTest {
final String code = "void main(String[] args) {}";
writeContentToTemporaryFile(code);
try (DocumentFile documentFile = new DocumentFile(temporaryFile)) {
try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) {
documentFile.insert(0, 0, "public ");
documentFile.insert(0, 0, "static ");
documentFile.delete(new RegionByLineImp(0, 0, 0, 4));
@ -166,7 +166,7 @@ public class DocumentFileTest {
final String code = "int main(String[] args) {}";
writeContentToTemporaryFile(code);
try (DocumentFile documentFile = new DocumentFile(temporaryFile)) {
try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) {
documentFile.replace(new RegionByLineImp(0, 0, 0, 3), "void");
}
@ -181,7 +181,7 @@ public class DocumentFileTest {
final String code = "int main(String[] args) {}";
writeContentToTemporaryFile(code);
try (DocumentFile documentFile = new DocumentFile(temporaryFile)) {
try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) {
documentFile.replace(new RegionByLineImp(0, 0, 0, "int".length()), "void");
documentFile.replace(new RegionByLineImp(0, 0, 4, 4 + "main".length()), "foo");
documentFile.replace(new RegionByLineImp(0, 0, 9, 9 + "String".length()), "CharSequence");
@ -198,7 +198,7 @@ public class DocumentFileTest {
final String code = "static int main(CharSequence[] args) {}";
writeContentToTemporaryFile(code);
try (DocumentFile documentFile = new DocumentFile(temporaryFile)) {
try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) {
documentFile.insert(0, 0, "public");
documentFile.delete(new RegionByLineImp(0, 0, 0, 6));
documentFile.replace(new RegionByLineImp(0, 0, 7, 7 + "int".length()), "void");
@ -224,7 +224,7 @@ public class DocumentFileTest {
expectedLineToOffset.add(40);
expectedLineToOffset.add(49);
try (DocumentFile documentFile = new DocumentFile(temporaryFile)) {
try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) {
assertEquals(expectedLineToOffset, documentFile.getLineToOffset());
}
}
@ -241,7 +241,7 @@ public class DocumentFileTest {
expectedLineToOffset.add(41);
expectedLineToOffset.add(51);
try (DocumentFile documentFile = new DocumentFile(temporaryFile)) {
try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) {
assertEquals(expectedLineToOffset, documentFile.getLineToOffset());
}
}
@ -258,7 +258,7 @@ public class DocumentFileTest {
expectedLineToOffset.add(41);
expectedLineToOffset.add(50);
try (DocumentFile documentFile = new DocumentFile(temporaryFile)) {
try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) {
assertEquals(expectedLineToOffset, documentFile.getLineToOffset());
}
}

View File

@ -10,6 +10,7 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
@ -39,7 +40,7 @@ public class DocumentOperationsApplierForNonOverlappingRegionsWithDocumentFileTe
public void insertAtStartOfTheDocumentShouldSucceed() throws IOException {
writeContentToTemporaryFile("static void main(String[] args) {}");
try (DocumentFile documentFile = new DocumentFile(temporaryFile)) {
try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) {
applier = new DocumentOperationsApplierForNonOverlappingRegions(documentFile);
applier.addDocumentOperation(new InsertDocumentOperation(0, 0, "public "));
@ -89,7 +90,7 @@ public class DocumentOperationsApplierForNonOverlappingRegionsWithDocumentFileTe
public void removeTokenShouldSucceed() throws IOException {
writeContentToTemporaryFile("public static void main(String[] args) {}");
try (DocumentFile documentFile = new DocumentFile(temporaryFile)) {
try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) {
applier = new DocumentOperationsApplierForNonOverlappingRegions(documentFile);
applier.addDocumentOperation(new DeleteDocumentOperation(0, 0, 7, 13));
@ -107,7 +108,7 @@ public class DocumentOperationsApplierForNonOverlappingRegionsWithDocumentFileTe
final String code = "static void main(final String[] args) {}";
writeContentToTemporaryFile(code);
try (DocumentFile documentFile = new DocumentFile(temporaryFile)) {
try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) {
applier = new DocumentOperationsApplierForNonOverlappingRegions(documentFile);
applier.addDocumentOperation(new InsertDocumentOperation(0, 0, "public "));
applier.addDocumentOperation(new DeleteDocumentOperation(0, 0, 17, 23));
@ -126,7 +127,7 @@ public class DocumentOperationsApplierForNonOverlappingRegionsWithDocumentFileTe
final String code = "void main(String[] args) {}";
writeContentToTemporaryFile(code);
try (DocumentFile documentFile = new DocumentFile(temporaryFile)) {
try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) {
applier = new DocumentOperationsApplierForNonOverlappingRegions(documentFile);
applier.addDocumentOperation(new InsertDocumentOperation(0, 0, "public "));
@ -149,7 +150,7 @@ public class DocumentOperationsApplierForNonOverlappingRegionsWithDocumentFileTe
final String code = "int main(String[] args) {}";
writeContentToTemporaryFile(code);
try (DocumentFile documentFile = new DocumentFile(temporaryFile)) {
try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) {
applier = new DocumentOperationsApplierForNonOverlappingRegions(documentFile);
applier.addDocumentOperation(new ReplaceDocumentOperation(0, 0, 0, "int".length(), "void"));
@ -182,7 +183,7 @@ public class DocumentOperationsApplierForNonOverlappingRegionsWithDocumentFileTe
}
private void shuffleAndApplyOperations(List<DocumentOperation> documentOperations) throws IOException {
try (DocumentFile documentFile = new DocumentFile(temporaryFile)) {
try (DocumentFile documentFile = new DocumentFile(temporaryFile, StandardCharsets.UTF_8)) {
applier = new DocumentOperationsApplierForNonOverlappingRegions(documentFile);
Collections.shuffle(documentOperations);