1 16 17 package org.apache.poi.hpsf.examples; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.ByteArrayOutputStream ; 21 import java.io.FileInputStream ; 22 import java.io.FileNotFoundException ; 23 import java.io.FileOutputStream ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 30 import org.apache.poi.hpsf.HPSFRuntimeException; 31 import org.apache.poi.hpsf.MarkUnsupportedException; 32 import org.apache.poi.hpsf.MutablePropertySet; 33 import org.apache.poi.hpsf.MutableSection; 34 import org.apache.poi.hpsf.NoPropertySetStreamException; 35 import org.apache.poi.hpsf.PropertySet; 36 import org.apache.poi.hpsf.PropertySetFactory; 37 import org.apache.poi.hpsf.SummaryInformation; 38 import org.apache.poi.hpsf.Util; 39 import org.apache.poi.hpsf.Variant; 40 import org.apache.poi.hpsf.WritingNotSupportedException; 41 import org.apache.poi.hpsf.wellknown.PropertyIDMap; 42 import org.apache.poi.poifs.eventfilesystem.POIFSReader; 43 import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent; 44 import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener; 45 import org.apache.poi.poifs.filesystem.DirectoryEntry; 46 import org.apache.poi.poifs.filesystem.DocumentInputStream; 47 import org.apache.poi.poifs.filesystem.POIFSDocumentPath; 48 import org.apache.poi.poifs.filesystem.POIFSFileSystem; 49 50 89 public class WriteAuthorAndTitle 90 { 91 98 public static void main(final String [] args) throws IOException 99 { 100 101 if (args.length != 2) 102 { 103 System.err.println("Usage: " + WriteAuthorAndTitle.class.getName() + 104 " originPOIFS destinationPOIFS"); 105 System.exit(1); 106 } 107 108 109 final String srcName = args[0]; 110 final String dstName = args[1]; 111 112 115 final POIFSReader r = new POIFSReader(); 116 final ModifySICopyTheRest msrl = new ModifySICopyTheRest(dstName); 117 r.registerListener(msrl); 118 r.read(new FileInputStream (srcName)); 119 120 121 msrl.close(); 122 } 123 124 125 126 134 static class ModifySICopyTheRest implements POIFSReaderListener 135 { 136 String dstName; 137 OutputStream out; 138 POIFSFileSystem poiFs; 139 140 141 150 public ModifySICopyTheRest(final String dstName) 151 { 152 this.dstName = dstName; 153 poiFs = new POIFSFileSystem(); 154 } 155 156 157 161 public void processPOIFSReaderEvent(final POIFSReaderEvent event) 162 { 163 165 final POIFSDocumentPath path = event.getPath(); 166 final String name = event.getName(); 167 final DocumentInputStream stream = event.getStream(); 168 169 Throwable t = null; 170 171 try 172 { 173 175 if (PropertySet.isPropertySetStream(stream)) 176 { 177 179 PropertySet ps = null; 180 try 181 { 182 ps = PropertySetFactory.create(stream); 183 } 184 catch (NoPropertySetStreamException ex) 185 { 186 188 } 189 190 193 if (ps.isSummaryInformation()) 194 196 editSI(poiFs, path, name, ps); 197 else 198 201 copy(poiFs, path, name, ps); 202 } 203 else 204 206 copy(poiFs, event.getPath(), event.getName(), stream); 207 } 208 catch (MarkUnsupportedException ex) 209 { 210 t = ex; 211 } 212 catch (IOException ex) 213 { 214 t = ex; 215 } 216 catch (WritingNotSupportedException ex) 217 { 218 t = ex; 219 } 220 221 226 if (t != null) 227 { 228 throw new HPSFRuntimeException 229 ("Could not read file \"" + path + "/" + name + 230 "\". Reason: " + Util.toString(t)); 231 } 232 } 233 234 235 246 public void editSI(final POIFSFileSystem poiFs, 247 final POIFSDocumentPath path, 248 final String name, 249 final PropertySet si) 250 throws WritingNotSupportedException, IOException 251 { 252 253 final DirectoryEntry de = getPath(poiFs, path); 254 255 257 final MutablePropertySet mps = new MutablePropertySet(si); 258 259 261 final MutableSection s = 262 (MutableSection) mps.getSections().get(0); 263 264 265 s.setProperty(PropertyIDMap.PID_AUTHOR, Variant.VT_LPSTR, 266 "Rainer Klute"); 267 s.setProperty(PropertyIDMap.PID_TITLE, Variant.VT_LPWSTR, 268 "Test"); 269 270 272 final InputStream pss = mps.toInputStream(); 273 274 275 de.createDocument(name, pss); 276 } 277 278 279 289 public void copy(final POIFSFileSystem poiFs, 290 final POIFSDocumentPath path, 291 final String name, 292 final PropertySet ps) 293 throws WritingNotSupportedException, IOException 294 { 295 final DirectoryEntry de = getPath(poiFs, path); 296 final MutablePropertySet mps = new MutablePropertySet(ps); 297 de.createDocument(name, mps.toInputStream()); 298 } 299 300 301 302 310 public void copy(final POIFSFileSystem poiFs, 311 final POIFSDocumentPath path, 312 final String name, 313 final DocumentInputStream stream) throws IOException 314 { 315 final DirectoryEntry de = getPath(poiFs, path); 316 final ByteArrayOutputStream out = new ByteArrayOutputStream (); 317 int c; 318 while ((c = stream.read()) != -1) 319 out.write(c); 320 stream.close(); 321 out.close(); 322 final InputStream in = 323 new ByteArrayInputStream (out.toByteArray()); 324 de.createDocument(name, in); 325 } 326 327 328 334 public void close() throws FileNotFoundException , IOException 335 { 336 out = new FileOutputStream (dstName); 337 poiFs.writeFilesystem(out); 338 out.close(); 339 } 340 341 342 343 346 private final Map paths = new HashMap (); 347 348 349 350 373 public DirectoryEntry getPath(final POIFSFileSystem poiFs, 374 final POIFSDocumentPath path) 375 { 376 try 377 { 378 379 final String s = path.toString(); 380 DirectoryEntry de = (DirectoryEntry) paths.get(s); 381 if (de != null) 382 383 return de; 384 385 387 int l = path.length(); 388 if (l == 0) 389 391 de = poiFs.getRoot(); 392 else 393 { 394 396 de = getPath(poiFs, path.getParent()); 397 398 de = de.createDirectory(path.getComponent 399 (path.length() - 1)); 400 } 401 paths.put(s, de); 402 return de; 403 } 404 catch (IOException ex) 405 { 406 409 ex.printStackTrace(System.err); 410 throw new RuntimeException (ex.toString()); 411 413 } 415 } 416 } 417 418 } 419 | Popular Tags |