poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cen...@apache.org
Subject svn commit: r1595048 - in /poi/trunk: src/java/org/apache/poi/hssf/model/ src/java/org/apache/poi/hssf/record/ src/testcases/org/apache/poi/hssf/usermodel/ test-data/spreadsheet/
Date Thu, 15 May 2014 21:14:53 GMT
Author: centic
Date: Thu May 15 21:14:52 2014
New Revision: 1595048

URL: http://svn.apache.org/r1595048
Log:
Bug 56325: fix Exception when removing sheets with named ranges in the workbook

Added:
    poi/trunk/test-data/spreadsheet/56325.xls
Modified:
    poi/trunk/src/java/org/apache/poi/hssf/model/InternalWorkbook.java
    poi/trunk/src/java/org/apache/poi/hssf/model/LinkTable.java
    poi/trunk/src/java/org/apache/poi/hssf/record/ExternSheetRecord.java
    poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java

Modified: poi/trunk/src/java/org/apache/poi/hssf/model/InternalWorkbook.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/model/InternalWorkbook.java?rev=1595048&r1=1595047&r2=1595048&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/model/InternalWorkbook.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/model/InternalWorkbook.java Thu May 15 21:14:52
2014
@@ -716,11 +716,15 @@ public final class InternalWorkbook {
                 // Bump down by one, so still points
                 //  at the same sheet
                 nr.setSheetNumber(nr.getSheetNumber()-1);
-                
-                // also update the link-table as otherwise references might point at invalid
sheets
-                linkTable.updateIndexToInternalSheet(i, -1);
             }
         }
+        
+        // also tell the LinkTable about the removed sheet
+        // +1 because we already removed it from the count of sheets!
+        for(int i = sheetIndex+1;i < getNumSheets()+1;i++) {
+            // also update the link-table as otherwise references might point at invalid
sheets
+            linkTable.removeSheet(i);
+        }
     }
 
     /**

Modified: poi/trunk/src/java/org/apache/poi/hssf/model/LinkTable.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/model/LinkTable.java?rev=1595048&r1=1595047&r2=1595048&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/model/LinkTable.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/model/LinkTable.java Thu May 15 21:14:52 2014
@@ -414,9 +414,17 @@ final class LinkTable {
 	public int getIndexToInternalSheet(int extRefIndex) {
 		return _externSheetRecord.getFirstSheetIndexFromRefIndex(extRefIndex);
 	}
-	
-	public void updateIndexToInternalSheet(int extRefIndex, int offset) {
-		_externSheetRecord.adjustIndex(extRefIndex, offset);
+
+	/**
+	 * @deprecated Was prevously used for removing sheets, which we now do differently 
+	 */
+	@Deprecated
+    public void updateIndexToInternalSheet(int extRefIndex, int offset) {
+        _externSheetRecord.adjustIndex(extRefIndex, offset);
+    }
+
+	public void removeSheet(int sheetIdx) {
+		_externSheetRecord.removeSheet(sheetIdx);
 	}
 
 	public int getSheetIndexFromExternSheetIndex(int extRefIndex) {
@@ -453,8 +461,8 @@ final class LinkTable {
 	 */
 	private int findFirstRecordLocBySid(short sid) {
 		int index = 0;
-		for (Iterator iterator = _workbookRecordList.iterator(); iterator.hasNext(); ) {
-			Record record = ( Record ) iterator.next();
+		for (Iterator<Record> iterator = _workbookRecordList.iterator(); iterator.hasNext();
) {
+			Record record = iterator.next();
 
 			if (record.getSid() == sid) {
 				return index;

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/ExternSheetRecord.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/ExternSheetRecord.java?rev=1595048&r1=1595047&r2=1595048&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/ExternSheetRecord.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/ExternSheetRecord.java Thu May 15 21:14:52
2014
@@ -164,8 +164,32 @@ public class ExternSheetRecord extends S
 		return _list.get(i);
 	}
 	
-	public void adjustIndex(int extRefIndex, int offset) {
-		getRef(extRefIndex).adjustIndex(offset);
+    /**
+     * @deprecated Was prevously used for removing sheets, which we now do differently 
+     */
+    @Deprecated
+    public void adjustIndex(int extRefIndex, int offset) {
+        getRef(extRefIndex).adjustIndex(offset);
+    }
+	
+	public void removeSheet(int sheetIdx) {
+        int nItems = _list.size();
+        int toRemove = -1;
+        for (int i = 0; i < nItems; i++) {
+            RefSubRecord refSubRecord = _list.get(i);
+            if(refSubRecord.getFirstSheetIndex() == sheetIdx && 
+                    refSubRecord.getLastSheetIndex() == sheetIdx) {
+                toRemove = i;
+            } else if (refSubRecord.getFirstSheetIndex() > sheetIdx && 
+                    refSubRecord.getLastSheetIndex() > sheetIdx) {
+                _list.set(i, new RefSubRecord(refSubRecord.getExtBookIndex(), refSubRecord.getFirstSheetIndex()-1,
refSubRecord.getLastSheetIndex()-1));
+            }
+        }
+        
+        // finally remove entries for sheet indexes that we remove
+        if(toRemove != -1) {
+            _list.remove(toRemove);
+        }
 	}
 	
 	/**

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java?rev=1595048&r1=1595047&r2=1595048&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java Thu May 15 21:14:52
2014
@@ -27,9 +27,11 @@ import static org.junit.Assert.fail;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Calendar;
@@ -2582,4 +2584,32 @@ public final class TestBugs extends Base
         
         assertEquals(5, cf.getNumConditionalFormattings());
     }
+
+    @Test
+    public void bug56325() throws IOException {
+        HSSFWorkbook wb;
+
+        File file = HSSFTestDataSamples.getSampleFile("56325.xls");
+        InputStream stream = new FileInputStream(file);
+        try {
+            POIFSFileSystem fs = new POIFSFileSystem(stream);
+            wb = new HSSFWorkbook(fs);            
+        } finally {
+            stream.close();
+        }
+        
+        assertEquals(3, wb.getNumberOfSheets());
+        wb.removeSheetAt(0);
+        assertEquals(2, wb.getNumberOfSheets());
+
+        wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+        assertEquals(2, wb.getNumberOfSheets());
+        wb.removeSheetAt(0);
+        assertEquals(1, wb.getNumberOfSheets());
+        wb.removeSheetAt(0);
+        assertEquals(0, wb.getNumberOfSheets());
+        
+        wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
+        assertEquals(0, wb.getNumberOfSheets());
+    }
 }

Added: poi/trunk/test-data/spreadsheet/56325.xls
URL: http://svn.apache.org/viewvc/poi/trunk/test-data/spreadsheet/56325.xls?rev=1595048&view=auto
==============================================================================
Files poi/trunk/test-data/spreadsheet/56325.xls (added) and poi/trunk/test-data/spreadsheet/56325.xls
Thu May 15 21:14:52 2014 differ



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message