File: ExtractMetadataPacket.java

package info (click to toggle)
xmlgraphics-commons 2.3-1%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,328 kB
  • sloc: java: 35,593; xml: 2,127; makefile: 6
file content (98 lines) | stat: -rw-r--r-- 3,499 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id: ExtractMetadataPacket.java 606567 2007-12-23 15:52:49Z jeremias $ */

package xmp;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.xml.transform.TransformerException;

import org.apache.xmlgraphics.xmp.Metadata;
import org.apache.xmlgraphics.xmp.XMPArray;
import org.apache.xmlgraphics.xmp.XMPConstants;
import org.apache.xmlgraphics.xmp.XMPPacketParser;
import org.apache.xmlgraphics.xmp.XMPProperty;

/**
 * This example shows how to parse an XMP packet from an arbitrary file.
 */
public class ExtractMetadataPacket {

    private static void parseMetadata() throws IOException, TransformerException {
        URL url = ExtractMetadataPacket.class.getResource("xmp-sandbox.fop.trunk.pdf");
        InputStream in = url.openStream();
        try {
            Metadata meta = XMPPacketParser.parse(in);
            if (meta == null) {
                System.err.println("No XMP packet found!");
            } else {
                dumpSomeMetadata(meta);
            }
        } finally {
            in.close();
        }
    }

    private static void dumpSomeMetadata(Metadata meta) {
        XMPProperty prop;
        prop = meta.getProperty(XMPConstants.DUBLIN_CORE_NAMESPACE, "creator");
        if (prop != null) {
            XMPArray array;
            array = prop.getArrayValue();
            for (int i = 0, c = array.getSize(); i < c; i++) {
                System.out.println("Creator: " + array.getValue(i));
            }
        }
        prop = meta.getProperty(XMPConstants.DUBLIN_CORE_NAMESPACE, "title");
        if (prop != null) {
            System.out.println("Title: " + prop.getValue());
        }
        prop = meta.getProperty(XMPConstants.XMP_BASIC_NAMESPACE, "CreateDate");
        if (prop != null) {
            System.out.println("Creation Date: " + prop.getValue());
        }
        prop = meta.getProperty(XMPConstants.XMP_BASIC_NAMESPACE, "CreatorTool");
        if (prop != null) {
            System.out.println("Creator Tool: " + prop.getValue());
        }
        prop = meta.getProperty(XMPConstants.ADOBE_PDF_NAMESPACE, "Producer");
        if (prop != null) {
            System.out.println("Producer: " + prop.getValue());
        }
        prop = meta.getProperty(XMPConstants.ADOBE_PDF_NAMESPACE, "PDFVersion");
        if (prop != null) {
            System.out.println("PDF version: " + prop.getValue());
        }
    }

    /**
     * Command-line interface.
     * @param args the command-line arguments
     */
    public static void main(String[] args) {
        try {
            parseMetadata();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}