#60662 - Slide import delete unrecognized elements in group shape

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1783486 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2017-02-18 01:07:47 +00:00
parent 9783654301
commit 7810671d2d
2 changed files with 88 additions and 29 deletions

View File

@ -67,6 +67,7 @@ implements XSLFShapeContainer, GroupShape<XSLFShape,XSLFTextParagraph> {
_grpSpPr = shape.getGrpSpPr(); _grpSpPr = shape.getGrpSpPr();
} }
@Override
protected CTGroupShapeProperties getGrpSpPr() { protected CTGroupShapeProperties getGrpSpPr() {
return _grpSpPr; return _grpSpPr;
} }
@ -159,6 +160,7 @@ implements XSLFShapeContainer, GroupShape<XSLFShape,XSLFTextParagraph> {
* *
* @return an iterator over the shapes in this sheet * @return an iterator over the shapes in this sheet
*/ */
@Override
public Iterator<XSLFShape> iterator(){ public Iterator<XSLFShape> iterator(){
return _shapes.iterator(); return _shapes.iterator();
} }
@ -166,6 +168,7 @@ implements XSLFShapeContainer, GroupShape<XSLFShape,XSLFTextParagraph> {
/** /**
* Remove the specified shape from this group * Remove the specified shape from this group
*/ */
@Override
public boolean removeShape(XSLFShape xShape) { public boolean removeShape(XSLFShape xShape) {
XmlObject obj = xShape.getXmlObject(); XmlObject obj = xShape.getXmlObject();
CTGroupShape grpSp = (CTGroupShape)getXmlObject(); CTGroupShape grpSp = (CTGroupShape)getXmlObject();
@ -214,6 +217,7 @@ implements XSLFShapeContainer, GroupShape<XSLFShape,XSLFTextParagraph> {
return _drawing; return _drawing;
} }
@Override
public XSLFAutoShape createAutoShape(){ public XSLFAutoShape createAutoShape(){
XSLFAutoShape sh = getDrawing().createAutoShape(); XSLFAutoShape sh = getDrawing().createAutoShape();
_shapes.add(sh); _shapes.add(sh);
@ -221,6 +225,7 @@ implements XSLFShapeContainer, GroupShape<XSLFShape,XSLFTextParagraph> {
return sh; return sh;
} }
@Override
public XSLFFreeformShape createFreeform(){ public XSLFFreeformShape createFreeform(){
XSLFFreeformShape sh = getDrawing().createFreeform(); XSLFFreeformShape sh = getDrawing().createFreeform();
_shapes.add(sh); _shapes.add(sh);
@ -228,6 +233,7 @@ implements XSLFShapeContainer, GroupShape<XSLFShape,XSLFTextParagraph> {
return sh; return sh;
} }
@Override
public XSLFTextBox createTextBox(){ public XSLFTextBox createTextBox(){
XSLFTextBox sh = getDrawing().createTextBox(); XSLFTextBox sh = getDrawing().createTextBox();
_shapes.add(sh); _shapes.add(sh);
@ -235,6 +241,7 @@ implements XSLFShapeContainer, GroupShape<XSLFShape,XSLFTextParagraph> {
return sh; return sh;
} }
@Override
public XSLFConnectorShape createConnector(){ public XSLFConnectorShape createConnector(){
XSLFConnectorShape sh = getDrawing().createConnector(); XSLFConnectorShape sh = getDrawing().createConnector();
_shapes.add(sh); _shapes.add(sh);
@ -242,6 +249,7 @@ implements XSLFShapeContainer, GroupShape<XSLFShape,XSLFTextParagraph> {
return sh; return sh;
} }
@Override
public XSLFGroupShape createGroup(){ public XSLFGroupShape createGroup(){
XSLFGroupShape sh = getDrawing().createGroup(); XSLFGroupShape sh = getDrawing().createGroup();
_shapes.add(sh); _shapes.add(sh);
@ -249,6 +257,7 @@ implements XSLFShapeContainer, GroupShape<XSLFShape,XSLFTextParagraph> {
return sh; return sh;
} }
@Override
public XSLFPictureShape createPicture(PictureData pictureData){ public XSLFPictureShape createPicture(PictureData pictureData){
if (!(pictureData instanceof XSLFPictureData)) { if (!(pictureData instanceof XSLFPictureData)) {
throw new IllegalArgumentException("pictureData needs to be of type XSLFPictureData"); throw new IllegalArgumentException("pictureData needs to be of type XSLFPictureData");
@ -327,36 +336,52 @@ implements XSLFShapeContainer, GroupShape<XSLFShape,XSLFTextParagraph> {
@Override @Override
void copy(XSLFShape src){ void copy(XSLFShape src){
XSLFGroupShape gr = (XSLFGroupShape)src; XSLFGroupShape gr = (XSLFGroupShape)src;
// clear shapes
clear();
// recursively update each shape
for(XSLFShape shape : gr.getShapes()) {
XSLFShape newShape;
if (shape instanceof XSLFTextBox) {
newShape = createTextBox();
} else if (shape instanceof XSLFAutoShape) {
newShape = createAutoShape();
} else if (shape instanceof XSLFConnectorShape) {
newShape = createConnector();
} else if (shape instanceof XSLFFreeformShape) {
newShape = createFreeform();
} else if (shape instanceof XSLFPictureShape) {
XSLFPictureShape p = (XSLFPictureShape)shape;
XSLFPictureData pd = p.getPictureData();
XSLFPictureData pdNew = getSheet().getSlideShow().addPicture(pd.getData(), pd.getType());
newShape = createPicture(pdNew);
} else if (shape instanceof XSLFGroupShape) {
newShape = createGroup();
} else if (shape instanceof XSLFTable) {
newShape = createTable();
} else {
_logger.log(POILogger.WARN, "copying of class "+shape.getClass()+" not supported.");
continue;
}
newShape.copy(shape); // recursively update each shape
List<XSLFShape> tgtShapes = getShapes();
List<XSLFShape> srcShapes = gr.getShapes();
// workaround for a call by XSLFSheet.importContent:
// if we have already the same amount of child shapes
// then assume, that we've been called by import content and only need to update the children
if (tgtShapes.size() == srcShapes.size()) {
for(int i = 0; i < tgtShapes.size(); i++){
XSLFShape s1 = srcShapes.get(i);
XSLFShape s2 = tgtShapes.get(i);
s2.copy(s1);
}
} else {
// otherwise recreate the shapes from scratch
clear();
// recursively update each shape
for(XSLFShape shape : srcShapes) {
XSLFShape newShape;
if (shape instanceof XSLFTextBox) {
newShape = createTextBox();
} else if (shape instanceof XSLFAutoShape) {
newShape = createAutoShape();
} else if (shape instanceof XSLFConnectorShape) {
newShape = createConnector();
} else if (shape instanceof XSLFFreeformShape) {
newShape = createFreeform();
} else if (shape instanceof XSLFPictureShape) {
XSLFPictureShape p = (XSLFPictureShape)shape;
XSLFPictureData pd = p.getPictureData();
XSLFPictureData pdNew = getSheet().getSlideShow().addPicture(pd.getData(), pd.getType());
newShape = createPicture(pdNew);
} else if (shape instanceof XSLFGroupShape) {
newShape = createGroup();
} else if (shape instanceof XSLFTable) {
newShape = createTable();
} else {
_logger.log(POILogger.WARN, "copying of class "+shape.getClass()+" not supported.");
continue;
}
newShape.copy(shape);
}
} }
} }
@ -364,6 +389,7 @@ implements XSLFShapeContainer, GroupShape<XSLFShape,XSLFTextParagraph> {
* Removes all of the elements from this container (optional operation). * Removes all of the elements from this container (optional operation).
* The container will be empty after this call returns. * The container will be empty after this call returns.
*/ */
@Override
public void clear() { public void clear() {
List<XSLFShape> shapes = new ArrayList<XSLFShape>(getShapes()); List<XSLFShape> shapes = new ArrayList<XSLFShape>(getShapes());
for(XSLFShape shape : shapes){ for(XSLFShape shape : shapes){
@ -371,6 +397,7 @@ implements XSLFShapeContainer, GroupShape<XSLFShape,XSLFTextParagraph> {
} }
} }
@Override
public void addShape(XSLFShape shape) { public void addShape(XSLFShape shape) {
throw new UnsupportedOperationException( throw new UnsupportedOperationException(
"Adding a shape from a different container is not supported -" "Adding a shape from a different container is not supported -"

View File

@ -52,6 +52,7 @@ import org.apache.poi.sl.usermodel.VerticalAlignment;
import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor; import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;
import org.apache.poi.xslf.usermodel.XMLSlideShow; import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFAutoShape; import org.apache.poi.xslf.usermodel.XSLFAutoShape;
import org.apache.poi.xslf.usermodel.XSLFGroupShape;
import org.apache.poi.xslf.usermodel.XSLFHyperlink; import org.apache.poi.xslf.usermodel.XSLFHyperlink;
import org.apache.poi.xslf.usermodel.XSLFPictureData; import org.apache.poi.xslf.usermodel.XSLFPictureData;
import org.apache.poi.xslf.usermodel.XSLFPictureShape; import org.apache.poi.xslf.usermodel.XSLFPictureShape;
@ -67,6 +68,8 @@ import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun; import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.openxmlformats.schemas.drawingml.x2006.main.CTOuterShadowEffect;
import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
public class TestXSLFBugs { public class TestXSLFBugs {
@ -592,4 +595,33 @@ public class TestXSLFBugs {
ppt.createSlide(); ppt.createSlide();
ppt.close(); ppt.close();
} }
@Test
public void bug60662() throws IOException {
XMLSlideShow src = new XMLSlideShow();
XSLFSlide sl = src.createSlide();
XSLFGroupShape gs = sl.createGroup();
gs.setAnchor(new Rectangle2D.Double(100,100,100,100));
gs.setInteriorAnchor(new Rectangle2D.Double(0,0,100,100));
XSLFAutoShape as = gs.createAutoShape();
as.setAnchor(new Rectangle2D.Double(0,0,100,100));
as.setShapeType(ShapeType.STAR_24);
as.setFillColor(Color.YELLOW);
CTShape csh = (CTShape)as.getXmlObject();
CTOuterShadowEffect shadow = csh.getSpPr().addNewEffectLst().addNewOuterShdw();
shadow.setDir(270000);
shadow.setDist(100000);
shadow.addNewSrgbClr().setVal(new byte[] {0x00, (byte)0xFF, 0x00});
XMLSlideShow dst = new XMLSlideShow();
XSLFSlide sl2 = dst.createSlide();
sl2.importContent(sl);
XSLFGroupShape gs2 = (XSLFGroupShape)sl2.getShapes().get(0);
XSLFAutoShape as2 = (XSLFAutoShape)gs2.getShapes().get(0);
CTShape csh2 = (CTShape)as2.getXmlObject();
assertTrue(csh2.getSpPr().isSetEffectLst());
dst.close();
src.close();
}
} }