#62319 - handle slide layouts in extractor

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1829677 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2018-04-20 20:00:46 +00:00
parent 7ddef62cc4
commit a276d5d3f6
5 changed files with 52 additions and 28 deletions

View File

@ -116,30 +116,6 @@ public class SlideShowExtractor<
public String getText() {
final StringBuilder sb = new StringBuilder();
if (masterByDefault) {
for (final MasterSheet<S,P> master : slideshow.getSlideMasters()) {
for (final Shape<S,P> shape : master) {
if (shape instanceof TextShape) {
final TextShape<S,P> ts = (TextShape<S,P>)shape;
final String text = ts.getText();
if (text == null || text.isEmpty() || "*".equals(text)) {
continue;
}
if (ts.isPlaceholder()) {
// don't bother about boiler plate text on master sheets
LOG.log(POILogger.INFO, "Ignoring boiler plate (placeholder) text on slide master:", text);
continue;
}
sb.append(text);
if (!text.endsWith("\n")) {
sb.append("\n");
}
}
}
}
}
for (final Slide<S, P> slide : slideshow.getSlides()) {
sb.append(getText(slide));
}
@ -154,6 +130,17 @@ public class SlideShowExtractor<
printShapeText(slide, sb);
}
if (masterByDefault) {
final MasterSheet<S,P> ms = slide.getMasterSheet();
printSlideMaster(ms, sb);
// only print slide layout, if it's a different instance
final MasterSheet<S,P> sl = slide.getSlideLayout();
if (sl != ms) {
printSlideMaster(sl, sb);
}
}
if (commentsByDefault) {
printComments(slide, sb);
}
@ -165,6 +152,31 @@ public class SlideShowExtractor<
return sb.toString();
}
private void printSlideMaster(final MasterSheet<S,P> master, final StringBuilder sb) {
if (master == null) {
return;
}
for (final Shape<S,P> shape : master) {
if (shape instanceof TextShape) {
final TextShape<S,P> ts = (TextShape<S,P>)shape;
final String text = ts.getText();
if (text == null || text.isEmpty() || "*".equals(text)) {
continue;
}
if (ts.isPlaceholder()) {
// don't bother about boiler plate text on master sheets
LOG.log(POILogger.INFO, "Ignoring boiler plate (placeholder) text on slide master:", text);
continue;
}
sb.append(text);
if (!text.endsWith("\n")) {
sb.append("\n");
}
}
}
}
private String printHeaderReturnFooter(final Sheet<S,P> sheet, final StringBuilder sb) {
final Sheet<S, P> m = (sheet instanceof Slide) ? sheet.getMasterSheet() : sheet;
final StringBuilder footer = new StringBuilder("\n");

View File

@ -76,4 +76,11 @@ public interface Slide<
* @return the comment(s) for this slide
*/
List<? extends Comment> getComments();
/**
* @return the assigned slide layout
*
* @since POI 4.0.0
*/
MasterSheet getSlideLayout();
}

View File

@ -141,6 +141,7 @@ implements Slide<XSLFShape,XSLFTextParagraph> {
return getSlideLayout();
}
@Override
public XSLFSlideLayout getSlideLayout(){
if(_layout == null){
for (POIXMLDocumentPart p : getRelations()) {

View File

@ -177,7 +177,6 @@ public class TestXSLFPowerPointExtractor {
}
@Test
@Ignore("currently slidelayouts aren't yet supported")
public void testGetMasterText() throws Exception {
try (XMLSlideShow xml = openPPTX("WithMaster.pptx");
SlideShowExtractor extractor = new SlideShowExtractor(xml)) {
@ -205,13 +204,10 @@ public class TestXSLFPowerPointExtractor {
String wholeText =
"First page title\n" +
"First page subtitle\n" +
"This is the Master Title\n" +
"This text comes from the Master Slide\n" +
"\n" +
// TODO Detect we didn't have a title, and include the master one
"2nd page subtitle\n" +
"Footer from the master slide\n" +
"This is the Master Title\n" +
"This text comes from the Master Slide\n";
assertEquals(wholeText, text);
}

View File

@ -501,4 +501,12 @@ public final class HSLFSlide extends HSLFSheet implements Slide<HSLFShape,HSLFTe
return false;
}
}
@Override
public HSLFMasterSheet getSlideLayout(){
// TODO: find out how we can find the mastersheet base on the slide layout type, i.e.
// getSlideRecord().getSlideAtom().getSSlideLayoutAtom().getGeometryType()
return getMasterSheet();
}
}