poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From one...@apache.org
Subject svn commit: r1749293 - in /poi/trunk/src: java/org/apache/poi/hssf/usermodel/HSSFName.java java/org/apache/poi/ss/usermodel/Name.java ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java testcases/org/apache/poi/ss/usermodel/BaseTestNamedRange.java
Date Mon, 20 Jun 2016 10:27:53 GMT
Author: onealj
Date: Mon Jun 20 10:27:52 2016
New Revision: 1749293

URL: http://svn.apache.org/viewvc?rev=1749293&view=rev
Log:
bug 56781: disallow names with symbols, except underscore, period, and backslash

Modified:
    poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFName.java
    poi/trunk/src/java/org/apache/poi/ss/usermodel/Name.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java
    poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestNamedRange.java

Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFName.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFName.java?rev=1749293&r1=1749292&r2=1749293&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFName.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFName.java Mon Jun 20 10:27:52 2016
@@ -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 N
 
     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+"'");
         }
     }
 

Modified: poi/trunk/src/java/org/apache/poi/ss/usermodel/Name.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/usermodel/Name.java?rev=1749293&r1=1749292&r2=1749293&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/usermodel/Name.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/usermodel/Name.java Mon Jun 20 10:27:52 2016
@@ -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();
 

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java?rev=1749293&r1=1749292&r2=1749293&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java Mon Jun 20 10:27:52
2016
@@ -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.spread
  * @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 N
         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+"'");
         }
     }
 }

Modified: poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestNamedRange.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestNamedRange.java?rev=1749293&r1=1749292&r2=1749293&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestNamedRange.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestNamedRange.java Mon Jun 20
10:27:52 2016
@@ -25,6 +25,7 @@ import static org.junit.Assert.assertTru
 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());
+            }
+        }
+        
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message