Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/main/java/org/jabref/logic/journals/AbbreviationFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@

public final class AbbreviationFormat {

public static final char DELIMITER = ';';
public static final char DELIMITER = ',';
public static final char ESCAPE = '\\';
public static final char QUOTE = '"';

private AbbreviationFormat() {
}

public static CSVFormat getCSVFormat() {
public static CSVFormat getCSVFormatWithDelimiter(char delimiter) {
return CSVFormat.DEFAULT.builder()
.setIgnoreEmptyLines(true)
.setDelimiter(DELIMITER)
.setDelimiter(delimiter)
.setEscape(ESCAPE)
.setQuote(QUOTE)
.setTrim(true)
.build();
}

public static CSVFormat getCSVFormatWithDefaultDilimeter() {
return getCSVFormatWithDelimiter(DELIMITER);
}
}
57 changes: 25 additions & 32 deletions src/main/java/org/jabref/logic/journals/AbbreviationParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,37 @@

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;

import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Reads abbreviation files (CSV format) into a list of Abbreviations.
*/
public class AbbreviationParser {

private static final Logger LOGGER = LoggerFactory.getLogger(AbbreviationParser.class);
private static final Character[] DELIMITERS = {';', ','};
private static final char NO_DELIMITER = '\0'; // empty char

// Ensures ordering while preventing duplicates
private final LinkedHashSet<Abbreviation> abbreviations = new LinkedHashSet<>();

void readJournalListFromResource(String resourceFileName) {
try (InputStream stream = JournalAbbreviationRepository.class.getResourceAsStream(resourceFileName);
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) {
readJournalList(reader);
} catch (IOException e) {
LOGGER.error(String.format("Could not read journal list from file %s", resourceFileName), e);
}
}

public void readJournalListFromFile(Path file) throws IOException {
readJournalListFromFile(file, StandardCharsets.UTF_8);
}

public void readJournalListFromFile(Path file, Charset encoding) throws IOException {
try (BufferedReader reader = Files.newBufferedReader(file, encoding)) {
readJournalList(reader);
}
}

/**
/*
* Read the given file, which should contain a list of journal names and their abbreviations. Each line should be
* formatted as: "Full Journal Name;Abbr. Journal Name;[Shortest Unique Abbreviation]"
* formatted as: "Full Journal Name,Abbr. Journal Name,[Shortest Unique Abbreviation]"
* Tries to detect the delimiter, if comma or semicolon is used to ensure backwards compatibility
*
* @param reader a given file into a Reader object
* @param file Path the given file
*/
private void readJournalList(Reader reader) throws IOException {
try (CSVParser csvParser = new CSVParser(reader, AbbreviationFormat.getCSVFormat())) {
void readJournalListFromFile(Path file) throws IOException {
char delimiter = detectDelimiter(file);

try (CSVParser csvParser = new CSVParser(Files.newBufferedReader(file, StandardCharsets.UTF_8), AbbreviationFormat.getCSVFormatWithDelimiter(delimiter))) {
for (CSVRecord csvRecord : csvParser) {
String name = csvRecord.size() > 0 ? csvRecord.get(0) : "";
String abbreviation = csvRecord.size() > 1 ? csvRecord.get(1) : "";
Expand All @@ -70,6 +49,20 @@ private void readJournalList(Reader reader) throws IOException {
}
}

private char detectDelimiter(Path file) throws IOException {
try (BufferedReader reader = Files.newBufferedReader(file, StandardCharsets.UTF_8)) {
String line = reader.readLine();

if (line == null) {
return NO_DELIMITER;
}
return Arrays.stream(DELIMITERS)
.filter(s -> line.contains(s.toString()))
.findFirst()
.orElse(NO_DELIMITER);
}
}

public Collection<Abbreviation> getAbbreviations() {
return abbreviations;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private AbbreviationWriter() {
*/
public static void writeOrCreate(Path path, List<Abbreviation> abbreviations) throws IOException {
try (OutputStreamWriter writer = new OutputStreamWriter(Files.newOutputStream(path), StandardCharsets.UTF_8);
CSVPrinter csvPrinter = new CSVPrinter(writer, AbbreviationFormat.getCSVFormat())) {
CSVPrinter csvPrinter = new CSVPrinter(writer, AbbreviationFormat.getCSVFormatWithDefaultDilimeter())) {
for (Abbreviation entry : abbreviations) {
if (entry.isDefaultShortestUniqueAbbreviation()) {
csvPrinter.printRecord(entry.getName(), entry.getAbbreviation());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static JournalAbbreviationRepository loadRepository(JournalAbbreviationPr

// Read external lists
List<String> lists = journalAbbreviationPreferences.getExternalJournalLists();
if (!(lists.isEmpty())) {
if (lists != null && !(lists.isEmpty())) {
// reversing ensures that the latest lists overwrites the former one
Collections.reverse(lists);
for (String filename : lists) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,23 @@ public class JournalAbbreviationRepository {
private final TreeSet<Abbreviation> customAbbreviations = new TreeSet<>();

public JournalAbbreviationRepository(Path journalList) {
MVStore store = new MVStore.Builder().readOnly().fileName(journalList.toAbsolutePath().toString()).open();
MVMap<String, Abbreviation> mvFullToAbbreviationObject = store.openMap("FullToAbbreviation");

mvFullToAbbreviationObject.forEach((name, abbreviation) -> {
String abbrevationString = abbreviation.getAbbreviation();
String shortestUniqueAbbreviation = abbreviation.getShortestUniqueAbbreviation();
Abbreviation newAbbreviation = new Abbreviation(
name,
abbrevationString,
shortestUniqueAbbreviation
);
fullToAbbreviationObject.put(name, newAbbreviation);
abbreviationToAbbreviationObject.put(abbrevationString, newAbbreviation);
dotlessToAbbreviationObject.put(newAbbreviation.getDotlessAbbreviation(), newAbbreviation);
shortestUniqueToAbbreviationObject.put(shortestUniqueAbbreviation, newAbbreviation);
});
MVMap<String, Abbreviation> mvFullToAbbreviationObject;
try (MVStore store = new MVStore.Builder().readOnly().fileName(journalList.toAbsolutePath().toString()).open()) {
mvFullToAbbreviationObject = store.openMap("FullToAbbreviation");
mvFullToAbbreviationObject.forEach((name, abbreviation) -> {
String abbrevationString = abbreviation.getAbbreviation();
String shortestUniqueAbbreviation = abbreviation.getShortestUniqueAbbreviation();
Abbreviation newAbbreviation = new Abbreviation(
name,
abbrevationString,
shortestUniqueAbbreviation
);
fullToAbbreviationObject.put(name, newAbbreviation);
abbreviationToAbbreviationObject.put(abbrevationString, newAbbreviation);
dotlessToAbbreviationObject.put(newAbbreviation.getDotlessAbbreviation(), newAbbreviation);
shortestUniqueToAbbreviationObject.put(shortestUniqueAbbreviation, newAbbreviation);
});
}
}

private static boolean isMatched(String name, Abbreviation abbreviation) {
Expand All @@ -57,10 +58,9 @@ private static boolean isMatchedAbbreviated(String name, Abbreviation abbreviati
if (isExpanded) {
return false;
}
boolean isAbbreviated = name.equalsIgnoreCase(abbreviation.getAbbreviation())
return name.equalsIgnoreCase(abbreviation.getAbbreviation())
|| name.equalsIgnoreCase(abbreviation.getDotlessAbbreviation())
|| name.equalsIgnoreCase(abbreviation.getShortestUniqueAbbreviation());
return isAbbreviated;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ public TestAbbreviation(String name, String abbreviation, String shortestUniqueA
@Override
public String toString() {
if (showShortestUniqueAbbreviation) {
return this.getName() + ";" + this.getAbbreviation() + ";" + this.getShortestUniqueAbbreviation();
return this.getName() + "," + this.getAbbreviation() + "," + this.getShortestUniqueAbbreviation();
}
return this.getName() + ";" + this.getAbbreviation();
return this.getName() + "," + this.getAbbreviation();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.jabref.logic.journals;

import java.io.BufferedWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Set;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class AbbreviationParserTest {

private Path csvFile;
private final AbbreviationParser parser = new AbbreviationParser();

private final Abbreviation abbreviation = new Abbreviation("Long Name", "L.N.", "L.N.");

@BeforeEach
void setup(@TempDir Path tempDir) {
csvFile = tempDir.resolve("test.csv");
}

@Test
void testReadingFileFromCSVWithSemicolon() throws Exception {
// String name, String abbreviation, String shortestUniqueAbbreviation
String testAbbrev = "Long Name;L.N.;L.N.";
try (BufferedWriter writer = Files.newBufferedWriter(csvFile, StandardCharsets.UTF_8)) {
writer.write(testAbbrev);
}
parser.readJournalListFromFile(csvFile);
assertEquals(Set.of(abbreviation), parser.getAbbreviations());
}

@Test
void testReadingFileFromCSVWithComma() throws Exception {
String testAbbrev = "Long Name,L.N.,L.N.";
try (BufferedWriter writer = Files.newBufferedWriter(csvFile, StandardCharsets.UTF_8)) {
writer.write(testAbbrev);
}
parser.readJournalListFromFile(csvFile);
assertEquals(Set.of(abbreviation), parser.getAbbreviations());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void shortestUniqueAbbreviationWrittenIfItDiffers(@TempDir Path tempDir) throws
AbbreviationWriter.writeOrCreate(
csvFile,
List.of(abbreviation));
assertEquals(List.of("Full;Abbr;A"), Files.readAllLines(csvFile));
assertEquals(List.of("Full,Abbr,A"), Files.readAllLines(csvFile));
}

@Test
Expand All @@ -31,6 +31,6 @@ void doNotWriteShortestUniqueAbbreviationWrittenIfItDiffers(@TempDir Path tempDi
AbbreviationWriter.writeOrCreate(
csvFile,
List.of(abbreviation));
assertEquals(List.of("Full;Abbr"), Files.readAllLines(csvFile));
assertEquals(List.of("Full,Abbr"), Files.readAllLines(csvFile));
}
}