From 7c53483dd5c3080a7ffd67deb79f8ac3a630dffb Mon Sep 17 00:00:00 2001 From: Avik Sengupta Date: Mon, 27 Oct 2003 02:01:21 +0000 Subject: [PATCH] fix for 13921, sync from branch git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353412 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/record/BoundSheetRecord.java | 12 +++++++++++ .../poi/hssf/usermodel/HSSFWorkbook.java | 10 ++++++++-- .../poi/hssf/record/TestBoundSheetRecord.java | 20 +++++++++++++++++++ .../poi/hssf/usermodel/TestCloneSheet.java | 2 +- 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java b/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java index ee3922c7a5..f84b5baa90 100644 --- a/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java +++ b/src/java/org/apache/poi/hssf/record/BoundSheetRecord.java @@ -194,10 +194,22 @@ public class BoundSheetRecord /** * Set the sheetname for this sheet. (this appears in the tabs at the bottom) * @param sheetname the name of the sheet + * @thows IllegalArgumentException if sheet name will cause excel to crash. */ public void setSheetname( String sheetname ) { + + if ((sheetname == null) || (sheetname.length()==0) + || (sheetname.length()>31) + || (sheetname.indexOf("/") > -1) + || (sheetname.indexOf("\\") > -1) + || (sheetname.indexOf("?") > -1) + || (sheetname.indexOf("*") > -1) + || (sheetname.indexOf("]") > -1) + || (sheetname.indexOf("[") > -1) ){ + throw new IllegalArgumentException("Sheet name cannot be blank, greater than 31 chars, or contain any of /\\*?[]"); + } field_5_sheetname = sheetname; } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index 8b8d7c274b..799029b9b1 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -270,7 +270,9 @@ public class HSSFWorkbook /** - * set the sheet name. + * set the sheet name. + * Will throw IllegalArgumentException if the name is greater than 31 chars + * or contains /\?*[] * @param sheet number (0 based) * @param sheet name */ @@ -371,7 +373,11 @@ public class HSSFWorkbook windowTwo.setPaged(sheets.size() == 1); sheets.add(clonedSheet); - workbook.setSheetName(sheets.size()-1, srcName+"[1]"); + if (srcName.length()<28) { + workbook.setSheetName(sheets.size()-1, srcName+"(2)"); + }else { + workbook.setSheetName(sheets.size()-1,srcName.substring(0,28)+"(2)"); + } return clonedSheet; } return null; diff --git a/src/testcases/org/apache/poi/hssf/record/TestBoundSheetRecord.java b/src/testcases/org/apache/poi/hssf/record/TestBoundSheetRecord.java index 2058c2f9e1..4ca6075354 100644 --- a/src/testcases/org/apache/poi/hssf/record/TestBoundSheetRecord.java +++ b/src/testcases/org/apache/poi/hssf/record/TestBoundSheetRecord.java @@ -92,5 +92,25 @@ public class TestBoundSheetRecord assertEquals(" 2 + 2 + 4 + 2 + 1 + 1 + len(str) * 2", 24, record.getRecordSize()); } + + public void testName() { + BoundSheetRecord record = new BoundSheetRecord(); + record.setSheetname("1234567890223456789032345678904"); + assertTrue("Success", true); + try { + record.setSheetname("12345678902234567890323456789042"); + assertTrue("Should have thrown IllegalArgumentException, but didnt", false); + } catch (IllegalArgumentException e) { + assertTrue("succefully threw exception",true); + } + + try { + record.setSheetname("s//*s"); + assertTrue("Should have thrown IllegalArgumentException, but didnt", false); + } catch (IllegalArgumentException e) { + assertTrue("succefully threw exception",true); + } + + } } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestCloneSheet.java b/src/testcases/org/apache/poi/hssf/usermodel/TestCloneSheet.java index 052516108c..6ccf29ec27 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestCloneSheet.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestCloneSheet.java @@ -79,7 +79,7 @@ public class TestCloneSheet extends TestCase { s.addMergedRegion(new Region((short)0,(short)0,(short)1,(short)1)); b.cloneSheet(0); } - catch(Exception e){fail(e.getMessage());} + catch(Exception e){e.printStackTrace();fail(e.getMessage());} } }