KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > content > metadata > OfficeMetadataExtracter


1 /*
2  * Copyright (C) 2005 Jesper Steen Møller
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.content.metadata;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.Serializable JavaDoc;
22 import java.util.Arrays JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Map JavaDoc;
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 /**
40  *
41  * @author Jesper Steen Møller
42  */

43 public class OfficeMetadataExtracter extends AbstractMetadataExtracter
44 {
45     public static String JavaDoc[] SUPPORTED_MIMETYPES = new String JavaDoc[] {
46         MimetypeMap.MIMETYPE_WORD,
47         MimetypeMap.MIMETYPE_EXCEL,
48         MimetypeMap.MIMETYPE_PPT };
49
50     public OfficeMetadataExtracter()
51     {
52         super(new HashSet JavaDoc<String JavaDoc>(Arrays.asList(SUPPORTED_MIMETYPES)), 1.0, 1000);
53     }
54
55     public void extractInternal(ContentReader reader, final Map JavaDoc<QName, Serializable JavaDoc> destination) throws Throwable JavaDoc
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                         // Titled aspect
68
trimPut(ContentModel.PROP_TITLE, si.getTitle(), destination);
69                         trimPut(ContentModel.PROP_DESCRIPTION, si.getSubject(), destination);
70
71                         // Auditable aspect
72
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 // DocumentSummaryInformation dsi = (DocumentSummaryInformation) ps;
79

80                         // These are not really interesting to any aspect:
81
// trimPut(ContentModel.PROP_xxx, dsi.getCompany(),
82
// destination);
83
// trimPut(ContentModel.PROP_yyy, dsi.getManager(),
84
// destination);
85
}
86                 }
87                 catch (Exception JavaDoc ex)
88                 {
89                     throw new ContentIOException("Property set stream: " + event.getPath() + event.getName(), ex);
90                 }
91             }
92         };
93         InputStream JavaDoc 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 JavaDoc e) {}
106             }
107         }
108     }
109 }
110
Popular Tags