-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Added reading and writing with JSON for save actions metadata #15159
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
Open
ZiadAbdElFatah
wants to merge
16
commits into
JabRef:main
Choose a base branch
from
ZiadAbdElFatah:fix-for-issue-10371
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
73d5696
Added unit test for parsing JSON
ZiadAbdElFatah 334ac5e
Merge branch 'JabRef:main' into fix-for-issue-10371
ZiadAbdElFatah 20d9f79
Merge branch 'main' into fix-for-issue-10371
ZiadAbdElFatah 5ef0cab
Added reading and writing using JSON for save actions
ZiadAbdElFatah e8cd953
Merge branch 'main' into fix-for-issue-10371
ZiadAbdElFatah 01abb06
Merge branch 'JabRef:main' into fix-for-issue-10371
ZiadAbdElFatah 55ed8c4
Merge branch 'main' into fix-for-issue-10371
ZiadAbdElFatah 664bd70
Used the """ for the test strings
ZiadAbdElFatah 1723416
Refactored the architecture to DTO and added converters
ZiadAbdElFatah 89c9eb3
Solved failing check
ZiadAbdElFatah 7b1c83a
Solved failing check
ZiadAbdElFatah 7edd3a8
Merge branch 'main' into fix-for-issue-10371
ZiadAbdElFatah 8a223ae
Chore(deps): Bump org.libreoffice:libreoffice in /versions (#15450)
dependabot[bot] 33bff31
Added the ADR annotation back
ZiadAbdElFatah 3394f31
Merge branch 'main' into fix-for-issue-10371
ZiadAbdElFatah ddc5d0d
Merge branch 'main' into fix-for-issue-10371
calixtus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
jablib/src/main/java/org/jabref/logic/cleanup/SaveActionsConverter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package org.jabref.logic.cleanup; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.StringJoiner; | ||
|
|
||
| import org.jabref.model.metadata.SaveActionsDTO; | ||
|
|
||
| public class SaveActionsConverter { | ||
| public static FieldFormatterCleanupActions fromDTO(SaveActionsDTO saveActionsDTO) { | ||
| boolean enabled = saveActionsDTO.state; | ||
| StringBuilder actionsStringBuilder = new StringBuilder(); | ||
| for (Map.Entry<String, List<String>> entry : saveActionsDTO.actions.entrySet()) { | ||
| StringJoiner joiner = new StringJoiner(","); | ||
| for (String formatter : entry.getValue()) { | ||
| joiner.add(formatter); | ||
| } | ||
| actionsStringBuilder.append(entry.getKey()) | ||
| .append("[") | ||
| .append(joiner) | ||
| .append(']'); | ||
| } | ||
| List<FieldFormatterCleanup> actions = FieldFormatterCleanupMapper.parseActions(actionsStringBuilder.toString()); | ||
| return new FieldFormatterCleanupActions(enabled, actions); | ||
| } | ||
|
|
||
| public static SaveActionsDTO toDTO(FieldFormatterCleanupActions saveActions) { | ||
| SaveActionsDTO saveActionsDTO = new SaveActionsDTO(); | ||
| saveActionsDTO.state = saveActions.isEnabled(); | ||
| for (FieldFormatterCleanup action : saveActions.getConfiguredActions()) { | ||
| String field = action.getField().getName(); | ||
| String formatter = action.getFormatter().getKey(); | ||
| saveActionsDTO.actions | ||
| .computeIfAbsent(field, _ -> new ArrayList<>()) | ||
| .add(formatter); | ||
| } | ||
| return saveActionsDTO; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,8 @@ | |
| import javax.xml.parsers.ParserConfigurationException; | ||
|
|
||
| import org.jabref.logic.bibtex.FieldWriter; | ||
| import org.jabref.logic.cleanup.FieldFormatterCleanupActions; | ||
| import org.jabref.logic.cleanup.SaveActionsConverter; | ||
| import org.jabref.logic.exporter.BibDatabaseWriter; | ||
| import org.jabref.logic.exporter.SaveConfiguration; | ||
| import org.jabref.logic.groups.GroupsFactory; | ||
|
|
@@ -35,6 +37,7 @@ | |
| import org.jabref.logic.importer.Parser; | ||
| import org.jabref.logic.importer.ParserResult; | ||
| import org.jabref.logic.importer.util.MetaDataParser; | ||
| import org.jabref.logic.importer.util.SaveActionsDTOConverter; | ||
| import org.jabref.logic.l10n.Localization; | ||
| import org.jabref.logic.os.OS; | ||
| import org.jabref.model.database.BibDatabase; | ||
|
|
@@ -53,13 +56,16 @@ | |
| import org.jabref.model.groups.GroupHierarchyType; | ||
| import org.jabref.model.groups.GroupTreeNode; | ||
| import org.jabref.model.metadata.MetaData; | ||
| import org.jabref.model.metadata.SaveActionsDTO; | ||
| import org.jabref.model.util.DummyFileUpdateMonitor; | ||
| import org.jabref.model.util.FileUpdateMonitor; | ||
|
|
||
| import com.dd.plist.BinaryPropertyListParser; | ||
| import com.dd.plist.NSArray; | ||
| import com.dd.plist.NSDictionary; | ||
| import com.dd.plist.NSString; | ||
| import com.google.gson.Gson; | ||
| import com.google.gson.JsonObject; | ||
| import io.github.adr.linked.ADR; | ||
| import org.jspecify.annotations.NonNull; | ||
| import org.slf4j.Logger; | ||
|
|
@@ -113,6 +119,7 @@ public class BibtexParser implements Parser { | |
|
|
||
| private ParserResult parserResult; | ||
| private final MetaDataParser metaDataParser; | ||
| private Optional<JsonObject> parsedJsonMetaData = Optional.empty(); | ||
| private final Map<String, String> parsedBibDeskGroups; | ||
|
|
||
| private GroupTreeNode bibDeskGroupTreeNode; | ||
|
|
@@ -295,6 +302,14 @@ private ParserResult parseFileContent() throws IOException { | |
| ); | ||
| } | ||
| parserResult.setMetaData(metaData); | ||
|
|
||
| parsedJsonMetaData.ifPresent(json -> { | ||
| if (json.has(MetaData.SAVE_ACTIONS)) { | ||
| SaveActionsDTO saveActionsDTO = SaveActionsDTOConverter.fromJson(json.getAsJsonObject(MetaData.SAVE_ACTIONS)); | ||
| FieldFormatterCleanupActions saveActions = SaveActionsConverter.fromDTO(saveActionsDTO); | ||
| metaData.setSaveActions(saveActions); | ||
| } | ||
| }); | ||
| } catch (ParseException exception) { | ||
| parserResult.addException(new ParserResult.Range(startLine, startColumn, line, column), exception); | ||
| } | ||
|
|
@@ -401,9 +416,17 @@ private void parseJabRefComment(Map<String, String> meta) { | |
| } catch (ParseException ex) { | ||
| parserResult.addException(new ParserResult.Range(startLine, startColumn, line, column), ex); | ||
| } | ||
| } else if (comment.startsWith(MetaData.META_FLAG_V1)) { | ||
| parsedJsonMetaData = parseCommentToJson(comment); | ||
| } | ||
| } | ||
|
|
||
| Optional<JsonObject> parseCommentToJson(String comment) { | ||
| Gson gson = new Gson(); | ||
| String content = comment.substring(MetaData.META_FLAG_V1.length()); | ||
| return Optional.ofNullable(gson.fromJson(content, JsonObject.class)); | ||
| } | ||
|
Comment on lines
+424
to
+428
Contributor
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. 1. parsecommenttojson uncaught jsonsyntaxexception JSON metadata parsing uses gson.fromJson(...) without handling malformed JSON, which can throw a runtime exception and abort import. This violates the requirement for robust, contextual error handling during parsing of external file content. Agent Prompt
|
||
|
|
||
| /// Adds BibDesk group entries to the JabRef database | ||
| private void addBibDeskGroupEntriesToJabRefGroups() { | ||
| for (String groupName : parsedBibDeskGroups.keySet()) { | ||
|
|
||
43 changes: 43 additions & 0 deletions
43
jablib/src/main/java/org/jabref/logic/importer/util/SaveActionsDTOConverter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package org.jabref.logic.importer.util; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import org.jabref.model.metadata.SaveActionsDTO; | ||
|
|
||
| import com.google.gson.JsonArray; | ||
| import com.google.gson.JsonElement; | ||
| import com.google.gson.JsonObject; | ||
|
|
||
| public class SaveActionsDTOConverter { | ||
| public static SaveActionsDTO fromJson(JsonObject saveActionsJson) { | ||
| SaveActionsDTO saveActionsDTO = new SaveActionsDTO(); | ||
| saveActionsDTO.state = saveActionsJson.get("state").getAsBoolean(); | ||
| for (Map.Entry<String, JsonElement> entry : saveActionsJson.entrySet()) { | ||
| // Already parsed before | ||
| if ("state".equals(entry.getKey())) { | ||
| continue; | ||
| } | ||
| List<String> actions = new ArrayList<>(); | ||
| for (JsonElement action : entry.getValue().getAsJsonArray()) { | ||
| actions.add(action.getAsString()); | ||
| } | ||
| saveActionsDTO.actions.put(entry.getKey(), actions); | ||
| } | ||
| return saveActionsDTO; | ||
| } | ||
|
|
||
| public static JsonObject toJson(SaveActionsDTO saveActionsDTO) { | ||
| JsonObject saveActionsJson = new JsonObject(); | ||
| saveActionsJson.addProperty("state", saveActionsDTO.state); | ||
| for (Map.Entry<String, List<String>> entry : saveActionsDTO.actions.entrySet()) { | ||
| JsonArray formatters = new JsonArray(); | ||
| for (String formatter : entry.getValue()) { | ||
| formatters.add(formatter); | ||
| } | ||
| saveActionsJson.add(entry.getKey(), formatters); | ||
| } | ||
| return saveActionsJson; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
jablib/src/main/java/org/jabref/model/metadata/SaveActionsDTO.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package org.jabref.model.metadata; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class SaveActionsDTO { | ||
| public boolean state = false; | ||
| public Map<String, List<String>> actions = new HashMap<>(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
2. Json meta duplicated on save
🐞 Bug✓ CorrectnessAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools