Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into fixlookupfulltext
Browse files Browse the repository at this point in the history
* upstream/master:
  fix bug where main file directory is not set correctly (#5215)
  Use UTF-8 as default encoding for files (#5213)
  fix: missing separator for unchanged diff words
  fix: use UTF-8 as default encoding for files ~ resolves #5133
  Minor improvements to TexParser
  Fix lookup entry types in enums (#5209)
  • Loading branch information
Siedlerchr committed Aug 24, 2019
2 parents 33fb6c5 + dd0f304 commit 4ef1460
Show file tree
Hide file tree
Showing 17 changed files with 108 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public void init(BibEntry entry) {

currentEntry = entry;
Optional<String> citeKey = entry.getCiteKeyOptional();

if (citeKey.isPresent()) {
startSearch(citeKey.get());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private DiffHighlighting() {

public static List<Text> generateDiffHighlighting(String baseString, String modifiedString, String separator) {
List<String> stringList = Arrays.asList(baseString.split(separator));
List<Text> result = stringList.stream().map(DiffHighlighting::forUnchanged).collect(Collectors.toList());
List<Text> result = stringList.stream().map(text -> forUnchanged(text + separator)).collect(Collectors.toList());
List<AbstractDelta<String>> deltaList;
try {
deltaList = DiffUtils.diff(stringList, Arrays.asList(modifiedString.split(separator))).getDeltas();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -133,6 +135,7 @@ ValidationStatus mainFileDirValidationStatus() {
return mainFileDirValidator.getValidationStatus();
}

@Override
public boolean validateSettings() {
ValidationStatus status = mainFileDirValidationStatus();
if (!status.isValid()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.slf4j.LoggerFactory;

public class JournalAbbreviationLoader {

private static final Logger LOGGER = LoggerFactory.getLogger(JournalAbbreviationLoader.class);

// journal initialization
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/org/jabref/logic/texparser/DefaultTexParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)?",
Expand Down Expand Up @@ -76,7 +77,9 @@ public TexParserResult parse(List<Path> 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) == '%') {
Expand All @@ -86,15 +89,19 @@ public TexParserResult parse(List<Path> 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);
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/jabref/model/entry/types/EntryType.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public interface EntryType {
String getName();

String getDisplayName();

}
21 changes: 15 additions & 6 deletions src/main/java/org/jabref/model/entry/types/EntryTypeFactory.java
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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());
}
}

Expand All @@ -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<EntryType> types = new ArrayList<>(Arrays.<EntryType> asList(StandardEntryType.values()));
types.addAll(Arrays.<EntryType> asList(IEEETranEntryType.values()));

return types.stream().filter(type -> type.getName().equals(typeName.toLowerCase(Locale.ENGLISH))).findFirst().orElse(new UnknownEntryType(typeName));
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -44,12 +42,6 @@ public enum StandardEntryType implements EntryType {
this.displayName = displayName;
}

public static Optional<StandardEntryType> fromName(String name) {
return Arrays.stream(StandardEntryType.values())
.filter(field -> field.getName().equalsIgnoreCase(name))
.findAny();
}

@Override
public String getName() {
return displayName.toLowerCase(Locale.ENGLISH);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/jabref/model/texparser/TexParserResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class TexParserResult {

private final List<Path> fileList;
private final List<Path> nestedFiles;

// BibTeXKey --> set of citations
private final Multimap<String, Citation> citations;

public TexParserResult() {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/jabref/preferences/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,7 @@ private static void insertDefaultCleanupPreset(Map<String, Object> storage) {
storage.put(CLEANUP_FORMATTERS, convertListToString(Cleanups.DEFAULT_SAVE_ACTIONS.getAsStringList(OS.NEWLINE)));
}

@Override
public EntryEditorPreferences getEntryEditorPreferences() {
return new EntryEditorPreferences(getEntryEditorTabList(),
getLatexFieldFormatterPreferences(),
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -2034,6 +2035,7 @@ public Map<String, SortType> getMainTableColumnSortTypes() {
return map;
}

@Override
public FileDragDropPreferenceType getEntryEditorFileLinkPreference() {
return FileDragDropPreferenceType.valueOf(get(ENTRY_EDITOR_DRAG_DROP_PREFERENCE_TYPE));
}
Expand Down
47 changes: 43 additions & 4 deletions src/test/java/org/jabref/logic/texparser/DefaultTexParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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);
}
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/org/jabref/model/entry/EntryTypeFactoryTest.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Man \cite{Lässt} auf verweisen.
Danach wir anschließend mittels \cite{anykey}.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Man einfache \cite{Lste}.
Danach wir anschließend mittels \cite{anykey}.
2 changes: 1 addition & 1 deletion src/test/resources/org/jabref/logic/texparser/utf-8.tex
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Danach wir \cite{anschließend} mittels.
Danach wir anschließend mittels \cite{anykey}.

0 comments on commit 4ef1460

Please sign in to comment.