Skip to content

Commit

Permalink
Export linked files
Browse files Browse the repository at this point in the history
Don't replace existings
  • Loading branch information
Siedlerchr committed Apr 5, 2017
1 parent c9eeefd commit 9ef9760
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/logic/exporter/ExportFormats.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static void initAllExports(Map<String, ExportFormat> 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()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<BibEntry> entries) throws Exception {
Expand All @@ -23,12 +32,14 @@ public void performExport(final BibDatabaseContext databaseContext, String file,
for (ParsedFileField fileEntry : files) {
String fileName = fileEntry.getLink();

// databaseContext.getFileDirectories(preferences)
// Optional<File> oldFile = FileUtil.expandFilename(fileName,
// databaseContext.getFileDirectories(Globals.prefs.getFileDirectoryPreferences()));
Optional<Path> 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);
});

}

Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/jabref/logic/util/io/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -460,7 +462,7 @@ public static Optional<Path> 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();
Expand Down

0 comments on commit 9ef9760

Please sign in to comment.