diff --git a/src/main/java/org/jabref/logic/importer/fileformat/BibtexParser.java b/src/main/java/org/jabref/logic/importer/fileformat/BibtexParser.java index ebb1282a787..c4c6e455110 100644 --- a/src/main/java/org/jabref/logic/importer/fileformat/BibtexParser.java +++ b/src/main/java/org/jabref/logic/importer/fileformat/BibtexParser.java @@ -58,6 +58,9 @@ import com.dd.plist.BinaryPropertyListParser; import com.dd.plist.NSDictionary; import com.dd.plist.NSString; +import com.google.common.annotations.VisibleForTesting; +import com.google.gson.Gson; +import com.google.gson.JsonObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; @@ -354,8 +357,8 @@ private void parseJabRefComment(Map meta) { // We remove all line breaks in the metadata // These have been inserted to prevent too long lines when the file was saved, and are not part of the data. String comment = buffer.toString().replaceAll("[\\x0d\\x0a]", ""); - if (comment.substring(0, Math.min(comment.length(), MetaData.META_FLAG.length())).equals(MetaData.META_FLAG)) { - if (comment.startsWith(MetaData.META_FLAG)) { + + if (comment.startsWith(MetaData.META_FLAG)) { String rest = comment.substring(MetaData.META_FLAG.length()); int pos = rest.indexOf(':'); @@ -366,9 +369,7 @@ private void parseJabRefComment(Map meta) { // meta comments are always re-written by JabRef and not stored in the file dumpTextReadSoFarToString(); } - } - } else if (comment.substring(0, Math.min(comment.length(), MetaData.ENTRYTYPE_FLAG.length())) - .equals(MetaData.ENTRYTYPE_FLAG)) { + } else if (comment.startsWith(MetaData.ENTRYTYPE_FLAG)) { // A custom entry type can also be stored in a // "@comment" Optional typ = MetaDataParser.parseCustomEntryType(comment); @@ -389,6 +390,13 @@ private void parseJabRefComment(Map meta) { } } + @VisibleForTesting + Optional parseCommentToJson(String comment) { + String content = comment.substring(comment.indexOf(MetaData.META_FLAG_VERSION_010) + MetaData.META_FLAG_VERSION_010.length()); + Gson gson = new Gson(); + return Optional.ofNullable(gson.fromJson(content, JsonObject.class)); + } + /** * Adds BibDesk group entries to the JabRef database */ diff --git a/src/main/java/org/jabref/logic/importer/util/MetaDataParser.java b/src/main/java/org/jabref/logic/importer/util/MetaDataParser.java index d1e66146412..b7029c295c5 100644 --- a/src/main/java/org/jabref/logic/importer/util/MetaDataParser.java +++ b/src/main/java/org/jabref/logic/importer/util/MetaDataParser.java @@ -96,7 +96,7 @@ public static Optional parseCustomEntryType(String comment) { public MetaData parse(Map data, Character keywordSeparator) throws ParseException { return parse(new MetaData(), data, keywordSeparator); } - + /** * Parses the data map and changes the given {@link MetaData} instance respectively. * diff --git a/src/main/java/org/jabref/model/metadata/MetaData.java b/src/main/java/org/jabref/model/metadata/MetaData.java index c7be01ce09b..0a251745b5b 100644 --- a/src/main/java/org/jabref/model/metadata/MetaData.java +++ b/src/main/java/org/jabref/model/metadata/MetaData.java @@ -31,13 +31,12 @@ import com.google.common.eventbus.EventBus; import com.tobiasdiez.easybind.optional.OptionalBinding; import com.tobiasdiez.easybind.optional.OptionalWrapper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; @AllowedToUseLogic("because it needs access to citation pattern and cleanups") public class MetaData { public static final String META_FLAG = "jabref-meta: "; + public static final String META_FLAG_VERSION_010 = "jabref-meta-0.1.0"; public static final String ENTRYTYPE_FLAG = "jabref-entrytype: "; public static final String SAVE_ORDER_CONFIG = "saveOrderConfig"; // ToDo: Rename in next major version to saveOrder, adapt testbibs public static final String SAVE_ACTIONS = "saveActions"; @@ -58,8 +57,6 @@ public class MetaData { public static final char SEPARATOR_CHARACTER = ';'; public static final String SEPARATOR_STRING = String.valueOf(SEPARATOR_CHARACTER); - private static final Logger LOGGER = LoggerFactory.getLogger(MetaData.class); - private final EventBus eventBus = new EventBus(); private final Map citeKeyPatterns = new HashMap<>(); // private final Map userFileDirectory = new HashMap<>(); // 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 2243a401b81..b71add189c3 100644 --- a/src/test/java/org/jabref/logic/importer/fileformat/BibtexParserTest.java +++ b/src/test/java/org/jabref/logic/importer/fileformat/BibtexParserTest.java @@ -57,6 +57,8 @@ import org.jabref.model.groups.WordKeywordGroup; import org.jabref.model.metadata.SaveOrder; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @@ -2238,4 +2240,42 @@ void parseInvalidBibDeskFilesResultsInWarnings() throws IOException { assertEquals(List.of(firstEntry, secondEntry), result.getDatabase().getEntries()); } + + @Test + void parseCommentToJson() { + String entries = + """ + @Comment{jabref-meta-0.1.0 + { + "saveActions" : + { + "state": true, + "date": ["normalize_date", "action2"], + "pages" : ["normalize_page_numbers"], + "month" : ["normalize_month"] + } + } + """; + BibtexParser parser = new BibtexParser(importFormatPreferences); + Optional actualJson = parser.parseCommentToJson(entries); + assertEquals(actualJson, Optional.of(getExpectedJson())); + } + + private JsonObject getExpectedJson() { + JsonObject saveActions = new JsonObject(); + saveActions.addProperty("state", true); + JsonArray dateArray = new JsonArray(); + dateArray.add("normalize_date"); + dateArray.add("action2"); + saveActions.add("date", dateArray); + JsonArray pagesArray = new JsonArray(); + pagesArray.add("normalize_page_numbers"); + saveActions.add("pages", pagesArray); + JsonArray monthArray = new JsonArray(); + monthArray.add("normalize_month"); + saveActions.add("month", monthArray); + JsonObject expectedJson = new JsonObject(); + expectedJson.add("saveActions", saveActions); + return expectedJson; + } }