diff --git a/src/main/java/org/jabref/gui/entryeditor/LatexCitationsTabViewModel.java b/src/main/java/org/jabref/gui/entryeditor/LatexCitationsTabViewModel.java index b844475b1db..9ef038f50b5 100644 --- a/src/main/java/org/jabref/gui/entryeditor/LatexCitationsTabViewModel.java +++ b/src/main/java/org/jabref/gui/entryeditor/LatexCitationsTabViewModel.java @@ -77,6 +77,7 @@ public void init(BibEntry entry) { currentEntry = entry; Optional citeKey = entry.getCiteKeyOptional(); + if (citeKey.isPresent()) { startSearch(citeKey.get()); } else { diff --git a/src/main/java/org/jabref/gui/mergeentries/DiffHighlighting.java b/src/main/java/org/jabref/gui/mergeentries/DiffHighlighting.java index acffbc7acc0..4b93edbb8c8 100644 --- a/src/main/java/org/jabref/gui/mergeentries/DiffHighlighting.java +++ b/src/main/java/org/jabref/gui/mergeentries/DiffHighlighting.java @@ -22,7 +22,7 @@ private DiffHighlighting() { public static List generateDiffHighlighting(String baseString, String modifiedString, String separator) { List stringList = Arrays.asList(baseString.split(separator)); - List result = stringList.stream().map(DiffHighlighting::forUnchanged).collect(Collectors.toList()); + List result = stringList.stream().map(text -> forUnchanged(text + separator)).collect(Collectors.toList()); List> deltaList; try { deltaList = DiffUtils.diff(stringList, Arrays.asList(modifiedString.split(separator))).getDeltas(); diff --git a/src/main/java/org/jabref/gui/preferences/FileTabViewModel.java b/src/main/java/org/jabref/gui/preferences/FileTabViewModel.java index 990f65e8526..28047e8e9a3 100644 --- a/src/main/java/org/jabref/gui/preferences/FileTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/FileTabViewModel.java @@ -77,6 +77,7 @@ public FileTabViewModel(DialogService dialogService, JabRefPreferences preferenc ); } + @Override public void setValues() { openLastStartupProperty.setValue(preferences.getBoolean(JabRefPreferences.OPEN_LAST_EDITED)); backupOldFileProperty.setValue(preferences.getBoolean(JabRefPreferences.BACKUP)); @@ -88,7 +89,7 @@ public void setValues() { selectedNewLineSeparatorProperty.setValue(preferences.getNewLineSeparator()); alwaysReformatBibProperty.setValue(preferences.getBoolean(JabRefPreferences.REFORMAT_FILE_ON_SAVE_AND_EXPORT)); - mainFileDirProperty.setValue(preferences.getAsOptional(StandardField.FILE + FilePreferences.DIR_SUFFIX).orElse("")); + mainFileDirProperty.setValue(preferences.getAsOptional(StandardField.FILE.getName() + FilePreferences.DIR_SUFFIX).orElse("")); useBibLocationAsPrimaryProperty.setValue(preferences.getBoolean(JabRefPreferences.BIB_LOC_AS_PRIMARY_DIR)); if (preferences.getBoolean(JabRefPreferences.AUTOLINK_USE_REG_EXP_SEARCH_KEY)) { // Flipped around autolinkUseRegexProperty.setValue(true); @@ -104,6 +105,7 @@ public void setValues() { autosaveLocalLibraries.setValue(preferences.getBoolean(JabRefPreferences.LOCAL_AUTO_SAVE)); } + @Override public void storeSettings() { preferences.putBoolean(JabRefPreferences.OPEN_LAST_EDITED, openLastStartupProperty.getValue()); preferences.putBoolean(JabRefPreferences.BACKUP, backupOldFileProperty.getValue()); @@ -119,7 +121,7 @@ public void storeSettings() { preferences.setNewLineSeparator(selectedNewLineSeparatorProperty.getValue()); preferences.putBoolean(JabRefPreferences.REFORMAT_FILE_ON_SAVE_AND_EXPORT, alwaysReformatBibProperty.getValue()); - preferences.put(StandardField.FILE + FilePreferences.DIR_SUFFIX, mainFileDirProperty.getValue()); + preferences.put(StandardField.FILE.getName() + FilePreferences.DIR_SUFFIX, mainFileDirProperty.getValue()); preferences.putBoolean(JabRefPreferences.BIB_LOC_AS_PRIMARY_DIR, useBibLocationAsPrimaryProperty.getValue()); preferences.putBoolean(JabRefPreferences.AUTOLINK_USE_REG_EXP_SEARCH_KEY, autolinkUseRegexProperty.getValue()); preferences.putBoolean(JabRefPreferences.AUTOLINK_EXACT_KEY_ONLY, autolinkFileExactBibtexProperty.getValue()); @@ -133,6 +135,7 @@ ValidationStatus mainFileDirValidationStatus() { return mainFileDirValidator.getValidationStatus(); } + @Override public boolean validateSettings() { ValidationStatus status = mainFileDirValidationStatus(); if (!status.isValid()) { diff --git a/src/main/java/org/jabref/logic/journals/AbbreviationParser.java b/src/main/java/org/jabref/logic/journals/AbbreviationParser.java index 77d0483027f..282e590d3c9 100644 --- a/src/main/java/org/jabref/logic/journals/AbbreviationParser.java +++ b/src/main/java/org/jabref/logic/journals/AbbreviationParser.java @@ -4,7 +4,6 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; @@ -39,7 +38,8 @@ public void readJournalListFromResource(String resourceFileName) { } public void readJournalListFromFile(File file) throws FileNotFoundException { - try (FileReader reader = new FileReader(Objects.requireNonNull(file))) { + try (FileInputStream stream = new FileInputStream(Objects.requireNonNull(file)); + InputStreamReader reader = new InputStreamReader(stream, Objects.requireNonNull(StandardCharsets.UTF_8))) { readJournalList(reader); } catch (FileNotFoundException e) { throw e; diff --git a/src/main/java/org/jabref/logic/journals/JournalAbbreviationLoader.java b/src/main/java/org/jabref/logic/journals/JournalAbbreviationLoader.java index f1cde1f65b2..fce5ccc2b58 100644 --- a/src/main/java/org/jabref/logic/journals/JournalAbbreviationLoader.java +++ b/src/main/java/org/jabref/logic/journals/JournalAbbreviationLoader.java @@ -11,7 +11,6 @@ import org.slf4j.LoggerFactory; public class JournalAbbreviationLoader { - private static final Logger LOGGER = LoggerFactory.getLogger(JournalAbbreviationLoader.class); // journal initialization diff --git a/src/main/java/org/jabref/logic/texparser/DefaultTexParser.java b/src/main/java/org/jabref/logic/texparser/DefaultTexParser.java index 04d73210410..0d6414892ef 100644 --- a/src/main/java/org/jabref/logic/texparser/DefaultTexParser.java +++ b/src/main/java/org/jabref/logic/texparser/DefaultTexParser.java @@ -2,6 +2,7 @@ import java.io.IOException; import java.io.LineNumberReader; +import java.io.Reader; import java.io.UncheckedIOException; import java.nio.channels.ClosedChannelException; import java.nio.file.Files; @@ -27,8 +28,8 @@ public class DefaultTexParser implements TexParser { private static final String TEX_EXT = ".tex"; /** - * It is allowed to add new cite commands for pattern matching. - * Some valid examples: "citep", "[cC]ite", and "[cC]ite(author|title|year|t|p)?". + * It is allowed to add new cite commands for pattern matching. Some valid examples: "citep", "[cC]ite", and + * "[cC]ite(author|title|year|t|p)?". */ private static final String[] CITE_COMMANDS = { "[cC]ite(alt|alp|author|authorfull|date|num|p|t|text|title|url|year|yearpar)?", @@ -76,7 +77,9 @@ public TexParserResult parse(List texFiles) { continue; } - try (LineNumberReader lineNumberReader = new LineNumberReader(new CharsetDetector().setText(Files.readAllBytes(file)).detect().getReader())) { + try ( + Reader reader = new CharsetDetector().setText(Files.readAllBytes(file)).detect().getReader(); + LineNumberReader lineNumberReader = new LineNumberReader(reader)) { for (String line = lineNumberReader.readLine(); line != null; line = lineNumberReader.readLine()) { // Skip comments and blank lines. if (line.trim().isEmpty() || line.trim().charAt(0) == '%') { @@ -86,15 +89,19 @@ public TexParserResult parse(List texFiles) { matchNestedFile(file, texFiles, referencedFiles, line); } } catch (ClosedChannelException e) { - LOGGER.error("Parsing has been interrupted"); - return null; + // User changed the underlying LaTeX file + // We ignore this error and just continue with parsing + LOGGER.info("Parsing has been interrupted"); } catch (IOException | UncheckedIOException e) { - LOGGER.error("Error while parsing file {}", file, e); + // Some weired error during reading + // We ignore this error and just continue with parsing + LOGGER.info("Error while parsing file {}", file, e); } } // Parse all files referenced by TEX files, recursively. if (!referencedFiles.isEmpty()) { + // modifies class variable texParserResult parse(referencedFiles); } diff --git a/src/main/java/org/jabref/model/entry/types/EntryType.java b/src/main/java/org/jabref/model/entry/types/EntryType.java index 90d0264c00d..8124e0690f2 100644 --- a/src/main/java/org/jabref/model/entry/types/EntryType.java +++ b/src/main/java/org/jabref/model/entry/types/EntryType.java @@ -10,4 +10,5 @@ public interface EntryType { String getName(); String getDisplayName(); + } diff --git a/src/main/java/org/jabref/model/entry/types/EntryTypeFactory.java b/src/main/java/org/jabref/model/entry/types/EntryTypeFactory.java index 64e4f6cfc64..6c9745bde10 100644 --- a/src/main/java/org/jabref/model/entry/types/EntryTypeFactory.java +++ b/src/main/java/org/jabref/model/entry/types/EntryTypeFactory.java @@ -1,7 +1,12 @@ package org.jabref.model.entry.types; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Locale; +import java.util.Objects; + import org.jabref.model.entry.BibEntryType; -import org.jabref.model.util.OptionalUtil; public class EntryTypeFactory { @@ -22,10 +27,10 @@ public static boolean isEqualNameAndFieldBased(BibEntryType type1, BibEntryType } else if ((type1 == null) || (type2 == null)) { return false; } else { - return type1.getType().equals(type2.getType()) - && type1.getRequiredFields().equals(type2.getRequiredFields()) - && type1.getOptionalFields().equals(type2.getOptionalFields()) - && type1.getSecondaryOptionalFields().equals(type2.getSecondaryOptionalFields()); + return Objects.equals(type1.getType(), type2.getType()) + && Objects.equals(type1.getRequiredFields(), type2.getRequiredFields()) + && Objects.equals(type1.getOptionalFields(), type2.getOptionalFields()) + && Objects.equals(type1.getSecondaryOptionalFields(), type2.getSecondaryOptionalFields()); } } @@ -42,6 +47,10 @@ private static boolean isBiblatex(EntryType type) { } public static EntryType parse(String typeName) { - return OptionalUtil.orElse(StandardEntryType.fromName(typeName), new UnknownEntryType(typeName)); + + List types = new ArrayList<>(Arrays. asList(StandardEntryType.values())); + types.addAll(Arrays. asList(IEEETranEntryType.values())); + + return types.stream().filter(type -> type.getName().equals(typeName.toLowerCase(Locale.ENGLISH))).findFirst().orElse(new UnknownEntryType(typeName)); } } diff --git a/src/main/java/org/jabref/model/entry/types/StandardEntryType.java b/src/main/java/org/jabref/model/entry/types/StandardEntryType.java index 51bfd341e23..de5ef6dc8fc 100644 --- a/src/main/java/org/jabref/model/entry/types/StandardEntryType.java +++ b/src/main/java/org/jabref/model/entry/types/StandardEntryType.java @@ -1,8 +1,6 @@ package org.jabref.model.entry.types; -import java.util.Arrays; import java.util.Locale; -import java.util.Optional; public enum StandardEntryType implements EntryType { // BibTeX @@ -44,12 +42,6 @@ public enum StandardEntryType implements EntryType { this.displayName = displayName; } - public static Optional fromName(String name) { - return Arrays.stream(StandardEntryType.values()) - .filter(field -> field.getName().equalsIgnoreCase(name)) - .findAny(); - } - @Override public String getName() { return displayName.toLowerCase(Locale.ENGLISH); diff --git a/src/main/java/org/jabref/model/entry/types/UnknownEntryType.java b/src/main/java/org/jabref/model/entry/types/UnknownEntryType.java index 40d0d9d1c1f..6b9d8682d1c 100644 --- a/src/main/java/org/jabref/model/entry/types/UnknownEntryType.java +++ b/src/main/java/org/jabref/model/entry/types/UnknownEntryType.java @@ -34,7 +34,7 @@ public boolean equals(Object o) { if (this == o) { return true; } - if (o == null || getClass() != o.getClass()) { + if ((o == null) || (getClass() != o.getClass())) { return false; } UnknownEntryType that = (UnknownEntryType) o; diff --git a/src/main/java/org/jabref/model/texparser/TexParserResult.java b/src/main/java/org/jabref/model/texparser/TexParserResult.java index cfbeea25b51..9b563d7e485 100644 --- a/src/main/java/org/jabref/model/texparser/TexParserResult.java +++ b/src/main/java/org/jabref/model/texparser/TexParserResult.java @@ -17,6 +17,8 @@ public class TexParserResult { private final List fileList; private final List nestedFiles; + + // BibTeXKey --> set of citations private final Multimap citations; public TexParserResult() { diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index 8b14874da52..5d732e43703 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -896,6 +896,7 @@ private static void insertDefaultCleanupPreset(Map storage) { storage.put(CLEANUP_FORMATTERS, convertListToString(Cleanups.DEFAULT_SAVE_ACTIONS.getAsStringList(OS.NEWLINE))); } + @Override public EntryEditorPreferences getEntryEditorPreferences() { return new EntryEditorPreferences(getEntryEditorTabList(), getLatexFieldFormatterPreferences(), @@ -1589,7 +1590,7 @@ private NameFormatterPreferences getNameFormatterPreferences() { public FileLinkPreferences getFileLinkPreferences() { return new FileLinkPreferences( - Collections.singletonList(get(StandardField.FILE + FilePreferences.DIR_SUFFIX)), + Collections.singletonList(get(StandardField.FILE.getName() + FilePreferences.DIR_SUFFIX)), fileDirForDatabase); } @@ -2034,6 +2035,7 @@ public Map getMainTableColumnSortTypes() { return map; } + @Override public FileDragDropPreferenceType getEntryEditorFileLinkPreference() { return FileDragDropPreferenceType.valueOf(get(ENTRY_EDITOR_DRAG_DROP_PREFERENCE_TYPE)); } diff --git a/src/test/java/org/jabref/logic/texparser/DefaultTexParserTest.java b/src/test/java/org/jabref/logic/texparser/DefaultTexParserTest.java index 00e99b191ea..a0638550877 100644 --- a/src/test/java/org/jabref/logic/texparser/DefaultTexParserTest.java +++ b/src/test/java/org/jabref/logic/texparser/DefaultTexParserTest.java @@ -70,7 +70,46 @@ public void testTwoCitationsSameLine() { } @Test - public void testFileEncoding() throws URISyntaxException { + public void testFileEncodingUtf8() throws URISyntaxException { + Path texFile = Paths.get(DefaultTexParserTest.class.getResource("utf-8.tex").toURI()); + + TexParserResult parserResult = new DefaultTexParser().parse(texFile); + TexParserResult expectedParserResult = new TexParserResult(); + + expectedParserResult.getFileList().add(texFile); + expectedParserResult.addKey("anykey", texFile, 1, 32, 45, "Danach wir anschließend mittels \\cite{anykey}."); + + assertEquals(expectedParserResult, parserResult); + } + + @Test + public void testFileEncodingIso88591() throws URISyntaxException { + Path texFile = Paths.get(DefaultTexParserTest.class.getResource("iso-8859-1.tex").toURI()); + + TexParserResult parserResult = new DefaultTexParser().parse(texFile); + TexParserResult expectedParserResult = new TexParserResult(); + + expectedParserResult.getFileList().add(texFile); + expectedParserResult.addKey("anykey", texFile, 1, 32, 45, "Danach wir anschließend mittels \\cite{anykey}."); + + assertEquals(expectedParserResult, parserResult); + } + + @Test + public void testFileEncodingIso885915() throws URISyntaxException { + Path texFile = Paths.get(DefaultTexParserTest.class.getResource("iso-8859-15.tex").toURI()); + + TexParserResult parserResult = new DefaultTexParser().parse(texFile); + TexParserResult expectedParserResult = new TexParserResult(); + + expectedParserResult.getFileList().add(texFile); + expectedParserResult.addKey("anykey", texFile, 1, 32, 45, "Danach wir anschließend mittels \\cite{anykey}."); + + assertEquals(expectedParserResult, parserResult); + } + + @Test + public void testFileEncodingForThreeFiles() throws URISyntaxException { Path texFile = Paths.get(DefaultTexParserTest.class.getResource("utf-8.tex").toURI()); Path texFile2 = Paths.get(DefaultTexParserTest.class.getResource("iso-8859-1.tex").toURI()); Path texFile3 = Paths.get(DefaultTexParserTest.class.getResource("iso-8859-15.tex").toURI()); @@ -79,9 +118,9 @@ public void testFileEncoding() throws URISyntaxException { TexParserResult expectedParserResult = new TexParserResult(); expectedParserResult.getFileList().addAll(Arrays.asList(texFile, texFile2, texFile3)); - expectedParserResult.addKey("anschließend", texFile, 1, 11, 30, "Danach wir \\cite{anschließend} mittels."); - expectedParserResult.addKey("Lässt", texFile2, 1, 4, 16, "Man \\cite{Lässt} auf verweisen."); - expectedParserResult.addKey("Läste", texFile3, 1, 13, 25, "Man einfache \\cite{Läste}."); + expectedParserResult.addKey("anykey", texFile, 1, 32, 45, "Danach wir anschließend mittels \\cite{anykey}."); + expectedParserResult.addKey("anykey", texFile2, 1, 32, 45, "Danach wir anschließend mittels \\cite{anykey}."); + expectedParserResult.addKey("anykey", texFile3, 1, 32, 45, "Danach wir anschließend mittels \\cite{anykey}."); assertEquals(expectedParserResult, parserResult); } diff --git a/src/test/java/org/jabref/model/entry/EntryTypeFactoryTest.java b/src/test/java/org/jabref/model/entry/EntryTypeFactoryTest.java new file mode 100644 index 00000000000..9cfef3dbff6 --- /dev/null +++ b/src/test/java/org/jabref/model/entry/EntryTypeFactoryTest.java @@ -0,0 +1,18 @@ +package org.jabref.model.entry; + +import org.jabref.model.entry.types.EntryType; +import org.jabref.model.entry.types.EntryTypeFactory; +import org.jabref.model.entry.types.IEEETranEntryType; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class EntryTypeFactoryTest { + + @Test + public void testParseEntryTypePatent() { + EntryType patent = IEEETranEntryType.Patent; + assertEquals(patent, EntryTypeFactory.parse("patent")); + } +} diff --git a/src/test/resources/org/jabref/logic/texparser/iso-8859-1.tex b/src/test/resources/org/jabref/logic/texparser/iso-8859-1.tex index 96705b39a73..7c4509be56c 100644 --- a/src/test/resources/org/jabref/logic/texparser/iso-8859-1.tex +++ b/src/test/resources/org/jabref/logic/texparser/iso-8859-1.tex @@ -1 +1 @@ -Man \cite{Lässt} auf verweisen. +Danach wir anschließend mittels \cite{anykey}. diff --git a/src/test/resources/org/jabref/logic/texparser/iso-8859-15.tex b/src/test/resources/org/jabref/logic/texparser/iso-8859-15.tex index c25dc50ced8..7c4509be56c 100644 --- a/src/test/resources/org/jabref/logic/texparser/iso-8859-15.tex +++ b/src/test/resources/org/jabref/logic/texparser/iso-8859-15.tex @@ -1 +1 @@ -Man einfache \cite{Läste}. +Danach wir anschließend mittels \cite{anykey}. diff --git a/src/test/resources/org/jabref/logic/texparser/utf-8.tex b/src/test/resources/org/jabref/logic/texparser/utf-8.tex index 0e3f607f98a..7c75324d8cd 100644 --- a/src/test/resources/org/jabref/logic/texparser/utf-8.tex +++ b/src/test/resources/org/jabref/logic/texparser/utf-8.tex @@ -1 +1 @@ -Danach wir \cite{anschließend} mittels. +Danach wir anschließend mittels \cite{anykey}.