1 17 package org.alfresco.repo.content.metadata; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.Serializable ; 22 import java.util.Arrays ; 23 import java.util.HashSet ; 24 import java.util.Map ; 25 26 import org.alfresco.model.ContentModel; 27 import org.alfresco.repo.content.MimetypeMap; 28 import org.alfresco.service.cmr.repository.ContentIOException; 29 import org.alfresco.service.cmr.repository.ContentReader; 30 import org.alfresco.service.namespace.QName; 31 import org.apache.poi.hpsf.DocumentSummaryInformation; 32 import org.apache.poi.hpsf.PropertySet; 33 import org.apache.poi.hpsf.PropertySetFactory; 34 import org.apache.poi.hpsf.SummaryInformation; 35 import org.apache.poi.poifs.eventfilesystem.POIFSReader; 36 import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent; 37 import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener; 38 39 43 public class OfficeMetadataExtracter extends AbstractMetadataExtracter 44 { 45 public static String [] SUPPORTED_MIMETYPES = new String [] { 46 MimetypeMap.MIMETYPE_WORD, 47 MimetypeMap.MIMETYPE_EXCEL, 48 MimetypeMap.MIMETYPE_PPT }; 49 50 public OfficeMetadataExtracter() 51 { 52 super(new HashSet <String >(Arrays.asList(SUPPORTED_MIMETYPES)), 1.0, 1000); 53 } 54 55 public void extractInternal(ContentReader reader, final Map <QName, Serializable > destination) throws Throwable 56 { 57 POIFSReaderListener readerListener = new POIFSReaderListener() 58 { 59 public void processPOIFSReaderEvent(final POIFSReaderEvent event) 60 { 61 try 62 { 63 PropertySet ps = PropertySetFactory.create(event.getStream()); 64 if (ps instanceof SummaryInformation) 65 { 66 SummaryInformation si = (SummaryInformation) ps; 67 trimPut(ContentModel.PROP_TITLE, si.getTitle(), destination); 69 trimPut(ContentModel.PROP_DESCRIPTION, si.getSubject(), destination); 70 71 trimPut(ContentModel.PROP_CREATED, si.getCreateDateTime(), destination); 73 trimPut(ContentModel.PROP_MODIFIED, si.getLastSaveDateTime(), destination); 74 trimPut(ContentModel.PROP_AUTHOR, si.getAuthor(), destination); 75 } 76 else if (ps instanceof DocumentSummaryInformation) 77 { 78 80 } 86 } 87 catch (Exception ex) 88 { 89 throw new ContentIOException("Property set stream: " + event.getPath() + event.getName(), ex); 90 } 91 } 92 }; 93 InputStream is = null; 94 try 95 { 96 is = reader.getContentInputStream(); 97 POIFSReader poiFSReader = new POIFSReader(); 98 poiFSReader.registerListener(readerListener, SummaryInformation.DEFAULT_STREAM_NAME); 99 poiFSReader.read(is); 100 } 101 finally 102 { 103 if (is != null) 104 { 105 try { is.close(); } catch (IOException e) {} 106 } 107 } 108 } 109 } 110 | Popular Tags |