From 602832bda6f45aa2217848e2ac4d12d1fc9664f0 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Sun, 3 May 2015 07:53:09 +0000 Subject: [PATCH] Somewhat speed up creating data formats with large counts, and add maximum format/style count checking. #57884 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1677368 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/xssf/model/StylesTable.java | 38 ++++++++++++------- 1 file changed, 24 insertions(+), 14 deletions(-) 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 53d1b76515..d0383754f0 100644 --- a/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java +++ b/src/ooxml/java/org/apache/poi/xssf/model/StylesTable.java @@ -22,7 +22,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Arrays; -import java.util.LinkedHashMap; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -30,6 +30,7 @@ import java.util.Map.Entry; import org.apache.poi.POIXMLDocumentPart; import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackageRelationship; +import org.apache.poi.ss.SpreadsheetVersion; import org.apache.poi.ss.usermodel.BuiltinFormats; import org.apache.poi.ss.usermodel.FontFamily; import org.apache.poi.ss.usermodel.FontScheme; @@ -59,11 +60,10 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.StyleSheetDocument; /** * Table of styles shared across all sheets in a workbook. - * - * @author ugo */ public class StylesTable extends POIXMLDocumentPart { - private final Map numberFormats = new LinkedHashMap(); + private final Map numberFormats = new HashMap(); + private final boolean[] usedNumberFormats = new boolean[SpreadsheetVersion.EXCEL2007.getMaxCellStyles()]; private final List fonts = new ArrayList(); private final List fills = new ArrayList(); private final List borders = new ArrayList(); @@ -76,6 +76,7 @@ public class StylesTable extends POIXMLDocumentPart { * The first style id available for use as a custom style */ public static final int FIRST_CUSTOM_STYLE_ID = BuiltinFormats.FIRST_USER_DEFINED_FORMAT_INDEX + 1; + private static final int MAXIMUM_STYLE_ID = SpreadsheetVersion.EXCEL2007.getMaxCellStyles(); private StyleSheetDocument doc; private ThemesTable theme; @@ -130,7 +131,9 @@ public class StylesTable extends POIXMLDocumentPart { CTNumFmts ctfmts = styleSheet.getNumFmts(); if( ctfmts != null){ for (CTNumFmt nfmt : ctfmts.getNumFmtArray()) { - numberFormats.put((int)nfmt.getNumFmtId(), nfmt.getFormatCode()); + int formatId = (int)nfmt.getNumFmtId(); + numberFormats.put(formatId, nfmt.getFormatCode()); + usedNumberFormats[formatId] = true; } } @@ -183,21 +186,24 @@ public class StylesTable extends POIXMLDocumentPart { public int putNumberFormat(String fmt) { if (numberFormats.containsValue(fmt)) { // Find the key, and return that - for(Integer key : numberFormats.keySet() ) { - if(numberFormats.get(key).equals(fmt)) { - return key; + for (Entry numFmt : numberFormats.entrySet()) { + if(numFmt.getValue().equals(fmt)) { + return numFmt.getKey(); } } throw new IllegalStateException("Found the format, but couldn't figure out where - should never happen!"); } // Find a spare key, and add that - int newKey = FIRST_CUSTOM_STYLE_ID; - while(numberFormats.containsKey(newKey)) { - newKey++; + for (int i=FIRST_CUSTOM_STYLE_ID; i MAXIMUM_STYLE_ID) + throw new IllegalStateException("The maximum number of Cell Styles was exceeded. " + + "You can define up to " + MAXIMUM_STYLE_ID + " style in a .xlsx Workbook"); + CTXf xf = CTXf.Factory.newInstance(); xf.setNumFmtId(0); xf.setFontId(0); xf.setFillId(0); xf.setBorderId(0); xf.setXfId(0); - int xfSize = styleXfs.size(); int indexXf = putCellXf(xf); return new XSSFCellStyle(indexXf - 1, xfSize - 1, this, theme); } -- 2.39.5