]> source.dussan.org Git - poi.git/commitdiff
Bug 56957: Avoid error if Workbook with empty SharedStringTable is written
authorDominik Stadler <centic@apache.org>
Mon, 26 Oct 2015 07:54:06 +0000 (07:54 +0000)
committerDominik Stadler <centic@apache.org>
Mon, 26 Oct 2015 07:54:06 +0000 (07:54 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1710521 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/openxml4j/opc/internal/marshallers/ZipPartMarshaller.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java
test-data/spreadsheet/56957.xlsx [new file with mode: 0644]

index 296f7a6c67a507ce90287e9663c1f1078b80536b..8860ae37e5d6b22d08fb38bc183bf6611102ee55 100644 (file)
@@ -38,6 +38,7 @@ import org.apache.poi.openxml4j.opc.internal.ZipHelper;
 import org.apache.poi.util.DocumentHelper;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
+import org.apache.poi.xssf.usermodel.XSSFRelation;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
@@ -63,6 +64,12 @@ public final class ZipPartMarshaller implements PartMarshaller {
                        // Normally should happen only in developement phase, so just throw
                        // exception
                }
+               
+               // check if there is anything to save for some parts. We don't do this for all parts as some code
+               // might depend on empty parts being saved, e.g. some unit tests verify this currently.
+               if(part.getSize() == 0 && part.getPartName().getName().equals(XSSFRelation.SHARED_STRINGS.getDefaultFileName())) {
+                   return true;
+               }
 
                ZipOutputStream zos = (ZipOutputStream) os;
                ZipEntry partEntry = new ZipEntry(ZipHelper
index f5fdd4363d1391a5c6f54091edfa7a5f31e25823..199a3fe3076ecaefd7ff10f8e327055f98462afc 100644 (file)
@@ -40,10 +40,12 @@ import org.apache.poi.POIXMLProperties;
 import org.apache.poi.hssf.HSSFTestDataSamples;
 import org.apache.poi.openxml4j.opc.ContentTypes;
 import org.apache.poi.openxml4j.opc.OPCPackage;
+import org.apache.poi.openxml4j.opc.PackageAccess;
 import org.apache.poi.openxml4j.opc.PackagePart;
 import org.apache.poi.openxml4j.opc.PackagePartName;
 import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
 import org.apache.poi.openxml4j.opc.PackagingURIHelper;
+import org.apache.poi.openxml4j.opc.internal.FileHelper;
 import org.apache.poi.openxml4j.opc.internal.MemoryPackagePart;
 import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart;
 import org.apache.poi.ss.SpreadsheetVersion;
@@ -1019,5 +1021,42 @@ public final class TestXSSFWorkbook extends BaseTestWorkbook {
         finally {
             IOUtils.closeQuietly(wb);
         }
+       }
+
+    @Test
+    public void testBug56957CloseWorkbook() throws Exception {
+        File file = TempFile.createTempFile("TestBug56957_", ".xlsx");
+        
+        try {
+            // as the file is written to, we make a copy before actually working on it
+            FileHelper.copyFile(new File("test-data/spreadsheet/56957.xlsx"), file);
+            
+            assertTrue(file.exists());
+            
+            // read-only mode works!
+            Workbook workbook = WorkbookFactory.create(OPCPackage.open(file, PackageAccess.READ));
+            System.out.println(workbook.getSheetAt(0).getRow(0).getCell(0, Row.CREATE_NULL_AS_BLANK).getDateCellValue().toString());
+            workbook.close();
+            workbook = null;
+    
+            workbook = WorkbookFactory.create(OPCPackage.open(file, PackageAccess.READ));
+            System.out.println(workbook.getSheetAt(0).getRow(0).getCell(0, Row.CREATE_NULL_AS_BLANK).getDateCellValue().toString());
+            workbook.close();
+            workbook = null;
+    
+            // now check read/write mode
+            workbook = WorkbookFactory.create(OPCPackage.open(file, PackageAccess.READ_WRITE));
+            System.out.println(workbook.getSheetAt(0).getRow(0).getCell(0, Row.CREATE_NULL_AS_BLANK).getDateCellValue().toString());
+            workbook.close();
+            workbook = null;
+    
+            workbook = WorkbookFactory.create(OPCPackage.open(file, PackageAccess.READ_WRITE));
+            System.out.println(workbook.getSheetAt(0).getRow(0).getCell(0, Row.CREATE_NULL_AS_BLANK).getDateCellValue().toString());
+            workbook.close();
+            workbook = null;
+        } finally {
+            assertTrue(file.exists());
+            assertTrue(file.delete());
+        }
     }
 }
diff --git a/test-data/spreadsheet/56957.xlsx b/test-data/spreadsheet/56957.xlsx
new file mode 100644 (file)
index 0000000..b58aa62
Binary files /dev/null and b/test-data/spreadsheet/56957.xlsx differ