1 /*
2  * $Id$
3  *
4  * Copyright 2005 by Bruno Lowagie.
5  *
6  * The contents of this file are subject to the Mozilla Public License Version 1.1
7  * (the "License"); you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the License.
13  *
14  * The Original Code is 'iText, a free JAVA-PDF library'.
15  *
16  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17  * the Initial Developer are Copyright (C) 1999-2005 by Bruno Lowagie.
18  * All Rights Reserved.
19  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20  * are Copyright (C) 2000-2005 by Paulo Soares. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source code
23  * where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above.  If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE
34  *
35  * This library is free software; you can redistribute it and/or modify it
36  * under the terms of the MPL as stated above or under the terms of the GNU
37  * Library General Public License as published by the Free Software Foundation;
38  * either version 2 of the License, or any later version.
39  *
40  * This library is distributed in the hope that it will be useful, but WITHOUT
41  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42  * FOR A PARTICULAR PURPOSE. See the GNU LIBRARY GENERAL PUBLIC LICENSE for more
43  * details.
44  *
45  * If you didn't download this code from the following link, you should check if
46  * you aren't using an obsolete version:
47  * http://www.lowagie.com/iText/
48  */
49 
50 package com.lowagie.text.xml.xmp;
51 
52 import com.lowagie.text.pdf.PdfWriter;
53 import java.io.IOException;
54 import java.io.OutputStream;
55 import java.io.OutputStreamWriter;
56 import java.util.Iterator;
57 import java.util.Map;
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 UTF8 = "UTF-8";
74 	/** A possible charset for the XMP. */
75 	public static final String UTF16 = "UTF-16";
76 	/** A possible charset for the XMP. */
77 	public static final String UTF16BE = "UTF-16BE";
78 	/** A possible charset for the XMP. */
79 	public static final String UTF16LE = "UTF-16LE";
80 
81 	/** String used to fill the extra space. */
82 	public static final String 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 writer;
89 
90 	/** The about string that goes into the rdf:Description tags. */
91 	protected String about;
92 
93 	/**
94 	 * Processing Instruction required at the start of an XMP stream
95 	 * @since iText 2.1.6
96 	 */
97 	public static final String XPACKET_PI_BEGIN = "<?xpacket begin=\"\uFEFF\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n";
98 
99 	/**
100 	 * Processing Instruction required at the end of an XMP stream for XMP streams that can be updated
101 	 * @since iText 2.1.6
102 	 */
103 	public static final String XPACKET_PI_END_W = "<?xpacket end=\"w\"?>";
104 
105 	/**
106 	 * Processing Instruction required at the end of an XMP stream for XMP streams that are read only
107 	 * @since iText 2.1.6
108 	 */
109 	public static final String XPACKET_PI_END_R = "<?xpacket end=\"r\"?>";
110 
111 	/** The end attribute. */
112 	protected char end = 'w';
113 
114 	/**
115 	 * Creates an XmpWriter.
116 	 * @param os
117 	 * @param utfEncoding
118 	 * @param extraSpace
119 	 * @throws IOException
120 	 */
XmpWriter(OutputStream os, String utfEncoding, int extraSpace)121 	public XmpWriter(OutputStream os, String utfEncoding, int extraSpace) throws IOException {
122 		this.extraSpace = extraSpace;
123 		writer = new OutputStreamWriter(os, utfEncoding);
124 		writer.write(XPACKET_PI_BEGIN);
125 		writer.write("<x:xmpmeta xmlns:x=\"adobe:ns:meta/\">\n");
126 		writer.write("<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n");
127 		about = "";
128 	}
129 
130 	/**
131 	 * Creates an XmpWriter.
132 	 * @param os
133 	 * @throws IOException
134 	 */
XmpWriter(OutputStream os)135 	public XmpWriter(OutputStream os) throws IOException {
136 		this(os, UTF8, 20);
137 	}
138 
139 	/** Sets the XMP to read-only */
setReadOnly()140 	public void setReadOnly() {
141 		end = 'r';
142 	}
143 
144 	/**
145 	 * @param about The about to set.
146 	 */
setAbout(String about)147 	public void setAbout(String about) {
148 		this.about = about;
149 	}
150 
151 	/**
152 	 * Adds an rdf:Description.
153 	 * @param xmlns
154 	 * @param content
155 	 * @throws IOException
156 	 */
addRdfDescription(String xmlns, String content)157 	public void addRdfDescription(String xmlns, String content) throws IOException {
158 		writer.write("<rdf:Description rdf:about=\"");
159 		writer.write(about);
160 		writer.write("\" ");
161 		writer.write(xmlns);
162 		writer.write(">");
163 		writer.write(content);
164 		writer.write("</rdf:Description>\n");
165 	}
166 
167 	/**
168 	 * Adds an rdf:Description.
169 	 * @param s
170 	 * @throws IOException
171 	 */
addRdfDescription(XmpSchema s)172 	public void addRdfDescription(XmpSchema s) throws IOException {
173 		writer.write("<rdf:Description rdf:about=\"");
174 		writer.write(about);
175 		writer.write("\" ");
176 		writer.write(s.getXmlns());
177 		writer.write(">");
178 		writer.write(s.toString());
179 		writer.write("</rdf:Description>\n");
180 	}
181 
182 	/**
183 	 * Flushes and closes the XmpWriter.
184 	 * @throws IOException
185 	 */
close()186 	public void close() throws IOException {
187 		writer.write("</rdf:RDF>");
188 		writer.write("</x:xmpmeta>\n");
189 		for (int i = 0; i < extraSpace; i++) {
190 			writer.write(EXTRASPACE);
191 		}
192 		writer.write(end == 'r' ? XPACKET_PI_END_R : XPACKET_PI_END_W);
193 		writer.flush();
194 		writer.close();
195 	}
196 
197     /**
198      * @param os
199      * @param info
200      * @throws IOException
201      */
XmpWriter(OutputStream os, PdfDictionary info, int PdfXConformance)202     public XmpWriter(OutputStream os, PdfDictionary info, int PdfXConformance) throws IOException {
203         this(os);
204         if (info != null) {
205         	DublinCoreSchema dc = new DublinCoreSchema();
206         	PdfSchema p = new PdfSchema();
207         	XmpBasicSchema basic = new XmpBasicSchema();
208         	PdfName key;
209         	PdfObject obj;
210         	for (Iterator it = info.getKeys().iterator(); it.hasNext();) {
211         		key = (PdfName)it.next();
212         		obj = info.get(key);
213         		if (obj == null)
214         			continue;
215         		if (PdfName.TITLE.equals(key)) {
216         			dc.addTitle(((PdfString)obj).toUnicodeString());
217         		}
218         		if (PdfName.AUTHOR.equals(key)) {
219         			dc.addAuthor(((PdfString)obj).toUnicodeString());
220         		}
221         		if (PdfName.SUBJECT.equals(key)) {
222         			dc.addSubject(((PdfString)obj).toUnicodeString());
223         			dc.addDescription(((PdfString)obj).toUnicodeString());
224         		}
225         		if (PdfName.KEYWORDS.equals(key)) {
226         			p.addKeywords(((PdfString)obj).toUnicodeString());
227         		}
228         		if (PdfName.CREATOR.equals(key)) {
229         			basic.addCreatorTool(((PdfString)obj).toUnicodeString());
230         		}
231         		if (PdfName.PRODUCER.equals(key)) {
232         			p.addProducer(((PdfString)obj).toUnicodeString());
233         		}
234         		if (PdfName.CREATIONDATE.equals(key)) {
235         			basic.addCreateDate(((PdfDate)obj).getW3CDate());
236         		}
237         		if (PdfName.MODDATE.equals(key)) {
238         			basic.addModDate(((PdfDate)obj).getW3CDate());
239         		}
240         	}
241         	if (dc.size() > 0) addRdfDescription(dc);
242         	if (p.size() > 0) addRdfDescription(p);
243         	if (basic.size() > 0) addRdfDescription(basic);
244             if (PdfXConformance == PdfWriter.PDFA1A || PdfXConformance == PdfWriter.PDFA1B) {
245                 PdfA1Schema a1 = new PdfA1Schema();
246                 if (PdfXConformance == PdfWriter.PDFA1A)
247                     a1.addConformance("A");
248                 else
249                     a1.addConformance("B");
250                 addRdfDescription(a1);
251             }
252         }
253     }
254 
255     /**
256      * @param os
257      * @param info
258      * @throws IOException
259      */
XmpWriter(OutputStream os, Map info)260     public XmpWriter(OutputStream os, Map info) throws IOException {
261         this(os);
262         if (info != null) {
263         	DublinCoreSchema dc = new DublinCoreSchema();
264         	PdfSchema p = new PdfSchema();
265         	XmpBasicSchema basic = new XmpBasicSchema();
266         	String key;
267         	String value;
268         	for (Iterator it = info.entrySet().iterator(); it.hasNext();) {
269         		Map.Entry entry = (Map.Entry) it.next();
270         		key = (String) entry.getKey();
271         		value = (String) entry.getValue();
272         		if (value == null)
273         			continue;
274         		if ("Title".equals(key)) {
275         			dc.addTitle(value);
276         		}
277         		if ("Author".equals(key)) {
278         			dc.addAuthor(value);
279         		}
280         		if ("Subject".equals(key)) {
281         			dc.addSubject(value);
282         			dc.addDescription(value);
283         		}
284         		if ("Keywords".equals(key)) {
285         			p.addKeywords(value);
286         		}
287         		if ("Creator".equals(key)) {
288         			basic.addCreatorTool(value);
289         		}
290         		if ("Producer".equals(key)) {
291         			p.addProducer(value);
292         		}
293         		if ("CreationDate".equals(key)) {
294         			basic.addCreateDate(PdfDate.getW3CDate(value));
295         		}
296         		if ("ModDate".equals(key)) {
297         			basic.addModDate(PdfDate.getW3CDate(value));
298         		}
299         	}
300         	if (dc.size() > 0) addRdfDescription(dc);
301         	if (p.size() > 0) addRdfDescription(p);
302         	if (basic.size() > 0) addRdfDescription(basic);
303         }
304     }
305 }