use of org.apache.jempbox.xmp.XMPMetadata in project jabref by JabRef.
the class XMPSchemaBibtexTest method testGetAllProperties.
@Test
public void testGetAllProperties() throws IOException {
XMPMetadata xmp = new XMPMetadata();
XMPSchemaBibtex bibtex = new XMPSchemaBibtex(xmp);
bibtex.setTextProperty("title", "BlaBla Ta Ta\nHello World");
bibtex.setTextProperty("abstract", "BlaBla Ta Ta\nHello World");
bibtex.setTextProperty("review", "BlaBla Ta Ta\nHello World");
bibtex.setTextProperty("note", "BlaBla Ta Ta\nHello World");
bibtex.setPersonList("author", "Mouse, Mickey and Bond, James");
Map<String, String> s = XMPSchemaBibtex.getAllProperties(bibtex, "bibtex");
Assert.assertEquals(5, s.size());
Assert.assertTrue(s.containsKey("title"));
Assert.assertTrue(s.containsKey("author"));
Assert.assertEquals("BlaBla Ta Ta Hello World", s.get("title"));
Assert.assertEquals("BlaBla Ta Ta\nHello World", s.get("abstract"));
Assert.assertEquals("BlaBla Ta Ta\nHello World", s.get("review"));
Assert.assertEquals("BlaBla Ta Ta\nHello World", s.get("note"));
Assert.assertEquals("Mickey Mouse and James Bond", s.get("author"));
}
use of org.apache.jempbox.xmp.XMPMetadata in project jabref by JabRef.
the class XMPSchemaBibtexTest method testSetBibtexEntry.
@Test
public void testSetBibtexEntry() throws IOException {
when(prefs.getFieldContentParserPreferences()).thenReturn(new FieldContentParserPreferences());
XMPMetadata xmp = new XMPMetadata();
XMPSchemaBibtex bibtex = new XMPSchemaBibtex(xmp);
BibEntry e = BibtexTestData.getBibtexEntry(prefs);
bibtex.setBibtexEntry(e, null);
BibEntry e2 = bibtex.getBibtexEntry();
assertEqualsBibtexEntry(e, e2);
}
use of org.apache.jempbox.xmp.XMPMetadata in project jabref by JabRef.
the class XMPUtil method toXMP.
/**
* Write the given BibtexEntries as XMP-metadata text to the given stream.
*
* The text that is written to the stream contains a complete XMP-document.
*
* @param bibtexEntries The BibtexEntries to write XMP-metadata for.
* @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.
* @throws TransformerException Thrown if the bibtexEntries could not transformed to XMP.
* @throws IOException Thrown if an IOException occured while writing to the stream.
* @see #toXMP(java.util.Collection, BibDatabase) if you don't need strings to be resolved.
*/
private static void toXMP(Collection<BibEntry> bibtexEntries, BibDatabase database, OutputStream outputStream, XMPPreferences xmpPreferences) throws IOException, TransformerException {
Collection<BibEntry> resolvedEntries;
if (database == null) {
resolvedEntries = bibtexEntries;
} else {
resolvedEntries = database.resolveForStrings(bibtexEntries, true);
}
XMPMetadata x = new XMPMetadata();
for (BibEntry e : resolvedEntries) {
XMPSchemaBibtex schema = new XMPSchemaBibtex(x);
x.addSchema(schema);
schema.setBibtexEntry(e, xmpPreferences);
}
x.save(outputStream);
}
use of org.apache.jempbox.xmp.XMPMetadata 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.XMPMetadata 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