From ea20b0b8853de575b73f193857bce059ec59e2ed Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Wed, 5 Oct 2011 21:05:00 +0000 Subject: [PATCH] Fix bug #51955 - XSSFReader supplied StylesTables need to have the theme data available git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1179440 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../poi/xssf/eventusermodel/XSSFReader.java | 19 ++++++++++++++++++- .../apache/poi/xssf/model/ThemesTable.java | 12 ++++++++++-- .../xssf/eventusermodel/TestXSSFReader.java | 7 +++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 069a8f77e..219eb3093 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 51955 - XSSFReader supplied StylesTables need to have the theme data available 51716 - Removed incorrect assert in SXSSFSheet#getSXSSFSheet 51834 - Opening and Writing .doc file results in corrupt document 51902 - Picture.fillRawImageContent - ArrayIndexOutOfBoundsException (duplicate) diff --git a/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFReader.java b/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFReader.java index e59636e64..59ea919c5 100644 --- a/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFReader.java +++ b/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFReader.java @@ -36,6 +36,7 @@ import org.apache.poi.openxml4j.opc.PackagingURIHelper; import org.apache.poi.xssf.model.CommentsTable; import org.apache.poi.xssf.model.SharedStringsTable; import org.apache.poi.xssf.model.StylesTable; +import org.apache.poi.xssf.model.ThemesTable; import org.apache.poi.xssf.usermodel.XSSFRelation; import org.apache.xmlbeans.XmlException; import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet; @@ -83,7 +84,15 @@ public class XSSFReader { */ public StylesTable getStylesTable() throws IOException, InvalidFormatException { ArrayList parts = pkg.getPartsByContentType( XSSFRelation.STYLES.getContentType()); - return parts.size() == 0 ? null : new StylesTable(parts.get(0), null); + if(parts.size() == 0) return null; + + // Create the Styles Table, and associate the Themes if present + StylesTable styles = new StylesTable(parts.get(0), null); + parts = pkg.getPartsByContentType( XSSFRelation.THEME.getContentType()); + if(parts.size() != 0) { + styles.setTheme(new ThemesTable(parts.get(0), null)); + } + return styles; } @@ -104,6 +113,14 @@ public class XSSFReader { return XSSFRelation.STYLES.getContents(workbookPart); } + /** + * Returns an InputStream to read the contents of the + * themes table. + */ + public InputStream getThemesData() throws IOException, InvalidFormatException { + return XSSFRelation.THEME.getContents(workbookPart); + } + /** * Returns an InputStream to read the contents of the * main Workbook, which contains key overall data for diff --git a/src/ooxml/java/org/apache/poi/xssf/model/ThemesTable.java b/src/ooxml/java/org/apache/poi/xssf/model/ThemesTable.java index 61ed79a32..3bd00f4a2 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/ThemesTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/ThemesTable.java @@ -16,10 +16,13 @@ ==================================================================== */ package org.apache.poi.xssf.model; +import java.io.IOException; + import org.apache.poi.POIXMLDocumentPart; import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackageRelationship; import org.apache.poi.xssf.usermodel.XSSFColor; +import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlObject; import org.openxmlformats.schemas.drawingml.x2006.main.CTColorScheme; import org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument; @@ -34,9 +37,14 @@ import org.openxmlformats.schemas.drawingml.x2006.main.CTColor; public class ThemesTable extends POIXMLDocumentPart { private ThemeDocument theme; - public ThemesTable(PackagePart part, PackageRelationship rel) throws Exception { + public ThemesTable(PackagePart part, PackageRelationship rel) throws IOException { super(part, rel); - theme = ThemeDocument.Factory.parse(part.getInputStream()); + + try { + theme = ThemeDocument.Factory.parse(part.getInputStream()); + } catch(XmlException e) { + throw new IOException(e.getLocalizedMessage()); + } } public ThemesTable(ThemeDocument theme) { diff --git a/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFReader.java b/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFReader.java index ab1113759..5baaebb90 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFReader.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFReader.java @@ -55,6 +55,13 @@ public final class TestXSSFReader extends TestCase { assertEquals(3, r.getStylesTable().getFonts().size()); assertEquals(0, r.getStylesTable()._getNumberFormatSize()); + + // The Styles Table should have the themes associated with it too + assertNotNull(r.getStylesTable().getTheme()); + + // Check we get valid data for the two + assertNotNull(r.getStylesData()); + assertNotNull(r.getThemesData()); } public void testStrings() throws Exception {