From 35b418594a122202182553f980a9eafca271001b Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Mon, 26 Sep 2022 20:39:05 +0200 Subject: [PATCH 1/2] Rename "getErrorMessage()" to "getWarningsAsString()" Co-authored-by: Christoph Co-authored-by: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Co-authored-by: Houssem Nasri --- .../java/org/jabref/cli/ArgumentProcessor.java | 2 +- src/main/java/org/jabref/gui/JabRefGUI.java | 2 +- .../java/org/jabref/gui/entryeditor/SourceTab.java | 2 +- .../jabref/gui/externalfiles/ImportHandler.java | 4 ++-- .../gui/importer/actions/OpenDatabaseAction.java | 2 +- .../jabref/logic/importer/ImportFormatReader.java | 2 +- .../org/jabref/logic/importer/ParserResult.java | 14 +++++++------- .../logic/importer/fetcher/MedlineFetcher.java | 2 +- .../importer/fileformat/ImporterTestEngine.java | 2 +- 9 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/jabref/cli/ArgumentProcessor.java b/src/main/java/org/jabref/cli/ArgumentProcessor.java index fad33ed0d0d..bc8d3b92418 100644 --- a/src/main/java/org/jabref/cli/ArgumentProcessor.java +++ b/src/main/java/org/jabref/cli/ArgumentProcessor.java @@ -140,7 +140,7 @@ private static Optional importFile(String argument) { importResult.ifPresent(result -> { OutputPrinter printer = new SystemOutputPrinter(); if (result.hasWarnings()) { - printer.showMessage(result.getErrorMessage()); + printer.showMessage(result.getWarningsAsString()); } }); return importResult; diff --git a/src/main/java/org/jabref/gui/JabRefGUI.java b/src/main/java/org/jabref/gui/JabRefGUI.java index 2acb97939c1..f8d4a39b1b1 100644 --- a/src/main/java/org/jabref/gui/JabRefGUI.java +++ b/src/main/java/org/jabref/gui/JabRefGUI.java @@ -189,7 +189,7 @@ private void openDatabases() { for (ParserResult pr : failed) { String message = Localization.lang("Error opening file '%0'.", pr.getPath().map(Path::toString).orElse("(File name unknown)")) + "\n" + - pr.getErrorMessage(); + pr.getWarningsAsString(); mainFrame.getDialogService().showErrorDialogAndWait(Localization.lang("Error opening file"), message); } diff --git a/src/main/java/org/jabref/gui/entryeditor/SourceTab.java b/src/main/java/org/jabref/gui/entryeditor/SourceTab.java index 1e3de019011..2e49c1f623b 100644 --- a/src/main/java/org/jabref/gui/entryeditor/SourceTab.java +++ b/src/main/java/org/jabref/gui/entryeditor/SourceTab.java @@ -276,7 +276,7 @@ private void storeSource(BibEntry outOfFocusEntry, String text) { if (parserResult.hasWarnings()) { // put the warning into as exception text -> it will be displayed to the user - throw new IllegalStateException(parserResult.getErrorMessage()); + throw new IllegalStateException(parserResult.getWarningsAsString()); } NamedCompound compound = new NamedCompound(Localization.lang("source edit")); diff --git a/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java b/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java index 873ae09cb59..63851a11e39 100644 --- a/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java +++ b/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java @@ -114,7 +114,7 @@ protected List call() { List pdfEntriesInFile = pdfImporterResult.getDatabase().getEntries(); if (pdfImporterResult.hasWarnings()) { - addResultToList(file, false, Localization.lang("Error reading PDF content: %0", pdfImporterResult.getErrorMessage())); + addResultToList(file, false, Localization.lang("Error reading PDF content: %0", pdfImporterResult.getWarningsAsString())); } if (!pdfEntriesInFile.isEmpty()) { @@ -127,7 +127,7 @@ protected List call() { } else if (FileUtil.isBibFile(file)) { var bibtexParserResult = contentImporter.importFromBibFile(file, fileUpdateMonitor); if (bibtexParserResult.hasWarnings()) { - addResultToList(file, false, bibtexParserResult.getErrorMessage()); + addResultToList(file, false, bibtexParserResult.getWarningsAsString()); } entriesToAdd.addAll(bibtexParserResult.getDatabaseContext().getEntries()); diff --git a/src/main/java/org/jabref/gui/importer/actions/OpenDatabaseAction.java b/src/main/java/org/jabref/gui/importer/actions/OpenDatabaseAction.java index 45e95158e29..f1864c30748 100644 --- a/src/main/java/org/jabref/gui/importer/actions/OpenDatabaseAction.java +++ b/src/main/java/org/jabref/gui/importer/actions/OpenDatabaseAction.java @@ -206,7 +206,7 @@ private ParserResult loadDatabase(Path file) throws Exception { Globals.getFileUpdateMonitor()); if (result.hasWarnings()) { String content = Localization.lang("Please check your library file for wrong syntax.") - + "\n\n" + result.getErrorMessage(); + + "\n\n" + result.getWarningsAsString(); DefaultTaskExecutor.runInJavaFXThread(() -> dialogService.showWarningDialogAndWait(Localization.lang("Open library error"), content)); } diff --git a/src/main/java/org/jabref/logic/importer/ImportFormatReader.java b/src/main/java/org/jabref/logic/importer/ImportFormatReader.java index 67ceeb59647..9024d41a264 100644 --- a/src/main/java/org/jabref/logic/importer/ImportFormatReader.java +++ b/src/main/java/org/jabref/logic/importer/ImportFormatReader.java @@ -193,7 +193,7 @@ public UnknownFormatImport importUnknownFormat(Path filePath, FileUpdateMonitor parserResult.setPath(filePath); return new UnknownFormatImport(ImportFormatReader.BIBTEX_FORMAT, parserResult); } else { - throw new ImportException(parserResult.getErrorMessage()); + throw new ImportException(parserResult.getWarningsAsString()); } } catch (IOException ignore) { // Ignored diff --git a/src/main/java/org/jabref/logic/importer/ParserResult.java b/src/main/java/org/jabref/logic/importer/ParserResult.java index 2f134224562..af12ecf1b2b 100644 --- a/src/main/java/org/jabref/logic/importer/ParserResult.java +++ b/src/main/java/org/jabref/logic/importer/ParserResult.java @@ -52,7 +52,7 @@ public static ParserResult fromErrorMessage(String message) { return parserResult; } - private static String getErrorMessage(Exception exception) { + private static String getWarningsAsString(Exception exception) { String errorMessage = exception.getLocalizedMessage(); if (exception.getCause() != null) { errorMessage += " Caused by: " + exception.getCause().getLocalizedMessage(); @@ -61,7 +61,7 @@ private static String getErrorMessage(Exception exception) { } public static ParserResult fromError(Exception exception) { - return fromErrorMessage(getErrorMessage(exception)); + return fromErrorMessage(getWarningsAsString(exception)); } /** @@ -111,7 +111,7 @@ public void addWarning(String s) { } public void addException(Exception exception) { - String errorMessage = getErrorMessage(exception); + String errorMessage = getWarningsAsString(exception); addWarning(errorMessage); } @@ -123,6 +123,10 @@ public List warnings() { return new ArrayList<>(warnings); } + public String getWarningsAsString() { + return String.join(" ", warnings()); + } + public boolean isInvalid() { return invalid; } @@ -131,10 +135,6 @@ public void setInvalid(boolean invalid) { this.invalid = invalid; } - public String getErrorMessage() { - return String.join(" ", warnings()); - } - public BibDatabaseContext getDatabaseContext() { return new BibDatabaseContext(database, metaData, file); } diff --git a/src/main/java/org/jabref/logic/importer/fetcher/MedlineFetcher.java b/src/main/java/org/jabref/logic/importer/fetcher/MedlineFetcher.java index b37eb30daed..b42375118b0 100644 --- a/src/main/java/org/jabref/logic/importer/fetcher/MedlineFetcher.java +++ b/src/main/java/org/jabref/logic/importer/fetcher/MedlineFetcher.java @@ -173,7 +173,7 @@ private List fetchMedline(List ids) throws FetcherException { ParserResult result = new MedlineImporter().importDatabase( new BufferedReader(new InputStreamReader(data.getInputStream(), StandardCharsets.UTF_8))); if (result.hasWarnings()) { - LOGGER.warn(result.getErrorMessage()); + LOGGER.warn(result.getWarningsAsString()); } List resultList = result.getDatabase().getEntries(); resultList.forEach(this::doPostCleanup); diff --git a/src/test/java/org/jabref/logic/importer/fileformat/ImporterTestEngine.java b/src/test/java/org/jabref/logic/importer/fileformat/ImporterTestEngine.java index 9d24b9c96d7..fddab79e473 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/ImporterTestEngine.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/ImporterTestEngine.java @@ -50,7 +50,7 @@ public static void testIsNotRecognizedFormat(Importer importer, String fileName) public static void testImportEntries(Importer importer, String fileName, String fileType) throws IOException, ImportException { ParserResult parserResult = importer.importDatabase(getPath(fileName)); if (parserResult.isInvalid()) { - throw new ImportException(parserResult.getErrorMessage()); + throw new ImportException(parserResult.getWarningsAsString()); } List entries = parserResult.getDatabase() .getEntries(); From ac21220f213be501781053a4eb49c794b2603472 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Mon, 26 Sep 2022 20:55:08 +0200 Subject: [PATCH 2/2] Add toString() and refine test Co-authored-by: Christoph Co-authored-by: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Co-authored-by: Houssem Nasri Co-authored-by: Benedikt Tutzer --- .../jabref/logic/importer/ParserResult.java | 14 +++++++++++ .../jabref/model/database/BibDatabase.java | 12 ++++++++++ .../importer/fileformat/BibtexParserTest.java | 23 +++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/src/main/java/org/jabref/logic/importer/ParserResult.java b/src/main/java/org/jabref/logic/importer/ParserResult.java index af12ecf1b2b..caee3b8fe07 100644 --- a/src/main/java/org/jabref/logic/importer/ParserResult.java +++ b/src/main/java/org/jabref/logic/importer/ParserResult.java @@ -157,6 +157,20 @@ public boolean wasChangedOnMigration() { return changedOnMigration; } + @Override + public String toString() { + return "ParserResult{" + + "entryTypes=" + entryTypes + + ", warnings=" + warnings + + ", database=" + database + + ", metaData=" + metaData + + ", file=" + file + + ", invalid=" + invalid + + ", toOpenTab=" + toOpenTab + + ", changedOnMigration=" + changedOnMigration + + '}'; + } + public void setChangedOnMigration(boolean wasChangedOnMigration) { this.changedOnMigration = wasChangedOnMigration; } diff --git a/src/main/java/org/jabref/model/database/BibDatabase.java b/src/main/java/org/jabref/model/database/BibDatabase.java index 820b8dcc107..39f5e3f2fae 100644 --- a/src/main/java/org/jabref/model/database/BibDatabase.java +++ b/src/main/java/org/jabref/model/database/BibDatabase.java @@ -646,4 +646,16 @@ public void setNewLineSeparator(String newLineSeparator) { public String getNewLineSeparator() { return newLineSeparator; } + + @Override + public String toString() { + return "BibDatabase{" + + "entries.size()=" + entries.size() + + ", bibtexStrings.size()=" + bibtexStrings.size() + + ", preamble='" + preamble + '\'' + + ", epilog='" + epilog + '\'' + + ", sharedDatabaseID='" + sharedDatabaseID + '\'' + + ", newLineSeparator='" + newLineSeparator + '\'' + + '}'; + } } diff --git a/src/test/java/org/jabref/logic/importer/fileformat/BibtexParserTest.java b/src/test/java/org/jabref/logic/importer/fileformat/BibtexParserTest.java index 5eec134b1d3..b6c63caa022 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/BibtexParserTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/BibtexParserTest.java @@ -1923,4 +1923,27 @@ void bibTeXConstantAprilIsParsedAsStringMonthAprilWhenReadingTheField() throws P assertEquals(Optional.of("#apr#"), result.get().getField(StandardField.MONTH)); } + + @Test + void conflictMarkersResultInProperParseError() throws Exception { + String input = """ + @Article{first, + author = {Author}, + <<<<<<< HEAD:test.bib + title = {Title}, + ======= + title = {My title}, + >>>>>>> 77976da35a11db4580b80ae27e8d65caf5208086:test.bib + } + + @Article{second, + author = {Valid Author}, + } + """; + ParserResult parserResult = parser.parse(new StringReader(input)); + + ParserResult expected = ParserResult.fromErrorMessage("Found git conflict markers"); + + assertEquals(expected, parserResult); + } }