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 1 commit
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 @@ -20,6 +20,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilderFactory;
Expand Down Expand Up @@ -58,6 +59,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 @@ -339,7 +343,7 @@ private void parseAndAddEntry(String type) {
}
}

private void parseJabRefComment(Map<String, String> meta) {
void parseJabRefComment(Map<String, String> meta) {
Copy link
Member

Choose a reason for hiding this comment

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

This needs @VisibileForTesting?

StringBuilder buffer;
try {
buffer = parseBracketedFieldContent();
Expand All @@ -354,6 +358,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 (comment.substring(0, Math.min(comment.length(), MetaData.META_FLAG.length())).equals(MetaData.META_FLAG)) {
if (comment.startsWith(MetaData.META_FLAG)) {
String rest = comment.substring(MetaData.META_FLAG.length());
Expand Down Expand Up @@ -386,6 +391,22 @@ private void parseJabRefComment(Map<String, String> meta) {
} catch (ParseException ex) {
parserResult.addException(ex);
}
} else if (Pattern.compile("\\{.*}", Pattern.DOTALL).matcher(comment).find()) {
parseCommentToJson(comment);
Copy link
Member

Choose a reason for hiding this comment

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

The result is not handled. I think, it needs to be handled. It needs to be passed to the Metadata.parse method somehow.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry I made this PR too bigger again, so I decided only to add the unit test for parseCommentToJson and implement of it, I'll do more in next PR.

}
}

private void parseCommentToJson(String comment) {
Pattern pattern = Pattern.compile("\\{.*}", Pattern.DOTALL);
Matcher matcher = pattern.matcher(comment);
Copy link
Member

Choose a reason for hiding this comment

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

I think, just doing comments.substring(MetaData.META_FLAG_VERSION_010) should to the trick. - No need for more checking here.

if (matcher.find()) {
String jsonString = matcher.group();
Gson gson = new Gson();
JsonObject jsonObject = gson.fromJson(jsonString, JsonObject.class);
String jsonResult = gson.toJson(jsonObject);
BibEntry entry = new BibEntry();
entry.setField(StandardField.COMMENT, jsonResult);
database.insertEntry(entry);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2238,4 +2238,26 @@ void parseInvalidBibDeskFilesResultsInWarnings() throws IOException {

assertEquals(List.of(firstEntry, secondEntry), result.getDatabase().getEntries());
}

@Test
void parseJabRefSingleJsonComment() throws IOException {
String entries =
"""
@Comment{jabref-meta-0.1.0
{
"saveActions" :
{
"state": true,
"date": ["normalize_date", "action2"],
"pages" : ["normalize_page_numbers"],
"month" : ["normalize_month"]
}
}
}
""";
ParserResult result = parser.parse(new StringReader(entries));
BibEntry expectedEntry = new BibEntry();
expectedEntry.setField(StandardField.COMMENT, "{\"saveActions\":{\"state\":true,\"date\":[\"normalize_date\",\"action2\"],\"pages\":[\"normalize_page_numbers\"],\"month\":[\"normalize_month\"]}}");
assertEquals(expectedEntry, result.getDatabase().getEntries().getFirst());
}
}
Loading