Skip to content

Commit aa3657c

Browse files
frasca80Siedlerchr
authored andcommitted
Fix for BibTex source tab parsing issue if field contains {} (#4581)
* Cleanup interfaces (#4553) * improve styling of preferences side menu (#4556) * Added extra stats to be sent with MrDLib recommendations (#4452) * Refactor BibEntry deprecated method (#4554) * Refactor BibEntry deprecated method * Fixed error * More on checkstyle fixing * Fixed checkstyle issues * Added custom entrytype for types not registered in the enumerator. * Added getTypeOrDefault method refactor code to use it and fix NPE problem * Fixing checkstyle rules * More on checkstyle * More on getType getTypeOrDefault replacement * Revert Article EntryType into Electronic * Added break line between different packages * Refactor BibtextEntryTypes.getTypeOrDefault method * Removed unused import * Removed extra new line, checkstyle error fixing * Delete the deprecated BibEntry Constructor (#4560) * Bump xmpbox from 2.0.12 to 2.0.13 (#4561) Bumps xmpbox from 2.0.12 to 2.0.13. Signed-off-by: dependabot[bot] <[email protected]> * Bump checkstyle from 8.15 to 8.16 (#4562) Bumps [checkstyle](https://github.com/checkstyle/checkstyle) from 8.15 to 8.16. - [Release notes](https://github.com/checkstyle/checkstyle/releases) - [Commits](checkstyle/checkstyle@checkstyle-8.15...checkstyle-8.16) Signed-off-by: dependabot[bot] <[email protected]> * Do not extract file ending from Urls (#4547) * Fixes #4544 Do not extract file ending from Urls * Add tests * file type for any resource type * Keep simple file name extraction for files * checkstyle * Converts integrity check dialog to JavaFX (#4559) * Converts integrity check dialog to JavaFX Moreover: - Show entry by reference and not by id. Fixes #2181. - Fixes a few issues that occurred when opening the entry editor by code from the integrity dialog - Reuse gridpane in entry editor (should have a slightly superior performance) - Improve display of progress dialog - Invoke copy files task using central task executor * fix l10n fix aborting of copy files task and showing of integrity check dialog * fix l10n * Add uncaught exception message (#4565) * Add error message for uncaught exceptions Added a new view to the project. It's shown after the uncaught exception is logged. * Add simple text to fallback error view Added a label asking the user to look into the logfiles for more details. * Add error message to language files Added the error message to the german and english language files. * Add ErrorDialogAndWait Removed the FallbackErrorView. Added the showErrorDialogAndWait call instead. * BibTex parser triggered on focus out of the text area instead of on value change event * BibTex parser triggered on focus out of the text area instead of on value change event * Added a post-parsing validation to be sure there's no relevant unconsidered content into epilog. Added DialogService in SourceTab as current NotificationPane seems not working. * Added a post-parsing validation to be sure there's no relevant unconsidered content into epilog. Added DialogService in SourceTab as current NotificationPane seems not working. * Codacy/PR Quality Review change. * Codacy/PR Quality Review change.
1 parent 18aba35 commit aa3657c

File tree

4 files changed

+37
-8
lines changed

4 files changed

+37
-8
lines changed

src/main/java/org/jabref/gui/entryeditor/EntryEditor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ private List<EntryEditorTab> createTabs() {
300300
tabs.add(new RelatedArticlesTab(preferences, dialogService));
301301

302302
// Source tab
303-
sourceTab = new SourceTab(databaseContext, undoManager, preferences.getLatexFieldFormatterPreferences(), preferences.getImportFormatPreferences(), fileMonitor);
303+
sourceTab = new SourceTab(databaseContext, undoManager, preferences.getLatexFieldFormatterPreferences(), preferences.getImportFormatPreferences(), fileMonitor, dialogService);
304304
tabs.add(sourceTab);
305305
return tabs;
306306
}

src/main/java/org/jabref/gui/entryeditor/SourceTab.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import javafx.collections.ListChangeListener;
1414
import javafx.scene.control.Tooltip;
1515

16+
import org.jabref.gui.DialogService;
1617
import org.jabref.gui.icon.IconTheme;
1718
import org.jabref.gui.undo.CountingUndoManager;
1819
import org.jabref.gui.undo.NamedCompound;
@@ -53,8 +54,9 @@ public class SourceTab extends EntryEditorTab {
5354
private final ObservableRuleBasedValidator sourceValidator = new ObservableRuleBasedValidator(sourceIsValid);
5455
private final ImportFormatPreferences importFormatPreferences;
5556
private final FileUpdateMonitor fileMonitor;
57+
private final DialogService dialogService;
5658

57-
public SourceTab(BibDatabaseContext bibDatabaseContext, CountingUndoManager undoManager, LatexFieldFormatterPreferences fieldFormatterPreferences, ImportFormatPreferences importFormatPreferences, FileUpdateMonitor fileMonitor) {
59+
public SourceTab(BibDatabaseContext bibDatabaseContext, CountingUndoManager undoManager, LatexFieldFormatterPreferences fieldFormatterPreferences, ImportFormatPreferences importFormatPreferences, FileUpdateMonitor fileMonitor, DialogService dialogService) {
5860
this.mode = bibDatabaseContext.getMode();
5961
this.setText(Localization.lang("%0 source", mode.getFormattedName()));
6062
this.setTooltip(new Tooltip(Localization.lang("Show/edit %0 source", mode.getFormattedName())));
@@ -63,6 +65,7 @@ public SourceTab(BibDatabaseContext bibDatabaseContext, CountingUndoManager undo
6365
this.fieldFormatterPreferences = fieldFormatterPreferences;
6466
this.importFormatPreferences = importFormatPreferences;
6567
this.fileMonitor = fileMonitor;
68+
this.dialogService = dialogService;
6669

6770
}
6871

@@ -95,22 +98,29 @@ protected void bindToEntry(BibEntry entry) {
9598
if (sourceValidator.getValidationStatus().isValid()) {
9699
notificationPane.hide();
97100
} else {
98-
sourceValidator.getValidationStatus().getHighestMessage().ifPresent(validationMessage -> notificationPane.show(validationMessage.getMessage()));
101+
sourceValidator.getValidationStatus().getHighestMessage().ifPresent(validationMessage -> {
102+
notificationPane.show(validationMessage.getMessage());//this seems not working
103+
dialogService.showErrorDialogAndWait(validationMessage.getMessage());
104+
});
99105
}
100106
});
101107
this.setContent(codeArea);
102108

103-
// Store source for every change in the source code
109+
// Store source for on focus out event in the source code (within its text area)
104110
// and update source code for every change of entry field values
105-
BindingsHelper.bindContentBidirectional(entry.getFieldsObservable(), codeArea.textProperty(), this::storeSource, fields -> {
111+
BindingsHelper.bindContentBidirectional(entry.getFieldsObservable(), codeArea.focusedProperty(), onFocus -> {
112+
if (!onFocus) {
113+
storeSource(codeArea.textProperty().getValue());
114+
}
115+
}, fields -> {
106116
DefaultTaskExecutor.runAndWaitInJavaFXThread(() -> {
107117
codeArea.clear();
108118
try {
109119
codeArea.appendText(getSourceString(entry, mode, fieldFormatterPreferences));
110120
} catch (IOException ex) {
111121
codeArea.setEditable(false);
112122
codeArea.appendText(ex.getMessage() + "\n\n" +
113-
Localization.lang("Correct the entry, and reopen editor to display/edit source."));
123+
Localization.lang("Correct the entry, and reopen editor to display/edit source."));
114124
LOGGER.debug("Incorrect entry", ex);
115125
}
116126
});
@@ -141,6 +151,12 @@ private void storeSource(String text) {
141151
}
142152
}
143153

154+
if (parserResult.hasWarnings()) {
155+
// put the warning into as exception text -> it will be displayed to the user
156+
157+
throw new IllegalStateException(parserResult.getErrorMessage());
158+
}
159+
144160
NamedCompound compound = new NamedCompound(Localization.lang("source edit"));
145161
BibEntry newEntry = database.getEntries().get(0);
146162
String newKey = newEntry.getCiteKeyOptional().orElse(null);

src/main/java/org/jabref/logic/importer/fileformat/BibtexParser.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.Map;
1717
import java.util.Objects;
1818
import java.util.Optional;
19+
import java.util.regex.Pattern;
1920

2021
import org.jabref.logic.bibtex.FieldContentParser;
2122
import org.jabref.logic.exporter.BibtexDatabaseWriter;
@@ -70,7 +71,7 @@ public class BibtexParser implements Parser {
7071
private boolean eof;
7172
private int line = 1;
7273
private ParserResult parserResult;
73-
private MetaDataParser metaDataParser;
74+
private final MetaDataParser metaDataParser;
7475

7576
public BibtexParser(ImportFormatPreferences importFormatPreferences, FileUpdateMonitor fileMonitor) {
7677
this.importFormatPreferences = Objects.requireNonNull(importFormatPreferences);
@@ -211,9 +212,20 @@ private ParserResult parseFileContent() throws IOException {
211212

212213
parseRemainingContent();
213214

215+
checkEpilog();
216+
214217
return parserResult;
215218
}
216219

220+
private void checkEpilog() {
221+
// This is an incomplete and inaccurate try to verify if something went wrong with previous parsing activity even though there were no warnings so far
222+
// regex looks for something like 'identifier = blabla ,'
223+
if (!parserResult.hasWarnings() && Pattern.compile("\\w+\\s*=.*,").matcher(database.getEpilog()).find()) {
224+
parserResult.addWarning("following BibTex fragment has not been parsed:\n" + database.getEpilog());
225+
}
226+
227+
}
228+
217229
private void parseRemainingContent() {
218230
database.setEpilog(dumpTextReadSoFarToString().trim());
219231
}

src/test/java/org/jabref/gui/entryeditor/SourceTabTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import javafx.scene.control.TabPane;
77
import javafx.stage.Stage;
88

9+
import org.jabref.gui.FXDialogService;
910
import org.jabref.gui.undo.CountingUndoManager;
1011
import org.jabref.logic.bibtex.LatexFieldFormatterPreferences;
1112
import org.jabref.logic.importer.ImportFormatPreferences;
@@ -35,7 +36,7 @@ public class SourceTabTest {
3536
public void onStart(Stage stage) {
3637
area = new CodeArea();
3738
area.appendText("some example\n text to go here\n across a couple of \n lines....");
38-
sourceTab = new SourceTab(new BibDatabaseContext(), new CountingUndoManager(), new LatexFieldFormatterPreferences(), mock(ImportFormatPreferences.class), new DummyFileUpdateMonitor());
39+
sourceTab = new SourceTab(new BibDatabaseContext(), new CountingUndoManager(), new LatexFieldFormatterPreferences(), mock(ImportFormatPreferences.class), new DummyFileUpdateMonitor(), new FXDialogService());
3940
pane = new TabPane(
4041
new Tab("main area", area),
4142
new Tab("other tab", new Label("some text")),

0 commit comments

Comments
 (0)