Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve XMP handling - latex free field, remove file field #8789

Merged
merged 33 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
b6b527a
Make methods of XmpUtilWriter non-static
koppor May 11, 2022
c585b3d
Fix typos
koppor May 11, 2022
46769f6
Rewrite test to use builder (and no strange global variables)
koppor May 11, 2022
0b86570
Use "List.of"
koppor May 11, 2022
a7da8ec
XMP writing does not write the `file` field.
koppor May 11, 2022
3a2cbbe
Have test checking for conent (and not only size)
koppor May 11, 2022
e38b928
Remove "static" in XmpUtilReader, more comments
koppor May 11, 2022
a4ab7e1
Checkstyle fix
koppor May 11, 2022
88685e9
Small fix
koppor May 11, 2022
16e47d7
More checkstyle
koppor May 12, 2022
e8a939e
Remove iterating on tuples
koppor May 12, 2022
948e8bb
Implement brace removal
koppor May 12, 2022
93b3533
Fix checkstyle
koppor May 12, 2022
55fe396
Add missing CHANGELOG.md entry
koppor May 12, 2022
7f9a347
"BibTex" -> "BibTeX"
koppor May 12, 2022
7a9fc73
Make use of xmpPreferences.getKeywordSeparator()
koppor May 12, 2022
d2e1449
Streamline comments
koppor May 12, 2022
b12c66b
Writing BibTeX keeps the BibTeX code as is
koppor May 12, 2022
e2fcea4
Remove LaTeX content also in DI fields
koppor May 12, 2022
dd5af25
Remove oboslete imports
koppor May 12, 2022
abf5fec
Switch to enhanced switch statement
calixtus May 12, 2022
3d7aae7
Apply simple ide suggestions
calixtus May 12, 2022
22b7373
checkstyle
Siedlerchr May 12, 2022
63e9de3
small code style
Siedlerchr May 12, 2022
238741f
Moved XmpPreferences argument to constructor,
calixtus May 12, 2022
f639158
Merge remote-tracking branch 'upstream/fix-xmp' into fix-xmp
calixtus May 12, 2022
992f478
Order changed
Siedlerchr May 12, 2022
143697e
Text block
koppor May 12, 2022
d6efc69
Fix writing when privacy data is present
koppor May 12, 2022
5b0ed33
Fix expected
koppor May 12, 2022
04a9d7d
Fix
koppor May 12, 2022
9737803
More structure in the test
koppor May 12, 2022
5974c89
fix checkstyle
Siedlerchr May 12, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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