Bug 60059 - Deprecate xslf.usermodel.Drawing* - was: Can't change text of DrawingParagraph

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1766746 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2016-10-26 22:32:46 +00:00
parent e447ef1144
commit e4575a0dfe
11 changed files with 151 additions and 91 deletions

View File

@ -17,24 +17,25 @@
package org.apache.poi.xslf.extractor; package org.apache.poi.xslf.extractor;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import org.apache.poi.POIXMLTextExtractor; import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException; import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xslf.usermodel.DrawingParagraph;
import org.apache.poi.xslf.usermodel.DrawingTextBody;
import org.apache.poi.xslf.usermodel.DrawingTextPlaceholder;
import org.apache.poi.xslf.usermodel.XMLSlideShow; import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFCommentAuthors; import org.apache.poi.xslf.usermodel.XSLFCommentAuthors;
import org.apache.poi.xslf.usermodel.XSLFComments; import org.apache.poi.xslf.usermodel.XSLFComments;
import org.apache.poi.xslf.usermodel.XSLFCommonSlideData;
import org.apache.poi.xslf.usermodel.XSLFNotes; import org.apache.poi.xslf.usermodel.XSLFNotes;
import org.apache.poi.xslf.usermodel.XSLFRelation; import org.apache.poi.xslf.usermodel.XSLFRelation;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFShapeContainer;
import org.apache.poi.xslf.usermodel.XSLFSlide; import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFSlideLayout; import org.apache.poi.xslf.usermodel.XSLFSlideLayout;
import org.apache.poi.xslf.usermodel.XSLFSlideMaster; import org.apache.poi.xslf.usermodel.XSLFSlideMaster;
import org.apache.poi.xslf.usermodel.XSLFSlideShow; import org.apache.poi.xslf.usermodel.XSLFSlideShow;
import org.apache.poi.xslf.usermodel.XSLFTable;
import org.apache.poi.xslf.usermodel.XSLFTableCell;
import org.apache.poi.xslf.usermodel.XSLFTableRow;
import org.apache.poi.xslf.usermodel.XSLFTextShape;
import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.presentationml.x2006.main.CTComment; import org.openxmlformats.schemas.presentationml.x2006.main.CTComment;
import org.openxmlformats.schemas.presentationml.x2006.main.CTCommentAuthor; import org.openxmlformats.schemas.presentationml.x2006.main.CTCommentAuthor;
@ -115,18 +116,38 @@ public class XSLFPowerPointExtractor extends POIXMLTextExtractor {
/** /**
* Gets the requested text from the file * Gets the requested text from the file
*
* @param slideText Should we retrieve text from slides? * @param slideText Should we retrieve text from slides?
* @param notesText Should we retrieve text from notes? * @param notesText Should we retrieve text from notes?
* @param masterText Should we retrieve text from master slides? * @param masterText Should we retrieve text from master slides?
*
* @return the extracted text
*/ */
public String getText(boolean slideText, boolean notesText, boolean masterText) { public String getText(boolean slideText, boolean notesText, boolean masterText) {
StringBuffer text = new StringBuffer(); StringBuilder text = new StringBuilder();
List<XSLFSlide> slides = slideshow.getSlides(); for (XSLFSlide slide : slideshow.getSlides()) {
XSLFCommentAuthors commentAuthors = slideshow.getCommentAuthors(); text.append(getText(slide, slideText, notesText, masterText));
}
return text.toString();
}
/**
* Gets the requested text from the slide
*
* @param slide the slide to retrieve the text from
* @param slideText Should we retrieve text from slides?
* @param notesText Should we retrieve text from notes?
* @param masterText Should we retrieve text from master slides?
*
* @return the extracted text
*/
public static String getText(XSLFSlide slide, boolean slideText, boolean notesText, boolean masterText) {
StringBuilder text = new StringBuilder();
XSLFCommentAuthors commentAuthors = slide.getSlideShow().getCommentAuthors();
for (XSLFSlide slide : slides) {
try {
XSLFNotes notes = slide.getNotes(); XSLFNotes notes = slide.getNotes();
XSLFComments comments = slide.getComments(); XSLFComments comments = slide.getComments();
XSLFSlideLayout layout = slide.getSlideLayout(); XSLFSlideLayout layout = slide.getSlideLayout();
@ -137,15 +158,15 @@ public class XSLFPowerPointExtractor extends POIXMLTextExtractor {
// Do the slide's text if requested // Do the slide's text if requested
if (slideText) { if (slideText) {
extractText(slide.getCommonSlideData(), false, text); extractText(slide, false, text);
// If requested, get text from the master and it's layout // If requested, get text from the master and it's layout
if(masterText) { if(masterText) {
if(layout != null) { if(layout != null) {
extractText(layout.getCommonSlideData(), true, text); extractText(layout, true, text);
} }
if(master != null) { if(master != null) {
extractText(master.getCommonSlideData(), true, text); extractText(master, true, text);
} }
} }
@ -169,30 +190,34 @@ public class XSLFPowerPointExtractor extends POIXMLTextExtractor {
// Do the notes if requested // Do the notes if requested
if (notesText && notes != null) { if (notesText && notes != null) {
extractText(notes.getCommonSlideData(), false, text); extractText(notes, false, text);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
} }
return text.toString(); return text.toString();
} }
private void extractText(XSLFCommonSlideData data, boolean skipPlaceholders, StringBuffer text) { private static void extractText(XSLFShapeContainer data, boolean skipPlaceholders, StringBuilder text) {
for(DrawingTextBody textBody : data.getDrawingText()) { for (XSLFShape s : data) {
if(skipPlaceholders && textBody instanceof DrawingTextPlaceholder) { if (s instanceof XSLFShapeContainer) {
DrawingTextPlaceholder ph = (DrawingTextPlaceholder)textBody; extractText((XSLFShapeContainer)s, skipPlaceholders, text);
if(! ph.isPlaceholderCustom()) { } else if (s instanceof XSLFTextShape) {
XSLFTextShape ts = (XSLFTextShape)s;
// Skip non-customised placeholder text // Skip non-customised placeholder text
continue; if (!(skipPlaceholders && ts.isPlaceholder())) {
text.append(ts.getText());
text.append("\n");
} }
} else if (s instanceof XSLFTable) {
XSLFTable ts = (XSLFTable)s;
// Skip non-customised placeholder text
for (XSLFTableRow r : ts) {
for (XSLFTableCell c : r) {
text.append(c.getText());
text.append("\t");
} }
for (DrawingParagraph p : textBody.getParagraphs()) {
text.append(p.getText());
text.append("\n"); text.append("\n");
} }
} }
} }
}
} }

View File

@ -17,12 +17,17 @@
package org.apache.poi.xslf.usermodel; package org.apache.poi.xslf.usermodel;
import org.apache.poi.util.Removal;
import org.apache.xmlbeans.XmlCursor; import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlObject; import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun; import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextLineBreak; import org.openxmlformats.schemas.drawingml.x2006.main.CTTextLineBreak;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph; import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
/*
* @deprecated POI 3.16 beta 1. Instead iterate over the shapes/notes of the slides
*/
@Removal(version="3.18")
public class DrawingParagraph { public class DrawingParagraph {
private final CTTextParagraph p; private final CTTextParagraph p;

View File

@ -17,9 +17,14 @@
package org.apache.poi.xslf.usermodel; package org.apache.poi.xslf.usermodel;
import org.apache.poi.util.Removal;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTable; import org.openxmlformats.schemas.drawingml.x2006.main.CTTable;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTableRow; import org.openxmlformats.schemas.drawingml.x2006.main.CTTableRow;
/*
* @deprecated POI 3.16 beta 1. use {@link XSLFTable} instead
*/
@Removal(version="3.18")
public class DrawingTable { public class DrawingTable {
private final CTTable table; private final CTTable table;

View File

@ -17,8 +17,13 @@
package org.apache.poi.xslf.usermodel; package org.apache.poi.xslf.usermodel;
import org.apache.poi.util.Removal;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTableCell; import org.openxmlformats.schemas.drawingml.x2006.main.CTTableCell;
/*
* @deprecated POI 3.16 beta 1. use {@link XSLFTable} instead
*/
@Removal(version="3.18")
public class DrawingTableCell { public class DrawingTableCell {
private final CTTableCell cell; private final CTTableCell cell;
private final DrawingTextBody drawingTextBody; private final DrawingTextBody drawingTextBody;

View File

@ -17,9 +17,14 @@
package org.apache.poi.xslf.usermodel; package org.apache.poi.xslf.usermodel;
import org.apache.poi.util.Removal;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTableCell; import org.openxmlformats.schemas.drawingml.x2006.main.CTTableCell;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTableRow; import org.openxmlformats.schemas.drawingml.x2006.main.CTTableRow;
/*
* @deprecated POI 3.16 beta 1. use {@link XSLFTable} instead
*/
@Removal(version="3.18")
public class DrawingTableRow { public class DrawingTableRow {
private final CTTableRow row; private final CTTableRow row;

View File

@ -17,9 +17,14 @@
package org.apache.poi.xslf.usermodel; package org.apache.poi.xslf.usermodel;
import org.apache.poi.util.Removal;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody; import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph; import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
/*
* @deprecated POI 3.16 beta 1. use {@link XSLFTable} instead
*/
@Removal(version="3.18")
public class DrawingTextBody { public class DrawingTextBody {
private final CTTextBody textBody; private final CTTextBody textBody;

View File

@ -17,6 +17,7 @@
package org.apache.poi.xslf.usermodel; package org.apache.poi.xslf.usermodel;
import org.apache.poi.util.Removal;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody; import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
import org.openxmlformats.schemas.presentationml.x2006.main.CTPlaceholder; import org.openxmlformats.schemas.presentationml.x2006.main.CTPlaceholder;
import org.openxmlformats.schemas.presentationml.x2006.main.STPlaceholderType; import org.openxmlformats.schemas.presentationml.x2006.main.STPlaceholderType;
@ -26,6 +27,10 @@ import org.openxmlformats.schemas.presentationml.x2006.main.STPlaceholderType;
* @author nick * @author nick
* *
*/ */
/*
* @deprecated POI 3.16 beta 1. use ??? instead
*/
@Removal(version="3.18")
public class DrawingTextPlaceholder extends DrawingTextBody { public class DrawingTextPlaceholder extends DrawingTextBody {
private final CTPlaceholder placeholder; private final CTPlaceholder placeholder;

View File

@ -25,6 +25,7 @@ import java.util.List;
import org.apache.poi.POIXMLException; import org.apache.poi.POIXMLException;
import org.apache.poi.util.Beta; import org.apache.poi.util.Beta;
import org.apache.poi.util.Removal;
import org.apache.xmlbeans.XmlCursor; import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject; import org.apache.xmlbeans.XmlObject;
@ -38,6 +39,10 @@ import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFra
import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape; import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
import org.openxmlformats.schemas.presentationml.x2006.main.CTShape; import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
/*
* @deprecated POI 3.16 beta 1. - iterate over the shapes of a slide instead
*/
@Removal(version="3.18")
@Beta @Beta
public class XSLFCommonSlideData { public class XSLFCommonSlideData {
private final CTCommonSlideData data; private final CTCommonSlideData data;

View File

@ -47,6 +47,7 @@ import org.apache.poi.sl.usermodel.Sheet;
import org.apache.poi.util.Beta; import org.apache.poi.util.Beta;
import org.apache.poi.util.IOUtils; import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Internal; import org.apache.poi.util.Internal;
import org.apache.poi.util.Removal;
import org.apache.xmlbeans.XmlObject; import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions; import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.presentationml.x2006.main.CTCommonSlideData; import org.openxmlformats.schemas.presentationml.x2006.main.CTCommonSlideData;
@ -128,11 +129,19 @@ implements XSLFShapeContainer, Sheet<XSLFShape,XSLFTextParagraph> {
*/ */
public abstract XmlObject getXmlObject(); public abstract XmlObject getXmlObject();
/*
* @deprecated POI 3.16 beta 1. use {@link XSLFTable} instead
*/
@Removal(version="3.18")
@Internal @Internal
public XSLFCommonSlideData getCommonSlideData() { public XSLFCommonSlideData getCommonSlideData() {
return _commonSlideData; return _commonSlideData;
} }
/*
* @deprecated POI 3.16 beta 1. use {@link XSLFTable} instead
*/
@Removal(version="3.18")
protected void setCommonSlideData(CTCommonSlideData data) { protected void setCommonSlideData(CTCommonSlideData data) {
if(data == null) { if(data == null) {
_commonSlideData = null; _commonSlideData = null;
@ -550,7 +559,6 @@ implements XSLFShapeContainer, Sheet<XSLFShape,XSLFTextParagraph> {
* @param packagePart package part containing the data to import * @param packagePart package part containing the data to import
* @return ID of the created relationship * @return ID of the created relationship
*/ */
@SuppressWarnings("resource")
String importBlip(String blipId, PackagePart packagePart) { String importBlip(String blipId, PackagePart packagePart) {
PackageRelationship blipRel = packagePart.getRelationship(blipId); PackageRelationship blipRel = packagePart.getRelationship(blipId);
PackagePart blipPart; PackagePart blipPart;
@ -573,7 +581,6 @@ implements XSLFShapeContainer, Sheet<XSLFShape,XSLFTextParagraph> {
/** /**
* Import a package part into this sheet. * Import a package part into this sheet.
*/ */
@SuppressWarnings("resource")
PackagePart importPart(PackageRelationship srcRel, PackagePart srcPafrt) { PackagePart importPart(PackageRelationship srcRel, PackagePart srcPafrt) {
PackagePart destPP = getPackagePart(); PackagePart destPP = getPackagePart();
PackagePartName srcPPName = srcPafrt.getPartName(); PackagePartName srcPPName = srcPafrt.getPartName();

View File

@ -51,6 +51,7 @@ import org.apache.poi.sl.usermodel.PictureData;
import org.apache.poi.sl.usermodel.PictureData.PictureType; import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.apache.poi.sl.usermodel.ShapeType; import org.apache.poi.sl.usermodel.ShapeType;
import org.apache.poi.sl.usermodel.VerticalAlignment; import org.apache.poi.sl.usermodel.VerticalAlignment;
import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;
import org.apache.poi.xslf.usermodel.DrawingParagraph; import org.apache.poi.xslf.usermodel.DrawingParagraph;
import org.apache.poi.xslf.usermodel.DrawingTextBody; import org.apache.poi.xslf.usermodel.DrawingTextBody;
import org.apache.poi.xslf.usermodel.XMLSlideShow; import org.apache.poi.xslf.usermodel.XMLSlideShow;
@ -252,14 +253,7 @@ public class TestXSLFBugs {
} }
protected String getSlideText(XSLFSlide slide) { protected String getSlideText(XSLFSlide slide) {
StringBuffer text = new StringBuffer(); return XSLFPowerPointExtractor.getText(slide, true, false, false);
for(DrawingTextBody textBody : slide.getCommonSlideData().getDrawingText()) {
for (DrawingParagraph p : textBody.getParagraphs()) {
text.append(p.getText());
text.append("\n");
}
}
return text.toString();
} }
@Test @Test

View File

@ -97,21 +97,21 @@ public class TestXSLFPowerPointExtractor {
// Just notes, no slides // Just notes, no slides
text = extractor.getText(false, true); text = extractor.getText(false, true);
assertEquals("\n\n\n\n", text); assertEquals("\n\n1\n\n\n2\n", text);
// Both // Both
text = extractor.getText(true, true, false); text = extractor.getText(true, true, false);
String bothText = String bothText =
"Lorem ipsum dolor sit amet\n" + "Lorem ipsum dolor sit amet\n" +
"Nunc at risus vel erat tempus posuere. Aenean non ante.\n" + "Nunc at risus vel erat tempus posuere. Aenean non ante.\n" +
"\n\n\n" + "\n\n\n1\n" +
"Lorem ipsum dolor sit amet\n" + "Lorem ipsum dolor sit amet\n" +
"Lorem\n" + "Lorem\n" +
"ipsum\n" + "ipsum\n" +
"dolor\n" + "dolor\n" +
"sit\n" + "sit\n" +
"amet\n" + "amet\n" +
"\n\n\n"; "\n\n\n2\n";
assertEquals(bothText, text); assertEquals(bothText, text);
// With Slides and Master Text // With Slides and Master Text
@ -134,22 +134,21 @@ public class TestXSLFPowerPointExtractor {
String snmText = String snmText =
"Lorem ipsum dolor sit amet\n" + "Lorem ipsum dolor sit amet\n" +
"Nunc at risus vel erat tempus posuere. Aenean non ante.\n" + "Nunc at risus vel erat tempus posuere. Aenean non ante.\n" +
"\n" + "\n\n\n1\n" +
"\n\n" +
"Lorem ipsum dolor sit amet\n" + "Lorem ipsum dolor sit amet\n" +
"Lorem\n" + "Lorem\n" +
"ipsum\n" + "ipsum\n" +
"dolor\n" + "dolor\n" +
"sit\n" + "sit\n" +
"amet\n" + "amet\n" +
"\n\n\n"; "\n\n\n2\n";
assertEquals(snmText, text); assertEquals(snmText, text);
// Via set defaults // Via set defaults
extractor.setSlidesByDefault(false); extractor.setSlidesByDefault(false);
extractor.setNotesByDefault(true); extractor.setNotesByDefault(true);
text = extractor.getText(); text = extractor.getText();
assertEquals("\n\n\n\n", text); assertEquals("\n\n1\n\n\n2\n", text);
extractor.close(); extractor.close();
xmlA.close(); xmlA.close();