Return-Path: Delivered-To: apmail-poi-commits-archive@minotaur.apache.org Received: (qmail 11697 invoked from network); 11 Feb 2011 10:03:51 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 11 Feb 2011 10:03:51 -0000 Received: (qmail 12860 invoked by uid 500); 11 Feb 2011 10:03:51 -0000 Delivered-To: apmail-poi-commits-archive@poi.apache.org Received: (qmail 12830 invoked by uid 500); 11 Feb 2011 10:03:50 -0000 Mailing-List: contact commits-help@poi.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@poi.apache.org Delivered-To: mailing list commits@poi.apache.org Received: (qmail 12820 invoked by uid 99); 11 Feb 2011 10:03:49 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 11 Feb 2011 10:03:49 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 11 Feb 2011 10:03:47 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 3A08723888BD; Fri, 11 Feb 2011 10:03:26 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1069730 - in /poi/trunk/src: documentation/content/xdocs/status.xml java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java Date: Fri, 11 Feb 2011 10:03:26 -0000 To: commits@poi.apache.org From: yegor@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110211100326.3A08723888BD@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: yegor Date: Fri Feb 11 10:03:25 2011 New Revision: 1069730 URL: http://svn.apache.org/viewvc?rev=1069730&view=rev Log: fixed HSSFWorkbook.createCellStyle to throw exception if the maximum number of cell styles was exceeded Modified: poi/trunk/src/documentation/content/xdocs/status.xml poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java Modified: poi/trunk/src/documentation/content/xdocs/status.xml URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=1069730&r1=1069729&r2=1069730&view=diff ============================================================================== --- poi/trunk/src/documentation/content/xdocs/status.xml (original) +++ poi/trunk/src/documentation/content/xdocs/status.xml Fri Feb 11 10:03:25 2011 @@ -34,6 +34,7 @@ + fixed HSSFWorkbook.createCellStyle to throw exception if the maximum number of cell styles was exceeded 50539 - Better fix for html-style br tags (invalid XML) inside XSSF documents 49928 - allow overridden built-in formats in HSSFCellStyle 50607 - Added implementation for CLEAN(), CHAR() and ADDRESS() Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java?rev=1069730&r1=1069729&r2=1069730&view=diff ============================================================================== --- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java (original) +++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java Fri Feb 11 10:03:25 2011 @@ -57,7 +57,6 @@ import org.apache.poi.hssf.record.aggreg import org.apache.poi.hssf.record.common.UnicodeString; import org.apache.poi.ss.formula.ptg.Area3DPtg; import org.apache.poi.ss.formula.ptg.MemFuncPtg; -import org.apache.poi.ss.formula.ptg.NameXPtg; import org.apache.poi.ss.formula.ptg.OperandPtg; import org.apache.poi.ss.formula.ptg.Ptg; import org.apache.poi.ss.formula.ptg.Ref3DPtg; @@ -68,7 +67,6 @@ import org.apache.poi.poifs.filesystem.D import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.ss.formula.udf.AggregatingUDFFinder; import org.apache.poi.ss.formula.udf.UDFFinder; -import org.apache.poi.ss.usermodel.CreationHelper; import org.apache.poi.ss.usermodel.Row.MissingCellPolicy; import org.apache.poi.ss.formula.FormulaType; import org.apache.poi.ss.util.WorkbookUtil; @@ -92,6 +90,16 @@ public final class HSSFWorkbook extends private static final int MAX_ROW = 0xFFFF; private static final short MAX_COLUMN = (short)0x00FF; + /** + * The maximum number of cell styles in a .xls workbook. + * The 'official' limit is 4,000, but POI allows a slightly larger number. + * This extra delta takes into account built-in styles that are automatically + * created for new workbooks + * + * See http://office.microsoft.com/en-us/excel-help/excel-specifications-and-limits-HP005199291.aspx + */ + private static final int MAX_STYLES = 4030; + private static final int DEBUG = POILogger.DEBUG; /** @@ -1123,12 +1131,19 @@ public final class HSSFWorkbook extends } /** - * create a new Cell style and add it to the workbook's style table + * Create a new Cell style and add it to the workbook's style table. + * You can define up to 4000 unique styles in a .xls workbook. + * * @return the new Cell Style object + * @throws IllegalStateException if the maximum number of cell styles exceeded the limit */ public HSSFCellStyle createCellStyle() { + if(workbook.getNumExFormats() == MAX_STYLES) { + throw new IllegalStateException("The maximum number of cell styles was exceeded. " + + "You can define up to 4000 styles in a .xls workbook"); + } ExtendedFormatRecord xfr = workbook.createCellXF(); short index = (short) (getNumCellStyles() - 1); HSSFCellStyle style = new HSSFCellStyle(index, xfr, this); Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java?rev=1069730&r1=1069729&r2=1069730&view=diff ============================================================================== --- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java (original) +++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFWorkbook.java Fri Feb 11 10:03:25 2011 @@ -559,4 +559,23 @@ public final class TestHSSFWorkbook exte assertEquals("replaceMe", cell .getRichStringCellValue().getString()); } } + public void testCellStylesLimit() { + HSSFWorkbook wb = new HSSFWorkbook(); + int numBuiltInStyles = wb.getNumCellStyles(); + int MAX_STYLES = 4030; + int limit = MAX_STYLES - numBuiltInStyles; + for(int i=0; i < limit; i++){ + HSSFCellStyle style = wb.createCellStyle(); + } + + assertEquals(MAX_STYLES, wb.getNumCellStyles()); + try { + HSSFCellStyle style = wb.createCellStyle(); + fail("expected exception"); + } catch (IllegalStateException e){ + assertEquals("The maximum number of cell styles was exceeded. " + + "You can define up to 4000 styles in a .xls workbook", e.getMessage()); + } + assertEquals(MAX_STYLES, wb.getNumCellStyles()); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org For additional commands, e-mail: commits-help@poi.apache.org