diff --git a/src/main/java/org/jabref/gui/ClipBoardManager.java b/src/main/java/org/jabref/gui/ClipBoardManager.java index c7c6ce2b9d4..26bc331fbb4 100644 --- a/src/main/java/org/jabref/gui/ClipBoardManager.java +++ b/src/main/java/org/jabref/gui/ClipBoardManager.java @@ -1,6 +1,8 @@ package org.jabref.gui; +import java.io.ByteArrayInputStream; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.List; import java.util.Optional; @@ -28,13 +30,11 @@ import org.slf4j.LoggerFactory; public class ClipBoardManager { - public static final DataFormat XML = new DataFormat("application/xml"); - + private static final Logger LOGGER = LoggerFactory.getLogger(ClipBoardManager.class); private final Clipboard clipboard; - private final ImportFormatReader importFormatReader; public ClipBoardManager() { @@ -63,9 +63,8 @@ public String getContents() { String result = clipboard.getString(); if (result == null) { return ""; - } else { - return result; } + return result; } public void setHtmlContent(String html) { @@ -89,40 +88,55 @@ public void setContent(List entries) throws IOException { clipboard.setContent(content); } - public List extractEntries() { + public List extractData() { Object entries = clipboard.getContent(DragAndDropDataFormats.ENTRIES); + if (entries == null) { + return handleStringData(clipboard.getString()); + } + return handleBibTeXData((String) entries); + } + + private List handleBibTeXData(String entries) { BibtexParser parser = new BibtexParser(Globals.prefs.getImportFormatPreferences(), Globals.getFileUpdateMonitor()); - if (entries != null) { - // We have determined that the clipboard data is a set of entries (serialized as a string). - try { - return parser.parseEntries((String) entries); - } catch (ParseException ex) { - LOGGER.error("Could not paste", ex); - } - } else { - String data = clipboard.getString(); - if (data != null) { - try { - // fetch from doi - Optional doi = DOI.parse(data); - if (doi.isPresent()) { - LOGGER.info("Found DOI in clipboard"); - Optional entry = new DoiFetcher(Globals.prefs.getImportFormatPreferences()).performSearchById(doi.get().getDOI()); - return OptionalUtil.toList(entry); - } else { - try { - UnknownFormatImport unknownFormatImport = importFormatReader.importUnknownFormat(data); - return unknownFormatImport.parserResult.getDatabase().getEntries(); - } catch (ImportException e) { - // import failed and result will be empty - } - } - } catch (FetcherException ex) { - LOGGER.error("Error while fetching", ex); - } - } + try { + return parser.parseEntries(new ByteArrayInputStream(entries.getBytes(StandardCharsets.UTF_8))); + } catch (ParseException ex) { + LOGGER.error("Could not paste", ex); + return Collections.emptyList(); + } + } + + private List handleStringData(String data) { + if (data == null || data.isEmpty()) { + return Collections.emptyList(); + } + + Optional doi = DOI.parse(data); + if (doi.isPresent()) { + return fetchByDOI(doi.get()); + } + + return tryImportFormats(data); + } + + private List tryImportFormats(String data) { + try { + UnknownFormatImport unknownFormatImport = importFormatReader.importUnknownFormat(data); + return unknownFormatImport.parserResult.getDatabase().getEntries(); + } catch (ImportException ignored) { + return Collections.emptyList(); + } + } + + private List fetchByDOI(DOI doi) { + LOGGER.info("Found DOI in clipboard"); + try { + Optional entry = new DoiFetcher(Globals.prefs.getImportFormatPreferences()).performSearchById(doi.getDOI()); + return OptionalUtil.toList(entry); + } catch (FetcherException ex) { + LOGGER.error("Error while fetching", ex); + return Collections.emptyList(); } - return Collections.emptyList(); } } diff --git a/src/main/java/org/jabref/gui/maintable/MainTable.java b/src/main/java/org/jabref/gui/maintable/MainTable.java index 892116fb337..6232d33a961 100644 --- a/src/main/java/org/jabref/gui/maintable/MainTable.java +++ b/src/main/java/org/jabref/gui/maintable/MainTable.java @@ -209,7 +209,7 @@ private void clearAndSelectLast() { public void paste() { // Find entries in clipboard - List entriesToAdd = Globals.clipboardManager.extractEntries(); + List entriesToAdd = Globals.clipboardManager.extractData(); if (!entriesToAdd.isEmpty()) { // Add new entries