Search in sources :

Example 16 with XMPMetadata

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"));
}
Also used : XMPMetadata(org.apache.jempbox.xmp.XMPMetadata) Test(org.junit.Test)

Example 17 with XMPMetadata

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);
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) FieldContentParserPreferences(org.jabref.logic.bibtex.FieldContentParserPreferences) XMPMetadata(org.apache.jempbox.xmp.XMPMetadata) Test(org.junit.Test)

Example 18 with XMPMetadata

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);
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) TypedBibEntry(org.jabref.logic.TypedBibEntry) XMPMetadata(org.apache.jempbox.xmp.XMPMetadata)

Example 19 with XMPMetadata

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);
        }
    }
}
Also used : BibEntry(org.jabref.model.entry.BibEntry) TypedBibEntry(org.jabref.logic.TypedBibEntry) COSVisitorException(org.apache.pdfbox.exceptions.COSVisitorException) XMPSchema(org.apache.jempbox.xmp.XMPSchema) PDMetadata(org.apache.pdfbox.pdmodel.common.PDMetadata) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PDDocumentCatalog(org.apache.pdfbox.pdmodel.PDDocumentCatalog) XMPMetadata(org.apache.jempbox.xmp.XMPMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) TransformerException(javax.xml.transform.TransformerException)

Example 20 with XMPMetadata

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);
}
Also used : XMPSchemaDublinCore(org.apache.jempbox.xmp.XMPSchemaDublinCore) BibEntry(org.jabref.model.entry.BibEntry) TypedBibEntry(org.jabref.logic.TypedBibEntry) XMPMetadata(org.apache.jempbox.xmp.XMPMetadata) XMPSchema(org.apache.jempbox.xmp.XMPSchema) ByteArrayInputStream(java.io.ByteArrayInputStream) PDMetadata(org.apache.pdfbox.pdmodel.common.PDMetadata) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PDDocumentCatalog(org.apache.pdfbox.pdmodel.PDDocumentCatalog)

Aggregations

XMPMetadata (org.apache.jempbox.xmp.XMPMetadata)24 Test (org.junit.Test)12 BibEntry (org.jabref.model.entry.BibEntry)11 XMPSchemaDublinCore (org.apache.jempbox.xmp.XMPSchemaDublinCore)10 PDDocumentCatalog (org.apache.pdfbox.pdmodel.PDDocumentCatalog)10 PDMetadata (org.apache.pdfbox.pdmodel.common.PDMetadata)10 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)9 XMPSchema (org.apache.jempbox.xmp.XMPSchema)8 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IOException (java.io.IOException)4 Calendar (java.util.Calendar)4 LinkedList (java.util.LinkedList)4 PDDocumentInformation (org.apache.pdfbox.pdmodel.PDDocumentInformation)4 TypedBibEntry (org.jabref.logic.TypedBibEntry)4 Document (org.w3c.dom.Document)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 InputStream (java.io.InputStream)3 List (java.util.List)3 XMPSchemaBasic (org.apache.jempbox.xmp.XMPSchemaBasic)3 ParserResult (org.jabref.logic.importer.ParserResult)3