]> source.dussan.org Git - poi.git/commitdiff
57482 Handle XSLX files with no shared strings table in read-only mode
authorNick Burch <nick@apache.org>
Thu, 22 Jan 2015 12:15:59 +0000 (12:15 +0000)
committerNick Burch <nick@apache.org>
Thu, 22 Jan 2015 12:15:59 +0000 (12:15 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1653825 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
test-data/spreadsheet/57482-OnlyNumeric.xlsx

index ec23f209bee0a2df251129023d5f9091971c10d5..ae09af141df67a3f39b0bc9ac4fe353ce37e9432 100644 (file)
@@ -44,6 +44,7 @@ import org.apache.poi.POIXMLProperties;
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
 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.PackageRelationship;
@@ -343,16 +344,25 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
                     elIdMap.put(p.getPackageRelationship().getId(), (ExternalLinksTable)p);
                 }
             }
+            boolean packageReadOnly = (getPackage().getPackageAccess() == PackageAccess.READ);
             
             if (stylesSource == null) {
                 // Create Styles if it is missing
-                stylesSource = (StylesTable)createRelationship(XSSFRelation.STYLES, XSSFFactory.getInstance());
+                if (packageReadOnly) {
+                    stylesSource = new StylesTable();
+                } else {
+                    stylesSource = (StylesTable)createRelationship(XSSFRelation.STYLES, XSSFFactory.getInstance());
+                }
             }
             stylesSource.setTheme(theme);
 
             if (sharedStringSource == null) {
                 // Create SST if it is missing
-                sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, XSSFFactory.getInstance());
+                if (packageReadOnly) {
+                    sharedStringSource = new SharedStringsTable();
+                } else {
+                    sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, XSSFFactory.getInstance());
+                }
             }
             
             // Load individual sheets. The order of sheets is defined by the order
index 32f707a530adb22939bf0b8e37ad2af26eca1d9c..7ab9ff7d02443b304eafb7abe8a7e1583fc10486 100644 (file)
@@ -43,6 +43,7 @@ import org.apache.poi.POIXMLProperties;
 import org.apache.poi.hssf.HSSFTestDataSamples;
 import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
 import org.apache.poi.openxml4j.opc.OPCPackage;
 import org.apache.poi.openxml4j.opc.PackageAccess;
 import org.apache.poi.openxml4j.opc.PackagePart;
@@ -2083,7 +2084,6 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
      * A .xlsx file with no Shared Strings table should open fine
      *  in read-only mode
      */
-    @Ignore
     @Test
     public void bug57482() throws Exception {
         for (PackageAccess access : new PackageAccess[] {
@@ -2092,6 +2092,7 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
             File file = HSSFTestDataSamples.getSampleFile("57482-OnlyNumeric.xlsx");
             OPCPackage pkg = OPCPackage.open(file, access);
             try {
+                // Try to open it and read the contents
                 XSSFWorkbook wb = new XSSFWorkbook(pkg);
                 assertNotNull(wb.getSharedStringSource());
                 assertEquals(0, wb.getSharedStringSource().getCount());
@@ -2101,8 +2102,34 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
                 assertEquals("1",  fmt.formatCellValue(s.getRow(0).getCell(0)));
                 assertEquals("11", fmt.formatCellValue(s.getRow(0).getCell(1)));
                 assertEquals("5",  fmt.formatCellValue(s.getRow(4).getCell(0)));
+                
+                // Add a text cell
+                s.getRow(0).createCell(3).setCellValue("Testing");
+                assertEquals("Testing",  fmt.formatCellValue(s.getRow(0).getCell(3)));
+                
+                // Try to write-out and read again, should only work
+                //  in read-write mode, not read-only mode
+                try {
+                    wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
+                    if (access == PackageAccess.READ)
+                        fail("Shouln't be able to write from read-only mode");
+                } catch (InvalidOperationException e) {
+                    if (access == PackageAccess.READ) {
+                        // Expected
+                    } else {
+                        // Shouldn't occur in write-mode
+                        throw e;
+                    }
+                }
+                
+                // Check again
+                s = wb.getSheetAt(0);
+                assertEquals("1",  fmt.formatCellValue(s.getRow(0).getCell(0)));
+                assertEquals("11", fmt.formatCellValue(s.getRow(0).getCell(1)));
+                assertEquals("5",  fmt.formatCellValue(s.getRow(4).getCell(0)));
+                assertEquals("Testing",  fmt.formatCellValue(s.getRow(0).getCell(3)));
             } finally {
-                pkg.close();
+                pkg.revert();
             }
         }
     }
index fd1c4214db7d87d2408133f05ff77c41632e1a20..c905891a735ff5285929335586c02e3e8ba65b16 100644 (file)
Binary files a/test-data/spreadsheet/57482-OnlyNumeric.xlsx and b/test-data/spreadsheet/57482-OnlyNumeric.xlsx differ