Skip to content

Commit

Permalink
Improve XMP handling - latex free field, remove file field (#8789)
Browse files Browse the repository at this point in the history
* Make methods of XmpUtilWriter non-static

* Fix typos

* Rewrite test to use builder (and no strange global variables)

* Use "List.of"

* XMP writing does not write the `file` field.

* Have test checking for conent (and not only size)

* Remove "static" in XmpUtilReader, more comments

* Checkstyle fix

* Small fix

* More checkstyle

* Remove iterating on tuples

* Implement brace removal

* Fix checkstyle

* Add missing CHANGELOG.md entry

* "BibTex" -> "BibTeX"

* Make use of xmpPreferences.getKeywordSeparator()

* Streamline comments

* Writing BibTeX keeps the BibTeX code as is

* Remove LaTeX content also in DI fields

* Remove oboslete imports

* Switch to enhanced switch statement

* Apply simple ide suggestions

* checkstyle

* small code style

* Moved XmpPreferences argument to constructor,
applied more ide suggestions

* Order changed

* Text block

Co-authored-by: Christoph <siedlerkiller@gmail.com>
Co-authored-by: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com>

* Fix writing when privacy data is present

Co-authored-by: Christoph <siedlerkiller@gmail.com>
Co-authored-by: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com>

* Fix expected

Co-authored-by: Christoph <siedlerkiller@gmail.com>
Co-authored-by: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com>

* Fix

Co-authored-by: Christoph <siedlerkiller@gmail.com>
Co-authored-by: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com>

* More structure in the test

* fix checkstyle

Co-authored-by: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com>
Co-authored-by: Siedlerchr <siedlerkiller@gmail.com>
  • Loading branch information
3 people committed May 12, 2022
1 parent 8bd859d commit b938f18
Show file tree
Hide file tree
Showing 19 changed files with 541 additions and 485 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
### Changed

- We improved the Latex2Unicode conversion [#8639](https://github.com/JabRef/jabref/pull/8639)
- Writing BibTeX data into a PDF (XMP) removes braces. [#8452](https://github.com/JabRef/jabref/issues/8452)
- Writing BibTeX data into a PDF (XMP) does not write the `file` field.
- Writing BibTeX data into a PDF (XMP) considers the configured keyword separator (and does not use "," as default any more)

### Fixed

Expand Down
27 changes: 14 additions & 13 deletions src/main/java/org/jabref/gui/exporter/WriteMetadataToPdfAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import javafx.application.Platform;
import javafx.geometry.Insets;
Expand Down Expand Up @@ -129,8 +128,8 @@ private void writeMetadata() {
.map(file -> file.findIn(stateManager.getActiveDatabase().get(), filePreferences))
.filter(Optional::isPresent)
.map(Optional::get)
.filter(path -> FileUtil.isPDFFile(path))
.collect(Collectors.toList());
.filter(FileUtil::isPDFFile)
.toList();

Platform.runLater(() -> optionsDialog.getProgressArea()
.appendText(entry.getCitationKey().orElse(Localization.lang("undefined")) + "\n"));
Expand All @@ -144,13 +143,14 @@ private void writeMetadata() {
if (Files.exists(file)) {
try {
writeMetadataToFile(file, entry, stateManager.getActiveDatabase().get(), database);
Platform.runLater(
() -> optionsDialog.getProgressArea().appendText(" " + Localization.lang("OK") + ".\n"));
Platform.runLater(() ->
optionsDialog.getProgressArea()
.appendText(" " + Localization.lang("OK") + ".\n"));
entriesChanged++;
} catch (Exception e) {
Platform.runLater(() -> {
optionsDialog.getProgressArea().appendText(" " + Localization.lang("Error while writing") + " '"
+ file.toString() + "':\n");
optionsDialog.getProgressArea()
.appendText(" " + Localization.lang("Error while writing") + " '" + file + "':\n");
optionsDialog.getProgressArea().appendText(" " + e.getLocalizedMessage() + "\n");
});
errors++;
Expand All @@ -160,23 +160,24 @@ private void writeMetadata() {
Platform.runLater(() -> {
optionsDialog.getProgressArea()
.appendText(" " + Localization.lang("Skipped - PDF does not exist") + ":\n");
optionsDialog.getProgressArea().appendText(" " + file.toString() + "\n");
optionsDialog.getProgressArea()
.appendText(" " + file + "\n");
});
}
}
}

if (optionsDialog.isCanceled()) {
Platform.runLater(
() -> optionsDialog.getProgressArea().appendText("\n" + Localization.lang("Operation canceled.") + "\n"));
Platform.runLater(() ->
optionsDialog.getProgressArea().appendText("\n" + Localization.lang("Operation canceled.") + "\n"));
break;
}
}
Platform.runLater(() -> {
optionsDialog.getProgressArea()
.appendText("\n"
+ Localization.lang("Finished writing metadata for %0 file (%1 skipped, %2 errors).", String
.valueOf(entriesChanged), String.valueOf(skipped), String.valueOf(errors)));
+ Localization.lang("Finished writing metadata for %0 file (%1 skipped, %2 errors).",
String.valueOf(entriesChanged), String.valueOf(skipped), String.valueOf(errors)));
optionsDialog.done();
});

Expand All @@ -192,7 +193,7 @@ private void writeMetadata() {
* This writes both XMP data and embeds a corresponding .bib file
*/
synchronized private void writeMetadataToFile(Path file, BibEntry entry, BibDatabaseContext databaseContext, BibDatabase database) throws Exception {
XmpUtilWriter.writeXmp(file, entry, database, xmpPreferences);
new XmpUtilWriter(xmpPreferences).writeXmp(file, entry, database);

EmbeddedBibFilePdfExporter embeddedBibExporter = new EmbeddedBibFilePdfExporter(databaseContext.getMode(), entryTypesManager, fieldWriterPreferences);
embeddedBibExporter.exportToFileByPath(databaseContext, database, filePreferences, file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ public void writeMetadataToPdf() {
synchronized (linkedFile) {
try {
// Similar code can be found at {@link org.jabref.gui.exporter.WriteMetadataToPdfAction.writeMetadataToFile}
XmpUtilWriter.writeXmp(file.get(), entry, databaseContext.getDatabase(), preferences.getXmpPreferences());
new XmpUtilWriter(preferences.getXmpPreferences()).writeXmp(file.get(), entry, databaseContext.getDatabase());

EmbeddedBibFilePdfExporter embeddedBibExporter = new EmbeddedBibFilePdfExporter(databaseContext.getMode(), Globals.entryTypesManager, preferences.getFieldWriterPreferences());
embeddedBibExporter.exportToFileByPath(databaseContext, databaseContext.getDatabase(), preferences.getFilePreferences(), file.get());
Expand Down Expand Up @@ -462,7 +462,7 @@ public void download() {

BackgroundTask<Path> downloadTask = prepareDownloadTask(targetDirectory.get(), urlDownload);
downloadTask.onSuccess(destination -> {
boolean isDuplicate = false;
boolean isDuplicate;
try {
isDuplicate = FileNameUniqueness.isDuplicatedFile(targetDirectory.get(), destination.getFileName(), dialogService);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public int compare(BibEntry e1, BibEntry e2) {
}
}

// If the field is author or editor, we rearrange names so they are
// If the field is author or editor, we rearrange names to achieve that they are
// sorted according to last name.
if (sortField.getProperties().contains(FieldProperty.PERSON_NAMES)) {
if (f1 != null) {
Expand Down
11 changes: 3 additions & 8 deletions src/main/java/org/jabref/logic/exporter/XmpExporter.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.jabref.logic.exporter;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
Expand Down Expand Up @@ -57,7 +55,7 @@ public void export(BibDatabaseContext databaseContext, Path file, List<BibEntry>
if (file.getParent() == null) {
entryFile = Path.of(suffix);
} else {
entryFile = Path.of(file.getParent().toString() + "/" + suffix);
entryFile = Path.of(file.getParent() + "/" + suffix);
}
this.writeBibToXmp(entryFile, Collections.singletonList(entry));
}
Expand All @@ -67,10 +65,7 @@ public void export(BibDatabaseContext databaseContext, Path file, List<BibEntry>
}

private void writeBibToXmp(Path file, List<BibEntry> entries) throws IOException {
String xmpContent = XmpUtilWriter.generateXmpStringWithoutXmpDeclaration(entries, this.xmpPreferences);
try (BufferedWriter writer = Files.newBufferedWriter(file, StandardCharsets.UTF_8)) {
writer.write(xmpContent);
writer.flush();
}
String xmpContent = new XmpUtilWriter(this.xmpPreferences).generateXmpStringWithoutXmpDeclaration(entries);
Files.writeString(file, xmpContent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void export(BibDatabaseContext databaseContext, Path pdfFile, List<BibEnt
Objects.requireNonNull(entries);

if (pdfFile.toString().endsWith(".pdf")) {
XmpUtilWriter.writeXmp(pdfFile, entries, databaseContext.getDatabase(), xmpPreferences);
new XmpUtilWriter(xmpPreferences).writeXmp(pdfFile, entries, databaseContext.getDatabase());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public ParserResult importDatabase(String data) throws IOException {
@Override
public ParserResult importDatabase(Path filePath) {
final ArrayList<BibEntry> result = new ArrayList<>(1);
try (PDDocument document = XmpUtilReader.loadWithAutomaticDecryption(filePath)) {
try (PDDocument document = new XmpUtilReader().loadWithAutomaticDecryption(filePath)) {
String firstPageContents = getFirstPageContents(document);
Optional<BibEntry> entry = getEntryFromPDFContent(firstPageContents, OS.NEWLINE);
entry.ifPresent(result::add);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public ParserResult importDatabase(String data) throws IOException {

@Override
public ParserResult importDatabase(Path filePath) {
try (PDDocument document = XmpUtilReader.loadWithAutomaticDecryption(filePath)) {
try (PDDocument document = new XmpUtilReader().loadWithAutomaticDecryption(filePath)) {
return new ParserResult(getEmbeddedBibFileEntries(document));
} catch (EncryptedPdfsNotSupportedException e) {
return ParserResult.fromErrorMessage(Localization.lang("Decryption not supported."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public ParserResult importDatabase(String data) throws IOException {
@Override
public ParserResult importDatabase(Path filePath) {
List<BibEntry> result = new ArrayList<>(1);
try (PDDocument document = XmpUtilReader.loadWithAutomaticDecryption(filePath)) {
try (PDDocument document = new XmpUtilReader().loadWithAutomaticDecryption(filePath)) {
String firstPageContents = getFirstPageContents(document);
BibtexParser parser = new BibtexParser(importFormatPreferences, new DummyFileUpdateMonitor());
result = parser.parseEntries(firstPageContents);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public ParserResult importDatabase(String data) throws IOException {
public ParserResult importDatabase(Path filePath) {
Objects.requireNonNull(filePath);
try {
return new ParserResult(XmpUtilReader.readXmp(filePath, xmpPreferences));
return new ParserResult(new XmpUtilReader().readXmp(filePath, xmpPreferences));
} catch (IOException exception) {
return ParserResult.fromError(exception);
}
Expand Down
Loading

0 comments on commit b938f18

Please sign in to comment.