From a3d9e50d1b7c909c0fb10e3f0c50e0444a44fc50 Mon Sep 17 00:00:00 2001 From: Jason Height Date: Thu, 14 Oct 2004 03:38:20 +0000 Subject: [PATCH] Implemented row bounds. Will raise an IndexOutOfBoundsException if the excel limits are not observerd. Interestingly column bounds were previouly implemented, but row bounds were not. Anyhow this fixes bug 15102 git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353608 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/hssf/record/RowRecord.java | 6 ++++ .../apache/poi/hssf/usermodel/HSSFRow.java | 10 ++----- .../apache/poi/hssf/usermodel/HSSFSheet.java | 4 ++- .../poi/hssf/usermodel/TestHSSFRow.java | 28 +++++++++++++++++++ 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/java/org/apache/poi/hssf/record/RowRecord.java b/src/java/org/apache/poi/hssf/record/RowRecord.java index 6fd67c4faf..486a57e2ef 100644 --- a/src/java/org/apache/poi/hssf/record/RowRecord.java +++ b/src/java/org/apache/poi/hssf/record/RowRecord.java @@ -35,6 +35,12 @@ public class RowRecord implements Comparable { public final static short sid = 0x208; + + /** The maximum row number that excel can handle (zero bazed) ie 65536 rows is + * max number of rows. + */ + public final static int MAX_ROW_NUMBER = 65535; + //private short field_1_row_number; private int field_1_row_number; private short field_2_first_col; diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java b/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java index c84df9032e..f8a33427fb 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFRow.java @@ -84,7 +84,6 @@ public class HSSFRow //protected HSSFRow(Workbook book, Sheet sheet, short rowNum) protected HSSFRow(Workbook book, Sheet sheet, int rowNum) { - this.rowNum = rowNum; cells = new HashMap(10); // new ArrayList(INITIAL_CAPACITY); this.book = book; this.sheet = sheet; @@ -94,7 +93,6 @@ public class HSSFRow row.setLastCol((short) -1); row.setFirstCol((short) -1); - // row.setRowNumber(rowNum); setRowNum(rowNum); } @@ -110,17 +108,12 @@ public class HSSFRow protected HSSFRow(Workbook book, Sheet sheet, RowRecord record) { - //this.rowNum = rowNum; cells = new HashMap(); // ArrayList(INITIAL_CAPACITY); this.book = book; this.sheet = sheet; row = record; - // row.setHeight(record.getHeight()); - // row.setRowNumber(rowNum); setRowNum(record.getRowNumber()); - -// addColumns(book, sheet, record); } /** @@ -206,11 +199,14 @@ public class HSSFRow /** * set the row number of this row. * @param rowNum the row number (0-based) + * @throws IndexOutOfBoundsException if the row number is not within the range 0-65535. */ //public void setRowNum(short rowNum) public void setRowNum(int rowNum) { + if ((rowNum < 0) || (rowNum > RowRecord.MAX_ROW_NUMBER)) + throw new IndexOutOfBoundsException("Row number must be between 0 and "+RowRecord.MAX_ROW_NUMBER+", was <"+rowNum+">"); this.rowNum = rowNum; if (row != null) { diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java index c381e1f1a3..8c3eb00d53 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java @@ -242,10 +242,12 @@ public class HSSFSheet int rownum = lastrow - 1; HSSFRow r = getRow(rownum); - while (r == null && rownum >= 0) + while (r == null && rownum > 0) { r = getRow(--rownum); } + if (r == null) + return -1; return rownum; } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFRow.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFRow.java index 8258b8b868..c82956db61 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFRow.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFRow.java @@ -99,4 +99,32 @@ public class TestHSSFRow } + + public void testRowBounds() + throws Exception + { + HSSFWorkbook workbook = new HSSFWorkbook(); + HSSFSheet sheet = workbook.createSheet(); + //Test low row bound + HSSFRow row = sheet.createRow( (short) 0); + //Test low row bound exception + boolean caughtException = false; + try { + row = sheet.createRow(-1); + } catch (IndexOutOfBoundsException ex) { + caughtException = true; + } + assertTrue(caughtException); + //Test high row bound + row = sheet.createRow(65535); + //Test high row bound exception + caughtException = false; + try { + row = sheet.createRow(65536); + } catch (IndexOutOfBoundsException ex) { + caughtException = true; + } + assertTrue(caughtException); + } + } -- 2.39.5