mirror of https://github.com/apache/poi.git
#62051 - Two shapes have the same shapeId within the same slide.
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1831947 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
92268ed278
commit
82774d4dc2
|
@ -54,4 +54,18 @@ public interface Shape<
|
|||
* if null, the bounds of the shape are used.
|
||||
*/
|
||||
void draw(Graphics2D graphics, Rectangle2D bounds);
|
||||
|
||||
|
||||
/**
|
||||
* Returns a unique identifier for this shape within the current slide.
|
||||
* This ID may be used to assist in uniquely identifying this object so that it can
|
||||
* be referred to by other parts of the document.
|
||||
* <p>
|
||||
* If multiple objects within the same slide share the same id attribute value,
|
||||
* then the document shall be considered non-conformant.
|
||||
* </p>
|
||||
*
|
||||
* @return unique id of this shape
|
||||
*/
|
||||
int getShapeId();
|
||||
}
|
||||
|
|
|
@ -67,7 +67,7 @@ public class XSLFAutoShape extends XSLFTextShape
|
|||
CTShapeNonVisual nvSpPr = ct.addNewNvSpPr();
|
||||
CTNonVisualDrawingProps cnv = nvSpPr.addNewCNvPr();
|
||||
cnv.setName("AutoShape " + shapeId);
|
||||
cnv.setId(shapeId + 1);
|
||||
cnv.setId(shapeId);
|
||||
nvSpPr.addNewCNvSpPr();
|
||||
nvSpPr.addNewNvPr();
|
||||
CTShapeProperties spPr = ct.addNewSpPr();
|
||||
|
|
|
@ -51,7 +51,7 @@ public class XSLFConnectorShape extends XSLFSimpleShape
|
|||
CTConnectorNonVisual nvSpPr = ct.addNewNvCxnSpPr();
|
||||
CTNonVisualDrawingProps cnv = nvSpPr.addNewCNvPr();
|
||||
cnv.setName("Connector " + shapeId);
|
||||
cnv.setId(shapeId + 1);
|
||||
cnv.setId(shapeId);
|
||||
nvSpPr.addNewCNvCxnSpPr();
|
||||
nvSpPr.addNewNvPr();
|
||||
CTShapeProperties spPr = ct.addNewSpPr();
|
||||
|
|
|
@ -18,26 +18,13 @@ package org.apache.poi.xslf.usermodel;
|
|||
|
||||
import java.awt.Color;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import org.apache.poi.POIXMLDocumentPart.RelationPart;
|
||||
import org.apache.poi.hpsf.ClassID;
|
||||
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
||||
import org.apache.poi.openxml4j.opc.OPCPackage;
|
||||
import org.apache.poi.openxml4j.opc.PackagePart;
|
||||
import org.apache.poi.openxml4j.opc.PackagePartName;
|
||||
import org.apache.poi.openxml4j.opc.PackageRelationship;
|
||||
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
|
||||
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
|
||||
import org.apache.poi.util.Beta;
|
||||
import org.apache.xmlbeans.XmlObject;
|
||||
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
|
||||
import org.openxmlformats.schemas.presentationml.x2006.main.CTConnector;
|
||||
import org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame;
|
||||
import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
|
||||
import org.openxmlformats.schemas.presentationml.x2006.main.CTOleObject;
|
||||
import org.openxmlformats.schemas.presentationml.x2006.main.CTPicture;
|
||||
import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
|
||||
|
||||
|
@ -45,7 +32,6 @@ import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
|
|||
@Beta
|
||||
public class XSLFDrawing {
|
||||
private XSLFSheet _sheet;
|
||||
private int _shapeId = 1;
|
||||
private CTGroupShape _spTree;
|
||||
|
||||
/*package*/ XSLFDrawing(XSLFSheet sheet, CTGroupShape spTree){
|
||||
|
@ -58,14 +44,14 @@ public class XSLFDrawing {
|
|||
// ignore them for now
|
||||
if (o instanceof CTNonVisualDrawingProps) {
|
||||
CTNonVisualDrawingProps p = (CTNonVisualDrawingProps)o;
|
||||
_shapeId = (int)Math.max(_shapeId, p.getId());
|
||||
sheet.registerShapeId((int)p.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public XSLFAutoShape createAutoShape(){
|
||||
CTShape sp = _spTree.addNewSp();
|
||||
sp.set(XSLFAutoShape.prototype(_shapeId++));
|
||||
sp.set(XSLFAutoShape.prototype(_sheet.allocateShapeId()));
|
||||
XSLFAutoShape shape = new XSLFAutoShape(sp, _sheet);
|
||||
shape.setAnchor(new Rectangle2D.Double());
|
||||
return shape;
|
||||
|
@ -73,7 +59,7 @@ public class XSLFDrawing {
|
|||
|
||||
public XSLFFreeformShape createFreeform(){
|
||||
CTShape sp = _spTree.addNewSp();
|
||||
sp.set(XSLFFreeformShape.prototype(_shapeId++));
|
||||
sp.set(XSLFFreeformShape.prototype(_sheet.allocateShapeId()));
|
||||
XSLFFreeformShape shape = new XSLFFreeformShape(sp, _sheet);
|
||||
shape.setAnchor(new Rectangle2D.Double());
|
||||
return shape;
|
||||
|
@ -81,7 +67,7 @@ public class XSLFDrawing {
|
|||
|
||||
public XSLFTextBox createTextBox(){
|
||||
CTShape sp = _spTree.addNewSp();
|
||||
sp.set(XSLFTextBox.prototype(_shapeId++));
|
||||
sp.set(XSLFTextBox.prototype(_sheet.allocateShapeId()));
|
||||
XSLFTextBox shape = new XSLFTextBox(sp, _sheet);
|
||||
shape.setAnchor(new Rectangle2D.Double());
|
||||
return shape;
|
||||
|
@ -89,7 +75,7 @@ public class XSLFDrawing {
|
|||
|
||||
public XSLFConnectorShape createConnector(){
|
||||
CTConnector sp = _spTree.addNewCxnSp();
|
||||
sp.set(XSLFConnectorShape.prototype(_shapeId++));
|
||||
sp.set(XSLFConnectorShape.prototype(_sheet.allocateShapeId()));
|
||||
XSLFConnectorShape shape = new XSLFConnectorShape(sp, _sheet);
|
||||
shape.setAnchor(new Rectangle2D.Double());
|
||||
shape.setLineColor(Color.black);
|
||||
|
@ -98,33 +84,33 @@ public class XSLFDrawing {
|
|||
}
|
||||
|
||||
public XSLFGroupShape createGroup(){
|
||||
CTGroupShape obj = _spTree.addNewGrpSp();
|
||||
obj.set(XSLFGroupShape.prototype(_shapeId++));
|
||||
XSLFGroupShape shape = new XSLFGroupShape(obj, _sheet);
|
||||
CTGroupShape sp = _spTree.addNewGrpSp();
|
||||
sp.set(XSLFGroupShape.prototype(_sheet.allocateShapeId()));
|
||||
XSLFGroupShape shape = new XSLFGroupShape(sp, _sheet);
|
||||
shape.setAnchor(new Rectangle2D.Double());
|
||||
return shape;
|
||||
}
|
||||
|
||||
public XSLFPictureShape createPicture(String rel){
|
||||
CTPicture obj = _spTree.addNewPic();
|
||||
obj.set(XSLFPictureShape.prototype(_shapeId++, rel));
|
||||
XSLFPictureShape shape = new XSLFPictureShape(obj, _sheet);
|
||||
CTPicture sp = _spTree.addNewPic();
|
||||
sp.set(XSLFPictureShape.prototype(_sheet.allocateShapeId(), rel));
|
||||
XSLFPictureShape shape = new XSLFPictureShape(sp, _sheet);
|
||||
shape.setAnchor(new Rectangle2D.Double());
|
||||
return shape;
|
||||
}
|
||||
|
||||
public XSLFTable createTable(){
|
||||
CTGraphicalObjectFrame obj = _spTree.addNewGraphicFrame();
|
||||
obj.set(XSLFTable.prototype(_shapeId++));
|
||||
XSLFTable shape = new XSLFTable(obj, _sheet);
|
||||
CTGraphicalObjectFrame sp = _spTree.addNewGraphicFrame();
|
||||
sp.set(XSLFTable.prototype(_sheet.allocateShapeId()));
|
||||
XSLFTable shape = new XSLFTable(sp, _sheet);
|
||||
shape.setAnchor(new Rectangle2D.Double());
|
||||
return shape;
|
||||
}
|
||||
|
||||
public XSLFObjectShape createOleShape(String pictureRel) {
|
||||
CTGraphicalObjectFrame obj = _spTree.addNewGraphicFrame();
|
||||
obj.set(XSLFObjectShape.prototype(_shapeId++, pictureRel));
|
||||
XSLFObjectShape shape = new XSLFObjectShape(obj, _sheet);
|
||||
CTGraphicalObjectFrame sp = _spTree.addNewGraphicFrame();
|
||||
sp.set(XSLFObjectShape.prototype(_sheet.allocateShapeId(), pictureRel));
|
||||
XSLFObjectShape shape = new XSLFObjectShape(sp, _sheet);
|
||||
shape.setAnchor(new Rectangle2D.Double());
|
||||
return shape;
|
||||
}
|
||||
|
|
|
@ -232,7 +232,7 @@ public class XSLFFreeformShape extends XSLFAutoShape
|
|||
CTShapeNonVisual nvSpPr = ct.addNewNvSpPr();
|
||||
CTNonVisualDrawingProps cnv = nvSpPr.addNewCNvPr();
|
||||
cnv.setName("Freeform " + shapeId);
|
||||
cnv.setId(shapeId + 1);
|
||||
cnv.setId(shapeId);
|
||||
nvSpPr.addNewCNvSpPr();
|
||||
nvSpPr.addNewNvPr();
|
||||
CTShapeProperties spPr = ct.addNewSpPr();
|
||||
|
|
|
@ -174,9 +174,12 @@ implements XSLFShapeContainer, GroupShape<XSLFShape,XSLFTextParagraph> {
|
|||
public boolean removeShape(XSLFShape xShape) {
|
||||
XmlObject obj = xShape.getXmlObject();
|
||||
CTGroupShape grpSp = (CTGroupShape)getXmlObject();
|
||||
getSheet().deregisterShapeId(xShape.getShapeId());
|
||||
if(obj instanceof CTShape){
|
||||
grpSp.getSpList().remove(obj);
|
||||
} else if (obj instanceof CTGroupShape){
|
||||
XSLFGroupShape gs = (XSLFGroupShape)xShape;
|
||||
new ArrayList<>(gs.getShapes()).forEach(gs::removeShape);
|
||||
grpSp.getGrpSpList().remove(obj);
|
||||
} else if (obj instanceof CTConnector){
|
||||
grpSp.getCxnSpList().remove(obj);
|
||||
|
@ -203,7 +206,7 @@ implements XSLFShapeContainer, GroupShape<XSLFShape,XSLFTextParagraph> {
|
|||
CTGroupShapeNonVisual nvSpPr = ct.addNewNvGrpSpPr();
|
||||
CTNonVisualDrawingProps cnv = nvSpPr.addNewCNvPr();
|
||||
cnv.setName("Group " + shapeId);
|
||||
cnv.setId(shapeId + 1);
|
||||
cnv.setId(shapeId);
|
||||
|
||||
nvSpPr.addNewCNvGrpSpPr();
|
||||
nvSpPr.addNewNvPr();
|
||||
|
|
|
@ -268,8 +268,8 @@ public class XSLFObjectShape extends XSLFGraphicFrame implements ObjectShape<XSL
|
|||
CTNonVisualDrawingProps cnv = nvGr.addNewCNvPr();
|
||||
// usually the shape name has its index based on the n-th embeding, but having
|
||||
// the prototype separate from the actual updating of the object, we use the shape id
|
||||
cnv.setName("Object " + (shapeId + 1));
|
||||
cnv.setId(shapeId + 1);
|
||||
cnv.setName("Object " + shapeId);
|
||||
cnv.setId(shapeId);
|
||||
|
||||
// add empty property elements otherwise Powerpoint doesn't load the file ...
|
||||
nvGr.addNewCNvGraphicFramePr();
|
||||
|
|
|
@ -68,7 +68,7 @@ public class XSLFPictureShape extends XSLFSimpleShape
|
|||
CTPictureNonVisual nvSpPr = ct.addNewNvPicPr();
|
||||
CTNonVisualDrawingProps cnv = nvSpPr.addNewCNvPr();
|
||||
cnv.setName("Picture " + shapeId);
|
||||
cnv.setId(shapeId + 1);
|
||||
cnv.setId(shapeId);
|
||||
nvSpPr.addNewCNvPicPr().addNewPicLocks().setNoChangeAspect(true);
|
||||
nvSpPr.addNewNvPr();
|
||||
|
||||
|
|
|
@ -102,17 +102,7 @@ public abstract class XSLFShape implements Shape<XSLFShape,XSLFTextParagraph> {
|
|||
return getCNvPr().getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a unique identifier for this shape within the current document.
|
||||
* This ID may be used to assist in uniquely identifying this object so that it can
|
||||
* be referred to by other parts of the document.
|
||||
* <p>
|
||||
* If multiple objects within the same document share the same id attribute value,
|
||||
* then the document shall be considered non-conformant.
|
||||
* </p>
|
||||
*
|
||||
* @return unique id of this shape
|
||||
*/
|
||||
@Override
|
||||
public int getShapeId() {
|
||||
return (int)getCNvPr().getId();
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ import java.io.IOException;
|
|||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.BitSet;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
@ -79,6 +80,8 @@ implements XSLFShapeContainer, Sheet<XSLFShape,XSLFTextParagraph> {
|
|||
private Map<Integer, XSLFSimpleShape> _placeholderByIdMap;
|
||||
private Map<Integer, XSLFSimpleShape> _placeholderByTypeMap;
|
||||
|
||||
private final BitSet shapeIds = new BitSet();
|
||||
|
||||
public XSLFSheet() {
|
||||
super();
|
||||
}
|
||||
|
@ -105,6 +108,26 @@ implements XSLFShapeContainer, Sheet<XSLFShape,XSLFTextParagraph> {
|
|||
throw new IllegalStateException("SlideShow was not found");
|
||||
}
|
||||
|
||||
protected int allocateShapeId() {
|
||||
final int nextId = shapeIds.nextClearBit(1);
|
||||
shapeIds.set(nextId);
|
||||
return nextId;
|
||||
}
|
||||
|
||||
protected void registerShapeId(final int shapeId) {
|
||||
if (shapeIds.get(shapeId)) {
|
||||
LOG.log(POILogger.WARN, "shape id "+shapeId+" has been already used.");
|
||||
}
|
||||
shapeIds.set(shapeId);
|
||||
}
|
||||
|
||||
protected void deregisterShapeId(final int shapeId) {
|
||||
if (!shapeIds.get(shapeId)) {
|
||||
LOG.log(POILogger.WARN, "shape id "+shapeId+" hasn't been registered.");
|
||||
}
|
||||
shapeIds.clear(shapeId);
|
||||
}
|
||||
|
||||
protected static List<XSLFShape> buildShapes(CTGroupShape spTree, XSLFShapeContainer parent){
|
||||
final XSLFSheet sheet = (parent instanceof XSLFSheet) ? (XSLFSheet)parent : ((XSLFShape)parent).getSheet();
|
||||
|
||||
|
@ -328,9 +351,12 @@ implements XSLFShapeContainer, Sheet<XSLFShape,XSLFTextParagraph> {
|
|||
public boolean removeShape(XSLFShape xShape) {
|
||||
XmlObject obj = xShape.getXmlObject();
|
||||
CTGroupShape spTree = getSpTree();
|
||||
deregisterShapeId(xShape.getShapeId());
|
||||
if(obj instanceof CTShape){
|
||||
spTree.getSpList().remove(obj);
|
||||
} else if (obj instanceof CTGroupShape) {
|
||||
XSLFGroupShape gs = (XSLFGroupShape)xShape;
|
||||
new ArrayList<>(gs.getShapes()).forEach(gs::removeShape);
|
||||
spTree.getGrpSpList().remove(obj);
|
||||
} else if (obj instanceof CTConnector) {
|
||||
spTree.getCxnSpList().remove(obj);
|
||||
|
|
|
@ -166,7 +166,7 @@ public class XSLFTable extends XSLFGraphicFrame implements Iterable<XSLFTableRow
|
|||
|
||||
CTNonVisualDrawingProps cnv = nvGr.addNewCNvPr();
|
||||
cnv.setName("Table " + shapeId);
|
||||
cnv.setId(shapeId + 1);
|
||||
cnv.setId(shapeId);
|
||||
nvGr.addNewCNvGraphicFramePr().addNewGraphicFrameLocks().setNoGrp(true);
|
||||
nvGr.addNewNvPr();
|
||||
|
||||
|
|
|
@ -46,7 +46,7 @@ public class XSLFTextBox extends XSLFAutoShape
|
|||
CTShapeNonVisual nvSpPr = ct.addNewNvSpPr();
|
||||
CTNonVisualDrawingProps cnv = nvSpPr.addNewCNvPr();
|
||||
cnv.setName("TextBox " + shapeId);
|
||||
cnv.setId(shapeId + 1);
|
||||
cnv.setId(shapeId);
|
||||
nvSpPr.addNewCNvSpPr().setTxBox(true);
|
||||
nvSpPr.addNewNvPr();
|
||||
CTShapeProperties spPr = ct.addNewSpPr();
|
||||
|
|
|
@ -36,8 +36,11 @@ import java.io.File;
|
|||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
@ -56,10 +59,10 @@ import org.apache.poi.sl.usermodel.PaintStyle.SolidPaint;
|
|||
import org.apache.poi.sl.usermodel.PaintStyle.TexturePaint;
|
||||
import org.apache.poi.sl.usermodel.PictureData;
|
||||
import org.apache.poi.sl.usermodel.PictureData.PictureType;
|
||||
import org.apache.poi.sl.usermodel.Shape;
|
||||
import org.apache.poi.sl.usermodel.ShapeType;
|
||||
import org.apache.poi.sl.usermodel.VerticalAlignment;
|
||||
import org.apache.poi.util.IOUtils;
|
||||
import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;
|
||||
import org.apache.poi.xslf.usermodel.XMLSlideShow;
|
||||
import org.apache.poi.xslf.usermodel.XSLFAutoShape;
|
||||
import org.apache.poi.xslf.usermodel.XSLFGroupShape;
|
||||
|
@ -693,13 +696,6 @@ public class TestXSLFBugs {
|
|||
assertNotNull(notesSlide);
|
||||
}
|
||||
|
||||
/*OutputStream stream = new FileOutputStream("/tmp/test.pptx");
|
||||
try {
|
||||
ppt.write(stream);
|
||||
} finally {
|
||||
stream.close();
|
||||
}*/
|
||||
|
||||
ppt.close();
|
||||
}
|
||||
|
||||
|
@ -727,4 +723,32 @@ public class TestXSLFBugs {
|
|||
}
|
||||
ppt.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void bug62051() throws IOException {
|
||||
final Function<List<XSLFShape>, int[]> ids = (shapes) ->
|
||||
shapes.stream().mapToInt(Shape::getShapeId).toArray();
|
||||
|
||||
try (final XMLSlideShow ppt = new XMLSlideShow()) {
|
||||
final XSLFSlide slide = ppt.createSlide();
|
||||
final List<XSLFShape> shapes = new ArrayList<>();
|
||||
shapes.add(slide.createAutoShape());
|
||||
final XSLFGroupShape g1 = slide.createGroup();
|
||||
shapes.add(g1);
|
||||
final XSLFGroupShape g2 = g1.createGroup();
|
||||
shapes.add(g2);
|
||||
shapes.add(g2.createAutoShape());
|
||||
shapes.add(slide.createAutoShape());
|
||||
shapes.add(g2.createAutoShape());
|
||||
shapes.add(g1.createAutoShape());
|
||||
assertArrayEquals(new int[]{ 2,3,4,5,6,7,8 }, ids.apply(shapes));
|
||||
g1.removeShape(g2);
|
||||
shapes.remove(5);
|
||||
shapes.remove(3);
|
||||
shapes.remove(2);
|
||||
shapes.add(g1.createAutoShape());
|
||||
assertArrayEquals(new int[]{ 2,3,6,8,4 }, ids.apply(shapes));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -94,7 +94,7 @@ public class TestXSLFTable {
|
|||
assertNotNull(tbl.getCTTable().getTblGrid());
|
||||
assertNotNull(tbl.getCTTable().getTblPr());
|
||||
assertTrue(tbl.getXmlObject() instanceof CTGraphicalObjectFrame);
|
||||
assertEquals("Table 1", tbl.getShapeName());
|
||||
assertEquals("Table 2", tbl.getShapeName());
|
||||
assertEquals(2, tbl.getShapeId());
|
||||
assertEquals(0, tbl.getRows().size());
|
||||
assertEquals(0, tbl.getCTTable().sizeOfTrArray());
|
||||
|
|
|
@ -538,9 +538,7 @@ public abstract class HSLFShape implements Shape<HSLFShape,HSLFTextParagraph> {
|
|||
return new Color(r, g, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return id for the shape.
|
||||
*/
|
||||
@Override
|
||||
public int getShapeId(){
|
||||
EscherSpRecord spRecord = getEscherChild(EscherSpRecord.RECORD_ID);
|
||||
return spRecord == null ? 0 : spRecord.getShapeId();
|
||||
|
|
Loading…
Reference in New Issue