Search in sources :

Example 6 with LinkedFile

use of org.jabref.model.entry.LinkedFile in project jabref by JabRef.

the class UpgradePdfPsToFileCleanup method cleanup.

@Override
public List<FieldChange> cleanup(BibEntry entry) {
    List<FieldChange> changes = new ArrayList<>();
    // If there are already links in the file field, keep those on top:
    String oldFileContent = entry.getField(FieldName.FILE).orElse(null);
    List<LinkedFile> fileList = new ArrayList<>(entry.getFiles());
    int oldItemCount = fileList.size();
    for (Map.Entry<String, String> field : fields.entrySet()) {
        entry.getField(field.getKey()).ifPresent(o -> {
            if (o.trim().isEmpty()) {
                return;
            }
            File f = new File(o);
            LinkedFile flEntry = new LinkedFile(f.getName(), o, field.getValue());
            fileList.add(flEntry);
            entry.clearField(field.getKey());
            changes.add(new FieldChange(entry, field.getKey(), o, null));
        });
    }
    if (fileList.size() != oldItemCount) {
        String newValue = FileFieldWriter.getStringRepresentation(fileList);
        entry.setField(FieldName.FILE, newValue);
        changes.add(new FieldChange(entry, FieldName.FILE, oldFileContent, newValue));
    }
    return changes;
}
Also used : LinkedFile(org.jabref.model.entry.LinkedFile) ArrayList(java.util.ArrayList) FieldChange(org.jabref.model.FieldChange) Map(java.util.Map) HashMap(java.util.HashMap) File(java.io.File) LinkedFile(org.jabref.model.entry.LinkedFile)

Example 7 with LinkedFile

use of org.jabref.model.entry.LinkedFile in project jabref by JabRef.

the class FileLinksCleanup method cleanup.

@Override
public List<FieldChange> cleanup(BibEntry entry) {
    Optional<String> oldValue = entry.getField(FieldName.FILE);
    if (!oldValue.isPresent()) {
        return Collections.emptyList();
    }
    List<LinkedFile> fileList = entry.getFiles();
    // Parsing automatically moves a single description to link, so we just need to write the fileList back again
    String newValue = FileFieldWriter.getStringRepresentation(fileList);
    if (!oldValue.get().equals(newValue)) {
        entry.setField(FieldName.FILE, newValue);
        FieldChange change = new FieldChange(entry, FieldName.FILE, oldValue.get(), newValue);
        return Collections.singletonList(change);
    }
    return Collections.emptyList();
}
Also used : LinkedFile(org.jabref.model.entry.LinkedFile) FieldChange(org.jabref.model.FieldChange)

Example 8 with LinkedFile

use of org.jabref.model.entry.LinkedFile in project jabref by JabRef.

the class WrapFileLinks method format.

@Override
public String format(String field) {
    if (field == null) {
        return "";
    }
    StringBuilder sb = new StringBuilder();
    // Build the list containing the links:
    List<LinkedFile> fileList = FileFieldParser.parse(field);
    // counter for relevant iterations
    int piv = 1;
    for (LinkedFile flEntry : fileList) {
        // Use this entry if we don't discriminate on types, or if the type fits:
        if ((fileType == null) || flEntry.getFileType().equalsIgnoreCase(fileType)) {
            for (FormatEntry entry : format) {
                switch(entry.getType()) {
                    case STRING:
                        sb.append(entry.getString());
                        break;
                    case ITERATION_COUNT:
                        sb.append(piv);
                        break;
                    case FILE_PATH:
                        List<String> dirs;
                        // starting the export, which contains the database's file directory:
                        if ((prefs.getFileDirForDatabase() == null) || prefs.getFileDirForDatabase().isEmpty()) {
                            dirs = prefs.getGeneratedDirForDatabase();
                        } else {
                            dirs = prefs.getFileDirForDatabase();
                        }
                        String pathString = flEntry.findIn(dirs.stream().map(Paths::get).collect(Collectors.toList())).map(path -> path.toAbsolutePath().toString()).orElse(flEntry.getLink());
                        sb.append(replaceStrings(pathString));
                        break;
                    case RELATIVE_FILE_PATH:
                        /*
                         * Stumbled over this while investigating
                         *
                         * https://sourceforge.net/tracker/index.php?func=detail&aid=1469903&group_id=92314&atid=600306
                         */
                        //f.toURI().toString();
                        sb.append(replaceStrings(flEntry.getLink()));
                        break;
                    case FILE_EXTENSION:
                        FileHelper.getFileExtension(flEntry.getLink()).ifPresent(extension -> sb.append(replaceStrings(extension)));
                        break;
                    case FILE_TYPE:
                        sb.append(replaceStrings(flEntry.getFileType()));
                        break;
                    case FILE_DESCRIPTION:
                        sb.append(replaceStrings(flEntry.getDescription()));
                        break;
                    default:
                        break;
                }
            }
            // update counter
            piv++;
        }
    }
    return sb.toString();
}
Also used : FileFieldParser(org.jabref.model.entry.FileFieldParser) List(java.util.List) Paths(java.nio.file.Paths) Map(java.util.Map) AbstractParamLayoutFormatter(org.jabref.logic.layout.AbstractParamLayoutFormatter) HashMap(java.util.HashMap) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) Collectors(java.util.stream.Collectors) LinkedFile(org.jabref.model.entry.LinkedFile) ArrayList(java.util.ArrayList) FileHelper(org.jabref.model.util.FileHelper) LinkedFile(org.jabref.model.entry.LinkedFile) Paths(java.nio.file.Paths)

Example 9 with LinkedFile

use of org.jabref.model.entry.LinkedFile in project jabref by JabRef.

the class EntryAnnotationImporter method importAnnotationsFromFiles.

/**
     * Reads the annotations from the files that are attached to a BibEntry.
     *
     * @param databaseContext The context is needed for the importer.
     * @return Map from each PDF to a list of file annotations
     */
public Map<String, List<FileAnnotation>> importAnnotationsFromFiles(BibDatabaseContext databaseContext) {
    Map<String, List<FileAnnotation>> annotations = new HashMap<>();
    AnnotationImporter importer = new PdfAnnotationImporter();
    //import annotationsOfFiles if the selected files are valid which is checked in getFilteredFileList()
    for (LinkedFile linkedFile : this.getFilteredFileList()) {
        linkedFile.findIn(databaseContext, JabRefPreferences.getInstance().getFileDirectoryPreferences()).ifPresent(file -> annotations.put(file.getFileName().toString(), importer.importAnnotations(file)));
    }
    return annotations;
}
Also used : LinkedFile(org.jabref.model.entry.LinkedFile) HashMap(java.util.HashMap) List(java.util.List)

Example 10 with LinkedFile

use of org.jabref.model.entry.LinkedFile in project jabref by JabRef.

the class FileLink method format.

@Override
public String format(String field) {
    if (field == null) {
        return "";
    }
    List<LinkedFile> fileList = FileFieldParser.parse(field);
    LinkedFile link = null;
    if (fileType == null) {
        // No file type specified. Simply take the first link.
        if (!(fileList.isEmpty())) {
            link = fileList.get(0);
        }
    } else {
        // A file type is specified:
        for (LinkedFile flEntry : fileList) {
            if (flEntry.getFileType().equalsIgnoreCase(fileType)) {
                link = flEntry;
                break;
            }
        }
    }
    if (link == null) {
        return "";
    }
    List<String> dirs;
    // starting the export, which contains the database's file directory:
    if (prefs.getFileDirForDatabase() == null) {
        dirs = prefs.getGeneratedDirForDatabase();
    } else {
        dirs = prefs.getFileDirForDatabase();
    }
    return link.findIn(dirs.stream().map(Paths::get).collect(Collectors.toList())).map(path -> path.normalize().toString()).orElse(link.getLink());
}
Also used : FileFieldParser(org.jabref.model.entry.FileFieldParser) List(java.util.List) ParamLayoutFormatter(org.jabref.logic.layout.ParamLayoutFormatter) Paths(java.nio.file.Paths) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) Collectors(java.util.stream.Collectors) LinkedFile(org.jabref.model.entry.LinkedFile) LinkedFile(org.jabref.model.entry.LinkedFile) Paths(java.nio.file.Paths)

Aggregations

LinkedFile (org.jabref.model.entry.LinkedFile)28 Test (org.junit.Test)13 File (java.io.File)11 Path (java.nio.file.Path)8 FieldChange (org.jabref.model.FieldChange)6 ArrayList (java.util.ArrayList)5 BibEntry (org.jabref.model.entry.BibEntry)5 HashMap (java.util.HashMap)3 List (java.util.List)3 ExternalFileType (org.jabref.gui.externalfiletype.ExternalFileType)3 UnknownExternalFileType (org.jabref.gui.externalfiletype.UnknownExternalFileType)3 FileListEntry (org.jabref.gui.filelist.FileListEntry)3 LayoutFormatterPreferences (org.jabref.logic.layout.LayoutFormatterPreferences)3 IOException (java.io.IOException)2 Paths (java.nio.file.Paths)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Log (org.apache.commons.logging.Log)2 LogFactory (org.apache.commons.logging.LogFactory)2 FileFieldParser (org.jabref.model.entry.FileFieldParser)2