custom schema to XMP metadata

老子叫甜甜 提交于 2019-12-04 22:28:19

Finally I got it working myself. The solution is to use the another constructor of the XMPMetadata class that accepts a predefined document class.

        document = PDDocument.load("/home/silver/SVNRoot/ecomail/trunk/sample1.pdf");

        PDDocumentCatalog catalog2 = document.getDocumentCatalog();
        PDMetadata metadataStream2 = catalog2.getMetadata();
        System.out.println(metadataStream2.getInputStreamAsString());
        InputStream xmpIn = metadataStream2.createInputStream();

        DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
        f.setExpandEntityReferences(true);
        f.setIgnoringComments(true);
        f.setIgnoringElementContentWhitespace(true);
        f.setValidating(false);
        f.setCoalescing(true);
        f.setNamespaceAware(true);
        DocumentBuilder builder = f.newDocumentBuilder();
        Document xmpDoc = builder.parse(xmpIn);

        EMXMetadata emxMetadata = new EMXMetadata(xmpDoc);
        EMXSchema emxSchema2 = emxMetadata.getEMXSchema();
        System.out.println("recipients : " + emxSchema2.getRecipients());

Now my custom emxMetadata contains non null emxSchema2 object and I can get back my recipient objects from it. However to make it work I had to modify EMXMetadata to support XMLNamespaceMapping for your schema class

public class EMXMetadata extends XMPMetadata {

public EMXMetadata() throws IOException {
    super();
    addXMLNSMapping(EMXSchema.NAMESPACE, EMXSchema.class);
}

public EMXMetadata(Document xmpDoc) {
    super(xmpDoc);
    addXMLNSMapping(EMXSchema.NAMESPACE, EMXSchema.class);
}

public EMXSchema addEMXSchema() {
    EMXSchema schema = new EMXSchema(this);
    return (EMXSchema) basicAddSchema(schema);
}

public EMXSchema getEMXSchema() throws IOException {
    return (EMXSchema) getSchemaByClass(EMXSchema.class);
}

}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!