Skip to content

Commit

Permalink
fix #5043: preserve encoding while copy and pasting in maintable
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-kolb committed Jun 12, 2019
1 parent eb42850 commit e5b190d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 37 deletions.
86 changes: 50 additions & 36 deletions src/main/java/org/jabref/gui/ClipBoardManager.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -28,13 +30,11 @@
import org.slf4j.LoggerFactory;

public class ClipBoardManager {
private static final Logger LOGGER = LoggerFactory.getLogger(ClipBoardManager.class);

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() {
Expand Down Expand Up @@ -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) {
Expand All @@ -89,40 +88,55 @@ public void setContent(List<BibEntry> entries) throws IOException {
clipboard.setContent(content);
}

public List<BibEntry> extractEntries() {
public List<BibEntry> extractData() {
Object entries = clipboard.getContent(DragAndDropDataFormats.ENTRIES);

if (entries == null) {
return handleStringData(clipboard.getString());
}
return handleBibTeXData((String) entries);
}

private List<BibEntry> 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 = DOI.parse(data);
if (doi.isPresent()) {
LOGGER.info("Found DOI in clipboard");
Optional<BibEntry> 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<BibEntry> handleStringData(String data) {
if (data == null || data.isEmpty()) {
return Collections.emptyList();
}

Optional<DOI> doi = DOI.parse(data);
if (doi.isPresent()) {
return fetchByDOI(doi.get());
}

return tryImportFormats(data);
}

private List<BibEntry> tryImportFormats(String data) {
try {
UnknownFormatImport unknownFormatImport = importFormatReader.importUnknownFormat(data);
return unknownFormatImport.parserResult.getDatabase().getEntries();
} catch (ImportException ignored) {
return Collections.emptyList();
}
}

private List<BibEntry> fetchByDOI(DOI doi) {
LOGGER.info("Found DOI in clipboard");
try {
Optional<BibEntry> 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();
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/maintable/MainTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ private void clearAndSelectLast() {

public void paste() {
// Find entries in clipboard
List<BibEntry> entriesToAdd = Globals.clipboardManager.extractEntries();
List<BibEntry> entriesToAdd = Globals.clipboardManager.extractData();

if (!entriesToAdd.isEmpty()) {
// Add new entries
Expand Down

0 comments on commit e5b190d

Please sign in to comment.