summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJaven O'Neal <onealj@apache.org>2016-06-20 10:27:52 +0000
committerJaven O'Neal <onealj@apache.org>2016-06-20 10:27:52 +0000
commit4b4306b825239ecb29a281dba7e63eb26507e855 (patch)
tree69360fbe4524b12d538607aa960e422ea2ec63ab
parente3a1b43327a902c495aa83ccd7b384108c574056 (diff)
downloadpoi-4b4306b825239ecb29a281dba7e63eb26507e855.tar.gz
poi-4b4306b825239ecb29a281dba7e63eb26507e855.zip
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
-rw-r--r--src/java/org/apache/poi/hssf/usermodel/HSSFName.java13
-rw-r--r--src/java/org/apache/poi/ss/usermodel/Name.java2
-rw-r--r--src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java16
-rw-r--r--src/testcases/org/apache/poi/ss/usermodel/BaseTestNamedRange.java44
4 files changed, 66 insertions, 9 deletions
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFName.java b/src/java/org/apache/poi/hssf/usermodel/HSSFName.java
index 13818f5b29..f6135c951e 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFName.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFName.java
@@ -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+"'");
}
}
diff --git a/src/java/org/apache/poi/ss/usermodel/Name.java b/src/java/org/apache/poi/ss/usermodel/Name.java
index a07154e5fe..983e438b5d 100644
--- a/src/java/org/apache/poi/ss/usermodel/Name.java
+++ b/src/java/org/apache/poi/ss/usermodel/Name.java
@@ -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();
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java
index 1e592514ad..c9db0bfda3 100644
--- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java
+++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java
@@ -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;
@@ -52,6 +56,11 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedName;
* @author Yegor Kozlov
*/
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
@@ -344,12 +353,11 @@ public final class XSSFName implements Name {
XSSFName cf = (XSSFName) o;
return _ctName.toString().equals(cf.getCTName().toString());
}
-
+
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+"'");
}
}
}
diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestNamedRange.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestNamedRange.java
index 18a0ef3e0e..463314fc10 100644
--- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestNamedRange.java
+++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestNamedRange.java
@@ -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());
+ }
+ }
+
+ }
}