use of org.jabref.model.FieldChange in project jabref by JabRef.
the class SpecialFieldsUtilsTest method syncSpecialFieldsFromKeywordCausesNoChangeWhenKeywordsAreEmpty.
@Test
public void syncSpecialFieldsFromKeywordCausesNoChangeWhenKeywordsAreEmpty() {
BibEntry entry = new BibEntry();
List<FieldChange> changes = SpecialFieldsUtils.syncSpecialFieldsFromKeywords(entry, ',');
assertFalse(changes.size() > 0);
}
use of org.jabref.model.FieldChange in project jabref by JabRef.
the class SpecialFieldsUtilsTest method syncKeywordsFromSpecialFieldsForEmptyFieldCausesNoChange.
@Test
public void syncKeywordsFromSpecialFieldsForEmptyFieldCausesNoChange() {
BibEntry entry = new BibEntry();
List<FieldChange> changes = SpecialFieldsUtils.syncKeywordsFromSpecialFields(entry, ',');
assertFalse(changes.size() > 0);
}
use of org.jabref.model.FieldChange in project jabref by JabRef.
the class BibDatabaseWriter method savePartOfDatabase.
/**
* Saves the database, including only the specified entries.
*/
public E savePartOfDatabase(BibDatabaseContext bibDatabaseContext, List<BibEntry> entries, SavePreferences preferences) throws SaveException {
session = saveSessionFactory.createSaveSession(preferences.getEncodingOrDefault(), preferences.getMakeBackup());
Optional<String> sharedDatabaseIDOptional = bibDatabaseContext.getDatabase().getSharedDatabaseID();
if (sharedDatabaseIDOptional.isPresent()) {
writeDatabaseID(sharedDatabaseIDOptional.get());
}
// Map to collect entry type definitions that we must save along with entries using them.
Map<String, EntryType> typesToWrite = new TreeMap<>();
// Some file formats write something at the start of the file (like the encoding)
if (preferences.getSaveType() != SavePreferences.DatabaseSaveType.PLAIN_BIBTEX) {
writePrelogue(bibDatabaseContext, preferences.getEncoding());
}
// Write preamble if there is one.
writePreamble(bibDatabaseContext.getDatabase().getPreamble().orElse(""));
// Write strings if there are any.
writeStrings(bibDatabaseContext.getDatabase(), preferences.isReformatFile(), preferences.getLatexFieldFormatterPreferences());
// Write database entries.
List<BibEntry> sortedEntries = getSortedEntries(bibDatabaseContext, entries, preferences);
List<FieldChange> saveActionChanges = applySaveActions(sortedEntries, bibDatabaseContext.getMetaData());
session.addFieldChanges(saveActionChanges);
for (BibEntry entry : sortedEntries) {
// types (*not* all customized standard types) must be written.
if (!EntryTypes.getStandardType(entry.getType(), bibDatabaseContext.getMode()).isPresent()) {
// If user-defined entry type, then add it
// Otherwise (getType returns empty optional) it is a completely unknown entry type, so ignore it
EntryTypes.getType(entry.getType(), bibDatabaseContext.getMode()).ifPresent(entryType -> typesToWrite.put(entryType.getName(), entryType));
}
writeEntry(entry, bibDatabaseContext.getMode(), preferences.isReformatFile(), preferences.getLatexFieldFormatterPreferences());
}
if (preferences.getSaveType() != SavePreferences.DatabaseSaveType.PLAIN_BIBTEX) {
// Write meta data.
writeMetaData(bibDatabaseContext.getMetaData(), preferences.getGlobalCiteKeyPattern());
// Write type definitions, if any:
writeEntryTypeDefinitions(typesToWrite);
}
//finally write whatever remains of the file, but at least a concluding newline
writeEpilogue(bibDatabaseContext.getDatabase().getEpilog());
try {
session.getWriter().close();
} catch (IOException e) {
throw new SaveException(e);
}
return session;
}
use of org.jabref.model.FieldChange in project jabref by JabRef.
the class MoveFilesCleanup method cleanup.
@Override
public List<FieldChange> cleanup(BibEntry entry) {
Optional<Path> firstExistingFileDir = databaseContext.getFirstExistingFileDir(fileDirectoryPreferences);
if (!firstExistingFileDir.isPresent()) {
return Collections.emptyList();
}
List<Path> paths = databaseContext.getFileDirectoriesAsPaths(fileDirectoryPreferences);
String defaultFileDirectory = firstExistingFileDir.get().toString();
Optional<Path> targetDirectory = FileHelper.expandFilenameAsPath(defaultFileDirectory, paths);
if (!targetDirectory.isPresent()) {
return Collections.emptyList();
}
List<LinkedFile> fileList;
List<LinkedFile> newFileList;
if (singleFileFieldCleanup != null) {
fileList = Arrays.asList(singleFileFieldCleanup);
//Add all other except the current selected file
newFileList = entry.getFiles().stream().filter(name -> !name.equals(singleFileFieldCleanup)).collect(Collectors.toList());
} else {
newFileList = new ArrayList<>();
fileList = entry.getFiles();
}
boolean changed = false;
for (LinkedFile fileEntry : fileList) {
String oldFileName = fileEntry.getLink();
Optional<Path> oldFile = fileEntry.findIn(paths);
if (!oldFile.isPresent() || !Files.exists(oldFile.get())) {
newFileList.add(fileEntry);
continue;
}
String targetDirName = "";
if (!fileDirPattern.isEmpty()) {
targetDirName = FileUtil.createFileNameFromPattern(databaseContext.getDatabase(), entry, fileDirPattern, layoutPrefs);
}
Path newTargetFile = targetDirectory.get().resolve(targetDirName).resolve(oldFile.get().getFileName());
if (Files.exists(newTargetFile)) {
// We do not overwrite already existing files
newFileList.add(fileEntry);
continue;
}
try {
if (!Files.exists(newTargetFile)) {
Files.createDirectories(newTargetFile);
}
} catch (IOException e) {
LOGGER.error("Could no create necessary target directoires for renaming", e);
}
if (FileUtil.renameFile(oldFile.get(), newTargetFile, true)) {
changed = true;
String newEntryFilePath = Paths.get(defaultFileDirectory).relativize(newTargetFile).toString();
LinkedFile newFileEntry = fileEntry;
if (!oldFileName.equals(newTargetFile.toString())) {
newFileEntry = new LinkedFile(fileEntry.getDescription(), newEntryFilePath, fileEntry.getFileType());
changed = true;
}
newFileList.add(newFileEntry);
}
}
if (changed) {
Optional<FieldChange> change = entry.setFiles(newFileList);
if (change.isPresent()) {
return Collections.singletonList(change.get());
} else {
return Collections.emptyList();
}
}
return Collections.emptyList();
}
use of org.jabref.model.FieldChange in project jabref by JabRef.
the class RelativePathsCleanup method cleanup.
@Override
public List<FieldChange> cleanup(BibEntry entry) {
List<LinkedFile> fileList = entry.getFiles();
List<LinkedFile> newFileList = new ArrayList<>();
boolean changed = false;
for (LinkedFile fileEntry : fileList) {
String oldFileName = fileEntry.getLink();
String newFileName = FileUtil.shortenFileName(Paths.get(oldFileName), databaseContext.getFileDirectoriesAsPaths(fileDirectoryPreferences)).toString();
LinkedFile newFileEntry = fileEntry;
if (!oldFileName.equals(newFileName)) {
newFileEntry = new LinkedFile(fileEntry.getDescription(), newFileName, fileEntry.getFileType());
changed = true;
}
newFileList.add(newFileEntry);
}
if (changed) {
Optional<FieldChange> change = entry.setFiles(newFileList);
if (change.isPresent()) {
return Collections.singletonList(change.get());
} else {
return Collections.emptyList();
}
}
return Collections.emptyList();
}
Aggregations