Skip to content
Closed
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -354,6 +357,7 @@ private void parseJabRefComment(Map<String, String> 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 (MetaData.META_FLAG.equals(comment.substring(0, Math.min(comment.length(), MetaData.META_FLAG.length())))) {
if (comment.startsWith(MetaData.META_FLAG)) {
String rest = comment.substring(MetaData.META_FLAG.length());
Expand Down Expand Up @@ -389,6 +393,13 @@ private void parseJabRefComment(Map<String, String> meta) {
}
}

@VisibleForTesting
Optional<JsonObject> parseCommentToJson(String comment) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not embedded in a real parsing of a future JabRef version. This should be done in this PR too so that we are sure about the architcture.

(That comment was risen, because not a pattern like String rest = comment.substring(MetaData.META_FLAG.length()); was used, but an offset...)

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
*/
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/org/jabref/model/metadata/MetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<EntryType, String> citeKeyPatterns = new HashMap<>(); // <BibType, Pattern>
private final Map<String, String> userFileDirectory = new HashMap<>(); // <User, FilePath>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.jabref.model.groups.WordKeywordGroup;
import org.jabref.model.metadata.SaveOrder;

import com.google.gson.JsonObject;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -2238,4 +2239,25 @@ 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
}
}
""";
BibtexParser parser = new BibtexParser(importFormatPreferences);
Optional<JsonObject> actualJson = parser.parseCommentToJson(entries);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if this will be the real API, but it is OK for me to let this through.

JsonObject saveActions = new JsonObject();
saveActions.addProperty("state", true);
JsonObject expectedJson = new JsonObject();
expectedJson.add("saveActions", saveActions);
assertEquals(actualJson, Optional.of(expectedJson));
}
}