bug 56781: disallow names with symbols, except underscore, period, and backslash

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1749293 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2016-06-20 10:27:52 +00:00
parent e3a1b43327
commit 4b4306b825
4 changed files with 66 additions and 9 deletions

View File

@ -17,6 +17,8 @@
package org.apache.poi.hssf.usermodel;
import java.util.regex.Pattern;
import org.apache.poi.hssf.model.HSSFFormulaParser;
import org.apache.poi.hssf.model.InternalWorkbook;
import org.apache.poi.hssf.record.NameCommentRecord;
@ -30,6 +32,11 @@ import org.apache.poi.ss.usermodel.Name;
* 'named range' or name of a user defined function.
*/
public final class HSSFName implements Name {
private static final Pattern isValidName = Pattern.compile(
"[\\p{IsAlphabetic}_\\\\]" +
"[\\p{IsAlphabetic}0-9_.\\\\]*",
Pattern.CASE_INSENSITIVE);
private HSSFWorkbook _book;
private NameRecord _definedNameRec;
private NameCommentRecord _commentRec;
@ -155,10 +162,8 @@ public final class HSSFName implements Name {
private static void validateName(String name){
if(name.length() == 0) throw new IllegalArgumentException("Name cannot be blank");
char c = name.charAt(0);
if(!(c == '_' || Character.isLetter(c)) || name.indexOf(' ') != -1) {
throw new IllegalArgumentException("Invalid name: '"+name+"'; Names must begin with a letter or underscore and not contain spaces");
if(!isValidName.matcher(name).matches()) {
throw new IllegalArgumentException("Invalid name: '"+name+"'");
}
}

View File

@ -55,7 +55,7 @@ public interface Name {
/**
* Get the sheets name which this named range is referenced to
*
* @return sheet name, which this named range refered to
* @return sheet name, which this named range referred to
*/
String getSheetName();

View File

@ -17,6 +17,10 @@
package org.apache.poi.xssf.usermodel;
import org.apache.poi.ss.formula.ptg.Ptg;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.poi.ss.formula.FormulaParser;
import org.apache.poi.ss.formula.FormulaType;
import org.apache.poi.ss.usermodel.Name;
@ -53,6 +57,11 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedName;
*/
public final class XSSFName implements Name {
private static final Pattern isValidName = Pattern.compile(
"[\\p{IsAlphabetic}_\\\\]" +
"[\\p{IsAlphabetic}0-9_.\\\\]*",
Pattern.CASE_INSENSITIVE);
/**
* A built-in defined name that specifies the workbook's print area
*/
@ -347,9 +356,8 @@ public final class XSSFName implements Name {
private static void validateName(String name){
if(name.length() == 0) throw new IllegalArgumentException("Name cannot be blank");
char c = name.charAt(0);
if(!(c == '_' || Character.isLetter(c)) || name.indexOf(' ') != -1) {
throw new IllegalArgumentException("Invalid name: '"+name+"'; Names must begin with a letter or underscore and not contain spaces");
if (!isValidName.matcher(name).matches()) {
throw new IllegalArgumentException("Invalid name: '"+name+"'");
}
}
}

View File

@ -25,6 +25,7 @@ import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.apache.poi.ss.ITestDataProvider;
@ -677,4 +678,47 @@ public abstract class BaseTestNamedRange {
wb.close();
}
//@Ignore
@Test
public void test56781() throws IOException {
Workbook wb = _testDataProvider.createWorkbook();
Name name = wb.createName();
for (String valid : Arrays.asList(
"Hello",
"number1",
"_underscore",
"p.e.r.o.i.d.s",
"\\Backslash",
"Backslash\\"
)) {
name.setNameName(valid);
}
try {
name.setNameName("");
fail("expected exception: (blank)");
} catch (final IllegalArgumentException e) {
assertEquals("Name cannot be blank", e.getMessage());
}
for (String invalid : Arrays.asList(
"1number",
"Sheet1!A1",
"Exclamation!",
"Has Space",
"Colon:",
"A-Minus",
"A+Plus",
"Dollar$")) {
try {
name.setNameName(invalid);
fail("expected exception: " + invalid);
} catch (final IllegalArgumentException e) {
assertEquals("Invalid name: '" + invalid + "'", e.getMessage());
}
}
}
}