KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > xml > xmp > XmpWriter


1 /*
2  * $Id: XmpWriter.java 2399 2006-09-21 16:45:04Z xlv $
3  * $Name$
4  *
5  * Copyright 2005 by Bruno Lowagie.
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999-2005 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000-2005 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU LIBRARY GENERAL PUBLIC LICENSE for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50
51 package com.lowagie.text.xml.xmp;
52
53 import java.io.IOException JavaDoc;
54 import java.io.OutputStream JavaDoc;
55 import java.io.OutputStreamWriter JavaDoc;
56 import java.util.Iterator JavaDoc;
57 import java.util.Map JavaDoc;
58
59 import com.lowagie.text.pdf.PdfDate;
60 import com.lowagie.text.pdf.PdfDictionary;
61 import com.lowagie.text.pdf.PdfName;
62 import com.lowagie.text.pdf.PdfObject;
63 import com.lowagie.text.pdf.PdfString;
64
65 /**
66  * With this class you can create an Xmp Stream that can be used for adding
67  * Metadata to a PDF Dictionary. Remark that this class doesn't cover the
68  * complete XMP specification.
69  */

70 public class XmpWriter {
71
72     /** A possible charset for the XMP. */
73     public static final String JavaDoc UTF8 = "UTF-8";
74     /** A possible charset for the XMP. */
75     public static final String JavaDoc UTF16 = "UTF-16";
76     /** A possible charset for the XMP. */
77     public static final String JavaDoc UTF16BE = "UTF-16BE";
78     /** A possible charset for the XMP. */
79     public static final String JavaDoc UTF16LE = "UTF-16LE";
80     
81     /** String used to fill the extra space. */
82     public static final String JavaDoc EXTRASPACE = " \n";
83     
84     /** You can add some extra space in the XMP packet; 1 unit in this variable represents 100 spaces and a newline. */
85     protected int extraSpace;
86     
87     /** The writer to which you can write bytes for the XMP stream. */
88     protected OutputStreamWriter JavaDoc writer;
89     
90     /** The about string that goes into the rdf:Description tags. */
91     protected String JavaDoc about;
92     
93     /** The end attribute. */
94     protected char end = 'w';
95     
96     /**
97      * Creates an XmpWriter.
98      * @param os
99      * @param utfEncoding
100      * @param extraSpace
101      * @throws IOException
102      */

103     public XmpWriter(OutputStream JavaDoc os, String JavaDoc utfEncoding, int extraSpace) throws IOException JavaDoc {
104         this.extraSpace = extraSpace;
105         writer = new OutputStreamWriter JavaDoc(os, utfEncoding);
106         writer.write("<?xpacket begin='\uFEFF' id='W5M0MpCehiHzreSzNTczkc9d' ?>\n");
107         writer.write("<x:xmpmeta xmlns:x='adobe:ns:meta/'>\n");
108         writer.write("<rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'>\n");
109         about = "";
110     }
111     
112     /**
113      * Creates an XmpWriter.
114      * @param os
115      * @throws IOException
116      */

117     public XmpWriter(OutputStream JavaDoc os) throws IOException JavaDoc {
118         this(os, UTF8, 20);
119     }
120     
121     /** Sets the XMP to read-only */
122     public void setReadOnly() {
123         end = 'r';
124     }
125     
126     /**
127      * @param about The about to set.
128      */

129     public void setAbout(String JavaDoc about) {
130         this.about = about;
131     }
132     
133     /**
134      * Adds an rdf:Description.
135      * @param xmlns
136      * @param content
137      * @throws IOException
138      */

139     public void addRdfDescription(String JavaDoc xmlns, String JavaDoc content) throws IOException JavaDoc {
140         writer.write("<rdf:Description rdf:about='");
141         writer.write(about);
142         writer.write("' ");
143         writer.write(xmlns);
144         writer.write(">");
145         writer.write(content);
146         writer.write("</rdf:Description>\n");
147     }
148     
149     /**
150      * Adds an rdf:Description.
151      * @param s
152      * @throws IOException
153      */

154     public void addRdfDescription(XmpSchema s) throws IOException JavaDoc {
155         writer.write("<rdf:Description rdf:about='");
156         writer.write(about);
157         writer.write("' ");
158         writer.write(s.getXmlns());
159         writer.write(">");
160         writer.write(s.toString());
161         writer.write("</rdf:Description>\n");
162     }
163     
164     /**
165      * Flushes and closes the XmpWriter.
166      * @throws IOException
167      */

168     public void close() throws IOException JavaDoc {
169         writer.write("</rdf:RDF>");
170         writer.write("</x:xmpmeta>\n");
171         for (int i = 0; i < extraSpace; i++) {
172             writer.write(EXTRASPACE);
173         }
174         writer.write("<?xpacket ends='" + end + "' ?>");
175         writer.flush();
176         writer.close();
177     }
178     
179     /**
180      * @param os
181      * @param info
182      * @throws IOException
183      */

184     public XmpWriter(OutputStream JavaDoc os, PdfDictionary info) throws IOException JavaDoc {
185         this(os);
186         if (info != null) {
187             DublinCoreSchema dc = new DublinCoreSchema();
188             PdfSchema p = new PdfSchema();
189             XmpBasicSchema basic = new XmpBasicSchema();
190             PdfName key;
191             PdfObject obj;
192             for (Iterator JavaDoc it = info.getKeys().iterator(); it.hasNext();) {
193                 key = (PdfName)it.next();
194                 obj = info.get(key);
195                 if (obj == null)
196                     continue;
197                 if (PdfName.TITLE.equals(key)) {
198                     dc.addTitle(((PdfString)obj).toUnicodeString());
199                 }
200                 if (PdfName.AUTHOR.equals(key)) {
201                     dc.addAuthor(((PdfString)obj).toUnicodeString());
202                 }
203                 if (PdfName.SUBJECT.equals(key)) {
204                     dc.addSubject(((PdfString)obj).toUnicodeString());
205                 }
206                 if (PdfName.KEYWORDS.equals(key)) {
207                     p.addKeywords(((PdfString)obj).toUnicodeString());
208                 }
209                 if (PdfName.CREATOR.equals(key)) {
210                     basic.addCreatorTool(((PdfString)obj).toUnicodeString());
211                 }
212                 if (PdfName.PRODUCER.equals(key)) {
213                     p.addProducer(((PdfString)obj).toUnicodeString());
214                 }
215                 if (PdfName.CREATIONDATE.equals(key)) {
216                     basic.addCreateDate(((PdfDate)obj).getW3CDate());
217                 }
218                 if (PdfName.MODDATE.equals(key)) {
219                     basic.addModDate(((PdfDate)obj).getW3CDate());
220                 }
221             }
222             if (dc.size() > 0) addRdfDescription(dc);
223             if (p.size() > 0) addRdfDescription(p);
224             if (basic.size() > 0) addRdfDescription(basic);
225         }
226     }
227     
228     /**
229      * @param os
230      * @param info
231      * @throws IOException
232      */

233     public XmpWriter(OutputStream JavaDoc os, Map JavaDoc info) throws IOException JavaDoc {
234         this(os);
235         if (info != null) {
236             DublinCoreSchema dc = new DublinCoreSchema();
237             PdfSchema p = new PdfSchema();
238             XmpBasicSchema basic = new XmpBasicSchema();
239             String JavaDoc key;
240             String JavaDoc value;
241             for (Iterator JavaDoc it = info.entrySet().iterator(); it.hasNext();) {
242                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
243                 key = (String JavaDoc) entry.getKey();
244                 value = (String JavaDoc) entry.getValue();
245                 if (value == null)
246                     continue;
247                 if ("Title".equals(key)) {
248                     dc.addTitle(value);
249                 }
250                 if ("Author".equals(key)) {
251                     dc.addAuthor(value);
252                 }
253                 if ("Subject".equals(key)) {
254                     dc.addSubject(value);
255                 }
256                 if ("Keywords".equals(key)) {
257                     p.addKeywords(value);
258                 }
259                 if ("Creator".equals(key)) {
260                     basic.addCreatorTool(value);
261                 }
262                 if ("Producer".equals(key)) {
263                     p.addProducer(value);
264                 }
265                 if ("CreationDate".equals(key)) {
266                     basic.addCreateDate(PdfDate.getW3CDate(value));
267                 }
268                 if ("ModDate".equals(key)) {
269                     basic.addModDate(PdfDate.getW3CDate(value));
270                 }
271             }
272             if (dc.size() > 0) addRdfDescription(dc);
273             if (p.size() > 0) addRdfDescription(p);
274             if (basic.size() > 0) addRdfDescription(basic);
275         }
276     }
277 }
Popular Tags