Provide a common ole2 implementation of POITextExtractor, which gives access to the...
[poi.git] / src / scratchpad / src / org / apache / poi / hslf / extractor / PowerPointExtractor.java
blobcd9fa282560b04222b7b0e74dd3150128c65e61e
2 /* ====================================================================
3 Licensed to the Apache Software Foundation (ASF) under one or more
4 contributor license agreements. See the NOTICE file distributed with
5 this work for additional information regarding copyright ownership.
6 The ASF licenses this file to You under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with
8 the License. You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================== */
21 package org.apache.poi.hslf.extractor;
23 import java.io.*;
24 import java.util.HashSet;
26 import org.apache.poi.POIOLE2TextExtractor;
27 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
28 import org.apache.poi.hslf.*;
29 import org.apache.poi.hslf.model.*;
30 import org.apache.poi.hslf.usermodel.*;
32 /**
33 * This class can be used to extract text from a PowerPoint file.
34 * Can optionally also get the notes from one.
36 * @author Nick Burch
39 public class PowerPointExtractor extends POIOLE2TextExtractor
41 private HSLFSlideShow _hslfshow;
42 private SlideShow _show;
43 private Slide[] _slides;
45 private boolean slidesByDefault = true;
46 private boolean notesByDefault = false;
48 /**
49 * Basic extractor. Returns all the text, and optionally all the notes
51 public static void main(String args[]) throws IOException
53 if(args.length < 1) {
54 System.err.println("Useage:");
55 System.err.println("\tPowerPointExtractor [-notes] <file>");
56 System.exit(1);
59 boolean notes = false;
60 String file;
61 if(args.length > 1) {
62 notes = true;
63 file = args[1];
64 } else {
65 file = args[0];
68 PowerPointExtractor ppe = new PowerPointExtractor(file);
69 System.out.println(ppe.getText(true,notes));
70 ppe.close();
73 /**
74 * Creates a PowerPointExtractor, from a file
75 * @param fileName The name of the file to extract from
77 public PowerPointExtractor(String fileName) throws IOException {
78 this(new FileInputStream(fileName));
80 /**
81 * Creates a PowerPointExtractor, from an Input Stream
82 * @param iStream The input stream containing the PowerPoint document
84 public PowerPointExtractor(InputStream iStream) throws IOException {
85 this(new POIFSFileSystem(iStream));
87 /**
88 * Creates a PowerPointExtractor, from an open POIFSFileSystem
89 * @param fs the POIFSFileSystem containing the PowerPoint document
91 public PowerPointExtractor(POIFSFileSystem fs) throws IOException {
92 this(new HSLFSlideShow(fs));
95 /**
96 * Creates a PowerPointExtractor, from a HSLFSlideShow
97 * @param ss the HSLFSlideShow to extract text from
99 public PowerPointExtractor(HSLFSlideShow ss) throws IOException {
100 super(ss);
101 _hslfshow = ss;
102 _show = new SlideShow(_hslfshow);
103 _slides = _show.getSlides();
107 * Shuts down the underlying streams
109 public void close() throws IOException {
110 _hslfshow.close();
111 _hslfshow = null;
112 _show = null;
113 _slides = null;
117 * Should a call to getText() return slide text?
118 * Default is yes
120 public void setSlidesByDefault(boolean slidesByDefault) {
121 this.slidesByDefault = slidesByDefault;
124 * Should a call to getText() return notes text?
125 * Default is no
127 public void setNotesByDefault(boolean notesByDefault) {
128 this.notesByDefault = notesByDefault;
132 * Fetches all the slide text from the slideshow,
133 * but not the notes, unless you've called
134 * setSlidesByDefault() and setNotesByDefault()
135 * to change this
137 public String getText() {
138 return getText(slidesByDefault,notesByDefault);
142 * Fetches all the notes text from the slideshow, but not the slide text
144 public String getNotes() {
145 return getText(false,true);
149 * Fetches text from the slideshow, be it slide text or note text.
150 * Because the final block of text in a TextRun normally have their
151 * last \n stripped, we add it back
152 * @param getSlideText fetch slide text
153 * @param getNoteText fetch note text
155 public String getText(boolean getSlideText, boolean getNoteText) {
156 StringBuffer ret = new StringBuffer();
158 if(getSlideText) {
159 for(int i=0; i<_slides.length; i++) {
160 Slide slide = _slides[i];
161 TextRun[] runs = slide.getTextRuns();
162 for(int j=0; j<runs.length; j++) {
163 TextRun run = runs[j];
164 if(run != null) {
165 String text = run.getText();
166 ret.append(text);
167 if(! text.endsWith("\n")) {
168 ret.append("\n");
173 if(getNoteText) {
174 ret.append("\n");
178 if(getNoteText) {
179 // Not currently using _notes, as that can have the notes of
180 // master sheets in. Grab Slide list, then work from there,
181 // but ensure no duplicates
182 HashSet seenNotes = new HashSet();
183 for(int i=0; i<_slides.length; i++) {
184 Notes notes = _slides[i].getNotesSheet();
185 if(notes == null) { continue; }
186 Integer id = new Integer(notes._getSheetNumber());
187 if(seenNotes.contains(id)) { continue; }
188 seenNotes.add(id);
190 TextRun[] runs = notes.getTextRuns();
191 if(runs != null && runs.length > 0) {
192 for(int j=0; j<runs.length; j++) {
193 TextRun run = runs[j];
194 String text = run.getText();
195 ret.append(text);
196 if(! text.endsWith("\n")) {
197 ret.append("\n");
204 return ret.toString();