mirror of https://github.com/apache/poi.git
[github-187] Add length validation for Excel DataValidations that are list literals. Thanks to Leo Webb. This closes #187
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1880727 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
eb850f02de
commit
a8d90aba91
Binary file not shown.
Binary file not shown.
|
@ -30,12 +30,14 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.STDataValidationOpera
|
||||||
public class XSSFDataValidationConstraint implements DataValidationConstraint {
|
public class XSSFDataValidationConstraint implements DataValidationConstraint {
|
||||||
/**
|
/**
|
||||||
* Excel validation constraints with static lists are delimited with optional whitespace and the Windows List Separator,
|
* Excel validation constraints with static lists are delimited with optional whitespace and the Windows List Separator,
|
||||||
* which is typically comma, but can be changed by users. POI will just assume comma.
|
* which is typically comma, but can be changed by users. POI will just assume comma.
|
||||||
|
* In addition, Excel validation with static lists has a maximum size of 255 characters, including separators and excluding quotes.
|
||||||
*/
|
*/
|
||||||
private static final String LIST_SEPARATOR = ",";
|
private static final String LIST_SEPARATOR = ",";
|
||||||
private static final Pattern LIST_SPLIT_REGEX = Pattern.compile("\\s*" + LIST_SEPARATOR + "\\s*");
|
private static final Pattern LIST_SPLIT_REGEX = Pattern.compile("\\s*" + LIST_SEPARATOR + "\\s*");
|
||||||
private static final String QUOTE = "\"";
|
private static final String QUOTE = "\"";
|
||||||
|
private static final int MAX_EXPLICIT_LIST_LENGTH = 257;
|
||||||
|
|
||||||
private String formula1;
|
private String formula1;
|
||||||
private String formula2;
|
private String formula2;
|
||||||
private int validationType = -1;
|
private int validationType = -1;
|
||||||
|
@ -204,6 +206,9 @@ public class XSSFDataValidationConstraint implements DataValidationConstraint {
|
||||||
if (isFormulaEmpty(formula1)) {
|
if (isFormulaEmpty(formula1)) {
|
||||||
throw new IllegalArgumentException("A valid formula or a list of values must be specified for list validation.");
|
throw new IllegalArgumentException("A valid formula or a list of values must be specified for list validation.");
|
||||||
}
|
}
|
||||||
|
if(formula1.length() > MAX_EXPLICIT_LIST_LENGTH) {
|
||||||
|
throw new IllegalArgumentException("A valid formula or a list of values must be less than or equal to 255 characters (including separators).");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if( isFormulaEmpty(formula1) ) {
|
if( isFormulaEmpty(formula1) ) {
|
||||||
throw new IllegalArgumentException("Formula is not specified. Formula is required for all validation types except explicit list validation.");
|
throw new IllegalArgumentException("Formula is not specified. Formula is required for all validation types except explicit list validation.");
|
||||||
|
|
|
@ -18,11 +18,18 @@ package org.apache.poi.xssf.usermodel;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import org.apache.poi.ss.formula.DataValidationEvaluator;
|
||||||
import org.apache.poi.ss.usermodel.DataValidationConstraint;
|
import org.apache.poi.ss.usermodel.DataValidationConstraint;
|
||||||
import org.apache.poi.ss.usermodel.DataValidationConstraint.ValidationType;
|
import org.apache.poi.ss.usermodel.DataValidationConstraint.ValidationType;
|
||||||
|
import org.apache.poi.xssf.XSSFTestDataSamples;
|
||||||
import org.apache.poi.ss.usermodel.DataValidationConstraint.OperatorType;
|
import org.apache.poi.ss.usermodel.DataValidationConstraint.OperatorType;
|
||||||
|
import org.apache.poi.ss.util.CellReference;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
public class TestXSSFDataValidationConstraint {
|
public class TestXSSFDataValidationConstraint {
|
||||||
static final int listType = ValidationType.LIST;
|
static final int listType = ValidationType.LIST;
|
||||||
static final int ignoredType = OperatorType.IGNORED;
|
static final int ignoredType = OperatorType.IGNORED;
|
||||||
|
@ -51,6 +58,20 @@ public class TestXSSFDataValidationConstraint {
|
||||||
assertEquals(literal.replace(" ", ""), constraint.getFormula1());
|
assertEquals(literal.replace(" ", ""), constraint.getFormula1());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void listLiteralsGreaterThan255CharactersThrows() {
|
||||||
|
String[] literal = IntStream.range(0, 129).mapToObj(i -> "a").toArray(String[]::new);
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> new XSSFDataValidationConstraint(literal));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void dataValidationListLiteralTooLongFromFile() {
|
||||||
|
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("DataValidationListTooLong.xlsx");
|
||||||
|
XSSFFormulaEvaluator fEval = wb.getCreationHelper().createFormulaEvaluator();
|
||||||
|
DataValidationEvaluator dvEval = new DataValidationEvaluator(wb, fEval);
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> dvEval.getValidationValuesForCell(new CellReference("Sheet0!A1")));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void rangeReference() {
|
public void rangeReference() {
|
||||||
// (unnamed range) reference list
|
// (unnamed range) reference list
|
||||||
|
|
Binary file not shown.
Loading…
Reference in New Issue