poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1653825 - in /poi/trunk: 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
Date Thu, 22 Jan 2015 12:15:59 GMT
Author: nick
Date: Thu Jan 22 12:15:59 2015
New Revision: 1653825

URL: http://svn.apache.org/r1653825
Log:
57482 Handle XSLX files with no shared strings table in read-only mode

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

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java?rev=1653825&r1=1653824&r2=1653825&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java Thu Jan 22 12:15:59
2015
@@ -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 POIXML
                     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

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java?rev=1653825&r1=1653824&r2=1653825&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java Thu Jan
22 12:15:59 2015
@@ -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
      * 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
             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
                 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();
             }
         }
     }

Modified: poi/trunk/test-data/spreadsheet/57482-OnlyNumeric.xlsx
URL: http://svn.apache.org/viewvc/poi/trunk/test-data/spreadsheet/57482-OnlyNumeric.xlsx?rev=1653825&r1=1653824&r2=1653825&view=diff
==============================================================================
Binary files - no diff available.



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


Mime
View raw message