]> source.dussan.org Git - poi.git/commitdiff
fixed bug 53028, added check before serialization if any cell contains 2 or more...
authorEvgeniy Berlog <berlog@apache.org>
Tue, 14 Aug 2012 18:22:31 +0000 (18:22 +0000)
committerEvgeniy Berlog <berlog@apache.org>
Tue, 14 Aug 2012 18:22:31 +0000 (18:22 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1373005 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/record/EscherAggregate.java
src/java/org/apache/poi/hssf/usermodel/HSSFPatriarch.java
src/java/org/apache/poi/hssf/usermodel/HSSFRow.java
src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
src/testcases/org/apache/poi/hssf/usermodel/TestComment.java
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFRow.java

index 7579e71cf7633ccce8c2e4e7b8c0184a1de97670..9ca4fefc06ac51300ef2245e9d80b9b6baa323e7 100644 (file)
@@ -773,7 +773,7 @@ public final class EscherAggregate extends AbstractEscherHolderRecord {
      *         Every HSSFComment shape has a link to a NoteRecord from the tailRec collection.
      */
     public Map<Integer, NoteRecord> getTailRecords() {
-        return tailRec;
+        return Collections.unmodifiableMap(tailRec);
     }
 
     /**
index de3a123bc69a7d8ebef820c1529738328222677a..3f634b02b97fe9a24653701be5a7feefb9971971 100644 (file)
 
 package org.apache.poi.hssf.usermodel;
 
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
+import java.util.*;
 
 import org.apache.poi.ddf.*;
 import org.apache.poi.hssf.model.DrawingManager2;
 import org.apache.poi.hssf.record.EscherAggregate;
+import org.apache.poi.hssf.record.NoteRecord;
+import org.apache.poi.hssf.util.CellReference;
 import org.apache.poi.ss.usermodel.Chart;
+import org.apache.poi.util.POILogFactory;
+import org.apache.poi.util.POILogger;
 import org.apache.poi.util.StringUtil;
 import org.apache.poi.util.Internal;
 import org.apache.poi.ss.usermodel.Drawing;
@@ -38,6 +39,7 @@ import org.apache.poi.ss.usermodel.ClientAnchor;
  * @author Glen Stampoultzis (glens at apache.org)
  */
 public final class HSSFPatriarch implements HSSFShapeContainer, Drawing {
+    private static POILogger log = POILogFactory.getLogger(HSSFPatriarch.class);
     private final List<HSSFShape> _shapes = new ArrayList<HSSFShape>();
 
     private final EscherSpgrRecord _spgrRecord;
@@ -55,6 +57,7 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing {
      * Creates the patriarch.
      *
      * @param sheet the sheet this patriarch is stored in.
+     * @param boundAggregate -low level representation of all binary data inside sheet
      */
     HSSFPatriarch(HSSFSheet sheet, EscherAggregate boundAggregate) {
         _sheet = sheet;
@@ -90,6 +93,27 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing {
         return newPatriarch;
     }
 
+    /**
+     * check if any shapes contain wrong data
+     * At now(13.08.2010) check if patriarch contains 2 or more comments with same coordinates
+     */
+    protected void preSerialize(){
+        Map<Integer, NoteRecord> tailRecords = _boundAggregate.getTailRecords();
+        /**
+         * contains coordinates of comments we iterate over
+         */
+        Set<String> coordinates = new HashSet<String>(tailRecords.size());
+        for(NoteRecord rec : tailRecords.values()){
+            String noteRef = new CellReference(rec.getRow(),
+                    rec.getColumn()).formatAsString(); // A1-style notation
+            if(coordinates.contains(noteRef )){
+                throw new IllegalStateException("found multiple cell comments for cell " + noteRef );
+            } else {
+                coordinates.add(noteRef);
+            }
+        }
+    }
+
     /**
      * @param shape to be removed
      * @return true of shape is removed
@@ -146,6 +170,7 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing {
      *
      * @param anchor the client anchor describes how this group is attached
      *               to the sheet.
+     * @param pictureIndex - pointer to the byte array saved inside workbook in escher bse record
      * @return the newly created shape.
      */
     public HSSFPicture createPicture(HSSFClientAnchor anchor, int pictureIndex) {
@@ -365,6 +390,7 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing {
 
     /**
      * Returns the aggregate escher record we're bound to
+     * @return - low level representation of sheet drawing data
      */
     protected EscherAggregate _getBoundAggregate() {
         return _boundAggregate;
@@ -406,9 +432,7 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing {
 
         for (int i = 0; i < spgrChildren.size(); i++) {
             EscherContainerRecord spContainer = spgrChildren.get(i);
-            if (i == 0) {
-                continue;
-            } else {
+            if (i != 0) {
                 HSSFShapeFactory.createShapeTree(spContainer, _boundAggregate, this, _sheet.getWorkbook().getRootDirectory());
             }
         }
index 2397129bf232b3d8db86fe0dce640ecde49006a2..0c6a612fcff09bb2c46bc9aeb3a72c2d5afd58da 100644 (file)
@@ -458,6 +458,7 @@ public final class HSSFRow implements Row {
     {
         if(height == -1){
             row.setHeight((short)(0xFF | 0x8000));
+            row.setBadFontHeight(false);
         } else {
             row.setBadFontHeight(true);
             row.setHeight(height);
index bb3c92ced07b617a62afa2c58fe105973ad649eb..7fc48825513bed3313562c05a4d1f1ba0a799cf1 100644 (file)
@@ -136,6 +136,15 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
         return sheet;
     }
 
+    /**
+     * check whether the data of sheet can be serialized
+     */
+    protected void preSerialize(){
+        if (_patriarch != null){
+            _patriarch.preSerialize();
+        }
+    }
+
     /**
      * Return the parent workbook
      *
@@ -215,6 +224,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
         HSSFRow row = new HSSFRow(_workbook, this, rownum);
         // new rows inherit default height from the sheet
         row.setHeight(getDefaultRowHeight());
+        row.getRowRecord().setBadFontHeight(false);
 
         addRow(row, true);
         return row;
index f1828fa387ee030f209fb8b03136b076934f157a..add83e7e6897815710925f2f4731b34b6fea5090 100644 (file)
@@ -1245,6 +1245,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
         // serialization is about to occur.
         for (int i = 0; i < nSheets; i++) {
             sheets[i].getSheet().preSerialize();
+            sheets[i].preSerialize();
         }
 
         int totalsize = workbook.getSize();
index 6c0919894f20352c5a644105a452de92341c460a..cac444baef211b18babb64fd7257678cdca3a2f5 100644 (file)
@@ -108,8 +108,10 @@ public class TestComment extends TestCase {
         int idx = wb.addPicture(new byte[]{1,2,3}, HSSFWorkbook.PICTURE_TYPE_PNG);\r
 \r
         HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor());\r
+        comment.setColumn(5);\r
         comment.setString(new HSSFRichTextString("comment1"));\r
         comment = patriarch.createCellComment(new HSSFClientAnchor(0,0,100,100,(short)0,0,(short)10,10));\r
+        comment.setRow(5);\r
         comment.setString(new HSSFRichTextString("comment2"));\r
         comment.setBackgroundImage(idx);\r
         assertEquals(comment.getBackgroundImageId(), idx);\r
@@ -265,4 +267,22 @@ public class TestComment extends TestCase {
         assertEquals(comment.getShapeId(), 2024);\r
         assertEquals(comment.getNoteRecord().getShapeId(), 1000);\r
     }\r
+    \r
+    public void testAttemptToSave2CommentsWithSameCoordinates(){\r
+        Object err = null;\r
+        \r
+        HSSFWorkbook wb = new HSSFWorkbook();\r
+        HSSFSheet sh = wb.createSheet();\r
+        HSSFPatriarch patriarch = sh.createDrawingPatriarch();\r
+        patriarch.createCellComment(new HSSFClientAnchor());\r
+        patriarch.createCellComment(new HSSFClientAnchor());\r
+        \r
+        try{\r
+            HSSFTestDataSamples.writeOutAndReadBack(wb);\r
+        } catch (IllegalStateException e){\r
+            err = 1;\r
+            assertEquals(e.getMessage(), "found multiple cell comments for cell $A$1");\r
+        }\r
+        assertNotNull(err);\r
+    }\r
 }\r
index 55ee03704200e281c0bc376f8993229a84546a6d..654db6aa0ab91d9875859c4bf4d3c43efb67a02d 100644 (file)
@@ -116,4 +116,21 @@ public final class TestHSSFRow extends BaseTestRow {
         assertEquals(2, row.getFirstCellNum());
         assertEquals(6, row.getLastCellNum());
     }
+
+    public void testRowHeight(){
+        HSSFWorkbook workbook = new HSSFWorkbook();
+        HSSFSheet sheet = workbook.createSheet();
+        HSSFRow row = sheet.createRow(0);
+
+        assertEquals(row.getHeight(), sheet.getDefaultRowHeight());
+        assertEquals(row.getRowRecord().getBadFontHeight(), false);
+
+        row.setHeight((short) 123);
+        assertEquals(row.getHeight(), 123);
+        assertEquals(row.getRowRecord().getBadFontHeight(), true);
+
+        row.setHeight((short) -1);
+        assertEquals(row.getHeight(), sheet.getDefaultRowHeight());
+        assertEquals(row.getRowRecord().getBadFontHeight(), false);
+    }
 }