jabref icon indicating copy to clipboard operation
jabref copied to clipboard

Improve KeywordList parsing to support escaped characters and nested structures

Open krishnagjsForGit opened this issue 8 months ago • 20 comments

Closes #12810

What I changed Implemented escape handling in KeywordList.parse(String, Character, Character) to support escaping the keyword separator using backslash (). This prevents incorrect splitting of keywords when delimiters appear within the keyword itself.

Refactored the parsing logic for clarity, including renaming loop variables for better readability and intent.

Added test cases in KeywordListTest to cover escape scenarios such as:

Escaped delimiter: "one\,two" → ["one,two"]

Escaped backslash: "one\\two" → ["one\two"]

Mixed escaped and unescaped delimiters.

Where the changes are KeywordList.java: Modified the parse method to include escape handling logic using a character-by-character loop.

KeywordListTest.java: Added JUnit test cases to ensure parsing behaves correctly with escaped delimiters and backslashes.

Why I made these changes Fixes issue #12810: Current parsing breaks when delimiters appear inside keywords (e.g., in MeSH terms). There was no way to escape the delimiter character, leading to incorrect keyword splitting.

Improves consistency: Provides a more robust, predictable behavior for keyword parsing, especially when users or importers use delimiters in keyword values.

Next Steps Awaiting review and feedback.

If the community prefers a different escape character or behavior, I’m happy to adjust the implementation.

After this is merged, similar logic could be extracted or reused where keyword parsing happens elsewhere (e.g., importers).

Mandatory checks

  • [x] I own the copyright of the code submitted and I license it under the MIT license
  • [/] Change in CHANGELOG.md described in a way that is understandable for the average user (if change is visible to the user)
  • [x] Tests created for changes (if applicable)
  • [x] Manually tested changed features in running JabRef (always required)
  • [/] Screenshots added in PR description (if change is visible to the user)
  • [/] Checked developer's documentation: Is the information available and up to date? If not, I outlined it in this pull request.
  • [x] Checked documentation: Is the information available and up to date? If not, I created an issue at https://github.com/JabRef/user-documentation/issues or, even better, I submitted a pull request to the documentation repository.

krishnagjsForGit avatar Apr 06 '25 03:04 krishnagjsForGit

can someone help me on this. is this a valid comment from Bot ? I have never used DisplayName annotation and I changed method name to be more comprehensive. Am I missing anything here ?

krishnagjsForGit avatar Apr 06 '25 04:04 krishnagjsForGit

Seems like a false positive from the bot @koppor

Siedlerchr avatar Apr 06 '25 08:04 Siedlerchr

@krishnagjsForGit please change the PR title to contain text summarizing the fix.

koppor avatar Apr 06 '25 08:04 koppor

@krishnagjsForGit please change the PR title to contain text summarizing the fix.

Done. is this fine @koppor ?

krishnagjsForGit avatar Apr 06 '25 10:04 krishnagjsForGit

I'm sorry for not getting back to you sooner. I have two suggestions:

  1. ANTLR probably won't help in this use case, since the delimiters are dynamically specified
  2. I am concerned regarding the Unicode handling of the code, since Java char is finicky, and potentially you want to use codepoints instead of .charAt
  3. One way to address 2 and increase readability without necessarily using ANTLR is to dynamically construct a Regular Expression with a lookbehind and use only .split and .replace. Take a look at this: https://regex101.com/r/O1kLWF/1

Yubo-Cao avatar Apr 20 '25 20:04 Yubo-Cao

I'm sorry for not getting back to you sooner. I have two suggestions:

  1. ANTLR probably won't help in this use case, since the delimiters are dynamically specified
  2. I am concerned regarding the Unicode handling of the code, since Java char is finicky, and potentially you want to use codepoints instead of .charAt
  3. One way to address 2 and increase readability without necessarily using ANTLR is to dynamically construct a Regular Expression with a lookbehind and use only .split and .replace. Take a look at this: https://regex101.com/r/O1kLWF/1

@koppor you want me to change the code accordingly ?

krishnagjsForGit avatar May 01 '25 06:05 krishnagjsForGit

I think the current code is good.

Is a result of current JabRefs way of parsing. If it was done "correctly", the Bib parser would do the escape handling - and all other data structures would use tokens. But this is much much effort to change.

koppor avatar May 01 '25 07:05 koppor

@krishnagjsForGit please add a CHANGELOG.md entry

koppor avatar May 01 '25 07:05 koppor

@ungerts thank you for helping with reviews!

subhramit avatar May 02 '25 09:05 subhramit

@krishnagjsForGit please add a CHANGELOG.md entry

Can you please check if I updated correctly ?

krishnagjsForGit avatar May 02 '25 17:05 krishnagjsForGit

Seems like it was not a clean merge. @koppor may be able to help with some git trick Otherwise, you'll have to git reset and push a fresh commit.

subhramit avatar May 02 '25 17:05 subhramit

How did you merge, by the way? Did you do git pull upstream main?

subhramit avatar May 02 '25 17:05 subhramit

Seems like it was not a clean merge. @koppor may be able to help with some git trick Otherwise, you'll have to git reset and push a fresh commit.

Just checking the output of GitHub here, I also suggest git reset origin/main after a git merge origin/main. - A force push afterwards is OK. Please use gitk --all& as a double check - and run this command before any action and leave the window open. You can press F5 to refresh.

koppor avatar May 02 '25 22:05 koppor

Alternatively, one could work with git cherry-pick on a new branch - and just cherry-pick the "good" commits.

koppor avatar May 02 '25 22:05 koppor

Something went amiss and PR got closed. Can I create a separate Clean PR and fix this ? @koppor

krishnagjsForGit avatar May 03 '25 03:05 krishnagjsForGit

Something went amiss and PR got closed. Can I create a separate Clean PR and fix this ? @koppor

Yes, please.

koppor avatar May 03 '25 07:05 koppor

I restored the state as of April, 16th - commit 081b9dc1d3. Mayb,e this helps.

If you do a git fetch origin and then git reset --hard origin/fix-for-issue-12810 you should be on the same page.

Attention: The command git reset --hard .... throws away all your work you did inbetween. Not sure if you did something...

koppor avatar May 09 '25 21:05 koppor

@trag-bot didn't find any issues in the code! ✅✨

trag-bot[bot] avatar Jul 07 '25 21:07 trag-bot[bot]

I saw no activtiy since 2 months, thus closing this and making it free for another contributor.

koppor avatar Jul 07 '25 21:07 koppor

Your code currently does not meet JabRef's code guidelines. We use Checkstyle to identify issues. You can see which checks are failing by locating the box "Some checks were not successful" on the pull request page. To see the test output, locate "Tests / Checkstyle (pull_request)" and click on it.

In case of issues with the import order, double check that you activated Auto Import. You can trigger fixing imports by pressing Ctrl+Alt+O to trigger Optimize Imports.

Please carefully follow the setup guide for the codestyle. Afterwards, please run checkstyle locally and fix the issues, commit, and push.

jabref-machine avatar Jul 07 '25 21:07 jabref-machine