poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ye...@apache.org
Subject svn commit: r811994 - in /poi/trunk: src/java/org/apache/poi/hssf/usermodel/ src/java/org/apache/poi/ss/ src/testcases/org/apache/poi/hssf/usermodel/ src/testcases/org/apache/poi/ss/usermodel/ test-data/spreadsheet/
Date Mon, 07 Sep 2009 04:55:32 GMT
Author: yegor
Date: Mon Sep  7 04:55:32 2009
New Revision: 811994

URL: http://svn.apache.org/viewvc?rev=811994&view=rev
Log:
Do not allow  text longer than 32,767 characters in HSSF cells, see Bugzilla 47751

Added:
    poi/trunk/test-data/spreadsheet/46368.xls   (with props)
Modified:
    poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
    poi/trunk/src/java/org/apache/poi/ss/SpreadsheetVersion.java
    poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
    poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java
    poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java

Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java?rev=811994&r1=811993&r2=811994&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java Mon Sep  7 04:55:32 2009
@@ -540,6 +540,11 @@
             setCellType(CELL_TYPE_BLANK, false, row, col, styleIndex);
             return;
         }
+
+        if(hvalue.length() > SpreadsheetVersion.EXCEL97.getMaxTextLength()){
+            throw new IllegalArgumentException("The maximum length of cell contents (text)
is 32,767 characters");
+        }
+
         if (_cellType == CELL_TYPE_FORMULA) {
             // Set the 'pre-evaluated result' for the formula
             // note - formulas do not preserve text formatting.

Modified: poi/trunk/src/java/org/apache/poi/ss/SpreadsheetVersion.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/SpreadsheetVersion.java?rev=811994&r1=811993&r2=811994&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/SpreadsheetVersion.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/SpreadsheetVersion.java Mon Sep  7 04:55:32 2009
@@ -36,9 +36,11 @@
 	 * <li>The total number of available rows is 64k (2^16)</li>
 	 * <li>The maximum number of arguments to a function is 30</li>
 	 * <li>Number of conditional format conditions on a cell is 3</li>
+     * <li>Length of text cell contents is unlimited </li>
+     * <li>Length of text cell contents is 32767</li>
 	 * </ul>
 	 */
-	EXCEL97(0x10000, 0x0100, 30, 3),
+	EXCEL97(0x10000, 0x0100, 30, 3, 32767),
 
 	/**
 	 * Excel2007
@@ -49,21 +51,24 @@
 	 * <li>The maximum number of arguments to a function is 255</li>
 	 * <li>Number of conditional format conditions on a cell is unlimited
 	 * (actually limited by available memory in Excel)</li>
+     * <li>Length of text cell contents is unlimited </li>
 	 * <ul>
 	 */
-	EXCEL2007(0x100000, 0x4000, 255, Integer.MAX_VALUE);
+	EXCEL2007(0x100000, 0x4000, 255, Integer.MAX_VALUE, Integer.MAX_VALUE);
 
 	private final int _maxRows;
 	private final int _maxColumns;
 	private final int _maxFunctionArgs;
 	private final int _maxCondFormats;
+    private final int _maxTextLength;
 
-	private SpreadsheetVersion(int maxRows, int maxColumns, int maxFunctionArgs, int maxCondFormats)
{
+	private SpreadsheetVersion(int maxRows, int maxColumns, int maxFunctionArgs, int maxCondFormats,
int maxText) {
 		_maxRows = maxRows;
 		_maxColumns = maxColumns;
 		_maxFunctionArgs = maxFunctionArgs;
 		_maxCondFormats = maxCondFormats;
-	}
+        _maxTextLength = maxText;
+    }
 
 	/**
 	 * @return the maximum number of usable rows in each spreadsheet
@@ -116,4 +121,12 @@
 	public String getLastColumnName() {
 		return CellReference.convertNumToColString(getLastColumnIndex());
 	}
+
+    /**
+     * @return the maximum length of a text cell
+     */
+    public int getMaxTextLength() {
+        return _maxTextLength;
+    }
+
 }

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java?rev=811994&r1=811993&r2=811994&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java Mon Sep  7 04:55:32
2009
@@ -38,7 +38,7 @@
 import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
 import org.apache.poi.hssf.record.formula.DeletedArea3DPtg;
 import org.apache.poi.hssf.record.formula.Ptg;
-import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues;
+import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.util.TempFile;
 
 /**
@@ -1494,4 +1494,19 @@
         assertEquals(893, wb.getNumberOfNames());
         assertEquals("Matthew\\Matthew11_1\\Matthew2331_1\\Matthew2351_1\\Matthew2361_1___lab",
wb.getNameName(300));
     }
+
+    /**
+     * HSSFRichTextString.length() returns negative for really long strings.
+     * The test file was created in OpenOffice 3.0 as Excel does not allow cell text longer
than 32,767 characters
+     */
+    public void test46368() {
+        HSSFWorkbook wb = openSample("46368.xls");
+    	HSSFSheet s = wb.getSheetAt(0);
+        HSSFCell cell1 = s.getRow(0).getCell(0);
+        assertEquals(32770, cell1.getStringCellValue().length());
+
+        HSSFCell cell2 = s.getRow(2).getCell(0);
+        assertEquals(32766, cell2.getStringCellValue().length());
+    }
+
 }

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java?rev=811994&r1=811993&r2=811994&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java Mon Sep  7 04:55:32
2009
@@ -19,6 +19,7 @@
 
 import java.util.Date;
 import java.util.GregorianCalendar;
+import java.io.FileOutputStream;
 
 import junit.framework.AssertionFailedError;
 
@@ -30,6 +31,8 @@
 import org.apache.poi.hssf.record.StringRecord;
 import org.apache.poi.ss.usermodel.BaseTestCell;
 import org.apache.poi.ss.usermodel.ErrorConstants;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.SpreadsheetVersion;
 
 /**
  * Tests various functionality having to do with {@link HSSFCell}.  For instance support
for
@@ -291,5 +294,34 @@
 		Record dbcr = recs[index++];
 		assertEquals(DBCellRecord.class, dbcr.getClass());
 	}
+
+    /**
+     *  The maximum length of cell contents (text) is 32,767 characters.
+     */
+    public void testMaxTextLength(){
+        HSSFSheet sheet = new HSSFWorkbook().createSheet();
+        HSSFCell cell = sheet.createRow(0).createCell(0);
+
+        int maxlen = SpreadsheetVersion.EXCEL97.getMaxTextLength();
+        assertEquals(32767, maxlen);
+
+        StringBuffer b = new StringBuffer() ;
+
+        // 32767 is okay
+        for( int i = 0 ; i < maxlen ; i++ )
+        {
+            b.append( "X" ) ;
+        }
+        cell.setCellValue(b.toString());
+
+        b.append("X");
+        // 32768 produces an invalid XLS file
+        try {
+            cell.setCellValue(b.toString());
+            fail("Expected exception");
+        } catch (IllegalArgumentException e){
+            assertEquals("The maximum length of cell contents (text) is 32,767 characters",
e.getMessage());
+        }
+    }
 }
 

Modified: poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java?rev=811994&r1=811993&r2=811994&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java Mon Sep
 7 04:55:32 2009
@@ -218,38 +218,6 @@
         assertTrue("no errors parsing formula", true);
     }
 
-    /**
-     * HSSFRichTextString.length() returns negative for really
-     *  long strings
-     */
-    public void test46368() {
-        Workbook wb = getTestDataProvider().createWorkbook();
-    	Sheet s = wb.createSheet();
-    	Row r = s.createRow(0);
-    	for(int i=0; i<15; i++) {
-    		int len = 32760 + i;
-    		Cell c = r.createCell(i);
-
-    		StringBuffer sb = new StringBuffer();
-    		for(int j=0; j<len; j++) {
-    			sb.append("x");
-    		}
-    		RichTextString rtr = wb.getCreationHelper().createRichTextString(sb.toString());
-    		assertEquals(len, rtr.length());
-    		c.setCellValue(rtr);
-    	}
-
-    	// Save and reload
-    	wb = getTestDataProvider().writeOutAndReadBack(wb);
-    	s = wb.getSheetAt(0);
-    	r = s.getRow(0);
-    	for(int i=0; i<15; i++) {
-    		int len = 32760 + i;
-    		Cell c = r.getCell(i);
-    		assertEquals(len, c.getRichStringCellValue().length());
-    	}
-    }
-
     public void test18800() {
        Workbook book = getTestDataProvider().createWorkbook();
        book.createSheet("TEST");

Added: poi/trunk/test-data/spreadsheet/46368.xls
URL: http://svn.apache.org/viewvc/poi/trunk/test-data/spreadsheet/46368.xls?rev=811994&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/test-data/spreadsheet/46368.xls
------------------------------------------------------------------------------
    svn:executable = *

Propchange: poi/trunk/test-data/spreadsheet/46368.xls
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message