fixed XSSFWorkbook.createSheet to throw exception if sheet name begins or ends with a single quote ('), see Bugzilla 49875

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@992629 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2010-09-04 15:56:29 +00:00
parent c6219cadca
commit 3744428426
5 changed files with 63 additions and 86 deletions

View File

@ -34,6 +34,7 @@
<changes>
<release version="3.7-beta3" date="2010-??-??">
<action dev="poi-developers" type="fix">49875 - fixed XSSFWorkbook.createSheet to throw exception if sheet name begins or ends with a single quote (')</action>
<action dev="poi-developers" type="fix">49873 - fixed XSSFFormulaEvaluator to support blank cells</action>
<action dev="poi-developers" type="fix">49850 - added a getter for _iStartAt in ListFormatOverrideLevel</action>
<action dev="poi-developers" type="fix">49761 - change cell type to error when setting Double.NaN or Infinities</action>

View File

@ -26,6 +26,7 @@ import org.apache.poi.util.BitFieldFactory;
import org.apache.poi.util.HexDump;
import org.apache.poi.util.LittleEndianOutput;
import org.apache.poi.util.StringUtil;
import org.apache.poi.ss.util.WorkbookUtil;
/**
* Title: Bound Sheet Record (aka BundleSheet) (0x0085)<P>
@ -90,42 +91,11 @@ public final class BoundSheetRecord extends StandardRecord {
*/
public void setSheetname(String sheetName) {
validateSheetName(sheetName);
WorkbookUtil.validateSheetName(sheetName);
field_5_sheetname = sheetName;
field_4_isMultibyteUnicode = StringUtil.hasMultibyte(sheetName) ? 1 : 0;
}
private static void validateSheetName(String sheetName) {
if (sheetName == null) {
throw new IllegalArgumentException("sheetName must not be null");
}
int len = sheetName.length();
if (len < 1) {
throw new IllegalArgumentException("sheetName must not be empty string");
}
for (int i=0; i<len; i++) {
char ch = sheetName.charAt(i);
switch (ch) {
case '/':
case '\\':
case '?':
case '*':
case ']':
case '[':
break;
default:
// all other chars OK
continue;
}
throw new IllegalArgumentException("Invalid char (" + ch
+ ") found at index (" + i + ") in sheet name '" + sheetName + "'");
}
if (sheetName.charAt(0) == '\'' || sheetName.charAt(len-1) == '\'') {
throw new IllegalArgumentException("Invalid sheet name '" + sheetName
+ "'. Sheet names must not begin or end with (').");
}
}
/**
* get the offset in bytes of the Beginning of File Marker within the HSSF Stream part of the POIFS file
*

View File

@ -74,4 +74,59 @@ public class WorkbookUtil {
}
return result.toString();
}
/**
* Validates sheet name.
*
* <p>
* The character count <tt>MUST</tt> be greater than or equal to 1 and less than or equal to 31.
* The string MUST NOT contain the any of the following characters:
* <ul>
* <li> 0x0000 </li>
* <li> 0x0003 </li>
* <li> colon (:) </li>
* <li> backslash (\) </li>
* <li> asterisk (*) </li>
* <li> question mark (?) </li>
* <li> forward slash (/) </li>
* <li> opening square bracket ([) </li>
* <li> closing square bracket (]) </li>
* </ul>
* The string MUST NOT begin or end with the single quote (') character.
* </p>
*
* @param sheetName the name to validate
*/
public static void validateSheetName(String sheetName) {
if (sheetName == null) {
throw new IllegalArgumentException("sheetName must not be null");
}
int len = sheetName.length();
if (len < 1) {
throw new IllegalArgumentException("sheetName must not be empty string");
}
for (int i=0; i<len; i++) {
char ch = sheetName.charAt(i);
switch (ch) {
case '/':
case '\\':
case '?':
case '*':
case ']':
case '[':
case ':':
break;
default:
// all other chars OK
continue;
}
throw new IllegalArgumentException("Invalid char (" + ch
+ ") found at index (" + i + ") in sheet name '" + sheetName + "'");
}
if (sheetName.charAt(0) == '\'' || sheetName.charAt(len-1) == '\'') {
throw new IllegalArgumentException("Invalid sheet name '" + sheetName
+ "'. Sheet names must not begin or end with (').");
}
}
}

View File

@ -51,6 +51,7 @@ import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.util.WorkbookUtil;
import org.apache.poi.util.*;
import org.apache.poi.xssf.model.CalculationChain;
import org.apache.poi.xssf.model.MapInfo;
@ -511,7 +512,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
}
private CTSheet addSheet(String sheetname) {
validateSheetName(sheetname);
WorkbookUtil.validateSheetName(sheetname);
CTSheet sheet = workbook.getSheets().addNewSheet();
sheet.setName(sheetname);
@ -1099,11 +1100,10 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
* or contains /\?*[]
*
* @param sheet number (0 based)
* @see #validateSheetName(String)
*/
public void setSheetName(int sheet, String name) {
validateSheetIndex(sheet);
validateSheetName(name);
WorkbookUtil.validateSheetName(name);
if (containsSheet(name, sheet ))
throw new IllegalArgumentException( "The workbook already contains a sheet of this name" );
workbook.getSheets().getSheetArray(sheet).setName(name);
@ -1232,56 +1232,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
return false;
}
/**
* Validates sheet name.
*
* <p>
* The character count <tt>MUST</tt> be greater than or equal to 1 and less than or equal to 31.
* The string MUST NOT contain the any of the following characters:
* <ul>
* <li> 0x0000 </li>
* <li> 0x0003 </li>
* <li> colon (:) </li>
* <li> backslash (\) </li>
* <li> asterisk (*) </li>
* <li> question mark (?) </li>
* <li> forward slash (/) </li>
* <li> opening square bracket ([) </li>
* <li> closing square bracket (]) </li>
* </ul>
* The string MUST NOT begin or end with the single quote (') character.
* </p>
*
* @param sheetName the name to validate
*/
private static void validateSheetName(String sheetName) {
if (sheetName == null) {
throw new IllegalArgumentException("sheetName must not be null");
}
int len = sheetName.length();
if (len < 1 || len > 31) {
throw new IllegalArgumentException("sheetName '" + sheetName
+ "' is invalid - must be 1-30 characters long");
}
for (int i=0; i<len; i++) {
char ch = sheetName.charAt(i);
switch (ch) {
case '/':
case '\\':
case '?':
case '*':
case ']':
case '[':
break;
default:
// all other chars OK
continue;
}
throw new IllegalArgumentException("Invalid char (" + ch
+ ") found at index (" + i + ") in sheet name '" + sheetName + "'");
}
}
/**
* Gets a boolean value that indicates whether the date systems used in the workbook starts in 1904.
* <p>

View File

@ -72,7 +72,8 @@ public abstract class BaseTestWorkbook extends TestCase {
//names cannot be blank or contain any of /\*?[]
String[] invalidNames = {"", "Sheet/", "Sheet\\",
"Sheet?", "Sheet*", "Sheet[", "Sheet]"};
"Sheet?", "Sheet*", "Sheet[", "Sheet]", "'Sheet'",
"My:Sheet"};
for (String sheetName : invalidNames) {
try {
wb.createSheet(sheetName);