From 2900fcf9af78360d06c74100505bd6051410fa26 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 26 Jul 2018 14:18:13 +0000 Subject: [PATCH] start process of introducing an interface for Styles and Themes Tables git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1836732 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/xssf/eventusermodel/XLSX2CSV.java | 3 +- .../eventusermodel/XSSFSheetXMLHandler.java | 13 +- .../XSSFEventBasedExcelExtractor.java | 7 +- .../org/apache/poi/xssf/model/Styles.java | 114 ++++++++++++++++++ .../apache/poi/xssf/model/StylesTable.java | 22 +++- .../org/apache/poi/xssf/model/Themes.java | 19 +++ .../apache/poi/xssf/model/ThemesTable.java | 4 +- 7 files changed, 165 insertions(+), 17 deletions(-) create mode 100644 src/ooxml/java/org/apache/poi/xssf/model/Styles.java create mode 100644 src/ooxml/java/org/apache/poi/xssf/model/Themes.java diff --git a/src/examples/src/org/apache/poi/xssf/eventusermodel/XLSX2CSV.java b/src/examples/src/org/apache/poi/xssf/eventusermodel/XLSX2CSV.java index 899ea1581..0ed52e9b5 100644 --- a/src/examples/src/org/apache/poi/xssf/eventusermodel/XLSX2CSV.java +++ b/src/examples/src/org/apache/poi/xssf/eventusermodel/XLSX2CSV.java @@ -34,6 +34,7 @@ import org.apache.poi.ooxml.util.SAXHelper; import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler.SheetContentsHandler; import org.apache.poi.xssf.extractor.XSSFEventBasedExcelExtractor; import org.apache.poi.xssf.model.SharedStrings; +import org.apache.poi.xssf.model.Styles; import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.usermodel.XSSFComment; import org.xml.sax.ContentHandler; @@ -180,7 +181,7 @@ public class XLSX2CSV { * @throws SAXException if parsing the XML data fails. */ public void processSheet( - StylesTable styles, + Styles styles, SharedStrings strings, SheetContentsHandler sheetHandler, InputStream sheetInputStream) throws IOException, SAXException { diff --git a/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java b/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java index 257ba07f5..ffeee4046 100644 --- a/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java +++ b/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java @@ -27,10 +27,7 @@ import org.apache.poi.ss.usermodel.RichTextString; import org.apache.poi.ss.util.CellAddress; import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogger; -import org.apache.poi.xssf.model.Comments; -import org.apache.poi.xssf.model.CommentsTable; -import org.apache.poi.xssf.model.SharedStrings; -import org.apache.poi.xssf.model.StylesTable; +import org.apache.poi.xssf.model.*; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFComment; import org.apache.poi.xssf.usermodel.XSSFRichTextString; @@ -63,7 +60,7 @@ public class XSSFSheetXMLHandler extends DefaultHandler { /** * Table with the styles used for formatting */ - private StylesTable stylesTable; + private Styles stylesTable; /** * Table with cell comments @@ -117,7 +114,7 @@ public class XSSFSheetXMLHandler extends DefaultHandler { * @param strings Table of shared strings */ public XSSFSheetXMLHandler( - StylesTable styles, + Styles styles, CommentsTable comments, SharedStrings strings, SheetContentsHandler sheetContentsHandler, @@ -140,7 +137,7 @@ public class XSSFSheetXMLHandler extends DefaultHandler { * @param strings Table of shared strings */ public XSSFSheetXMLHandler( - StylesTable styles, + Styles styles, SharedStrings strings, SheetContentsHandler sheetContentsHandler, DataFormatter dataFormatter, @@ -155,7 +152,7 @@ public class XSSFSheetXMLHandler extends DefaultHandler { * @param strings Table of shared strings */ public XSSFSheetXMLHandler( - StylesTable styles, + Styles styles, SharedStrings strings, SheetContentsHandler sheetContentsHandler, boolean formulasNotResults) { diff --git a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFEventBasedExcelExtractor.java b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFEventBasedExcelExtractor.java index 526383334..14e12a252 100644 --- a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFEventBasedExcelExtractor.java +++ b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFEventBasedExcelExtractor.java @@ -39,10 +39,7 @@ import org.apache.poi.xssf.eventusermodel.ReadOnlySharedStringsTable; import org.apache.poi.xssf.eventusermodel.XSSFReader; import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler; import org.apache.poi.xssf.eventusermodel.XSSFSheetXMLHandler.SheetContentsHandler; -import org.apache.poi.xssf.model.Comments; -import org.apache.poi.xssf.model.CommentsTable; -import org.apache.poi.xssf.model.SharedStrings; -import org.apache.poi.xssf.model.StylesTable; +import org.apache.poi.xssf.model.*; import org.apache.poi.xssf.usermodel.XSSFComment; import org.apache.poi.xssf.usermodel.XSSFShape; import org.apache.poi.xssf.usermodel.XSSFSimpleShape; @@ -232,7 +229,7 @@ public class XSSFEventBasedExcelExtractor extends POIXMLTextExtractor */ public void processSheet( SheetContentsHandler sheetContentsExtractor, - StylesTable styles, + Styles styles, CommentsTable comments, SharedStrings strings, InputStream sheetInputStream) diff --git a/src/ooxml/java/org/apache/poi/xssf/model/Styles.java b/src/ooxml/java/org/apache/poi/xssf/model/Styles.java new file mode 100644 index 000000000..d217dad33 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xssf/model/Styles.java @@ -0,0 +1,114 @@ +package org.apache.poi.xssf.model; + +import org.apache.poi.xssf.usermodel.XSSFCellStyle; +import org.apache.poi.xssf.usermodel.XSSFFont; +import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder; +import org.apache.poi.xssf.usermodel.extensions.XSSFCellFill; + +public interface Styles { + + /** + * Get number format string given its id + * + * @param fmtId number format id + * @return number format code + */ + String getNumberFormatAt(short fmtId); + + /** + * Puts fmt in the numberFormats map if the format is not + * already in the the number format style table. + * Does nothing if fmt is already in number format style table. + * + * @param fmt the number format to add to number format style table + * @return the index of fmt in the number format style table + * @throws IllegalStateException if adding the number format to the styles table + * would exceed the max allowed. + */ + int putNumberFormat(String fmt); + + /** + * Add a number format with a specific ID into the numberFormats map. + * If a format with the same ID already exists, overwrite the format code + * with fmt + * This may be used to override built-in number formats. + * + * @param index the number format ID + * @param fmt the number format code + */ + void putNumberFormat(short index, String fmt); + + /** + * Remove a number format from the style table if it exists. + * All cell styles with this number format will be modified to use the default number format. + * + * @param index the number format id to remove + * @return true if the number format was removed + */ + boolean removeNumberFormat(short index); + + /** + * Remove a number format from the style table if it exists + * All cell styles with this number format will be modified to use the default number format + * + * @param fmt the number format to remove + * @return true if the number format was removed + */ + boolean removeNumberFormat(String fmt); + + XSSFFont getFontAt(int idx); + + /** + * Records the given font in the font table. + * Will re-use an existing font index if this + * font matches another, EXCEPT if forced + * registration is requested. + * This allows people to create several fonts + * then customise them later. + * Note - End Users probably want to call + * {@link XSSFFont#registerTo(StylesTable)} + */ + int putFont(XSSFFont font, boolean forceRegistration); + + /** + * Records the given font in the font table. + * Will re-use an existing font index if this + * font matches another. + */ + int putFont(XSSFFont font); + + /** + * + * @param idx style index + * @return XSSFCellStyle or null if idx is out of bounds for xfs array + */ + XSSFCellStyle getStyleAt(int idx); + + int putStyle(XSSFCellStyle style); + + XSSFCellBorder getBorderAt(int idx); + + /** + * Adds a border to the border style table if it isn't already in the style table + * Does nothing if border is already in borders style table + * + * @param border border to add + * @return the index of the added border + */ + int putBorder(XSSFCellBorder border); + + XSSFCellFill getFillAt(int idx); + + /** + * Adds a fill to the fill style table if it isn't already in the style table + * Does nothing if fill is already in fill style table + * + * @param fill fill to add + * @return the index of the added fill + */ + int putFill(XSSFCellFill fill); + + int getNumCellStyles(); + + int getNumDataFormats(); +} diff --git a/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java index 2d36fda24..eec2f146e 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java @@ -56,7 +56,7 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.*; /** * Table of styles shared across all sheets in a workbook. */ -public class StylesTable extends POIXMLDocumentPart { +public class StylesTable extends POIXMLDocumentPart implements Styles { private final SortedMap numberFormats = new TreeMap<>(); private final List fonts = new ArrayList<>(); private final List fills = new ArrayList<>(); @@ -261,6 +261,7 @@ public class StylesTable extends POIXMLDocumentPart { * @param fmtId number format id * @return number format code */ + @Override public String getNumberFormatAt(short fmtId) { return numberFormats.get(fmtId); } @@ -285,6 +286,7 @@ public class StylesTable extends POIXMLDocumentPart { * @throws IllegalStateException if adding the number format to the styles table * would exceed the {@link #MAXIMUM_NUMBER_OF_DATA_FORMATS} allowed. */ + @Override public int putNumberFormat(String fmt) { // Check if number format already exists if (numberFormats.containsValue(fmt)) { @@ -334,6 +336,7 @@ public class StylesTable extends POIXMLDocumentPart { * @param index the number format ID * @param fmt the number format code */ + @Override public void putNumberFormat(short index, String fmt) { numberFormats.put(index, fmt); } @@ -345,6 +348,7 @@ public class StylesTable extends POIXMLDocumentPart { * @param index the number format id to remove * @return true if the number format was removed */ + @Override public boolean removeNumberFormat(short index) { String fmt = numberFormats.remove(index); boolean removed = (fmt != null); @@ -366,11 +370,13 @@ public class StylesTable extends POIXMLDocumentPart { * @param fmt the number format to remove * @return true if the number format was removed */ + @Override public boolean removeNumberFormat(String fmt) { short id = getNumberFormatId(fmt); return removeNumberFormat(id); } + @Override public XSSFFont getFontAt(int idx) { return fonts.get(idx); } @@ -385,6 +391,7 @@ public class StylesTable extends POIXMLDocumentPart { * Note - End Users probably want to call * {@link XSSFFont#registerTo(StylesTable)} */ + @Override public int putFont(XSSFFont font, boolean forceRegistration) { int idx = -1; if(!forceRegistration) { @@ -399,6 +406,8 @@ public class StylesTable extends POIXMLDocumentPart { fonts.add(font); return idx; } + + @Override public int putFont(XSSFFont font) { return putFont(font, false); } @@ -408,6 +417,7 @@ public class StylesTable extends POIXMLDocumentPart { * @param idx style index * @return XSSFCellStyle or null if idx is out of bounds for xfs array */ + @Override public XSSFCellStyle getStyleAt(int idx) { int styleXfId = 0; @@ -422,6 +432,8 @@ public class StylesTable extends POIXMLDocumentPart { return new XSSFCellStyle(idx, styleXfId, this, theme); } + + @Override public int putStyle(XSSFCellStyle style) { CTXf mainXF = style.getCoreXf(); @@ -431,6 +443,7 @@ public class StylesTable extends POIXMLDocumentPart { return xfs.indexOf(mainXF); } + @Override public XSSFCellBorder getBorderAt(int idx) { return borders.get(idx); } @@ -442,6 +455,7 @@ public class StylesTable extends POIXMLDocumentPart { * @param border border to add * @return the index of the added border */ + @Override public int putBorder(XSSFCellBorder border) { int idx = borders.indexOf(border); if (idx != -1) { @@ -452,6 +466,7 @@ public class StylesTable extends POIXMLDocumentPart { return borders.size() - 1; } + @Override public XSSFCellFill getFillAt(int idx) { return fills.get(idx); } @@ -479,6 +494,7 @@ public class StylesTable extends POIXMLDocumentPart { * @param fill fill to add * @return the index of the added fill */ + @Override public int putFill(XSSFCellFill fill) { int idx = fills.indexOf(fill); if (idx != -1) { @@ -543,7 +559,8 @@ public class StylesTable extends POIXMLDocumentPart { /** * get the size of cell styles */ - public int getNumCellStyles(){ + @Override + public int getNumCellStyles() { // Each cell style has a unique xfs entry // Several might share the same styleXfs entry return xfs.size(); @@ -552,6 +569,7 @@ public class StylesTable extends POIXMLDocumentPart { /** * @return number of data formats in the styles table */ + @Override public int getNumDataFormats() { return numberFormats.size(); } diff --git a/src/ooxml/java/org/apache/poi/xssf/model/Themes.java b/src/ooxml/java/org/apache/poi/xssf/model/Themes.java new file mode 100644 index 000000000..8bc4d4743 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xssf/model/Themes.java @@ -0,0 +1,19 @@ +package org.apache.poi.xssf.model; + +import org.apache.poi.xssf.usermodel.XSSFColor; + +public interface Themes { + /** + * Convert a theme "index" (as used by fonts etc) into a color. + * @param idx A theme "index" + * @return The mapped XSSFColor, or null if not mapped. + */ + XSSFColor getThemeColor(int idx); + + /** + * If the colour is based on a theme, then inherit + * information (currently just colours) from it as + * required. + */ + void inheritFromThemeAsRequired(XSSFColor color); +} 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 b23a4681a..0e68bceac 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/ThemesTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/ThemesTable.java @@ -34,7 +34,7 @@ import org.openxmlformats.schemas.drawingml.x2006.main.ThemeDocument; * Class that represents theme of XLSX document. The theme includes specific * colors and fonts. */ -public class ThemesTable extends POIXMLDocumentPart { +public class ThemesTable extends POIXMLDocumentPart implements Themes { public enum ThemeElement { LT1(0, "Lt1"), DK1(1,"Dk1"), @@ -110,6 +110,7 @@ public class ThemesTable extends POIXMLDocumentPart { * @param idx A theme "index" * @return The mapped XSSFColor, or null if not mapped. */ + @Override public XSSFColor getThemeColor(int idx) { // Theme color references are NOT positional indices into the color scheme, // i.e. these keys are NOT the same as the order in which theme colors appear @@ -150,6 +151,7 @@ public class ThemesTable extends POIXMLDocumentPart { * information (currently just colours) from it as * required. */ + @Override public void inheritFromThemeAsRequired(XSSFColor color) { if(color == null) { // Nothing for us to do