001 /*
002 * JBoss, Home of Professional Open Source.
003 * Copyright 2008, Red Hat Middleware LLC, and individual contributors
004 * as indicated by the @author tags. See the copyright.txt file in the
005 * distribution for a full listing of individual contributors.
006 *
007 * This is free software; you can redistribute it and/or modify it
008 * under the terms of the GNU Lesser General Public License as
009 * published by the Free Software Foundation; either version 2.1 of
010 * the License, or (at your option) any later version.
011 *
012 * This software is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this software; if not, write to the Free
019 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021 */
022 package org.jboss.dna.sequencer.msoffice;
023
024 import java.io.IOException;
025 import java.io.InputStream;
026 import java.util.List;
027 import org.jboss.dna.common.monitor.ProgressMonitor;
028 import org.jboss.dna.graph.sequencers.SequencerContext;
029 import org.jboss.dna.graph.sequencers.SequencerOutput;
030 import org.jboss.dna.graph.sequencers.StreamSequencer;
031 import org.jboss.dna.sequencer.msoffice.excel.ExcelMetadata;
032 import org.jboss.dna.sequencer.msoffice.excel.ExcelMetadataReader;
033 import org.jboss.dna.sequencer.msoffice.powerpoint.PowerPointMetadataReader;
034 import org.jboss.dna.sequencer.msoffice.powerpoint.SlideMetadata;
035 import org.jboss.dna.sequencer.msoffice.word.WordMetadataReader;
036
037 /**
038 * A sequencer that processes the content of an MS Office document, extracts the metadata for the file, and then writes that
039 * metadata to the repository.
040 * <p>
041 * This sequencer produces data that corresponds to the following structure:
042 * <ul>
043 * <li><strong>msoffice:metadata</strong> node of type <code>msoffice:metadata</code>
044 * <ul>
045 * <li><strong>msoffice:title</strong> optional string property for the title of the documnt</li>
046 * <li><strong>msoffice:subject</strong> optional string property for the subject of the document</li>
047 * <li><strong>msoffice:author</strong> optional string property for the author of the document</li>
048 * <li><strong>msoffice:keywords</strong> optional string property for the document keywords</li>
049 * <li><strong>msoffice:comment</strong> optional string property for the document comment</li>
050 * <li><strong>msoffice:template</strong> optional string property for the template from which this document originates</li>
051 * <li><strong>msoffice:last_saved_by</strong> optional string property for the person that last saved this document</li>
052 * <li><strong>msoffice:revision</strong> optional string property for this document revision</li>
053 * <li><strong>msoffice:total_editing_time</strong> optional long property for the length this document has been edited</li>
054 * <li><strong>msoffice:last_printed</strong> optional date property for the date of last printing this document</li>
055 * <li><strong>msoffice:created</strong> date property for the date of creation of the document</li>
056 * <li><strong>msoffice:saved</strong> date property for the date of last save of this document</li>
057 * <li><strong>msoffice:pages</strong> long property for the number of pages of this document</li>
058 * <li><strong>msoffice:words</strong> long property for the number of words in this document</li>
059 * <li><strong>msoffice:characters</strong> long property for the number of characters in this document</li>
060 * <li><strong>msoffice:creating_application</strong> string property for the application used to create this document</li>
061 * <li><strong>msoffice:thumbnail</strong> optional binary property for the thumbanail of this document</li>
062 * <li><strong>msoffice:full_contents</strong> optional String property holding the text contents of an excel file</li>
063 * <li><strong>msoffice:sheet_name</strong> optional String property for the name of a sheet in excel (multiple)</li>
064 * </ul>
065 * </li>
066 * <li><strong>msoffice:slide</strong> node of type <code>msoffice:pptslide</code>
067 * <ul>
068 * <li><strong>msoffice:title</strong> optional String property for the title of a slide</li>
069 * <li><strong>msoffice:notes</strong> optional String property for the notes of a slide</li>
070 * <li><strong>msoffice:text</strong> optional String property for the text of a slide</li>
071 * <li><strong>msoffice:thumbnail</strong> optional binary property for the thumbnail of a slide (PNG image)</li>
072 * </ul>
073 * </li>
074 * </ul>
075 * </p>
076 *
077 * @author Michael Trezzi
078 */
079 public class MSOfficeMetadataSequencer implements StreamSequencer {
080
081 public static final String METADATA_NODE = "msoffice:metadata";
082 public static final String MSOFFICE_PRIMARY_TYPE = "jcr:primaryType";
083 public static final String MSOFFICE_TITLE = "msoffice:title";
084 public static final String MSOFFICE_SUBJECT = "msoffice:subject";
085 public static final String MSOFFICE_AUTHOR = "msoffice:author";
086 public static final String MSOFFICE_KEYWORDS = "msoffice:keywords";
087 public static final String MSOFFICE_COMMENT = "msoffice:comment";
088 public static final String MSOFFICE_TEMPLATE = "msoffice:template";
089 public static final String MSOFFICE_LAST_SAVED_BY = "msoffice:last_saved_by";
090 public static final String MSOFFICE_REVISION = "msoffice:revision";
091 public static final String MSOFFICE_TOTAL_EDITING_TIME = "msoffice:total_editing_time";
092 public static final String MSOFFICE_LAST_PRINTED = "msoffice:last_printed";
093 public static final String MSOFFICE_CREATED = "msoffice:created";
094 public static final String MSOFFICE_SAVED = "msoffice:saved";
095 public static final String MSOFFICE_PAGES = "msoffice:pages";
096 public static final String MSOFFICE_WORDS = "msoffice:words";
097 public static final String MSOFFICE_CHARACTERS = "msoffice:characters";
098 public static final String MSOFFICE_CREATING_APPLICATION = "msoffice:creating_application";
099 public static final String MSOFFICE_THUMBNAIL = "msoffice:thumbnail";
100
101 // PowerPoint specific
102 public static final String POWERPOINT_SLIDE_NODE = "msoffice:slide";
103 public static final String SLIDE_TITLE = "msoffice:title";
104 public static final String SLIDE_TEXT = "msoffice:text";
105 public static final String SLIDE_NOTES = "msoffice:notes";
106 public static final String SLIDE_THUMBNAIL = "msoffice:thumbnail";
107
108 // Excel specific
109 public static final String EXCEL_FULL_CONTENT = "msoffice:full_contents";
110 public static final String EXCEL_SHEET_NAME = "msoffice:sheet_name";
111
112 /**
113 * {@inheritDoc}
114 */
115 public void sequence( InputStream stream,
116 SequencerOutput output,
117 SequencerContext context,
118 ProgressMonitor progressMonitor ) {
119 progressMonitor.beginTask(10, MSOfficeMetadataI18n.sequencerTaskName);
120
121 MSOfficeMetadata metadata = MSOfficeMetadataReader.instance(stream);
122 progressMonitor.worked(3);
123
124 String mimeType = context.getMimeType();
125
126 if (metadata != null) {
127 output.setProperty(METADATA_NODE, MSOFFICE_PRIMARY_TYPE, "msoffice:metadata");
128 output.setProperty(METADATA_NODE, MSOFFICE_TITLE, metadata.getTitle());
129 output.setProperty(METADATA_NODE, MSOFFICE_SUBJECT, metadata.getSubject());
130 output.setProperty(METADATA_NODE, MSOFFICE_AUTHOR, metadata.getAuthor());
131 output.setProperty(METADATA_NODE, MSOFFICE_KEYWORDS, metadata.getKeywords());
132 output.setProperty(METADATA_NODE, MSOFFICE_COMMENT, metadata.getComment());
133 output.setProperty(METADATA_NODE, MSOFFICE_TEMPLATE, metadata.getTemplate());
134 output.setProperty(METADATA_NODE, MSOFFICE_LAST_SAVED_BY, metadata.getLastSavedBy());
135 output.setProperty(METADATA_NODE, MSOFFICE_REVISION, metadata.getRevision());
136 output.setProperty(METADATA_NODE, MSOFFICE_TOTAL_EDITING_TIME, metadata.getTotalEditingTime());
137 output.setProperty(METADATA_NODE, MSOFFICE_LAST_PRINTED, metadata.getLastPrinted());
138 output.setProperty(METADATA_NODE, MSOFFICE_CREATED, metadata.getCreated());
139 output.setProperty(METADATA_NODE, MSOFFICE_SAVED, metadata.getSaved());
140 output.setProperty(METADATA_NODE, MSOFFICE_PAGES, metadata.getPages());
141 output.setProperty(METADATA_NODE, MSOFFICE_WORDS, metadata.getWords());
142 output.setProperty(METADATA_NODE, MSOFFICE_CHARACTERS, metadata.getCharacters());
143 output.setProperty(METADATA_NODE, MSOFFICE_CREATING_APPLICATION, metadata.getCreatingApplication());
144 output.setProperty(METADATA_NODE, MSOFFICE_THUMBNAIL, metadata.getThumbnail());
145
146 } else {
147 return;
148 }
149 progressMonitor.worked(1);
150 // process PowerPoint specific metadata
151 if (mimeType.equals("application/vnd.ms-powerpoint")) { // replace true with check if it's ppt file being sequenced
152 try {
153 List<SlideMetadata> ppt_metadata = PowerPointMetadataReader.instance(stream);
154 if (ppt_metadata != null) {
155 for (SlideMetadata sm : ppt_metadata) {
156 output.setProperty(METADATA_NODE + "/" + POWERPOINT_SLIDE_NODE, SLIDE_TITLE, sm.getTitle());
157 output.setProperty(METADATA_NODE + "/" + POWERPOINT_SLIDE_NODE, SLIDE_TEXT, sm.getText());
158 output.setProperty(METADATA_NODE + "/" + POWERPOINT_SLIDE_NODE, SLIDE_NOTES, sm.getNotes());
159 output.setProperty(METADATA_NODE + "/" + POWERPOINT_SLIDE_NODE, SLIDE_THUMBNAIL, sm.getThumbnail());
160 }
161 }
162 } catch (IOException e) {
163 // There was an error reading, so log and continue ...
164 context.getLogger(this.getClass()).debug(e, "Error while extracting the PowerPoint metadata");
165 }
166 }
167 progressMonitor.worked(2);
168
169 if (mimeType.equals("application/vnd.ms-word")) {
170 // Sometime in the future this will sequence WORD Table of contents.
171 try {
172 /*WordMetadata wordMetadata =*/WordMetadataReader.invoke(stream);
173 } catch (IOException e) {
174 // There was an error reading, so log and continue ...
175 context.getLogger(this.getClass()).debug(e, "Error while extracting the Word document metadata");
176 }
177
178 }
179 progressMonitor.worked(2);
180
181 if (mimeType.equals("application/vnd.ms-excel")) {
182 try {
183 ExcelMetadata excel_metadata = ExcelMetadataReader.instance(stream);
184 if (excel_metadata != null) {
185 output.setProperty(METADATA_NODE, EXCEL_FULL_CONTENT, excel_metadata.getText());
186 for (String sheet : excel_metadata.getSheets()) {
187 output.setProperty(METADATA_NODE, EXCEL_SHEET_NAME, sheet);
188 }
189 }
190 } catch (IOException e) {
191 // There was an error reading, so log and continue ...
192 context.getLogger(this.getClass()).debug(e, "Error while extracting the Excel metadata");
193 }
194 }
195 progressMonitor.worked(2);
196 progressMonitor.done();
197 }
198 }