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 |
tobiasdiez
left a comment
There was a problem hiding this comment.
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.
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
to an instance variable which is initialized in the constructor. Then you can just use
clipBoardManager = new ClipBoardManager(clipboard, importFormatReader).
| 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.
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.
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.
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. |
tobiasdiez
left a comment
There was a problem hiding this comment.
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. |
lenhard
left a comment
There was a problem hiding this comment.
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 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.