Skip to content
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

Merged
merged 8 commits into from
Oct 6, 2017

Conversation

125m125
Copy link
Contributor

@125m125 125m125 commented Oct 1, 2017

see #3143
Use all importers to parse pasted entries. This allows the user to paste entries in different formats and not ony BibTex.

  • Change in CHANGELOG.md described
  • Tests created for changes
  • Manually tested changed features in running JabRef
  • Check documentation status (Issue created for outdated help page at help.jabref.org?)

@125m125 125m125 changed the title Fix for issue 3143: Import entry from clipboard in different formats [WIP]Fix for issue 3143: Import entry from clipboard in different formats Oct 1, 2017
@LinusDietz
Copy link
Member

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.
You can see if the tests a pass by calling gradle check. If you only want to check for the code style rules, you can call gradle checkstyleMain.

If you have any questions, feel free to ask them at slack: https://jabref.slack.com

@125m125
Copy link
Contributor Author

125m125 commented Oct 1, 2017

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 125m125 changed the title [WIP]Fix for issue 3143: Import entry from clipboard in different formats Fix for issue 3143: Import entry from clipboard in different formats Oct 1, 2017
@LinusDietz LinusDietz added component: import-load status: ready-for-review Pull Requests that are ready to be reviewed by the maintainers labels Oct 1, 2017
@Siedlerchr
Copy link
Member

@125m125 If you use eclipse, just rungradlew eclipseto import the correct code formatting rules

Copy link
Member

@tobiasdiez tobiasdiez left a 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");
Copy link
Member

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 {
Copy link
Member

@tobiasdiez tobiasdiez Oct 1, 2017

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);
Copy link
Member

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)) {
Copy link
Member

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?

@125m125
Copy link
Contributor Author

125m125 commented Oct 2, 2017

Thanks for the feedback. I integrated the requests with the last commit.
I also changed the PdfXmpImporter to return false when checking the format instead of throwing an exception to prevent the need for special treatment and make it consistent with the PdfContentImporter.

Copy link
Member

@tobiasdiez tobiasdiez left a 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.

@lenhard
Copy link
Member

lenhard commented Oct 3, 2017

Ok, then I'll add this to the 4.1 milestone.

@lenhard lenhard added this to the v4.1 milestone Oct 3, 2017
Copy link
Member

@lenhard lenhard left a 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?

@tobiasdiez
Copy link
Member

I think the error message comes from

try (InputStream stream = new ByteArrayInputStream(recommendationsAsString.getBytes())) {
saxParser.parse(stream, handler);
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);
return false;
}
} catch (ParserConfigurationException | SAXException e) {
LOGGER.error(e.getMessage(), e);

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).

@tobiasdiez tobiasdiez merged commit 5544007 into JabRef:master Oct 6, 2017
Siedlerchr added a commit that referenced this pull request Oct 7, 2017
* 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)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: import-load status: ready-for-review Pull Requests that are ready to be reviewed by the maintainers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants