-
-
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 6 commits
56c1bef
b2646ae
e12e655
465d6cf
830e7e6
065a784
3c4ee8f
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,8 @@ | |
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 org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.w3c.dom.Document; | ||
|
@@ -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 (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,7 +390,23 @@ private void parseJabRefComment(Map<String, String> meta) { | |
} catch (ParseException ex) { | ||
parserResult.addException(ex); | ||
} | ||
} else if (comment.substring(0, Math.min(comment.length(), MetaData.META_FLAG_VERSION_010.length())).equals(MetaData.META_FLAG_VERSION_010)) { | ||
parseCommentToJson(comment, meta); | ||
} | ||
} | ||
|
||
private JsonObject parseCommentToJson(String comment, Map<String, String> meta) { | ||
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. Remove the map as parameter. It should not be modified. The JsonObject should be separate - to enable handling v5.x meta data (Map) and v6.x metadata (JSON) 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. Moreover, return |
||
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); | ||
meta.putIfAbsent(MetaData.META_FLAG_VERSION_010, jsonResult); | ||
return jsonObject; | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
||
|
@@ -97,6 +100,77 @@ public MetaData parse(Map<String, String> data, Character keywordSeparator) thro | |
return parse(new MetaData(), data, keywordSeparator); | ||
} | ||
|
||
public MetaData parse(JsonObject data, Character keywordSeparator) throws ParseException { | ||
koppor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return parse(new MetaData(), data, keywordSeparator); | ||
} | ||
|
||
public MetaData parse(MetaData metaData, JsonObject data, Character keywordSeparator) throws ParseException { | ||
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. 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. 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. Please, either test this method or remove it - and put it to the next PR. 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. 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 + '-' | ||
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. | ||
* | ||
|
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.
Java strings support
startsWith
. I think, this should be used here instead of this complicated thign.