From 3e5e4ee2a38965f22c7a991a037cf8256a697670 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Wed, 22 Jun 2011 13:48:43 +0000 Subject: [PATCH] Bug 51332 - Fixed internal IDs of shapes generated by HSSFPatriarch when there are more than 1023 drawing objects git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1138465 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../apache/poi/hssf/model/AbstractShape.java | 24 ++++++++ .../apache/poi/hssf/model/ComboboxShape.java | 2 +- .../apache/poi/hssf/model/CommentShape.java | 6 ++ .../org/apache/poi/hssf/model/LineShape.java | 2 +- .../apache/poi/hssf/model/PictureShape.java | 8 +-- .../apache/poi/hssf/model/PolygonShape.java | 2 +- .../poi/hssf/model/SimpleFilledShape.java | 2 +- .../apache/poi/hssf/model/TextboxShape.java | 3 +- .../org/apache/poi/hssf/model/TestShapes.java | 58 +++++++++++++++++++ 10 files changed, 96 insertions(+), 12 deletions(-) create mode 100644 src/testcases/org/apache/poi/hssf/model/TestShapes.java diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 00bf44144..f1b78a6ba 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 51332 - Fixed internal IDs of shapes generated by HSSFPatriarch when there are more than 1023 drawing objects 48408 - Improved documentation for Sheet.setColumnWidth 51390 - Added handling of additional properties to HWPF ParagraphSprmCompressor 51389 - Support for sprmPJc paragraph SPRM in HWPF diff --git a/src/java/org/apache/poi/hssf/model/AbstractShape.java b/src/java/org/apache/poi/hssf/model/AbstractShape.java index 31c1d1d8a..3fd8883d6 100644 --- a/src/java/org/apache/poi/hssf/model/AbstractShape.java +++ b/src/java/org/apache/poi/hssf/model/AbstractShape.java @@ -150,4 +150,28 @@ public abstract class AbstractShape return options; // # options added } + /** + * Generate id for the CommonObjectDataSubRecord that stands behind this shape + * + *

+ * Typically objectId starts with 1, is unique among all Obj record within the worksheet stream + * and increments by 1 for every new shape. + * For most shapes there is a straight relationship between shapeId (generated by DDF) and objectId: + *

+ *

+ * shapeId is unique and starts with 1024, hence objectId can be derived as shapeId-1024. + *

+ *

+ * An exception from this rule is the CellComment shape whose objectId start with 1024. + * See {@link CommentShape#getCmoObjectId(int)} + *

+ * + * + * + * @param shapeId shape id as generated by drawing manager + * @return objectId object id that will be assigned to the Obj record + */ + int getCmoObjectId(int shapeId){ + return shapeId - 1024; + } } diff --git a/src/java/org/apache/poi/hssf/model/ComboboxShape.java b/src/java/org/apache/poi/hssf/model/ComboboxShape.java index df697c79f..7032687f7 100644 --- a/src/java/org/apache/poi/hssf/model/ComboboxShape.java +++ b/src/java/org/apache/poi/hssf/model/ComboboxShape.java @@ -50,7 +50,7 @@ public class ComboboxShape ObjRecord obj = new ObjRecord(); CommonObjectDataSubRecord c = new CommonObjectDataSubRecord(); c.setObjectType(HSSFSimpleShape.OBJECT_TYPE_COMBO_BOX); - c.setObjectId(shapeId); + c.setObjectId( getCmoObjectId(shapeId) ); c.setLocked(true); c.setPrintable(false); c.setAutofill(true); diff --git a/src/java/org/apache/poi/hssf/model/CommentShape.java b/src/java/org/apache/poi/hssf/model/CommentShape.java index 23242d56d..f739a44d7 100644 --- a/src/java/org/apache/poi/hssf/model/CommentShape.java +++ b/src/java/org/apache/poi/hssf/model/CommentShape.java @@ -136,4 +136,10 @@ public final class CommentShape extends TextboxShape { { return _note; } + + @Override + int getCmoObjectId(int shapeId){ + return shapeId; + } + } diff --git a/src/java/org/apache/poi/hssf/model/LineShape.java b/src/java/org/apache/poi/hssf/model/LineShape.java index 74913b3af..2480d4260 100644 --- a/src/java/org/apache/poi/hssf/model/LineShape.java +++ b/src/java/org/apache/poi/hssf/model/LineShape.java @@ -97,7 +97,7 @@ public class LineShape ObjRecord obj = new ObjRecord(); CommonObjectDataSubRecord c = new CommonObjectDataSubRecord(); c.setObjectType((short) ((HSSFSimpleShape)shape).getShapeType()); - c.setObjectId(shapeId); + c.setObjectId( getCmoObjectId(shapeId) ); c.setLocked(true); c.setPrintable(true); c.setAutofill(true); diff --git a/src/java/org/apache/poi/hssf/model/PictureShape.java b/src/java/org/apache/poi/hssf/model/PictureShape.java index 01cebe56d..fb5041e21 100644 --- a/src/java/org/apache/poi/hssf/model/PictureShape.java +++ b/src/java/org/apache/poi/hssf/model/PictureShape.java @@ -99,21 +99,15 @@ public class PictureShape ObjRecord obj = new ObjRecord(); CommonObjectDataSubRecord c = new CommonObjectDataSubRecord(); c.setObjectType((short) ((HSSFSimpleShape)shape).getShapeType()); -// c.setObjectId((short) ( 1 )); - c.setObjectId(shapeId); + c.setObjectId( getCmoObjectId(shapeId) ); c.setLocked(true); c.setPrintable(true); c.setAutofill(true); c.setAutoline(true); -// c.setReserved2( 0x012C0A84 ); c.setReserved2( 0x0 ); -// UnknownRecord sub1 = new UnknownRecord( (short)0x7, (short)0x2, new byte[] { 0x09, 0x00 } ); -// UnknownRecord sub2 = new UnknownRecord( (short)0x8, (short)0x2, new byte[] { 0x01, 0x00 } ); EndSubRecord e = new EndSubRecord(); obj.addSubRecord(c); -// obj.addSubRecord( sub1 ); -// obj.addSubRecord( sub2 ); obj.addSubRecord(e); return obj; diff --git a/src/java/org/apache/poi/hssf/model/PolygonShape.java b/src/java/org/apache/poi/hssf/model/PolygonShape.java index eeb18b071..02308bf48 100644 --- a/src/java/org/apache/poi/hssf/model/PolygonShape.java +++ b/src/java/org/apache/poi/hssf/model/PolygonShape.java @@ -134,7 +134,7 @@ public class PolygonShape ObjRecord obj = new ObjRecord(); CommonObjectDataSubRecord c = new CommonObjectDataSubRecord(); c.setObjectType( OBJECT_TYPE_MICROSOFT_OFFICE_DRAWING ); - c.setObjectId(shapeId); + c.setObjectId( getCmoObjectId(shapeId) ); c.setLocked( true ); c.setPrintable( true ); c.setAutofill( true ); diff --git a/src/java/org/apache/poi/hssf/model/SimpleFilledShape.java b/src/java/org/apache/poi/hssf/model/SimpleFilledShape.java index 8c14b3d39..739e1ce32 100644 --- a/src/java/org/apache/poi/hssf/model/SimpleFilledShape.java +++ b/src/java/org/apache/poi/hssf/model/SimpleFilledShape.java @@ -101,7 +101,7 @@ public class SimpleFilledShape ObjRecord obj = new ObjRecord(); CommonObjectDataSubRecord c = new CommonObjectDataSubRecord(); c.setObjectType( (short) ( (HSSFSimpleShape) shape ).getShapeType() ); - c.setObjectId( shapeId ); + c.setObjectId( getCmoObjectId(shapeId) ); c.setLocked( true ); c.setPrintable( true ); c.setAutofill( true ); diff --git a/src/java/org/apache/poi/hssf/model/TextboxShape.java b/src/java/org/apache/poi/hssf/model/TextboxShape.java index 4985ea31d..1ea79d094 100644 --- a/src/java/org/apache/poi/hssf/model/TextboxShape.java +++ b/src/java/org/apache/poi/hssf/model/TextboxShape.java @@ -59,7 +59,7 @@ public class TextboxShape ObjRecord obj = new ObjRecord(); CommonObjectDataSubRecord c = new CommonObjectDataSubRecord(); c.setObjectType( (short) ( (HSSFSimpleShape) shape ).getShapeType() ); - c.setObjectId( shapeId ); + c.setObjectId( getCmoObjectId(shapeId) ); c.setLocked( true ); c.setPrintable( true ); c.setAutofill( true ); @@ -166,4 +166,5 @@ public class TextboxShape { return escherTextbox; } + } diff --git a/src/testcases/org/apache/poi/hssf/model/TestShapes.java b/src/testcases/org/apache/poi/hssf/model/TestShapes.java new file mode 100644 index 000000000..d17178ee7 --- /dev/null +++ b/src/testcases/org/apache/poi/hssf/model/TestShapes.java @@ -0,0 +1,58 @@ +/* + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + */ + +package org.apache.poi.hssf.model; + +import junit.framework.TestCase; +import org.apache.poi.hssf.record.CommonObjectDataSubRecord; +import org.apache.poi.hssf.usermodel.HSSFClientAnchor; +import org.apache.poi.hssf.usermodel.HSSFComment; +import org.apache.poi.hssf.usermodel.HSSFPicture; +import org.apache.poi.hssf.usermodel.HSSFTextbox; + +/** + * + * @author Yegor Kozlov + */ +public final class TestShapes extends TestCase { + + /** + * Test generator of ids for the CommonObjectDataSubRecord record. + * + * See Bug 51332 + */ + public void testShapeId(){ + + HSSFClientAnchor anchor = new HSSFClientAnchor(); + AbstractShape shape; + CommonObjectDataSubRecord cmo; + + shape = new TextboxShape(new HSSFTextbox(null, anchor), 1025); + cmo = (CommonObjectDataSubRecord)shape.getObjRecord().getSubRecords().get(0); + assertEquals(1, cmo.getObjectId()); + + shape = new PictureShape(new HSSFPicture(null, anchor), 1026); + cmo = (CommonObjectDataSubRecord)shape.getObjRecord().getSubRecords().get(0); + assertEquals(2, cmo.getObjectId()); + + shape = new CommentShape(new HSSFComment(null, anchor), 1027); + cmo = (CommonObjectDataSubRecord)shape.getObjRecord().getSubRecords().get(0); + assertEquals(1027, cmo.getObjectId()); + } +}