diff options
-rw-r--r-- | src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java | 14 | ||||
-rw-r--r-- | src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java | 31 | ||||
-rw-r--r-- | test-data/spreadsheet/57482-OnlyNumeric.xlsx | bin | 8601 -> 10474 bytes |
3 files changed, 41 insertions, 4 deletions
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index ec23f209be..ae09af141d 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -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 diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java index 32f707a530..7ab9ff7d02 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -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(); } } } diff --git a/test-data/spreadsheet/57482-OnlyNumeric.xlsx b/test-data/spreadsheet/57482-OnlyNumeric.xlsx Binary files differindex fd1c4214db..c905891a73 100644 --- a/test-data/spreadsheet/57482-OnlyNumeric.xlsx +++ b/test-data/spreadsheet/57482-OnlyNumeric.xlsx |