use of org.apache.jempbox.xmp.XMPSchema in project jabref by JabRef.
the class XMPUtilTest method testWriteSingleUpdatesDCAndInfo.
@Test
public void testWriteSingleUpdatesDCAndInfo() throws IOException, TransformerException {
List<BibEntry> l = new LinkedList<>();
l.add(t3BibtexEntry());
XMPUtil.writeXMP(pdfFile, l, null, true, xmpPreferences);
try (PDDocument document = PDDocument.load(pdfFile.getAbsoluteFile())) {
if (document.isEncrypted()) {
Assert.fail("Cannot add metadata to encrypted document.");
}
Assert.assertEquals("Kelly Clarkson and Ozzy Osbourne", document.getDocumentInformation().getAuthor());
Assert.assertEquals("Hypersonic ultra-sound", document.getDocumentInformation().getTitle());
Assert.assertEquals("Huey Duck and Dewey Duck and Louie Duck", document.getDocumentInformation().getCustomMetadataValue("bibtex/editor"));
Assert.assertEquals("Clarkson06", document.getDocumentInformation().getCustomMetadataValue("bibtex/bibtexkey"));
Assert.assertEquals("peanut, butter, jelly", document.getDocumentInformation().getKeywords());
assertEqualsBibtexEntry(t3BibtexEntry(), XMPUtil.getBibtexEntryFromDocumentInformation(document.getDocumentInformation()).get());
PDDocumentCatalog catalog = document.getDocumentCatalog();
PDMetadata metaRaw = catalog.getMetadata();
if (metaRaw == null) {
Assert.fail();
return;
}
XMPMetadata meta = new XMPMetadata(XMLUtil.parse(metaRaw.createInputStream()));
meta.addXMLNSMapping(XMPSchemaBibtex.NAMESPACE, XMPSchemaBibtex.class);
// Check Dublin Core
List<XMPSchema> schemas = meta.getSchemasByNamespaceURI("http://purl.org/dc/elements/1.1/");
Assert.assertEquals(1, schemas.size());
XMPSchemaDublinCore dcSchema = (XMPSchemaDublinCore) schemas.iterator().next();
Assert.assertNotNull(dcSchema);
Assert.assertEquals("Hypersonic ultra-sound", dcSchema.getTitle());
Assert.assertEquals("1982-07", dcSchema.getSequenceList("dc:date").get(0));
Assert.assertEquals("Kelly Clarkson", dcSchema.getCreators().get(0));
Assert.assertEquals("Ozzy Osbourne", dcSchema.getCreators().get(1));
Assert.assertEquals("Huey Duck", dcSchema.getContributors().get(0));
Assert.assertEquals("Dewey Duck", dcSchema.getContributors().get(1));
Assert.assertEquals("Louie Duck", dcSchema.getContributors().get(2));
Assert.assertEquals("InProceedings".toLowerCase(), dcSchema.getTypes().get(0).toLowerCase());
Assert.assertTrue(dcSchema.getRelationships().contains("bibtex/bibtexkey/Clarkson06"));
Assert.assertEquals("peanut", dcSchema.getSubjects().get(0));
Assert.assertEquals("butter", dcSchema.getSubjects().get(1));
Assert.assertEquals("jelly", dcSchema.getSubjects().get(2));
/**
* Bibtexkey, Journal, pdf, booktitle
*/
Assert.assertEquals(4, dcSchema.getRelationships().size());
assertEqualsBibtexEntry(t3BibtexEntry(), XMPUtil.getBibtexEntryFromDublinCore(dcSchema, xmpPreferences).get());
}
}
use of org.apache.jempbox.xmp.XMPSchema in project jabref by JabRef.
the class XMPUtil method writeXMP.
/**
* Try to write the given BibTexEntry in the XMP-stream of the given
* PDF-file.
*
* Throws an IOException if the file cannot be read or written, so the user
* can remove a lock or cancel the operation.
*
* The method will overwrite existing BibTeX-XMP-data, but keep other
* existing metadata.
*
* @param file The file to write the entries to.
* @param bibtexEntries The entries to write to the file. *
* @param database maybenull An optional database which the given bibtex entries belong to, which will be used
* to resolve strings. If the database is null the strings will not be resolved.
* @param writePDFInfo Write information also in PDF document properties
* @throws TransformerException If the entry was malformed or unsupported.
* @throws IOException If the file could not be written to or could not be found.
*/
public static void writeXMP(File file, Collection<BibEntry> bibtexEntries, BibDatabase database, boolean writePDFInfo, XMPPreferences xmpPreferences) throws IOException, TransformerException {
Collection<BibEntry> resolvedEntries;
if (database == null) {
resolvedEntries = bibtexEntries;
} else {
resolvedEntries = database.resolveForStrings(bibtexEntries, false);
}
try (PDDocument document = PDDocument.load(file.getAbsoluteFile())) {
if (document.isEncrypted()) {
throw new EncryptedPdfsNotSupportedException();
}
if (writePDFInfo && (resolvedEntries.size() == 1)) {
XMPUtil.writeDocumentInformation(document, resolvedEntries.iterator().next(), null, xmpPreferences);
XMPUtil.writeDublinCore(document, resolvedEntries, null, xmpPreferences);
}
PDDocumentCatalog catalog = document.getDocumentCatalog();
PDMetadata metaRaw = catalog.getMetadata();
XMPMetadata meta;
if (metaRaw == null) {
meta = new XMPMetadata();
} else {
meta = new XMPMetadata(XMLUtil.parse(metaRaw.createInputStream()));
}
meta.addXMLNSMapping(XMPSchemaBibtex.NAMESPACE, XMPSchemaBibtex.class);
// Remove all current Bibtex-schemas
List<XMPSchema> schemas = meta.getSchemasByNamespaceURI(XMPSchemaBibtex.NAMESPACE);
for (XMPSchema schema : schemas) {
XMPSchemaBibtex bib = (XMPSchemaBibtex) schema;
bib.getElement().getParentNode().removeChild(bib.getElement());
}
for (BibEntry e : resolvedEntries) {
XMPSchemaBibtex bibtex = new XMPSchemaBibtex(meta);
meta.addSchema(bibtex);
bibtex.setBibtexEntry(e, xmpPreferences);
}
// Save to stream and then input that stream to the PDF
ByteArrayOutputStream os = new ByteArrayOutputStream();
meta.save(os);
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
PDMetadata metadataStream = new PDMetadata(document, is, false);
catalog.setMetadata(metadataStream);
// Save
try {
document.save(file.getAbsolutePath());
} catch (COSVisitorException e) {
LOGGER.debug("Could not write XMP metadata", e);
throw new TransformerException("Could not write XMP metadata: " + e.getLocalizedMessage(), e);
}
}
}
use of org.apache.jempbox.xmp.XMPSchema in project jabref by JabRef.
the class XMPUtil method writeDublinCore.
/**
* Try to write the given BibTexEntries as DublinCore XMP Schemas
*
* Existing DublinCore schemas in the document are removed
*
* @param document The pdf document to write to.
* @param entries The BibTeX entries that are written as schemas
* @param database maybenull An optional database which the given BibTeX entries belong to, which will be used to
* resolve strings. If the database is null the strings will not be resolved.
*/
private static void writeDublinCore(PDDocument document, Collection<BibEntry> entries, BibDatabase database, XMPPreferences xmpPreferences) throws IOException, TransformerException {
Collection<BibEntry> resolvedEntries;
if (database == null) {
resolvedEntries = entries;
} else {
resolvedEntries = database.resolveForStrings(entries, false);
}
PDDocumentCatalog catalog = document.getDocumentCatalog();
PDMetadata metaRaw = catalog.getMetadata();
XMPMetadata meta;
if (metaRaw == null) {
meta = new XMPMetadata();
} else {
meta = new XMPMetadata(XMLUtil.parse(metaRaw.createInputStream()));
}
// Remove all current Dublin-Core schemas
List<XMPSchema> schemas = meta.getSchemasByNamespaceURI(XMPSchemaDublinCore.NAMESPACE);
for (XMPSchema schema : schemas) {
schema.getElement().getParentNode().removeChild(schema.getElement());
}
for (BibEntry entry : resolvedEntries) {
XMPSchemaDublinCore dcSchema = new XMPSchemaDublinCore(meta);
XMPUtil.writeToDCSchema(dcSchema, entry, null, xmpPreferences);
meta.addSchema(dcSchema);
}
// Save to stream and then input that stream to the PDF
ByteArrayOutputStream os = new ByteArrayOutputStream();
meta.save(os);
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
PDMetadata metadataStream = new PDMetadata(document, is, false);
catalog.setMetadata(metadataStream);
}
Aggregations