View | Details | Raw Unified | Return to bug 48161
Collapse All | Expand All

(-)src/scratchpad/src/org/apache/poi/hslf/extractor/PowerPointExtractor.java (-28 / +41 lines)
Lines 45-50 Link Here
45
	private boolean _slidesByDefault = true;
45
	private boolean _slidesByDefault = true;
46
	private boolean _notesByDefault = false;
46
	private boolean _notesByDefault = false;
47
	private boolean _commentsByDefault = false;
47
	private boolean _commentsByDefault = false;
48
    private boolean _masterByDefault = false;
48
49
49
	/**
50
	/**
50
	 * Basic extractor. Returns all the text, and optionally all the notes
51
	 * Basic extractor. Returns all the text, and optionally all the notes
Lines 58-63 Link Here
58
59
59
		boolean notes = false;
60
		boolean notes = false;
60
		boolean comments = false;
61
		boolean comments = false;
62
        boolean master = true;
63
        
61
		String file;
64
		String file;
62
		if (args.length > 1) {
65
		if (args.length > 1) {
63
			notes = true;
66
			notes = true;
Lines 70-76 Link Here
70
		}
73
		}
71
74
72
		PowerPointExtractor ppe = new PowerPointExtractor(file);
75
		PowerPointExtractor ppe = new PowerPointExtractor(file);
73
		System.out.println(ppe.getText(true, notes, comments));
76
		System.out.println(ppe.getText(true, notes, comments, master));
74
	}
77
	}
75
78
76
	/**
79
	/**
Lines 137-148 Link Here
137
		this._commentsByDefault = commentsByDefault;
140
		this._commentsByDefault = commentsByDefault;
138
	}
141
	}
139
142
143
    /**
144
     * Should a call to getText() return text from master? Default is no
145
     */
146
    public void setMasterByDefault(boolean masterByDefault) {
147
        this._masterByDefault = masterByDefault;
148
    }
149
140
	/**
150
	/**
141
	 * Fetches all the slide text from the slideshow, but not the notes, unless
151
	 * Fetches all the slide text from the slideshow, but not the notes, unless
142
	 * you've called setSlidesByDefault() and setNotesByDefault() to change this
152
	 * you've called setSlidesByDefault() and setNotesByDefault() to change this
143
	 */
153
	 */
144
	public String getText() {
154
	public String getText() {
145
		return getText(_slidesByDefault, _notesByDefault, _commentsByDefault);
155
		return getText(_slidesByDefault, _notesByDefault, _commentsByDefault, _masterByDefault);
146
	}
156
	}
147
157
148
	/**
158
	/**
Lines 178-191 Link Here
178
	 * @param getNoteText fetch note text
188
	 * @param getNoteText fetch note text
179
	 */
189
	 */
180
	public String getText(boolean getSlideText, boolean getNoteText) {
190
	public String getText(boolean getSlideText, boolean getNoteText) {
181
		return getText(getSlideText, getNoteText, _commentsByDefault);
191
		return getText(getSlideText, getNoteText, _commentsByDefault, _masterByDefault);
182
	}
192
	}
183
193
184
	public String getText(boolean getSlideText, boolean getNoteText, boolean getCommentText) {
194
	public String getText(boolean getSlideText, boolean getNoteText, boolean getCommentText, boolean getMasterText) {
185
		StringBuffer ret = new StringBuffer();
195
		StringBuffer ret = new StringBuffer();
186
196
187
		if (getSlideText) {
197
		if (getSlideText) {
188
			for (int i = 0; i < _slides.length; i++) {
198
            if (getMasterText) {
199
                for (SlideMaster master : _show.getSlidesMasters()) {
200
                    textRunsToText(ret, master.getTextRuns());
201
                }
202
            }
203
204
            for (int i = 0; i < _slides.length; i++) {
189
				Slide slide = _slides[i];
205
				Slide slide = _slides[i];
190
206
191
				// Slide header, if set
207
				// Slide header, if set
Lines 195-213 Link Here
195
				}
211
				}
196
212
197
				// Slide text
213
				// Slide text
198
				TextRun[] runs = slide.getTextRuns();
214
                textRunsToText(ret, slide.getTextRuns());
199
				for (int j = 0; j < runs.length; j++) {
200
					TextRun run = runs[j];
201
					if (run != null) {
202
						String text = run.getText();
203
						ret.append(text);
204
						if (!text.endsWith("\n")) {
205
							ret.append("\n");
206
						}
207
					}
208
				}
209
215
210
				// Slide footer, if set
216
                // Slide footer, if set
211
				if (hf != null && hf.isFooterVisible() && hf.getFooterText() != null) {
217
				if (hf != null && hf.isFooterVisible() && hf.getFooterText() != null) {
212
					ret.append(hf.getFooterText() + "\n");
218
					ret.append(hf.getFooterText() + "\n");
213
				}
219
				}
Lines 249-265 Link Here
249
				}
255
				}
250
256
251
				// Notes text
257
				// Notes text
252
				TextRun[] runs = notes.getTextRuns();
258
                textRunsToText(ret, notes.getTextRuns());
253
				if (runs != null && runs.length > 0) {
254
					for (int j = 0; j < runs.length; j++) {
255
						TextRun run = runs[j];
256
						String text = run.getText();
257
						ret.append(text);
258
						if (!text.endsWith("\n")) {
259
							ret.append("\n");
260
						}
261
					}
262
				}
263
259
264
				// Repeat the notes footer, if set
260
				// Repeat the notes footer, if set
265
				if (hf != null && hf.isFooterVisible() && hf.getFooterText() != null) {
261
				if (hf != null && hf.isFooterVisible() && hf.getFooterText() != null) {
Lines 270-273 Link Here
270
266
271
		return ret.toString();
267
		return ret.toString();
272
	}
268
	}
269
270
    private void textRunsToText(StringBuffer ret, TextRun[] runs) {
271
        if (runs==null) {
272
            return;
273
        }
274
275
        for (int j = 0; j < runs.length; j++) {
276
            TextRun run = runs[j];
277
            if (run != null) {
278
                String text = run.getText();
279
                ret.append(text);
280
                if (!text.endsWith("\n")) {
281
                    ret.append("\n");
282
                }
283
            }
284
        }
285
    }
273
}
286
}
(-)src/scratchpad/testcases/org/apache/poi/hslf/extractor/TestExtractor.java (-3 / +11 lines)
Lines 48-54 Link Here
48
    //private String pdirname;
48
    //private String pdirname;
49
49
50
    protected void setUp() throws Exception {
50
    protected void setUp() throws Exception {
51
52
		ppe = new PowerPointExtractor(slTests.openResourceAsStream("basic_test_ppt_file.ppt"));
51
		ppe = new PowerPointExtractor(slTests.openResourceAsStream("basic_test_ppt_file.ppt"));
53
		ppe2 = new PowerPointExtractor(slTests.openResourceAsStream("with_textbox.ppt"));
52
		ppe2 = new PowerPointExtractor(slTests.openResourceAsStream("with_textbox.ppt"));
54
    }
53
    }
Lines 63-69 Link Here
63
		
62
		
64
		// 1 page example with text boxes
63
		// 1 page example with text boxes
65
		sheetText = ppe2.getText();
64
		sheetText = ppe2.getText();
66
		expectText = "Hello, World!!!\nI am just a poor boy\nThis is Times New Roman\nPlain Text \n"; 
65
		expectText = "Hello, World!!!\nI am just a poor boy\nThis is Times New Roman\nPlain Text \n";
67
66
68
		ensureTwoStringsTheSame(expectText, sheetText);
67
		ensureTwoStringsTheSame(expectText, sheetText);
69
    }
68
    }
Lines 112-118 Link Here
112
	 */
111
	 */
113
	public void testMissingCoreRecords() throws Exception {
112
	public void testMissingCoreRecords() throws Exception {
114
		ppe = new PowerPointExtractor(slTests.openResourceAsStream("missing_core_records.ppt"));
113
		ppe = new PowerPointExtractor(slTests.openResourceAsStream("missing_core_records.ppt"));
115
		
114
116
		String text = ppe.getText(true, false);
115
		String text = ppe.getText(true, false);
117
		String nText = ppe.getNotes();
116
		String nText = ppe.getNotes();
118
117
Lines 265-268 Link Here
265
	private static boolean contains(String text, String searchString) {
264
	private static boolean contains(String text, String searchString) {
266
		return text.indexOf(searchString) >=0;
265
		return text.indexOf(searchString) >=0;
267
	}
266
	}
267
268
    public void testMasterText() throws Exception {
269
		ppe = new PowerPointExtractor(slTests.openResourceAsStream("master_text.ppt"));
270
        ppe.setMasterByDefault(true);
271
272
		String text = ppe.getText();
273
		assertTrue(text.contains("Master Header Text"));
274
    }
275
268
}
276
}

Return to bug 48161