1 50 51 package com.lowagie.text.xml.xmp; 52 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 70 public class XmpWriter { 71 72 73 public static final String UTF8 = "UTF-8"; 74 75 public static final String UTF16 = "UTF-16"; 76 77 public static final String UTF16BE = "UTF-16BE"; 78 79 public static final String UTF16LE = "UTF-16LE"; 80 81 82 public static final String EXTRASPACE = " \n"; 83 84 85 protected int extraSpace; 86 87 88 protected OutputStreamWriter writer; 89 90 91 protected String about; 92 93 94 protected char end = 'w'; 95 96 103 public XmpWriter(OutputStream os, String utfEncoding, int extraSpace) throws IOException { 104 this.extraSpace = extraSpace; 105 writer = new OutputStreamWriter (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 117 public XmpWriter(OutputStream os) throws IOException { 118 this(os, UTF8, 20); 119 } 120 121 122 public void setReadOnly() { 123 end = 'r'; 124 } 125 126 129 public void setAbout(String about) { 130 this.about = about; 131 } 132 133 139 public void addRdfDescription(String xmlns, String content) throws IOException { 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 154 public void addRdfDescription(XmpSchema s) throws IOException { 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 168 public void close() throws IOException { 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 184 public XmpWriter(OutputStream os, PdfDictionary info) throws IOException { 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 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 233 public XmpWriter(OutputStream os, Map info) throws IOException { 234 this(os); 235 if (info != null) { 236 DublinCoreSchema dc = new DublinCoreSchema(); 237 PdfSchema p = new PdfSchema(); 238 XmpBasicSchema basic = new XmpBasicSchema(); 239 String key; 240 String value; 241 for (Iterator it = info.entrySet().iterator(); it.hasNext();) { 242 Map.Entry entry = (Map.Entry ) it.next(); 243 key = (String ) entry.getKey(); 244 value = (String ) 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 |