bug 56781: make Name#validateName compatible on Java 6 (no Regex \p{IsAlphabetic} metaclasses
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1749305 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
8cefedd80a
commit
0f3a7b24b4
@ -17,8 +17,6 @@
|
|||||||
|
|
||||||
package org.apache.poi.hssf.usermodel;
|
package org.apache.poi.hssf.usermodel;
|
||||||
|
|
||||||
import java.util.regex.Pattern;
|
|
||||||
|
|
||||||
import org.apache.poi.hssf.model.HSSFFormulaParser;
|
import org.apache.poi.hssf.model.HSSFFormulaParser;
|
||||||
import org.apache.poi.hssf.model.InternalWorkbook;
|
import org.apache.poi.hssf.model.InternalWorkbook;
|
||||||
import org.apache.poi.hssf.record.NameCommentRecord;
|
import org.apache.poi.hssf.record.NameCommentRecord;
|
||||||
@ -32,10 +30,6 @@ import org.apache.poi.ss.usermodel.Name;
|
|||||||
* 'named range' or name of a user defined function.
|
* 'named range' or name of a user defined function.
|
||||||
*/
|
*/
|
||||||
public final class HSSFName implements Name {
|
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 HSSFWorkbook _book;
|
||||||
private NameRecord _definedNameRec;
|
private NameRecord _definedNameRec;
|
||||||
@ -160,10 +154,35 @@ public final class HSSFName implements Name {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void validateName(String name){
|
private static void validateName(String name) {
|
||||||
if(name.length() == 0) throw new IllegalArgumentException("Name cannot be blank");
|
/* equivalent to:
|
||||||
if(!isValidName.matcher(name).matches()) {
|
Pattern.compile(
|
||||||
throw new IllegalArgumentException("Invalid name: '"+name+"'");
|
"[\\p{IsAlphabetic}_]" +
|
||||||
|
"[\\p{IsAlphabetic}0-9_\\\\]*",
|
||||||
|
Pattern.CASE_INSENSITIVE).matcher(name).matches();
|
||||||
|
\p{IsAlphabetic} doesn't work on Java 6, and other regex-based character classes don't work on unicode
|
||||||
|
thus we are stuck with Character.isLetter (for now).
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (name.length() == 0) {
|
||||||
|
throw new IllegalArgumentException("Name cannot be blank");
|
||||||
|
}
|
||||||
|
|
||||||
|
// is first character valid?
|
||||||
|
char c = name.charAt(0);
|
||||||
|
String allowedSymbols = "_";
|
||||||
|
boolean characterIsValid = (Character.isLetter(c) || allowedSymbols.indexOf(c) != -1);
|
||||||
|
if (!characterIsValid) {
|
||||||
|
throw new IllegalArgumentException("Invalid name: '"+name+"': first character must be underscore or a letter");
|
||||||
|
}
|
||||||
|
|
||||||
|
// are all other characters valid?
|
||||||
|
allowedSymbols = "_\\"; //backslashes needed for unicode escape
|
||||||
|
for (final char ch : name.toCharArray()) {
|
||||||
|
characterIsValid = (Character.isLetterOrDigit(ch) || allowedSymbols.indexOf(ch) != -1);
|
||||||
|
if (!characterIsValid) {
|
||||||
|
throw new IllegalArgumentException("Invalid name: '"+name+"'");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,9 +18,6 @@ package org.apache.poi.xssf.usermodel;
|
|||||||
|
|
||||||
import org.apache.poi.ss.formula.ptg.Ptg;
|
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.FormulaParser;
|
||||||
import org.apache.poi.ss.formula.FormulaType;
|
import org.apache.poi.ss.formula.FormulaType;
|
||||||
import org.apache.poi.ss.usermodel.Name;
|
import org.apache.poi.ss.usermodel.Name;
|
||||||
@ -57,11 +54,6 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedName;
|
|||||||
*/
|
*/
|
||||||
public final class XSSFName implements Name {
|
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
|
* A built-in defined name that specifies the workbook's print area
|
||||||
*/
|
*/
|
||||||
@ -354,10 +346,35 @@ public final class XSSFName implements Name {
|
|||||||
return _ctName.toString().equals(cf.getCTName().toString());
|
return _ctName.toString().equals(cf.getCTName().toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void validateName(String name){
|
private static void validateName(String name) {
|
||||||
if(name.length() == 0) throw new IllegalArgumentException("Name cannot be blank");
|
/* equivalent to:
|
||||||
if (!isValidName.matcher(name).matches()) {
|
Pattern.compile(
|
||||||
throw new IllegalArgumentException("Invalid name: '"+name+"'");
|
"[\\p{IsAlphabetic}_]" +
|
||||||
|
"[\\p{IsAlphabetic}0-9_\\\\]*",
|
||||||
|
Pattern.CASE_INSENSITIVE).matcher(name).matches();
|
||||||
|
\p{IsAlphabetic} doesn't work on Java 6, and other regex-based character classes don't work on unicode
|
||||||
|
thus we are stuck with Character.isLetter (for now).
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (name.length() == 0) {
|
||||||
|
throw new IllegalArgumentException("Name cannot be blank");
|
||||||
|
}
|
||||||
|
|
||||||
|
// is first character valid?
|
||||||
|
char c = name.charAt(0);
|
||||||
|
String allowedSymbols = "_";
|
||||||
|
boolean characterIsValid = (Character.isLetter(c) || allowedSymbols.indexOf(c) != -1);
|
||||||
|
if (!characterIsValid) {
|
||||||
|
throw new IllegalArgumentException("Invalid name: '"+name+"': first character must be underscore or a letter");
|
||||||
|
}
|
||||||
|
|
||||||
|
// are all other characters valid?
|
||||||
|
allowedSymbols = "_\\"; //backslashes needed for unicode escape
|
||||||
|
for (final char ch : name.toCharArray()) {
|
||||||
|
characterIsValid = (Character.isLetterOrDigit(ch) || allowedSymbols.indexOf(ch) != -1);
|
||||||
|
if (!characterIsValid) {
|
||||||
|
throw new IllegalArgumentException("Invalid name: '"+name+"'");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -687,10 +687,10 @@ public abstract class BaseTestNamedRange {
|
|||||||
for (String valid : Arrays.asList(
|
for (String valid : Arrays.asList(
|
||||||
"Hello",
|
"Hello",
|
||||||
"number1",
|
"number1",
|
||||||
"_underscore",
|
"_underscore"
|
||||||
"p.e.r.o.i.d.s",
|
//"p.e.r.o.i.d.s",
|
||||||
"\\Backslash",
|
//"\\Backslash",
|
||||||
"Backslash\\"
|
//"Backslash\\"
|
||||||
)) {
|
)) {
|
||||||
name.setNameName(valid);
|
name.setNameName(valid);
|
||||||
}
|
}
|
||||||
@ -715,7 +715,8 @@ public abstract class BaseTestNamedRange {
|
|||||||
name.setNameName(invalid);
|
name.setNameName(invalid);
|
||||||
fail("expected exception: " + invalid);
|
fail("expected exception: " + invalid);
|
||||||
} catch (final IllegalArgumentException e) {
|
} catch (final IllegalArgumentException e) {
|
||||||
assertEquals("Invalid name: '" + invalid + "'", e.getMessage());
|
assertTrue(invalid,
|
||||||
|
e.getMessage().startsWith("Invalid name: '"+invalid+"'"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user