Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new parseJabRefComment with unit test #12145

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,8 +357,8 @@ 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 (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(':');
Expand All @@ -366,9 +369,7 @@ private void parseJabRefComment(Map<String, String> 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<BibEntryType> typ = MetaDataParser.parseCustomEntryType(comment);
Expand All @@ -389,6 +390,13 @@ private void parseJabRefComment(Map<String, String> meta) {
}
}

@VisibleForTesting
Optional<JsonObject> 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
*/
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/org/jabref/logic/importer/util/MetaDataParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.Reader;
import java.io.StringReader;
import java.nio.file.Path;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
Expand Down Expand Up @@ -35,6 +36,8 @@
import org.jabref.model.strings.StringUtil;
import org.jabref.model.util.FileUpdateMonitor;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -97,6 +100,73 @@ public MetaData parse(Map<String, String> data, Character keywordSeparator) thro
return parse(new MetaData(), data, keywordSeparator);
}

public MetaData parse(MetaData metaData, JsonObject data, Character keywordSeparator) throws ParseException {
Copy link
Member

Choose a reason for hiding this comment

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

Add JavaDoc - I don't get this.

I think, this should not be in this PR for now - We only focus on the JSON - and then, we can work on the parsing of the JSON.

Copy link
Member

Choose a reason for hiding this comment

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

Please, either test this method or remove it - and put it to the next PR.

Copy link
Member

Choose a reason for hiding this comment

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

This method is should be static, shouldn't it?

CitationKeyPattern defaultCiteKeyPattern = CitationKeyPattern.NULL_CITATION_KEY_PATTERN;
Map<EntryType, CitationKeyPattern> nonDefaultCiteKeyPatterns = new HashMap<>();

// process groups (GROUPSTREE and GROUPSTREE_LEGACY) at the very end (otherwise it can happen that not all dependent data are set)
List<Map.Entry<String, String>> entryList = new ArrayList<>();
for (Map.Entry<String, JsonElement> entry : data.entrySet()) {
// Add each entry to the list, converting JsonElement to String
entryList.add(new AbstractMap.SimpleEntry<>(entry.getKey(), entry.getValue().getAsString()));
}

entryList.sort(groupsLast());

for (Map.Entry<String, String> entry : entryList) {
List<String> values = getAsList(entry.getValue());

if (entry.getKey().startsWith(MetaData.PREFIX_KEYPATTERN)) {
EntryType entryType = EntryTypeFactory.parse(entry.getKey().substring(MetaData.PREFIX_KEYPATTERN.length()));
nonDefaultCiteKeyPatterns.put(entryType, new CitationKeyPattern(getSingleItem(values)));
} else if (entry.getKey().startsWith(MetaData.SELECTOR_META_PREFIX)) {
// edge case, it might be one special field e.g. article from biblatex-apa, but we can't distinguish this from any other field and rather prefer to handle it as UnknownField
metaData.addContentSelector(ContentSelectors.parse(FieldFactory.parseField(entry.getKey().substring(MetaData.SELECTOR_META_PREFIX.length())), StringUtil.unquote(entry.getValue(), MetaData.ESCAPE_CHARACTER)));
} else if (entry.getKey().equals(MetaData.FILE_DIRECTORY)) {
metaData.setLibrarySpecificFileDirectory(parseDirectory(entry.getValue()));
} else if (entry.getKey().startsWith(MetaData.FILE_DIRECTORY + '-')) {
// The user name starts directly after FILE_DIRECTORY + '-'
String user = entry.getKey().substring(MetaData.FILE_DIRECTORY.length() + 1);
metaData.setUserFileDirectory(user, parseDirectory(entry.getValue()));
} else if (entry.getKey().startsWith(MetaData.FILE_DIRECTORY_LATEX)) {
// The user name starts directly after FILE_DIRECTORY_LATEX + '-'
Copy link
Member

Choose a reason for hiding this comment

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

These hacks should be removed when transitioning to JSON.

String user = entry.getKey().substring(MetaData.FILE_DIRECTORY_LATEX.length() + 1);
Path path = Path.of(parseDirectory(entry.getValue())).normalize();
metaData.setLatexFileDirectory(user, path);
} else if (entry.getKey().equals(MetaData.SAVE_ACTIONS)) {
metaData.setSaveActions(fieldFormatterCleanupsParse(values));
} else if (entry.getKey().equals(MetaData.DATABASE_TYPE)) {
metaData.setMode(BibDatabaseMode.parse(getSingleItem(values)));
} else if (entry.getKey().equals(MetaData.KEYPATTERNDEFAULT)) {
defaultCiteKeyPattern = new CitationKeyPattern(getSingleItem(values));
} else if (entry.getKey().equals(MetaData.PROTECTED_FLAG_META)) {
if (Boolean.parseBoolean(getSingleItem(values))) {
metaData.markAsProtected();
} else {
metaData.markAsNotProtected();
}
} else if (entry.getKey().equals(MetaData.SAVE_ORDER_CONFIG)) {
metaData.setSaveOrder(SaveOrder.parse(values));
} else if (entry.getKey().equals(MetaData.GROUPSTREE) || entry.getKey().equals(MetaData.GROUPSTREE_LEGACY)) {
metaData.setGroups(GroupsParser.importGroups(values, keywordSeparator, fileMonitor, metaData));
} else if (entry.getKey().equals(MetaData.GROUPS_SEARCH_SYNTAX_VERSION)) {
Version version = Version.parse(getSingleItem(values));
metaData.setGroupSearchSyntaxVersion(version);
} else if (entry.getKey().equals(MetaData.VERSION_DB_STRUCT)) {
metaData.setVersionDBStructure(getSingleItem(values));
} else {
// Keep meta data items that we do not know in the file
metaData.putUnknownMetaDataItem(entry.getKey(), values);
}
}

if (!defaultCiteKeyPattern.equals(CitationKeyPattern.NULL_CITATION_KEY_PATTERN) || !nonDefaultCiteKeyPatterns.isEmpty()) {
metaData.setCiteKeyPattern(defaultCiteKeyPattern, nonDefaultCiteKeyPatterns);
}

return metaData;
}

/**
* Parses the data map and changes the given {@link MetaData} instance respectively.
*
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,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;
Expand Down Expand Up @@ -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);
JsonObject actualJson = parser.parseCommentToJson(entries).orElse(null);
assertEquals(actualJson, getExpectedJson());
Copy link
Member

Choose a reason for hiding this comment

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

No. Use the power of Optionals.

Suggested change
JsonObject actualJson = parser.parseCommentToJson(entries).orElse(null);
assertEquals(actualJson, getExpectedJson());
Optional<JsonObject> 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;
Comment on lines +2265 to +2279
Copy link
Member

Choose a reason for hiding this comment

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

This reads very ugly.

According to https://stackoverflow.com/a/4696873/873282 it can be easier

Can you try with var?

var expected = new Object() {
...

Another thought: Just simplify the contained object.

you are testing the JSON reading - then put a simple JSON inside. Test one thing in one test :-)

Reason: The searialization of saveActions will probalby changed. Especially state will be called enabled for sure (because state has a different meaning)

}
}
Loading