-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
56c1bef
b2646ae
e12e655
465d6cf
830e7e6
065a784
3c4ee8f
fb161cf
ae98cdc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -58,6 +59,10 @@ | |
import com.dd.plist.BinaryPropertyListParser; | ||
import com.dd.plist.NSDictionary; | ||
import com.dd.plist.NSString; | ||
import com.google.gson.Gson; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
import com.google.gson.JsonSyntaxException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.w3c.dom.Document; | ||
|
@@ -339,7 +344,7 @@ private void parseAndAddEntry(String type) { | |
} | ||
} | ||
|
||
private void parseJabRefComment(Map<String, String> meta) { | ||
void parseJabRefComment(Map<String, String> meta) { | ||
StringBuilder buffer; | ||
try { | ||
buffer = parseBracketedFieldContent(); | ||
|
@@ -354,6 +359,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()); | ||
|
@@ -386,6 +392,36 @@ private void parseJabRefComment(Map<String, String> meta) { | |
} catch (ParseException ex) { | ||
parserResult.addException(ex); | ||
} | ||
} else if (isValidJson(comment)) { | ||
parseCommentToJson(comment); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
} | ||
|
||
public static boolean isValidJson(String jsonString) { | ||
try { | ||
String[] target = jsonString.split("\\{", 2); | ||
if (target.length < 2) { | ||
return false; | ||
} | ||
JsonParser.parseString("{" + target[1]); | ||
return true; | ||
} catch ( | ||
JsonSyntaxException e) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checkstyle is somewhat broken, please reduce to one line. |
||
return false; | ||
} | ||
} | ||
|
||
private void parseCommentToJson(String comment) { | ||
Pattern pattern = Pattern.compile("\\{.*}", Pattern.DOTALL); | ||
Matcher matcher = pattern.matcher(comment); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think, just doing |
||
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); | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs
@VisibileForTesting
?