Allow creating of an empty Themes Table on request

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1692211 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2015-07-22 00:04:59 +00:00
parent 8bb5d65cf8
commit a6779a3c02
5 changed files with 84 additions and 5 deletions

File diff suppressed because one or more lines are too long

View File

@ -35,7 +35,10 @@ import org.apache.poi.ss.usermodel.BuiltinFormats;
import org.apache.poi.ss.usermodel.FontFamily; import org.apache.poi.ss.usermodel.FontFamily;
import org.apache.poi.ss.usermodel.FontScheme; import org.apache.poi.ss.usermodel.FontScheme;
import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFactory;
import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRelation;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder; import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder;
import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill; import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill;
import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlException;
@ -57,7 +60,6 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType; import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPatternType;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument; import org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument;
/** /**
* Table of styles shared across all sheets in a workbook. * Table of styles shared across all sheets in a workbook.
*/ */
@ -79,6 +81,7 @@ public class StylesTable extends POIXMLDocumentPart {
private static final int MAXIMUM_STYLE_ID = SpreadsheetVersion.EXCEL2007.getMaxCellStyles(); private static final int MAXIMUM_STYLE_ID = SpreadsheetVersion.EXCEL2007.getMaxCellStyles();
private StyleSheetDocument doc; private StyleSheetDocument doc;
private XSSFWorkbook workbook;
private ThemesTable theme; private ThemesTable theme;
/** /**
@ -96,7 +99,17 @@ public class StylesTable extends POIXMLDocumentPart {
super(part, rel); super(part, rel);
readFrom(part.getInputStream()); readFrom(part.getInputStream());
} }
public void setWorkbook(XSSFWorkbook wb) {
this.workbook = wb;
}
/**
* Get the current Workbook's theme table, or null if the
* Workbook lacks any themes.
* <p>Use {@link #ensureThemesTable()} to have a themes table
* created if needed
*/
public ThemesTable getTheme() { public ThemesTable getTheme() {
return theme; return theme;
} }
@ -113,6 +126,17 @@ public class StylesTable extends POIXMLDocumentPart {
border.setThemesTable(theme); border.setThemesTable(theme);
} }
} }
/**
* If there isn't currently a {@link ThemesTable} for the
* current Workbook, then creates one and sets it up.
* After this, calls to {@link #getTheme()} won't give null
*/
public void ensureThemesTable() {
if (theme != null) return;
theme = (ThemesTable)workbook.createRelationship(XSSFRelation.THEME, XSSFFactory.getInstance());
}
/** /**
* Read this shared styles table from an XML file. * Read this shared styles table from an XML file.

View File

@ -17,12 +17,14 @@
package org.apache.poi.xssf.model; package org.apache.poi.xssf.model;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import org.apache.poi.POIXMLDocumentPart; import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackageRelationship; import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.xssf.usermodel.XSSFColor; import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.drawingml.x2006.main.CTColor; import org.openxmlformats.schemas.drawingml.x2006.main.CTColor;
import org.openxmlformats.schemas.drawingml.x2006.main.CTColorScheme; import org.openxmlformats.schemas.drawingml.x2006.main.CTColorScheme;
import org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument; import org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument;
@ -34,6 +36,15 @@ import org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument;
public class ThemesTable extends POIXMLDocumentPart { public class ThemesTable extends POIXMLDocumentPart {
private ThemeDocument theme; private ThemeDocument theme;
/**
* Create a new, empty ThemesTable
*/
public ThemesTable() {
super();
theme = ThemeDocument.Factory.newInstance();
theme.addNewTheme().addNewThemeElements();
}
/** /**
* Construct a ThemesTable. * Construct a ThemesTable.
* @param part A PackagePart. * @param part A PackagePart.
@ -120,4 +131,24 @@ public class ThemesTable extends POIXMLDocumentPart {
// All done // All done
} }
/**
* Write this table out as XML.
*
* @param out The stream to write to.
* @throws IOException if an error occurs while writing.
*/
public void writeTo(OutputStream out) throws IOException {
XmlOptions options = new XmlOptions(DEFAULT_XML_OPTIONS);
theme.save(out, options);
}
@Override
protected void commit() throws IOException {
PackagePart part = getPackagePart();
OutputStream out = part.getOutputStream();
writeTo(out);
out.close();
}
} }

View File

@ -153,8 +153,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
*/ */
private StylesTable stylesSource; private StylesTable stylesSource;
private ThemesTable theme;
/** /**
* The locator of user-defined functions. * The locator of user-defined functions.
* By default includes functions from the Excel Analysis Toolpack * By default includes functions from the Excel Analysis Toolpack
@ -337,6 +335,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
WorkbookDocument doc = WorkbookDocument.Factory.parse(getPackagePart().getInputStream()); WorkbookDocument doc = WorkbookDocument.Factory.parse(getPackagePart().getInputStream());
this.workbook = doc.getWorkbook(); this.workbook = doc.getWorkbook();
ThemesTable theme = null;
Map<String, XSSFSheet> shIdMap = new HashMap<String, XSSFSheet>(); Map<String, XSSFSheet> shIdMap = new HashMap<String, XSSFSheet>();
Map<String, ExternalLinksTable> elIdMap = new HashMap<String, ExternalLinksTable>(); Map<String, ExternalLinksTable> elIdMap = new HashMap<String, ExternalLinksTable>();
for(POIXMLDocumentPart p : getRelations()){ for(POIXMLDocumentPart p : getRelations()){
@ -362,6 +361,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
stylesSource = (StylesTable)createRelationship(XSSFRelation.STYLES, XSSFFactory.getInstance()); stylesSource = (StylesTable)createRelationship(XSSFRelation.STYLES, XSSFFactory.getInstance());
} }
} }
stylesSource.setWorkbook(this);
stylesSource.setTheme(theme); stylesSource.setTheme(theme);
if (sharedStringSource == null) { if (sharedStringSource == null) {
@ -428,6 +428,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, XSSFFactory.getInstance()); sharedStringSource = (SharedStringsTable)createRelationship(XSSFRelation.SHARED_STRINGS, XSSFFactory.getInstance());
stylesSource = (StylesTable)createRelationship(XSSFRelation.STYLES, XSSFFactory.getInstance()); stylesSource = (StylesTable)createRelationship(XSSFRelation.STYLES, XSSFFactory.getInstance());
stylesSource.setWorkbook(this);
namedRanges = new ArrayList<XSSFName>(); namedRanges = new ArrayList<XSSFName>();
sheets = new ArrayList<XSSFSheet>(); sheets = new ArrayList<XSSFSheet>();
@ -1578,7 +1579,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
* Returns the Theme of current workbook. * Returns the Theme of current workbook.
*/ */
public ThemesTable getTheme() { public ThemesTable getTheme() {
return theme; if (stylesSource == null) return null;
return stylesSource.getTheme();
} }
/** /**

View File

@ -18,6 +18,7 @@
package org.apache.poi.xssf.model; package org.apache.poi.xssf.model;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -29,6 +30,7 @@ import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor; import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test; import org.junit.Test;
@ -75,4 +77,24 @@ public class TestThemesTable {
fos.close(); fos.close();
} }
} }
@Test
public void testAddNew() throws Exception {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet s = wb.createSheet();
assertEquals(null, wb.getTheme());
StylesTable styles = wb.getStylesSource();
assertEquals(null, styles.getTheme());
styles.ensureThemesTable();
assertNotNull(styles.getTheme());
assertNotNull(wb.getTheme());
wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
styles = wb.getStylesSource();
assertNotNull(styles.getTheme());
assertNotNull(wb.getTheme());
}
} }