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;
}
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();
}
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();
}
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;
}
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());
}
Aggregations