01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: /* $Id: MergeMetadata.java 426584 2006-07-28 16:01:47Z jeremias $ */
19:
20: package xmp;
21:
22: import java.net.URL;
23: import java.util.Date;
24:
25: import javax.xml.transform.TransformerException;
26:
27: import org.apache.xmlgraphics.xmp.Metadata;
28: import org.apache.xmlgraphics.xmp.XMPConstants;
29: import org.apache.xmlgraphics.xmp.XMPParser;
30: import org.apache.xmlgraphics.xmp.XMPProperty;
31: import org.apache.xmlgraphics.xmp.XMPSerializer;
32: import org.apache.xmlgraphics.xmp.schemas.DublinCoreAdapter;
33: import org.xml.sax.SAXException;
34:
35: /**
36: * This example shows how to parse an XMP metadata file.
37: */
38: public class MergeMetadata {
39:
40: private static void mergeMetadata() throws TransformerException,
41: SAXException {
42: URL url = MergeMetadata.class.getResource("pdf-example.xmp");
43: Metadata meta1 = XMPParser.parseXMP(url);
44:
45: Metadata meta2 = new Metadata();
46: DublinCoreAdapter dc = new DublinCoreAdapter(meta2);
47: dc.setTitle("de", "Der Herr der Ringe");
48: dc.setTitle("en", "Lord of the Rings");
49: dc.addCreator("J.R.R. Tolkien"); //Will replace creator from pdf-example.xmp
50: dc.addDate(new Date());
51:
52: meta2.mergeInto(meta1);
53:
54: Metadata meta = meta1;
55: XMPProperty prop;
56: dc = new DublinCoreAdapter(meta);
57: String[] creators = dc.getCreators();
58: for (int i = 0, c = creators.length; i < c; i++) {
59: System.out.println("Creator: " + creators[i]);
60: }
61: System.out.println("Title: " + dc.getTitle());
62: System.out.println("Title de: " + dc.getTitle("de"));
63: System.out.println("Title en: " + dc.getTitle("en"));
64: prop = meta.getProperty(XMPConstants.XMP_BASIC_NAMESPACE,
65: "CreateDate");
66: System.out.println("Creation Date: " + prop.getValue());
67: prop = meta.getProperty(XMPConstants.XMP_BASIC_NAMESPACE,
68: "CreatorTool");
69: System.out.println("Creator Tool: " + prop.getValue());
70: prop = meta.getProperty(XMPConstants.ADOBE_PDF_NAMESPACE,
71: "Producer");
72: System.out.println("Producer: " + prop.getValue());
73: prop = meta.getProperty(XMPConstants.ADOBE_PDF_NAMESPACE,
74: "PDFVersion");
75: System.out.println("PDF version: " + prop.getValue());
76:
77: XMPSerializer.writeXMPPacket(meta, System.out, false);
78: }
79:
80: /**
81: * Command-line interface.
82: * @param args the command-line arguments
83: */
84: public static void main(String[] args) {
85: try {
86: mergeMetadata();
87: } catch (Exception e) {
88: e.printStackTrace();
89: }
90: }
91:
92: }
|