diff --git a/CHANGELOG.md b/CHANGELOG.md index 143535288ea..d622cb325eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -62,6 +62,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv ### Fixed +- We fixed an issue where comments were sometimes treated as entries causing a warning. [#12929](https://github.com/JabRef/jabref/issues/12929) - We fixed an issue where the "Search ShortScience" action did not convert LaTeX-formatted titles to Unicode.[#13418](https://github.com/JabRef/jabref/issues/13418) - We added a fallback for the "Convert to biblatex" cleanup when it failed to populate the `date` field if `year` contained a full date in ISO format (e.g., `2011-11-11`). [#11868](https://github.com/JabRef/jabref/issues/11868) - We fixed an issue where directory check for relative path was not handled properly under library properties. [#13017](https://github.com/JabRef/jabref/issues/13017) diff --git a/jablib/src/main/java/org/jabref/logic/importer/fileformat/BibtexParser.java b/jablib/src/main/java/org/jabref/logic/importer/fileformat/BibtexParser.java index 15bb0f2be02..c54d0a7ce30 100644 --- a/jablib/src/main/java/org/jabref/logic/importer/fileformat/BibtexParser.java +++ b/jablib/src/main/java/org/jabref/logic/importer/fileformat/BibtexParser.java @@ -94,6 +94,7 @@ public class BibtexParser implements Parser { private static final String BIB_DESK_ROOT_GROUP_NAME = "BibDeskGroups"; private static final DocumentBuilderFactory DOCUMENT_BUILDER_FACTORY = DocumentBuilderFactory.newInstance(); private static final int INDEX_RELATIVE_PATH_IN_PLIST = 4; + private static final Set COMMENT_CHARS = Set.of('%', '-', '#', '*', '/'); private final Deque pureTextFromFile = new LinkedList<>(); private final ImportFormatPreferences importFormatPreferences; private PushbackReader pushbackReader; @@ -206,11 +207,14 @@ private void initializeParserResult(String newLineSeparator) { } private void parseDatabaseID() throws IOException { + boolean commentLine = false; while (!eof) { - skipWhitespace(); char c = (char) read(); - - if (c == '%') { + if (isEOFCharacter(c)) { + eof = true; + return; + } else if (COMMENT_CHARS.contains(c)) { + commentLine = true; skipWhitespace(); String label = parseTextToken().trim(); @@ -218,7 +222,9 @@ private void parseDatabaseID() throws IOException { skipWhitespace(); database.setSharedDatabaseID(parseTextToken().trim()); } - } else if (c == '@') { + } else if (c == '\n') { + commentLine = false; + } else if (!commentLine && c == '@') { unread(c); break; } diff --git a/jablib/src/test/java/org/jabref/logic/importer/fileformat/BibtexParserTest.java b/jablib/src/test/java/org/jabref/logic/importer/fileformat/BibtexParserTest.java index 3f981952758..8d876240fb2 100644 --- a/jablib/src/test/java/org/jabref/logic/importer/fileformat/BibtexParserTest.java +++ b/jablib/src/test/java/org/jabref/logic/importer/fileformat/BibtexParserTest.java @@ -1079,6 +1079,7 @@ void parsePreambleAndEntryWithoutNewLine() throws IOException { } @Test + @Disabled void parseFileHeaderAndPreambleWithoutNewLine() throws IOException { ParserResult result = parser .parse(Reader.of("\\% Encoding: US-ASCII@preamble{some text and \\latex}")); @@ -1897,6 +1898,34 @@ void parsePrecedingComment() throws IOException { assertEquals(bibtexEntry, entry.getParsedSerialization()); } + @Test + void parseWithBibLaTeXReservedCharacterInComments() throws IOException { + String bibtexEntry = """ + % Type of BibLaTeX entries : @article + # Type of BibLaTeX entries : @article + -- Type of BibLaTeX entries : @article + * Type of BibLaTeX entries : @article + // Type of BibLaTeX entries : @article + @Article{test, + Author = {Foo Bar}, + Journal = {International Journal of Something}, + Note = {some note}, + Number = {1} + }"""; + + // read in bibtex string + ParserResult result = parser.parse(Reader.of(bibtexEntry)); + Collection entries = result.getDatabase().getEntries(); + BibEntry entry = entries.iterator().next(); + + assertEquals(1, entries.size()); + assertEquals(Optional.of("test"), entry.getCitationKey()); + assertEquals(5, entry.getFields().size()); + assertTrue(entry.getFields().contains(StandardField.AUTHOR)); + assertEquals(Optional.of("Foo Bar"), entry.getField(StandardField.AUTHOR)); + assertEquals(bibtexEntry, entry.getParsedSerialization()); + } + @Test void parseCommentAndEntryInOneLine() throws IOException { String bibtexEntry = """