Search in sources :

Example 1 with XMPSchemaDublinCore

use of org.apache.jempbox.xmp.XMPSchemaDublinCore in project jabref by JabRef.

the class XMPUtil method readXMP.

/**
     * Try to read the given BibTexEntry from the XMP-stream of the given
     * inputstream containing a PDF-file.
     *
     * @param inputStream The inputstream to read from.
     * @return list of BibEntries retrieved from the stream. May be empty, but never null
     * @throws IOException Throws an IOException if the file cannot be read, so the user than remove a lock or cancel
     *                     the operation.
     */
public static List<BibEntry> readXMP(InputStream inputStream, XMPPreferences xmpPreferences) throws IOException {
    List<BibEntry> result = new LinkedList<>();
    try (PDDocument document = loadWithAutomaticDecryption(inputStream)) {
        Optional<XMPMetadata> meta = XMPUtil.getXMPMetadata(document);
        if (meta.isPresent()) {
            List<XMPSchema> schemas = meta.get().getSchemasByNamespaceURI(XMPSchemaBibtex.NAMESPACE);
            for (XMPSchema schema : schemas) {
                XMPSchemaBibtex bib = (XMPSchemaBibtex) schema;
                BibEntry entry = bib.getBibtexEntry();
                if (entry.getType() == null) {
                    entry.setType(BibEntry.DEFAULT_TYPE);
                }
                result.add(entry);
            }
            // If we did not find anything have a look if a Dublin Core exists
            if (result.isEmpty()) {
                schemas = meta.get().getSchemasByNamespaceURI(XMPSchemaDublinCore.NAMESPACE);
                for (XMPSchema schema : schemas) {
                    XMPSchemaDublinCore dc = (XMPSchemaDublinCore) schema;
                    Optional<BibEntry> entry = XMPUtil.getBibtexEntryFromDublinCore(dc, xmpPreferences);
                    if (entry.isPresent()) {
                        if (entry.get().getType() == null) {
                            entry.get().setType(BibEntry.DEFAULT_TYPE);
                        }
                        result.add(entry.get());
                    }
                }
            }
        }
        if (result.isEmpty()) {
            // If we did not find any XMP metadata, search for non XMP metadata
            PDDocumentInformation documentInformation = document.getDocumentInformation();
            Optional<BibEntry> entry = XMPUtil.getBibtexEntryFromDocumentInformation(documentInformation);
            entry.ifPresent(result::add);
        }
    }
    // return empty list, if no metadata was found
    if (result.isEmpty()) {
        return Collections.emptyList();
    }
    return result;
}
Also used : XMPSchemaDublinCore(org.apache.jempbox.xmp.XMPSchemaDublinCore) BibEntry(org.jabref.model.entry.BibEntry) TypedBibEntry(org.jabref.logic.TypedBibEntry) XMPSchema(org.apache.jempbox.xmp.XMPSchema) LinkedList(java.util.LinkedList) XMPMetadata(org.apache.jempbox.xmp.XMPMetadata) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) PDDocumentInformation(org.apache.pdfbox.pdmodel.PDDocumentInformation)

Example 2 with XMPSchemaDublinCore

use of org.apache.jempbox.xmp.XMPSchemaDublinCore in project jabref by JabRef.

the class XMPUtilTest method testReadWriteDC.

@Test
public void testReadWriteDC() 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());
    }
}
Also used : XMPSchemaDublinCore(org.apache.jempbox.xmp.XMPSchemaDublinCore) BibEntry(org.jabref.model.entry.BibEntry) XMPMetadata(org.apache.jempbox.xmp.XMPMetadata) XMPSchema(org.apache.jempbox.xmp.XMPSchema) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) PDMetadata(org.apache.pdfbox.pdmodel.common.PDMetadata) LinkedList(java.util.LinkedList) PDDocumentCatalog(org.apache.pdfbox.pdmodel.PDDocumentCatalog) Test(org.junit.Test)

Example 3 with XMPSchemaDublinCore

use of org.apache.jempbox.xmp.XMPSchemaDublinCore in project jabref by JabRef.

the class XMPUtilTest method testResolveStrings2.

/**
     * A better testcase for resolveStrings. Makes sure that also the document information and dublin core are written
     * correctly.
     * <p/>
     * Data was contributed by Philip K.F. Hölzenspies (p.k.f.holzenspies [at] utwente.nl).
     *
     * @throws IOException
     * @throws TransformerException
     */
@Test
public void testResolveStrings2() throws IOException, TransformerException {
    try (BufferedReader fr = Files.newBufferedReader(Paths.get("src/test/resources/org/jabref/util/twente.bib"), StandardCharsets.UTF_8)) {
        ParserResult result = new BibtexParser(importFormatPreferences).parse(fr);
        Assert.assertEquals("Arvind", result.getDatabase().resolveForStrings("#Arvind#"));
        AuthorList originalAuthors = AuthorList.parse("Patterson, David and Arvind and Asanov\\'\\i{}c, Krste and Chiou, Derek and Hoe, James and Kozyrakis, Christos and Lu, S{hih-Lien} and Oskin, Mark and Rabaey, Jan and Wawrzynek, John");
        try {
            XMPUtil.writeXMP(pdfFile, result.getDatabase().getEntryByKey("Patterson06").get(), result.getDatabase(), xmpPreferences);
            // Test whether we the main function can load the bibtex correctly
            BibEntry b = XMPUtil.readXMP(pdfFile, xmpPreferences).get(0);
            Assert.assertNotNull(b);
            Assert.assertEquals(originalAuthors, AuthorList.parse(b.getField("author").get()));
            // Next check from Document Information
            try (PDDocument document = PDDocument.load(pdfFile.getAbsoluteFile())) {
                Assert.assertEquals(originalAuthors, AuthorList.parse(document.getDocumentInformation().getAuthor()));
                b = XMPUtil.getBibtexEntryFromDocumentInformation(document.getDocumentInformation()).get();
                Assert.assertEquals(originalAuthors, AuthorList.parse(b.getField("author").get()));
                // Now check from Dublin Core
                PDDocumentCatalog catalog = document.getDocumentCatalog();
                PDMetadata metaRaw = catalog.getMetadata();
                if (metaRaw == null) {
                    Assert.fail();
                    // To avoid warnings
                    return;
                }
                XMPMetadata meta = new XMPMetadata(XMLUtil.parse(metaRaw.createInputStream()));
                meta.addXMLNSMapping(XMPSchemaBibtex.NAMESPACE, XMPSchemaBibtex.class);
                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("David Patterson", dcSchema.getCreators().get(0));
                Assert.assertEquals("Arvind", dcSchema.getCreators().get(1));
                Assert.assertEquals("Krste Asanov\\'\\i{}c", dcSchema.getCreators().get(2));
                b = XMPUtil.getBibtexEntryFromDublinCore(dcSchema, xmpPreferences).get();
                Assert.assertNotNull(b);
                Assert.assertEquals(originalAuthors, AuthorList.parse(b.getField("author").get()));
            }
        } finally {
            if (!pdfFile.delete()) {
                System.err.println("Cannot delete temporary file");
            }
        }
    }
}
Also used : XMPSchemaDublinCore(org.apache.jempbox.xmp.XMPSchemaDublinCore) ParserResult(org.jabref.logic.importer.ParserResult) BibEntry(org.jabref.model.entry.BibEntry) XMPMetadata(org.apache.jempbox.xmp.XMPMetadata) XMPSchema(org.apache.jempbox.xmp.XMPSchema) BibtexParser(org.jabref.logic.importer.fileformat.BibtexParser) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) BufferedReader(java.io.BufferedReader) AuthorList(org.jabref.model.entry.AuthorList) PDMetadata(org.apache.pdfbox.pdmodel.common.PDMetadata) PDDocumentCatalog(org.apache.pdfbox.pdmodel.PDDocumentCatalog) Test(org.junit.Test)

Example 4 with XMPSchemaDublinCore

use of org.apache.jempbox.xmp.XMPSchemaDublinCore in project com.revolsys.open by revolsys.

the class SaveAsPdf method save.

public static void save() {
    final Project project = Project.get();
    final String directory = "/Users/paustin/Downloads/";
    final File file = new File(directory, project.getName() + ".pdf");
    try {
        final PDDocument document = new PDDocument();
        final Viewport2D viewport = project.getViewport();
        BoundingBox boundingBox = viewport.getBoundingBox();
        final int width = viewport.getViewWidthPixels();
        final int height = viewport.getViewHeightPixels();
        final int srid = boundingBox.getCoordinateSystemId();
        if (srid == 3857) {
            boundingBox = boundingBox.convert(boundingBox.getGeometryFactory().getGeographicGeometryFactory());
        }
        final PDRectangle pageSize = new PDRectangle(width, height);
        final PDPage page = new PDPage(pageSize);
        try (PdfViewport pdfViewport = new PdfViewport(document, page, project, width, height, boundingBox)) {
            final LayerRenderer<? extends Layer> renderer = project.getRenderer();
            renderer.render(pdfViewport);
        }
        document.addPage(page);
        final PDDocumentCatalog catalog = document.getDocumentCatalog();
        final PDMetadata metadata = new PDMetadata(document);
        catalog.setMetadata(metadata);
        // jempbox version
        final XMPMetadata xmp = new XMPMetadata();
        final XMPSchemaDublinCore xmpSchema = new XMPSchemaDublinCore(xmp);
        xmp.addSchema(xmpSchema);
        xmpSchema.setAbout("");
        metadata.importXMPMetadata(xmp);
        document.save(file);
    } catch (final Throwable e) {
        Logs.error(SaveAsPdf.class, "Unable to create PDF " + file, e);
    }
}
Also used : XMPSchemaDublinCore(org.apache.jempbox.xmp.XMPSchemaDublinCore) PDPage(org.apache.pdfbox.pdmodel.PDPage) PDMetadata(org.apache.pdfbox.pdmodel.common.PDMetadata) PDDocumentCatalog(org.apache.pdfbox.pdmodel.PDDocumentCatalog) Project(com.revolsys.swing.map.layer.Project) Viewport2D(com.revolsys.swing.map.Viewport2D) XMPMetadata(org.apache.jempbox.xmp.XMPMetadata) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) BoundingBox(com.revolsys.geometry.model.BoundingBox) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle) File(java.io.File)

Example 5 with XMPSchemaDublinCore

use of org.apache.jempbox.xmp.XMPSchemaDublinCore in project openolat by klemens.

the class PdfDocument method addMetadata.

public void addMetadata(String title, String subject, String author) throws IOException, TransformerException {
    PDDocumentCatalog catalog = document.getDocumentCatalog();
    PDDocumentInformation info = document.getDocumentInformation();
    Calendar date = Calendar.getInstance();
    info.setAuthor(author);
    info.setCreator(author);
    info.setCreationDate(date);
    info.setModificationDate(date);
    info.setTitle(title);
    info.setSubject(subject);
    XMPMetadata metadata = new XMPMetadata();
    XMPSchemaPDF pdfSchema = metadata.addPDFSchema();
    pdfSchema.setProducer("OpenOLAT");
    XMPSchemaBasic basicSchema = metadata.addBasicSchema();
    basicSchema.setModifyDate(date);
    basicSchema.setCreateDate(date);
    basicSchema.setCreatorTool("OpenOLAT");
    basicSchema.setMetadataDate(date);
    XMPSchemaDublinCore dcSchema = metadata.addDublinCoreSchema();
    dcSchema.setTitle(title);
    dcSchema.addCreator(author);
    dcSchema.setDescription(subject);
    PDMetadata metadataStream = new PDMetadata(document);
    metadataStream.importXMPMetadata(metadata);
    catalog.setMetadata(metadataStream);
}
Also used : XMPSchemaDublinCore(org.apache.jempbox.xmp.XMPSchemaDublinCore) XMPMetadata(org.apache.jempbox.xmp.XMPMetadata) XMPSchemaBasic(org.apache.jempbox.xmp.XMPSchemaBasic) Calendar(java.util.Calendar) XMPSchemaPDF(org.apache.jempbox.xmp.XMPSchemaPDF) PDMetadata(org.apache.pdfbox.pdmodel.common.PDMetadata) PDDocumentInformation(org.apache.pdfbox.pdmodel.PDDocumentInformation) PDDocumentCatalog(org.apache.pdfbox.pdmodel.PDDocumentCatalog)

Aggregations

XMPMetadata (org.apache.jempbox.xmp.XMPMetadata)10 XMPSchemaDublinCore (org.apache.jempbox.xmp.XMPSchemaDublinCore)10 PDDocumentCatalog (org.apache.pdfbox.pdmodel.PDDocumentCatalog)8 PDMetadata (org.apache.pdfbox.pdmodel.common.PDMetadata)8 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)7 XMPSchema (org.apache.jempbox.xmp.XMPSchema)6 BibEntry (org.jabref.model.entry.BibEntry)6 Calendar (java.util.Calendar)4 LinkedList (java.util.LinkedList)4 PDDocumentInformation (org.apache.pdfbox.pdmodel.PDDocumentInformation)4 Test (org.junit.Test)4 XMPSchemaBasic (org.apache.jempbox.xmp.XMPSchemaBasic)3 IOException (java.io.IOException)2 XMPSchemaPDF (org.apache.jempbox.xmp.XMPSchemaPDF)2 TypedBibEntry (org.jabref.logic.TypedBibEntry)2 AuthorList (org.jabref.model.entry.AuthorList)2 BoundingBox (com.revolsys.geometry.model.BoundingBox)1 Viewport2D (com.revolsys.swing.map.Viewport2D)1 Project (com.revolsys.swing.map.layer.Project)1 BufferedReader (java.io.BufferedReader)1