From cf1354dc6c6128d8fd024ee1f57359761cd5fd1f Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Wed, 22 Dec 2021 09:16:53 +0000 Subject: [PATCH] small refactor of ThemesTable git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1896269 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/xssf/model/ThemesTable.java | 36 +++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/ThemesTable.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/ThemesTable.java index 28d5a0633e..72fe4e0107 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/ThemesTable.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/ThemesTable.java @@ -19,6 +19,7 @@ package org.apache.poi.xssf.model; import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; import org.apache.poi.ooxml.POIXMLDocumentPart; @@ -62,7 +63,7 @@ public class ThemesTable extends POIXMLDocumentPart implements Themes { } private IndexedColorMap colorMap; - private final ThemeDocument theme; + private ThemeDocument theme; /** * Create a new, empty ThemesTable @@ -81,14 +82,22 @@ public class ThemesTable extends POIXMLDocumentPart implements Themes { */ public ThemesTable(PackagePart part) throws IOException { super(part); - - try { - theme = ThemeDocument.Factory.parse(part.getInputStream(), DEFAULT_XML_OPTIONS); - } catch(XmlException e) { - throw new IOException(e.getLocalizedMessage(), e); + try (InputStream stream = part.getInputStream()) { + readFrom(stream); } } + /** + * Construct a ThemesTable. + * @param stream input stream. + * + * @since POI 5.2.0 + */ + public ThemesTable(InputStream stream) throws IOException { + super(); + readFrom(stream); + } + /** * Construct a ThemesTable from an existing ThemeDocument. * @param theme A ThemeDocument. @@ -97,6 +106,21 @@ public class ThemesTable extends POIXMLDocumentPart implements Themes { this.theme = theme; } + /** + * Read this themes table from an XML file. + * + * @param is The input stream containing the XML document. + * @throws IOException if an error occurs while reading. + * @since POI 5.2.0 + */ + public void readFrom(InputStream is) throws IOException { + try { + theme = ThemeDocument.Factory.parse(is, DEFAULT_XML_OPTIONS); + } catch(XmlException e) { + throw new IOException(e.getLocalizedMessage(), e); + } + } + /** * called from {@link StylesTable} when setting theme, used to adjust colors if a custom indexed mapping is defined */ -- 2.39.5