Search in sources :

Example 11 with XMPMetadata

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

the class XMPUtilTest method testSimpleUpdate.

/**
     * Tests whether writing BibTex.xmp will preserve existing XMP-descriptions.
     *
     * @throws Exception (indicating an failure)
     */
@Test
public void testSimpleUpdate() throws Exception {
    String s = " <rdf:Description rdf:about=''" + "  xmlns:xmp='http://ns.adobe.com/xap/1.0/'>" + "  <xmp:CreatorTool>Acrobat PDFMaker 7.0.7</xmp:CreatorTool>" + "  <xmp:ModifyDate>2006-08-07T18:50:24+02:00</xmp:ModifyDate>" + "  <xmp:CreateDate>2006-08-07T14:44:24+02:00</xmp:CreateDate>" + "  <xmp:MetadataDate>2006-08-07T18:50:24+02:00</xmp:MetadataDate>" + " </rdf:Description>" + " <rdf:Description rdf:about=''" + "  xmlns:xapMM='http://ns.adobe.com/xap/1.0/mm/'>" + "  <xapMM:DocumentID>uuid:843cd67d-495e-4c1e-a4cd-64178f6b3299</xapMM:DocumentID>" + "  <xapMM:InstanceID>uuid:1e56b4c0-6782-440d-ba76-d2b3d87547d1</xapMM:InstanceID>" + "  <xapMM:VersionID>" + "   <rdf:Seq>" + "    <rdf:li>17</rdf:li>" + "   </rdf:Seq>" + "  </xapMM:VersionID>" + " </rdf:Description>" + " <rdf:Description rdf:about=''" + "  xmlns:dc='http://purl.org/dc/elements/1.1/'>" + "  <dc:format>application/pdf</dc:format>" + "</rdf:Description>";
    writeManually(pdfFile, XMPUtilTest.bibtexXPacket(s));
    // Nothing there yet, but should not crash
    Assert.assertEquals(Collections.emptyList(), XMPUtil.readXMP(pdfFile, xmpPreferences));
    s = " <rdf:Description rdf:about=''" + "  xmlns:xmp='http://ns.adobe.com/xap/1.0/'>" + "  <xmp:CreatorTool>Acrobat PDFMaker 7.0.7</xmp:CreatorTool>" + "  <xmp:ModifyDate>2006-08-07T18:50:24+02:00</xmp:ModifyDate>" + "  <xmp:CreateDate>2006-08-07T14:44:24+02:00</xmp:CreateDate>" + "  <xmp:MetadataDate>2006-08-07T18:50:24+02:00</xmp:MetadataDate>" + " </rdf:Description>" + " <rdf:Description rdf:about=''" + "  xmlns:xapMM='http://ns.adobe.com/xap/1.0/mm/'>" + "  <xapMM:DocumentID>uuid:843cd67d-495e-4c1e-a4cd-64178f6b3299</xapMM:DocumentID>" + "  <xapMM:InstanceID>uuid:1e56b4c0-6782-440d-ba76-d2b3d87547d1</xapMM:InstanceID>" + "  <xapMM:VersionID>" + "   <rdf:Seq>" + "    <rdf:li>17</rdf:li>" + "   </rdf:Seq>" + "  </xapMM:VersionID>" + " </rdf:Description>" + " <rdf:Description rdf:about=''" + "  xmlns:dc='http://purl.org/dc/elements/1.1/'>" + "  <dc:format>application/pdf</dc:format>" + "  <dc:title>" + "   <rdf:Alt>" + "    <rdf:li xml:lang='x-default'>Questionnaire.pdf</rdf:li>" + "   </rdf:Alt>" + "  </dc:title>" + "</rdf:Description>";
    writeManually(pdfFile, XMPUtilTest.bibtexXPacket(s));
    // Title is Questionnaire.pdf so the DublinCore fallback should hit
    // in...
    Assert.assertEquals(1, XMPUtil.readXMP(pdfFile, xmpPreferences).size());
    {
        // Now write new packet and check if it was correctly written
        XMPUtil.writeXMP(pdfFile, t1BibtexEntry(), null, xmpPreferences);
        List<BibEntry> l = XMPUtil.readXMP(pdfFile.getAbsoluteFile(), xmpPreferences);
        Assert.assertEquals(1, l.size());
        BibEntry e = l.get(0);
        assertEqualsBibtexEntry(t1BibtexEntry(), e);
        try (PDDocument document = PDDocument.load(pdfFile.getAbsoluteFile())) {
            if (document.isEncrypted()) {
                throw new IOException("Error: Cannot read metadata from encrypted document.");
            }
            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);
            List<XMPSchema> schemas = meta.getSchemas();
            Assert.assertEquals(4, schemas.size());
            schemas = meta.getSchemasByNamespaceURI(XMPSchemaBibtex.NAMESPACE);
            Assert.assertEquals(1, schemas.size());
            schemas = meta.getSchemasByNamespaceURI(XMPSchemaDublinCore.NAMESPACE);
            Assert.assertEquals(1, schemas.size());
            XMPSchemaDublinCore dc = (XMPSchemaDublinCore) schemas.get(0);
            Assert.assertEquals("application/pdf", dc.getFormat());
            schemas = meta.getSchemasByNamespaceURI(XMPSchemaBasic.NAMESPACE);
            Assert.assertEquals(1, schemas.size());
            XMPSchemaBasic bs = (XMPSchemaBasic) schemas.get(0);
            Assert.assertEquals("Acrobat PDFMaker 7.0.7", bs.getCreatorTool());
            Calendar c = Calendar.getInstance();
            c.clear();
            c.set(Calendar.YEAR, 2006);
            c.set(Calendar.MONTH, Calendar.AUGUST);
            c.set(Calendar.DATE, 7);
            c.set(Calendar.HOUR, 14);
            c.set(Calendar.MINUTE, 44);
            c.set(Calendar.SECOND, 24);
            c.setTimeZone(TimeZone.getTimeZone("GMT+2"));
            Calendar other = bs.getCreateDate();
            Assert.assertEquals(c.get(Calendar.YEAR), other.get(Calendar.YEAR));
            Assert.assertEquals(c.get(Calendar.MONTH), other.get(Calendar.MONTH));
            Assert.assertEquals(c.get(Calendar.DATE), other.get(Calendar.DATE));
            Assert.assertEquals(c.get(Calendar.HOUR), other.get(Calendar.HOUR));
            Assert.assertEquals(c.get(Calendar.MINUTE), other.get(Calendar.MINUTE));
            Assert.assertEquals(c.get(Calendar.SECOND), other.get(Calendar.SECOND));
            Assert.assertTrue(c.getTimeZone().hasSameRules(other.getTimeZone()));
            schemas = meta.getSchemasByNamespaceURI(XMPSchemaMediaManagement.NAMESPACE);
            Assert.assertEquals(1, schemas.size());
            XMPSchemaMediaManagement mm = (XMPSchemaMediaManagement) schemas.get(0);
            Assert.assertEquals("17", mm.getSequenceList("xapMM:VersionID").get(0));
        }
    }
    // Now alter the Bibtex entry, write it and do all the checks again
    BibEntry toSet = t1BibtexEntry();
    toSet.setField("author", "Pokemon!");
    XMPUtil.writeXMP(pdfFile, toSet, null, xmpPreferences);
    List<BibEntry> l = XMPUtil.readXMP(pdfFile.getAbsoluteFile(), xmpPreferences);
    Assert.assertEquals(1, l.size());
    BibEntry e = l.get(0);
    assertEqualsBibtexEntry(toSet, e);
    try (PDDocument document = PDDocument.load(pdfFile.getAbsoluteFile())) {
        if (document.isEncrypted()) {
            throw new IOException("Error: Cannot read metadata from encrypted document.");
        }
        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);
        List<XMPSchema> schemas = meta.getSchemas();
        Assert.assertEquals(4, schemas.size());
        schemas = meta.getSchemasByNamespaceURI(XMPSchemaBibtex.NAMESPACE);
        Assert.assertEquals(1, schemas.size());
        schemas = meta.getSchemasByNamespaceURI(XMPSchemaDublinCore.NAMESPACE);
        Assert.assertEquals(1, schemas.size());
        XMPSchemaDublinCore dc = (XMPSchemaDublinCore) schemas.get(0);
        Assert.assertEquals("application/pdf", dc.getFormat());
        schemas = meta.getSchemasByNamespaceURI(XMPSchemaBasic.NAMESPACE);
        Assert.assertEquals(1, schemas.size());
        XMPSchemaBasic bs = (XMPSchemaBasic) schemas.get(0);
        Assert.assertEquals("Acrobat PDFMaker 7.0.7", bs.getCreatorTool());
        Calendar c = Calendar.getInstance();
        c.clear();
        c.set(Calendar.YEAR, 2006);
        c.set(Calendar.MONTH, 7);
        c.set(Calendar.DATE, 7);
        c.set(Calendar.HOUR, 14);
        c.set(Calendar.MINUTE, 44);
        c.set(Calendar.SECOND, 24);
        c.setTimeZone(TimeZone.getTimeZone("GMT+2"));
        Calendar other = bs.getCreateDate();
        Assert.assertEquals(c.get(Calendar.YEAR), other.get(Calendar.YEAR));
        Assert.assertEquals(c.get(Calendar.MONTH), other.get(Calendar.MONTH));
        Assert.assertEquals(c.get(Calendar.DATE), other.get(Calendar.DATE));
        Assert.assertEquals(c.get(Calendar.HOUR), other.get(Calendar.HOUR));
        Assert.assertEquals(c.get(Calendar.MINUTE), other.get(Calendar.MINUTE));
        Assert.assertEquals(c.get(Calendar.SECOND), other.get(Calendar.SECOND));
        Assert.assertTrue(c.getTimeZone().hasSameRules(other.getTimeZone()));
        schemas = meta.getSchemasByNamespaceURI(XMPSchemaMediaManagement.NAMESPACE);
        Assert.assertEquals(1, schemas.size());
        XMPSchemaMediaManagement mm = (XMPSchemaMediaManagement) schemas.get(0);
        Assert.assertEquals("17", mm.getSequenceList("xapMM:VersionID").get(0));
    }
}
Also used : XMPSchemaDublinCore(org.apache.jempbox.xmp.XMPSchemaDublinCore) BibEntry(org.jabref.model.entry.BibEntry) XMPSchemaBasic(org.apache.jempbox.xmp.XMPSchemaBasic) XMPSchema(org.apache.jempbox.xmp.XMPSchema) Calendar(java.util.Calendar) IOException(java.io.IOException) PDMetadata(org.apache.pdfbox.pdmodel.common.PDMetadata) PDDocumentCatalog(org.apache.pdfbox.pdmodel.PDDocumentCatalog) XMPSchemaMediaManagement(org.apache.jempbox.xmp.XMPSchemaMediaManagement) XMPMetadata(org.apache.jempbox.xmp.XMPMetadata) PDDocument(org.apache.pdfbox.pdmodel.PDDocument) AuthorList(org.jabref.model.entry.AuthorList) List(java.util.List) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 12 with XMPMetadata

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

the class XMPUtilTest method testReadRawXMP.

@Test
public void testReadRawXMP() throws IOException, TransformerException {
    ParserResult result = BibtexParser.parse(new StringReader("@article{canh05," + "  author = {Crowston, K. and Annabi, H. and Howison, J. and Masango, C.},\n" + "  title = {Effective work practices for floss development: A model and propositions},\n" + "  booktitle = {Hawaii International Conference On System Sciences (HICSS)},\n" + "  year = {2005},\n" + "  owner = {oezbek},\n" + "  timestamp = {2006.05.29},\n" + "  url = {http://james.howison.name/publications.html}}"), importFormatPreferences);
    Collection<BibEntry> c = result.getDatabase().getEntries();
    Assert.assertEquals(1, c.size());
    BibEntry e = c.iterator().next();
    XMPUtil.writeXMP(pdfFile, e, null, xmpPreferences);
    Optional<XMPMetadata> metadata = XMPUtil.readRawXMP(pdfFile);
    Assert.assertTrue(metadata.isPresent());
    List<XMPSchema> schemas = metadata.get().getSchemas();
    Assert.assertEquals(2, schemas.size());
    schemas = metadata.get().getSchemasByNamespaceURI(XMPSchemaBibtex.NAMESPACE);
    Assert.assertEquals(1, schemas.size());
    XMPSchemaBibtex bib = (XMPSchemaBibtex) schemas.get(0);
    List<String> authors = bib.getSequenceList("author");
    Assert.assertEquals(4, authors.size());
    Assert.assertEquals("K. Crowston", authors.get(0));
    Assert.assertEquals("H. Annabi", authors.get(1));
    Assert.assertEquals("J. Howison", authors.get(2));
    Assert.assertEquals("C. Masango", authors.get(3));
    Assert.assertEquals("article", bib.getTextProperty("entrytype"));
    Assert.assertEquals("Effective work practices for floss development: A model and propositions", bib.getTextProperty("title"));
    Assert.assertEquals("Hawaii International Conference On System Sciences (HICSS)", bib.getTextProperty("booktitle"));
    Assert.assertEquals("2005", bib.getTextProperty("year"));
    Assert.assertEquals("oezbek", bib.getTextProperty("owner"));
    Assert.assertEquals("http://james.howison.name/publications.html", bib.getTextProperty("url"));
}
Also used : ParserResult(org.jabref.logic.importer.ParserResult) BibEntry(org.jabref.model.entry.BibEntry) XMPMetadata(org.apache.jempbox.xmp.XMPMetadata) XMPSchema(org.apache.jempbox.xmp.XMPSchema) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 13 with XMPMetadata

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

the class XMPSchemaBibtexTest method testSetGetTextPropertyString.

@Test
public void testSetGetTextPropertyString() throws IOException {
    XMPMetadata xmp = new XMPMetadata();
    XMPSchemaBibtex bibtex = new XMPSchemaBibtex(xmp);
    bibtex.setTextProperty("title", "The advanced Flux-Compensation for Delawney-Separation");
    Element e = bibtex.getElement();
    Assert.assertEquals("The advanced Flux-Compensation for Delawney-Separation", e.getAttribute("bibtex:title"));
    Assert.assertEquals("The advanced Flux-Compensation for Delawney-Separation", bibtex.getTextProperty("title"));
    bibtex.setTextProperty("title", "The advanced Flux-Correlation for Delawney-Separation");
    e = bibtex.getElement();
    Assert.assertEquals("The advanced Flux-Correlation for Delawney-Separation", e.getAttribute("bibtex:title"));
    Assert.assertEquals("The advanced Flux-Correlation for Delawney-Separation", bibtex.getTextProperty("title"));
    bibtex.setTextProperty("abstract", "   The abstract\n can go \n \n on several \n lines with \n many \n\n empty ones in \n between.");
    Assert.assertEquals("   The abstract\n can go \n \n on several \n lines with \n many \n\n empty ones in \n between.", bibtex.getTextProperty("abstract"));
}
Also used : XMPMetadata(org.apache.jempbox.xmp.XMPMetadata) Element(org.w3c.dom.Element) Test(org.junit.Test)

Example 14 with XMPMetadata

use of org.apache.jempbox.xmp.XMPMetadata 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());
    }
}
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 15 with XMPMetadata

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

the class XMPSchemaBibtexTest method testSetGetBagListString.

@Test
public void testSetGetBagListString() throws IOException {
    XMPMetadata xmp = new XMPMetadata();
    XMPSchemaBibtex bibtex = new XMPSchemaBibtex(xmp);
    bibtex.addBagValue("author", "Tom DeMarco");
    bibtex.addBagValue("author", "Kent Beck");
    {
        List<String> l = bibtex.getBagList("author");
        Assert.assertEquals(2, l.size());
        Assert.assertTrue(l.get(0).equals("Tom DeMarco") || l.get(1).equals("Tom DeMarco"));
        Assert.assertTrue(l.get(0).equals("Kent Beck") || l.get(1).equals("Kent Beck"));
    }
    {
        bibtex.removeBagValue("author", "Kent Beck");
        List<String> l = bibtex.getBagList("author");
        Assert.assertEquals(1, l.size());
        Assert.assertTrue(l.get(0).equals("Tom DeMarco"));
    }
    {
        // Already removed
        bibtex.removeBagValue("author", "Kent Beck");
        List<String> l = bibtex.getBagList("author");
        Assert.assertEquals(1, l.size());
        Assert.assertTrue(l.get(0).equals("Tom DeMarco"));
    }
    {
        // Duplicates allowed!
        bibtex.addBagValue("author", "Tom DeMarco");
        List<String> l = bibtex.getBagList("author");
        Assert.assertEquals(2, l.size());
        Assert.assertTrue(l.get(0).equals("Tom DeMarco"));
        Assert.assertTrue(l.get(1).equals("Tom DeMarco"));
    }
    // Removes both
    bibtex.removeBagValue("author", "Tom DeMarco");
    List<String> l = bibtex.getBagList("author");
    Assert.assertEquals(0, l.size());
}
Also used : XMPMetadata(org.apache.jempbox.xmp.XMPMetadata) NodeList(org.w3c.dom.NodeList) List(java.util.List) Test(org.junit.Test)

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