Skip to content

Commit

Permalink
Use Writer instead of Path
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor committed Jan 31, 2024
1 parent 2430500 commit a3a71fe
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 140 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package org.jabref.logic.quality.consistency;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.io.Writer;
import java.util.List;
import java.util.Set;

Expand All @@ -20,74 +16,35 @@
/**
* Outputs the findings as CSV.
* <p>
* Following symbols are used:
*
* <ul>
* <li><code>x</code> - required field is present</li>
* <li><code>o</code> - optional field is present</li>
* <li><code>?</code> - unknown field is present</li>
* </ul>
* <p>
* Note that this classification is based on JabRef's definition and might not match the publisher's definition.
*
* @implNote We could have implemented a <code>PaperConsistencyCheckResultFormatter</code>, but that would have been too much effort.
* The symbols from {@link PaperConsistencyCheckResultWriter} are used.
*/
public class PaperConsistencyCheckResultCsvWriter extends PaperConsistencyCheckResultWriter {

private static final String REQUIRED_FIELD_AT_ENTRY_TYPE_CELL_ENTRY = "x";
private static final String OPTIONAL_FIELD_AT_ENTRY_TYPE_CELL_ENTRY = "o";
private static final String UNKOWN_FIELD_AT_ENTRY_TYPE_CELL_ENTRY = "?";

private OutputStreamWriter writer;
private CSVPrinter csvPrinter;
private int columnCount;

public PaperConsistencyCheckResultCsvWriter(PaperConsistencyCheck.Result result, Path path) {
super(result, path);
public PaperConsistencyCheckResultCsvWriter(PaperConsistencyCheck.Result result, Writer writer) {
super(result, writer);
}

public PaperConsistencyCheckResultCsvWriter(PaperConsistencyCheck.Result result, Path path, BibEntryTypesManager entryTypesManager, BibDatabaseMode bibDatabaseMode) {
super(result, path, entryTypesManager, bibDatabaseMode);
public PaperConsistencyCheckResultCsvWriter(PaperConsistencyCheck.Result result, Writer writer, BibEntryTypesManager entryTypesManager, BibDatabaseMode bibDatabaseMode) {
super(result, writer, entryTypesManager, bibDatabaseMode);
}

@Override
public void writeFindings() throws IOException {
writer = new OutputStreamWriter(Files.newOutputStream(path), StandardCharsets.UTF_8);
csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT);

List<String> theHeading = new ArrayList(columnCount);
theHeading.add("entry type");
theHeading.add("citation key");
allFields.forEach(field -> {
theHeading.add(field.getDisplayName());
});
csvPrinter.printRecord(theHeading);
columnCount = allFields.size() + 2;

csvPrinter.printRecord(allFieldNames);
super.writeFindings();
}

@Override
protected void writeBibEntry(BibEntry bibEntry, String entryType, Set<Field> requiredFields, Set<Field> optionalFields) throws IOException {
List<String> theRecord = new ArrayList(columnCount);
theRecord.add(entryType);
theRecord.add(bibEntry.getCitationKey().orElse(""));
allFields.forEach(field -> {
theRecord.add(bibEntry.getField(field).map(value -> {
if (requiredFields.contains(field)) {
return REQUIRED_FIELD_AT_ENTRY_TYPE_CELL_ENTRY;
} else if (optionalFields.contains(field)) {
return OPTIONAL_FIELD_AT_ENTRY_TYPE_CELL_ENTRY;
} else {
return UNKOWN_FIELD_AT_ENTRY_TYPE_CELL_ENTRY;
}
}).orElse("-"));
});
List<String> theRecord = getFindingsAsList(bibEntry, entryType, requiredFields, optionalFields);
csvPrinter.printRecord(theRecord);
}

@Override
public void close() throws IOException {
csvPrinter.close();
writer.close();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package org.jabref.logic.quality.consistency;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.BibEntryTypesManager;
import org.jabref.model.entry.field.Field;

/**
* Outputs the findings as plain text.
* <p>
* The symbols from {@link PaperConsistencyCheckResultWriter} are used.
*/
public class PaperConsistencyCheckResultTxtWriter extends PaperConsistencyCheckResultWriter {

private List<Integer> columnWidths;

public PaperConsistencyCheckResultTxtWriter(PaperConsistencyCheck.Result result, Writer writer) {
super(result, writer);
initializeColumnWidths();
}

public PaperConsistencyCheckResultTxtWriter(PaperConsistencyCheck.Result result, Writer writer, BibEntryTypesManager entryTypesManager, BibDatabaseMode bibDatabaseMode) {
super(result, writer, entryTypesManager, bibDatabaseMode);
initializeColumnWidths();
}

public void writeFindings() throws IOException {
writer.write("Paper Consistency Check Result\n\n");
writer.write(allFieldNames.stream().collect(Collectors.joining(" | ", "", "\n")));
writer.write(columnWidths.stream().map(width -> "-".repeat(width)).collect(Collectors.joining(" | ", "| ", " | \n")));
super.writeFindings();
}

private void initializeColumnWidths() {
columnWidths = new ArrayList<>(allFieldNames.size() + 2);

Integer max = getColumnWidthOfEntryTypes();
columnWidths.add(max);

max = getColumnWidthOfCitationKeys(max);
columnWidths.add(max);

columnWidths.addAll(allFieldNames.stream().map(String::length).toList());
}

private Integer getColumnWidthOfEntryTypes() {
Integer max = result.entryTypeToResultMap().keySet()
.stream()
.map(entryType -> entryType.getDisplayName().length())
.max(Integer::compareTo)
.get();
max = Math.max(max, "entry type".length());
return max;
}

private Integer getColumnWidthOfCitationKeys(Integer max) {
result.entryTypeToResultMap().values()
.stream()
.flatMap(entryTypeResult -> entryTypeResult.sortedEntries().stream())
.map(entry -> entry.getCitationKey().orElse("").length())
.max(Integer::compareTo)
.get();
return Math.max(max, "citation key".length());
}

@Override
protected void writeBibEntry(BibEntry bibEntry, String entryType, Set<Field> requiredFields, Set<Field> optionalFields) throws IOException {
List<String> theRecord = getFindingsAsList(bibEntry, entryType, requiredFields, optionalFields);
String output = theRecord.stream()
.map(string -> String.format("%-" + columnWidths.get(theRecord.indexOf(string)) + "s", string))
.collect(Collectors.joining(" | ", "| ", " |\n"));
writer.write(output);
}

@Override
public void close() throws IOException {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import java.io.Closeable;
import java.io.IOException;
import java.nio.file.Path;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
Expand All @@ -21,28 +22,52 @@

import org.jooq.lambda.Unchecked;

/**
* Outputs the findings as CSV.
* <p>
* Following symbols are used (as default):
*
* <ul>
* <li><code>x</code> - required field is present</li>
* <li><code>o</code> - optional field is present</li>
* <li><code>?</code> - unknown field is present</li>
* </ul>
* <p>
* Note that this classification is based on JabRef's definition and might not match the publisher's definition.
*
* @implNote We could have implemented a <code>PaperConsistencyCheckResultFormatter</code>, but that would have been too much effort.
*/
public abstract class PaperConsistencyCheckResultWriter implements Closeable {

protected static final String REQUIRED_FIELD_AT_ENTRY_TYPE_CELL_ENTRY = "x";
protected static final String OPTIONAL_FIELD_AT_ENTRY_TYPE_CELL_ENTRY = "o";
protected static final String UNKNOWN_FIELD_AT_ENTRY_TYPE_CELL_ENTRY = "?";

protected final PaperConsistencyCheck.Result result;
protected final Path path;
protected final Writer writer;
protected final BibEntryTypesManager entryTypesManager;
protected final BibDatabaseMode bibDatabaseMode;
protected List<Field> allFields;
protected final List<String> allFieldNames;
protected final int columnCount;

private final List<Field> allFields;

public PaperConsistencyCheckResultWriter(PaperConsistencyCheck.Result result, Path path) {
this(result, path, new BibEntryTypesManager(), BibDatabaseMode.BIBTEX);
public PaperConsistencyCheckResultWriter(PaperConsistencyCheck.Result result, Writer writer) {
this(result, writer, new BibEntryTypesManager(), BibDatabaseMode.BIBTEX);
}

public PaperConsistencyCheckResultWriter(PaperConsistencyCheck.Result result, Path path, BibEntryTypesManager entryTypesManager, BibDatabaseMode bibDatabaseMode) {
public PaperConsistencyCheckResultWriter(PaperConsistencyCheck.Result result, Writer writer, BibEntryTypesManager entryTypesManager, BibDatabaseMode bibDatabaseMode) {
this.result = result;
this.path = path;
this.writer = writer;
this.entryTypesManager = entryTypesManager;
this.bibDatabaseMode = bibDatabaseMode;
this.allFields = result.entryTypeToResultMap().values().stream()
.flatMap(entryTypeResult -> entryTypeResult.fields().stream())
.sorted(Comparator.comparing(Field::getName))
.distinct()
.toList();
this.allFieldNames = getAllReportedFieldNames();
this.columnCount = allFields.size();
}

public void writeFindings() throws IOException {
Expand All @@ -53,6 +78,34 @@ public void writeFindings() throws IOException {
}));
}

private List<String> getAllReportedFieldNames() {
List<String> result = new ArrayList(allFields.size() + 2);
result.add("entry type");
result.add("citation key");
allFields.forEach(field -> {
result.add(field.getDisplayName());
});
return result;
}

protected List<String> getFindingsAsList(BibEntry bibEntry, String entryType, Set<Field> requiredFields, Set<Field> optionalFields) {
List<String> result = new ArrayList(columnCount);
result.add(entryType);
result.add(bibEntry.getCitationKey().orElse(""));
allFields.forEach(field -> {
result.add(bibEntry.getField(field).map(value -> {
if (requiredFields.contains(field)) {
return REQUIRED_FIELD_AT_ENTRY_TYPE_CELL_ENTRY;
} else if (optionalFields.contains(field)) {
return OPTIONAL_FIELD_AT_ENTRY_TYPE_CELL_ENTRY;
} else {
return UNKNOWN_FIELD_AT_ENTRY_TYPE_CELL_ENTRY;
}
}).orElse("-"));
});
return result;
}

protected void writeMapEntry(Map.Entry<EntryType, PaperConsistencyCheck.EntryTypeResult> mapEntry) {
String entryType = mapEntry.getKey().getDisplayName();

Expand Down
Loading

0 comments on commit a3a71fe

Please sign in to comment.