-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Fix for issue 3143: Import entry from clipboard in different formats #3243
Conversation
Hi @125m125, as I see you have some troubles with our checkstyle rules. I recommend you to use our IDE config files from https://github.com/JabRef/jabref/tree/master/config. Which IDE are you using? We recommend IntelliJ IDEA or Eclipse. If you have any questions, feel free to ask them at slack: https://jabref.slack.com |
I am using Eclipse and just had to format a file. Because the saveAction is set to only reformat edited lines a missing newline in the imports wasn't corrected. Also thanks for pointing out gradle check. I somehow missed that command. |
@125m125 If you use eclipse, just run |
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.
Thank you for your contribution!
The code looks fine to me for the majority. Especially, I really like the new added tests. Nonetheless, I have a few remarks that I would like to see fixed before your PR is merged.
|
||
clipBoardManager = new ClipBoardManager(importFormatReader); | ||
Clipboard clipboard = mock(Clipboard.class); | ||
Field field = ClipBoardManager.class.getDeclaredField("CLIPBOARD"); |
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.
I'm a big fan of these tests! The only thing that I don't like is this hack to set CLIPBOARD
. Instead I propose to convert
private static final Clipboard CLIPBOARD = Toolkit.getDefaultToolkit().getSystemClipboard(); |
to an instance variable which is initialized in the constructor. Then you can just use
clipBoardManager = new ClipBoardManager(clipboard, importFormatReader)
.
@@ -215,4 +218,39 @@ public UnknownFormatImport importUnknownFormat(Path filePath) throws ImportExcep | |||
|
|||
throw new ImportException(Localization.lang("Could not find a suitable import format.")); | |||
} | |||
|
|||
public UnknownFormatImport importUnknownFormatFromString(String data) throws ImportException { |
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.
As far as I can see, this is mostly a copy of importUnknownFormat(Path filePath)
and I would like to have a solution without code duplication. Maybe something along the following lines works?
private UnknownFormatImport importUnknownFormat(Function<Importer, ParserResult> importDatabase, Function<Importer, Boolean> isRecognizedFormat) {
// original method with obvious changes imFo.importDatabase -> importDatabase.apply(imFo)
}
public UnknownFormatImport importUnknownFormat(Path filePath) {
return importUnknownFormat(importer -> importer.importDatabase(filePath, importFormatPreferences.getEncoding(), importer -> importer .isRecognizedFormat(filePath, importFormatPreferences.getEncoding());
}
public UnknownFormatImport importUnknownFormat(String data) {
return importUnknownFormat(importer -> importer.importDatabase(data), importer -> importer .isRecognizedFormat(data));
}
It's a bit ugly. Do you have a better idea to reduce the code duplication?
// Cycle through all importers: | ||
for (Importer imFo : getImportFormats()) { | ||
try (StringReader in = new StringReader(data); BufferedReader input = new BufferedReader(in)) { | ||
ParserResult parserResult = imFo.importDatabase(input); |
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.
Can you please extract the method Importer.importDatabase(String data)
. This new method can be reused at least in the MrDLibFetcher
. Similarly, for the isRecognizedFormat
.
assertEquals(count, reader.importUnknownFormatFromString(data).parserResult.getDatabase().getEntries().size()); | ||
} catch (ImportException e) { | ||
// msbib test file does not contain an author | ||
if ("msbib".equals(format)) { |
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.
Why is this special treatment necessary for the import from a string, but not when the same content is imported from a file?
Thanks for the feedback. I integrated the requests with the last commit. |
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.
Thanks for the quick follow-up! The code looks very good now. I give my +1 for merge (although I have to admit that I've not tested the new functionality).
I think, however, that we should wait with merging it until 4.0 is released.
Ok, then I'll add this to the 4.1 milestone. |
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.
The code looks fine. The tests are especially nice!
I tested this PR in a running JabRef with the example linked in #3143. The pasting works, but I always get this message in the log: [Fatal Error] :1:1: Content is not allowed in prolog.
I am not 100% sure where it comes from, can you please investigate? Also, is there a way of avoiding it, after all the pasting works regardless?
I think the error message comes from jabref/src/main/java/org/jabref/logic/importer/fileformat/MrDLibImporter.java Lines 53 to 60 in ae6d01b
In my opinion, you can just delete these Logger.log statements (since its quite expected that a file in a wrong format is passed to isRecognizedFormat and this importer is not used outside of internal code anyways).
|
* upstream/master: (113 commits) Open statistics dialog from correct thread (#3272) Fix for issue 2811: bibtexkey generator does not use crossref information (#3248) Fix for issue 3143: Import entry from clipboard in different formats (#3243) French translation correction (#3262) Wait to ask to collect anonymous statistics in JabRefExecutorService to allow jvm to terminate (#3266) Directory pattern bracketed expressions (#3238) Show development information Release v4.0 add another author to mailmap moved changelog entry to the right category update new AUTHORS info Update log4j from 2.9.0 -> 2.9.1 fix dblp fetcher Add missing Turkish translation Add "-console" parameter for Windows launcher (#3242) Path check converted to if statement Changelog updated Fixed renaming files which are not in main directory. Only use last name for auto completion in search bar. Fixes JabRef#253 Implemented issue #3229 (#3233) ...
see #3143
Use all importers to parse pasted entries. This allows the user to paste entries in different formats and not ony BibTex.