From 9ef9760bd8687bc1491b1ec985a9415219921532 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Wed, 5 Apr 2017 20:43:24 +0200 Subject: [PATCH] Export linked files Don't replace existings --- .../jabref/logic/exporter/ExportFormats.java | 2 +- ...eExporter.java => LinkedFileExporter.java} | 27 +++++++++++++------ .../org/jabref/logic/util/io/FileUtil.java | 8 +++--- 3 files changed, 25 insertions(+), 12 deletions(-) rename src/main/java/org/jabref/logic/exporter/{PdfFileExporter.java => LinkedFileExporter.java} (51%) diff --git a/src/main/java/org/jabref/logic/exporter/ExportFormats.java b/src/main/java/org/jabref/logic/exporter/ExportFormats.java index 841ed37d47b..b3591d1bfef 100644 --- a/src/main/java/org/jabref/logic/exporter/ExportFormats.java +++ b/src/main/java/org/jabref/logic/exporter/ExportFormats.java @@ -61,7 +61,7 @@ public static void initAllExports(Map customFormats, ExportFormats.putFormat(new OpenDocumentSpreadsheetCreator()); ExportFormats.putFormat(new MSBibExportFormat()); ExportFormats.putFormat(new ModsExportFormat()); - ExportFormats.putFormat(new PdfFileExporter()); + ExportFormats.putFormat(new LinkedFileExporter()); // Now add custom export formats for (IExportFormat format : customFormats.values()) { diff --git a/src/main/java/org/jabref/logic/exporter/PdfFileExporter.java b/src/main/java/org/jabref/logic/exporter/LinkedFileExporter.java similarity index 51% rename from src/main/java/org/jabref/logic/exporter/PdfFileExporter.java rename to src/main/java/org/jabref/logic/exporter/LinkedFileExporter.java index fd29db4691d..a4e39ea7808 100644 --- a/src/main/java/org/jabref/logic/exporter/PdfFileExporter.java +++ b/src/main/java/org/jabref/logic/exporter/LinkedFileExporter.java @@ -1,17 +1,26 @@ package org.jabref.logic.exporter; +import java.io.File; import java.nio.charset.Charset; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.List; +import java.util.Optional; + +import org.jabref.Globals; import org.jabref.logic.TypedBibEntry; +import org.jabref.logic.util.io.FileUtil; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.ParsedFileField; -public class PdfFileExporter extends ExportFormat { +public class LinkedFileExporter extends ExportFormat { + + public LinkedFileExporter() { + super("Files Exporter", "Files", null, null, ".*"); - public PdfFileExporter() { - super("PDF files", "PDF", null, null, ".pdf"); } + @Override public void performExport(final BibDatabaseContext databaseContext, String file, final Charset encoding, List entries) throws Exception { @@ -23,12 +32,14 @@ public void performExport(final BibDatabaseContext databaseContext, String file, for (ParsedFileField fileEntry : files) { String fileName = fileEntry.getLink(); - // databaseContext.getFileDirectories(preferences) - // Optional oldFile = FileUtil.expandFilename(fileName, - // databaseContext.getFileDirectories(Globals.prefs.getFileDirectoryPreferences())); + Optional fileToExport = FileUtil.expandFilename(fileName, + databaseContext.getFileDirectories(Globals.prefs.getFileDirectoryPreferences())) + .map(File::toPath); - System.out.println("Export pdfs"); - // FileUtil.copyFile(oldFile.get().toPath(), file, false); + fileToExport.ifPresent(f -> { + Path newFilePath = Paths.get(file.replace(".*", "")).getParent().resolve(f.getFileName()); + FileUtil.copyFile(f, newFilePath, false); + }); } diff --git a/src/main/java/org/jabref/logic/util/io/FileUtil.java b/src/main/java/org/jabref/logic/util/io/FileUtil.java index 2fdda8cb1c6..c1718ff164d 100644 --- a/src/main/java/org/jabref/logic/util/io/FileUtil.java +++ b/src/main/java/org/jabref/logic/util/io/FileUtil.java @@ -37,12 +37,14 @@ import org.apache.commons.logging.LogFactory; public class FileUtil { + private static final Log LOGGER = LogFactory.getLog(FileUtil.class); private static final Pattern SLASH = Pattern.compile("/"); private static final Pattern BACKSLASH = Pattern.compile("\\\\"); - public static final boolean isPosixCompilant = FileSystems.getDefault().supportedFileAttributeViews().contains("posix"); + public static final boolean isPosixCompilant = FileSystems.getDefault().supportedFileAttributeViews() + .contains("posix"); private FileUtil() { } @@ -154,7 +156,7 @@ public static boolean copyFile(Path pathToSourceFile, Path pathToDestinationFile return false; } if (Files.exists(pathToDestinationFile) && !replaceExisting) { - LOGGER.error("Path to the destination file is not exists and the file shouldn't be replace."); + LOGGER.error("Path to the destination file exists and the file shouldn't be replaced."); return false; } try { @@ -460,7 +462,7 @@ public static Optional find(String filename, Path rootDirectory) { .filter(Files::isRegularFile) .filter(f -> f.getFileName().toString().equals(filename)) .findFirst(); - } catch(IOException ex) { + } catch (IOException ex) { LOGGER.error("Error trying to locate the file " + filename + " inside the directory " + rootDirectory); } return Optional.empty();