Skip to content

Commit

Permalink
[WIP] Dublin core
Browse files Browse the repository at this point in the history
  • Loading branch information
johannes-manner committed Feb 7, 2018
1 parent f3d86d5 commit 282f066
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 2,353 deletions.
65 changes: 29 additions & 36 deletions src/main/java/org/jabref/cli/XMPUtilMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -65,12 +66,10 @@ public static void main(String[] args) throws IOException, TransformerException
XMPPreferences xmpPreferences = Globals.prefs.getXMPPreferences();
ImportFormatPreferences importFormatPreferences = Globals.prefs.getImportFormatPreferences();

switch (args.length) {
case 0:
int argsLength = args.length;
if (argsLength == 0) {
usage();
break;
case 1:

} else if (argsLength == 1) {
if (args[0].endsWith(".pdf")) {
// Read from pdf and write as BibTex
List<BibEntry> l = XMPUtil.readXMP(new File(args[0]), xmpPreferences);
Expand All @@ -92,63 +91,57 @@ public static void main(String[] args) throws IOException, TransformerException

if (entries.isEmpty()) {
System.err.println("Could not find BibEntry in " + args[0]);
} else {
System.out.println(XMPUtil.toXMP(entries, result.getDatabase(), xmpPreferences));
}
}
} else {
usage();
}
break;
case 2:
if ("-x".equals(args[0]) && args[1].endsWith(".pdf")) {
// Read from pdf and write as BibTex
Optional<XMPMetadata> meta = XMPUtil.readRawXMP(new File(args[1]));
} else if (argsLength == 2) {
if ("-x".equals(args[0]) && args[1].endsWith(".pdf")) {
// Read from pdf and write as BibTex
Optional<XMPMetadata> meta = XMPUtil.readRawXMP(new File(args[1]));

if (meta.isPresent()) {
XMLUtil.save(meta.get().getXMPDocument(), System.out, StandardCharsets.UTF_8.name());
} else {
System.err.println("The given pdf does not contain any XMP-metadata.");
if (meta.isPresent()) {
XMLUtil.save(meta.get().getXMPDocument(), System.out, StandardCharsets.UTF_8.name());
} else {
System.err.println("The given pdf does not contain any XMP-metadata.");
}
return;
}
break;
}

if (args[0].endsWith(".bib") && args[1].endsWith(".pdf")) {
ParserResult result = new BibtexParser(importFormatPreferences, Globals.getFileUpdateMonitor()).parse(new FileReader(args[0]));
if (args[0].endsWith(".bib") && args[1].endsWith(".pdf")) {
ParserResult result = new BibtexParser(importFormatPreferences, Globals.getFileUpdateMonitor()).parse(new FileReader(args[0]));

Collection<BibEntry> entries = result.getDatabase().getEntries();
Collection<BibEntry> entries = result.getDatabase().getEntries();

if (entries.isEmpty()) {
System.err.println("Could not find BibEntry in " + args[0]);
} else {
XMPUtil.writeXMP(new File(args[1]), entries, result.getDatabase(), false, xmpPreferences);
System.out.println("XMP written.");
if (entries.isEmpty()) {
System.err.println("Could not find BibEntry in " + args[0]);
} else {
XMPUtil.writeXMP(new File(args[1]), entries, result.getDatabase(), false, xmpPreferences);
System.out.println("XMP written.");
}
return;
}
break;
}

usage();
break;
case 3:
usage();
} else if (argsLength == 3) {
if (!args[1].endsWith(".bib") && !args[2].endsWith(".pdf")) {
usage();
break;
return;
}

ParserResult result = new BibtexParser(importFormatPreferences, Globals.getFileUpdateMonitor()).parse(new FileReader(args[1]));

Optional<BibEntry> bibEntry = result.getDatabase().getEntryByKey(args[0]);

if (bibEntry.isPresent()) {
XMPUtil.writeXMP(new File(args[2]), bibEntry.get(), result.getDatabase(), xmpPreferences);
XMPUtil.writeXMP(Paths.get(args[2]), bibEntry.get(), result.getDatabase(), xmpPreferences);

System.out.println("XMP written.");
} else {
System.err.println("Could not find BibEntry " + args[0] + " in " + args[0]);
}
break;

default:
} else {
usage();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void run() {
for (Path file : files) {
if (Files.exists(file)) {
try {
XMPUtil.writeXMP(file.toFile(), entry, database, Globals.prefs.getXMPPreferences());
XMPUtil.writeXMP(file, entry, database, Globals.prefs.getXMPPreferences());
SwingUtilities.invokeLater(
() -> optDiag.getProgressArea().append(" " + Localization.lang("OK") + ".\n"));
entriesChanged++;
Expand Down
69 changes: 0 additions & 69 deletions src/main/java/org/jabref/gui/importer/EntryFromPDFCreator.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
package org.jabref.gui.importer;

import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Optional;

import org.jabref.Globals;
import org.jabref.JabRefGUI;
import org.jabref.gui.IconTheme;
import org.jabref.gui.externalfiletype.ExternalFileType;
import org.jabref.gui.externalfiletype.ExternalFileTypes;
import org.jabref.logic.xmp.XMPUtil;
import org.jabref.model.entry.BibEntry;
import org.jabref.pdfimport.PdfImporter;
import org.jabref.pdfimport.PdfImporter.ImportPdfFilesResult;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;

/**
* Uses XMPUtils to get one BibEntry for a PDF-File.
* Also imports the non-XMP Data (PDDocument-Information) using XMPUtil.getBibtexEntryFromDocumentInformation.
Expand Down Expand Up @@ -68,65 +58,6 @@ protected Optional<BibEntry> createBibtexEntry(File pdfFile) {
return Optional.empty();
}

/*addEntryDataFromPDDocumentInformation(pdfFile, entry);
addEntryDataFromXMP(pdfFile, entry);
if (entry.getField(FieldName.TITLE) == null) {
entry.setField(FieldName.TITLE, pdfFile.getName());
}
return entry;*/
}

/** Adds entry data read from the PDDocument information of the file.
* @param pdfFile
* @param entry
*/
private void addEntryDataFromPDDocumentInformation(File pdfFile, BibEntry entry) {
try (PDDocument document = PDDocument.load(pdfFile.getAbsoluteFile())) {
PDDocumentInformation pdfDocInfo = document
.getDocumentInformation();

if (pdfDocInfo != null) {
Optional<BibEntry> entryDI = XMPUtil
.getBibtexEntryFromDocumentInformation(document
.getDocumentInformation());
if (entryDI.isPresent()) {
addEntryDataToEntry(entry, entryDI.get());
Calendar creationDate = pdfDocInfo.getCreationDate();
if (creationDate != null) {
// default time stamp follows ISO-8601. Reason: https://xkcd.com/1179/
String date = LocalDate.of(Calendar.YEAR, Calendar.MONTH + 1, Calendar.DAY_OF_MONTH)
.format(DateTimeFormatter.ISO_LOCAL_DATE);
appendToField(entry, Globals.prefs.getTimestampPreferences().getTimestampField(), date);
}

if (pdfDocInfo.getCustomMetadataValue("bibtex/bibtexkey") != null) {
entry.setId(pdfDocInfo
.getCustomMetadataValue("bibtex/bibtexkey"));
}
}
}
} catch (IOException e) {
// no canceling here, just no data added.
}
}

/**
* Adds all data Found in all the entries of this XMP file to the given
* entry. This was implemented without having much knowledge of the XMP
* format.
*
* @param aFile
* @param entry
*/
private void addEntryDataFromXMP(File aFile, BibEntry entry) {
try {
List<BibEntry> entrys = XMPUtil.readXMP(aFile.getAbsoluteFile(), Globals.prefs.getXMPPreferences());
addEntrysToEntry(entry, entrys);
} catch (IOException e) {
// no canceling here, just no data added.
}
}

@Override
Expand Down
Loading

0 comments on commit 282f066

Please # to comment.