spliterator() {
return _entries.spliterator();
}
-
- /* ********** END begin implementation of POIFSViewable ********** */
-} // end public class DirectoryNode
-
+}
diff --git a/poi/src/main/java/org/apache/poi/ss/formula/DataValidationEvaluator.java b/poi/src/main/java/org/apache/poi/ss/formula/DataValidationEvaluator.java
index dcd98bb1dc..b2e4bc1e06 100644
--- a/poi/src/main/java/org/apache/poi/ss/formula/DataValidationEvaluator.java
+++ b/poi/src/main/java/org/apache/poi/ss/formula/DataValidationEvaluator.java
@@ -47,20 +47,20 @@ import org.apache.poi.util.StringUtil;
/**
* Evaluates Data Validation constraints.
*
- * For performance reasons, this class keeps a cache of all previously retrieved {@link DataValidation} instances.
- * Be sure to call {@link #clearAllCachedValues()} if any workbook validation definitions are
+ * For performance reasons, this class keeps a cache of all previously retrieved {@link DataValidation} instances.
+ * Be sure to call {@link #clearAllCachedValues()} if any workbook validation definitions are
* added, modified, or deleted.
*
* Changing cell values should be fine, as long as the corresponding {@link WorkbookEvaluator#clearAllCachedResultValues()}
* is called as well.
- *
+ *
*/
public class DataValidationEvaluator {
/**
* Expensive to compute, so cache them as they are retrieved.
*
- * Sheets don't implement equals, and since its an interface,
+ * Sheets don't implement equals, and since its an interface,
* there's no guarantee instances won't be recreated on the fly by some implementation.
* So we use sheet name.
*/
@@ -79,14 +79,14 @@ public class DataValidationEvaluator {
this.workbook = wb;
this.workbookEvaluator = provider._getWorkbookEvaluator();
}
-
+
/**
* @return evaluator
*/
protected WorkbookEvaluator getWorkbookEvaluator() {
return workbookEvaluator;
}
-
+
/**
* Call this whenever validation structures change,
* so future results stay in sync with the Workbook state.
@@ -94,7 +94,7 @@ public class DataValidationEvaluator {
public void clearAllCachedValues() {
validations.clear();
}
-
+
/**
* Lazy load validations by sheet, since reading the CT* types is expensive
*
@@ -109,14 +109,14 @@ public class DataValidationEvaluator {
}
return dvs;
}
-
+
/**
* Finds and returns the {@link DataValidation} for the cell, if there is
* one. Lookup is based on the first match from
* {@link DataValidation#getRegions()} for the cell's sheet. DataValidation
* regions must be in the same sheet as the DataValidation. Allowed values
* expressions may reference other sheets, however.
- *
+ *
* @param cell reference to check - use this in case the cell does not actually exist yet
* @return the DataValidation applicable to the given cell, or null if no
* validation applies
@@ -132,7 +132,7 @@ public class DataValidationEvaluator {
* {@link DataValidation#getRegions()} for the cell's sheet. DataValidation
* regions must be in the same sheet as the DataValidation. Allowed values
* expressions may reference other sheets, however.
- *
+ *
* @param cell reference to check
* @return the DataValidationContext applicable to the given cell, or null if no
* validation applies
@@ -166,7 +166,7 @@ public class DataValidationEvaluator {
* This method could throw an exception if the validation type is not LIST,
* but since this method is mostly useful in UI contexts, null seems the
* easier path.
- *
+ *
* @param cell reference to check - use this in case the cell does not actually exist yet
* @return returns an unmodifiable {@link List} of {@link ValueEval}s if applicable, or
* null
@@ -175,7 +175,7 @@ public class DataValidationEvaluator {
DataValidationContext context = getValidationContextForCell(cell);
if (context == null) return null;
-
+
return getValidationValuesForConstraint(context);
}
@@ -186,11 +186,11 @@ public class DataValidationEvaluator {
protected static List getValidationValuesForConstraint(DataValidationContext context) {
final DataValidationConstraint val = context.getValidation().getValidationConstraint();
if (val.getValidationType() != ValidationType.LIST) return null;
-
+
String formula = val.getFormula1();
-
+
final List values = new ArrayList<>();
-
+
if (val.getExplicitListValues() != null && val.getExplicitListValues().length > 0) {
// assumes parsing interprets the overloaded property right for XSSF
for (String s : val.getExplicitListValues()) {
@@ -221,7 +221,7 @@ public class DataValidationEvaluator {
* Note that to properly apply some validations, care must be taken to
* offset the base validation formula by the relative position of the
* current cell, or the wrong value is checked.
- *
+ *
* @param cellRef The reference of the cell to evaluate
* @return true if the cell has no validation or the cell value passes the
* defined validation, false if it fails
@@ -230,23 +230,23 @@ public class DataValidationEvaluator {
final DataValidationContext context = getValidationContextForCell(cellRef);
if (context == null) return true;
-
+
final Cell cell = SheetUtil.getCell(workbook.getSheet(cellRef.getSheetName()), cellRef.getRow(), cellRef.getCol());
-
+
// now we can validate the cell
-
+
// if empty, return not allowed flag
if ( cell == null
- || isType(cell, CellType.BLANK)
- || (isType(cell,CellType.STRING)
+ || isType(cell, CellType.BLANK)
+ || (isType(cell,CellType.STRING)
&& (cell.getStringCellValue() == null || cell.getStringCellValue().isEmpty())
)
) {
return context.getValidation().getEmptyCellAllowed();
}
-
+
// cell has a value
-
+
return ValidationEnum.isValid(cell, context);
}
@@ -259,18 +259,18 @@ public class DataValidationEvaluator {
*/
public static boolean isType(Cell cell, CellType type) {
final CellType cellType = cell.getCellType();
- return cellType == type
- || (cellType == CellType.FORMULA
+ return cellType == type
+ || (cellType == CellType.FORMULA
&& cell.getCachedFormulaResultType() == type
);
}
-
+
/**
* Not calling it ValidationType to avoid confusion for now with DataValidationConstraint.ValidationType.
* Definition order matches OOXML type ID indexes
*/
- public static enum ValidationEnum {
+ public enum ValidationEnum {
ANY {
public boolean isValidValue(Cell cell, DataValidationContext context) {
return true;
@@ -291,11 +291,11 @@ public class DataValidationEvaluator {
public boolean isValidValue(Cell cell, DataValidationContext context) {
final List valueList = getValidationValuesForConstraint(context);
if (valueList == null) return true; // special case
-
+
// compare cell value to each item
for (ValueEval listVal : valueList) {
ValueEval comp = listVal instanceof RefEval ? ((RefEval) listVal).getInnerValueEval(context.getSheetIndex()) : listVal;
-
+
// any value is valid if the list contains a blank value per Excel help
if (comp instanceof BlankEval) return true;
if (comp instanceof ErrorEval) continue; // nothing to check
@@ -310,7 +310,7 @@ public class DataValidationEvaluator {
// could this have trouble with double precision/rounding errors and date/time values?
// do we need to allow a "close enough" double fractional range?
// I see 17 digits after the decimal separator in XSSF files, and for time values,
- // there are sometimes discrepancies in the final decimal place.
+ // there are sometimes discrepancies in the final decimal place.
// I don't have a validation test case yet though. - GW
if (isType(cell, CellType.NUMERIC) && ((NumberEval) comp).getNumberValue() == cell.getNumericCellValue()) {
return true;
@@ -319,7 +319,7 @@ public class DataValidationEvaluator {
}
}
if (comp instanceof StringEval) {
- // interestingly, in testing, a validation value of the string "TRUE" or "true"
+ // interestingly, in testing, a validation value of the string "TRUE" or "true"
// did not match a boolean cell value of TRUE - so apparently cell type matters
// also, Excel validation is case insensitive - "true" is valid for the list value "TRUE"
if (isType(cell, CellType.STRING) && ((StringEval) comp).getStringValue().equalsIgnoreCase(cell.getStringCellValue())) {
@@ -368,16 +368,16 @@ public class DataValidationEvaluator {
}
},
;
-
+
public boolean isValidValue(Cell cell, DataValidationContext context) {
return isValidNumericCell(cell, context);
}
-
+
/**
* Uses the cell value, which may be the cached formula result value.
* We won't re-evaluate cells here. This validation would be after the cell value was updated externally.
- * Excel allows invalid values through methods like copy/paste, and only validates them when the user
- * interactively edits the cell.
+ * Excel allows invalid values through methods like copy/paste, and only validates them when the user
+ * interactively edits the cell.
* @return if the cell is a valid numeric cell for the validation or not
*/
protected boolean isValidNumericCell(Cell cell, DataValidationContext context) {
@@ -394,12 +394,12 @@ public class DataValidationEvaluator {
try {
Double t1 = evalOrConstant(context.getFormula1(), context);
// per Excel, a blank value for a numeric validation constraint formula validates true
- if (t1 == null) return true;
+ if (t1 == null) return true;
Double t2 = null;
if (context.getOperator() == OperatorType.BETWEEN || context.getOperator() == OperatorType.NOT_BETWEEN) {
t2 = evalOrConstant(context.getFormula2(), context);
// per Excel, a blank value for a numeric validation constraint formula validates true
- if (t2 == null) return true;
+ if (t2 == null) return true;
}
return OperatorEnum.values()[context.getOperator()].isValid(value, t1, t2);
} catch (NumberFormatException e) {
@@ -407,7 +407,7 @@ public class DataValidationEvaluator {
return false;
}
}
-
+
/**
* Evaluate a numeric formula value as either a constant or numeric expression.
* Note that Excel treats validations with constraint formulas that evaluate to null as valid,
@@ -435,12 +435,12 @@ public class DataValidationEvaluator {
if (eval instanceof StringEval) {
final String value = ((StringEval) eval).getStringValue();
if (StringUtil.isBlank(value)) return null;
- // try to parse the cell value as a double and return it
+ // try to parse the cell value as a double and return it
return Double.valueOf(value);
}
throw new NumberFormatException("Formula '" + formula + "' evaluates to something other than a number");
}
-
+
/**
* Validates against the type defined in context, as an index of the enum values array.
* @param cell Cell to check validity of
@@ -451,14 +451,14 @@ public class DataValidationEvaluator {
public static boolean isValid(Cell cell, DataValidationContext context) {
return values()[context.getValidation().getValidationConstraint().getValidationType()].isValidValue(cell, context);
}
-
+
}
-
+
/**
* Not calling it OperatorType to avoid confusion for now with DataValidationConstraint.OperatorType.
* Definition order matches OOXML type ID indexes
*/
- public static enum OperatorEnum {
+ public enum OperatorEnum {
BETWEEN {
public boolean isValid(Double cellValue, Double v1, Double v2) {
return cellValue.compareTo(v1) >= 0 && cellValue.compareTo(v2) <= 0;
@@ -500,9 +500,9 @@ public class DataValidationEvaluator {
}
},
;
-
+
public static final OperatorEnum IGNORED = BETWEEN;
-
+
/**
* Evaluates comparison using operator instance rules
* @param cellValue won't be null, assumption is previous checks handled that
@@ -512,7 +512,7 @@ public class DataValidationEvaluator {
*/
public abstract boolean isValid(Double cellValue, Double v1, Double v2);
}
-
+
/**
* This class organizes and encapsulates all the pieces of information related to a single
* data validation configuration for a single cell. It cleanly separates the validation region,
@@ -524,7 +524,7 @@ public class DataValidationEvaluator {
private final DataValidationEvaluator dve;
private final CellRangeAddressBase region;
private final CellReference target;
-
+
/**
* Populate the context with the necessary values.
*/
@@ -558,30 +558,30 @@ public class DataValidationEvaluator {
public CellReference getTarget() {
return target;
}
-
+
public int getOffsetColumns() {
return target.getCol() - region.getFirstColumn();
}
-
+
public int getOffsetRows() {
return target.getRow() - region.getFirstRow();
}
-
+
public int getSheetIndex() {
return dve.getWorkbookEvaluator().getSheetIndex(target.getSheetName());
}
-
+
public String getFormula1() {
return dv.getValidationConstraint().getFormula1();
}
-
+
public String getFormula2() {
return dv.getValidationConstraint().getFormula2();
}
-
+
public int getOperator() {
return dv.getValidationConstraint().getOperator();
}
-
+
}
}
diff --git a/poi/src/test/java/org/apache/poi/hssf/record/TestRecordInputStream.java b/poi/src/test/java/org/apache/poi/hssf/record/TestRecordInputStream.java
index 8622230338..7485a9738a 100644
--- a/poi/src/test/java/org/apache/poi/hssf/record/TestRecordInputStream.java
+++ b/poi/src/test/java/org/apache/poi/hssf/record/TestRecordInputStream.java
@@ -20,6 +20,8 @@ package org.apache.poi.hssf.record;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import java.io.IOException;
+
import org.apache.poi.util.HexRead;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
@@ -53,9 +55,8 @@ final class TestRecordInputStream {
+ "1A 59 00 8A 9E 8A " // 3 uncompressed unicode chars
;
-
@Test
- void testChangeOfCompressionFlag_bug25866() {
+ void testChangeOfCompressionFlag_bug25866() throws IOException {
byte[] changingFlagSimpleData = HexRead.readFromString(""
+ "AA AA " // fake SID
+ "06 00 " // first rec len 6
@@ -66,10 +67,13 @@ final class TestRecordInputStream {
// bug 45866 - compressByte in continue records must be 1 while reading unicode LE string
String actual = in.readUnicodeLEString(18);
assertEquals("\u591A\u8A00\u8A9E - Multilingual", actual);
+
+ in.mark(10);
+ in.reset();
}
@Test
- void testChangeFromUnCompressedToCompressed() {
+ void testChangeFromUnCompressedToCompressed() throws IOException {
byte[] changingFlagSimpleData = HexRead.readFromString(""
+ "AA AA " // fake SID
+ "0F 00 " // first rec len 15
@@ -78,10 +82,13 @@ final class TestRecordInputStream {
RecordInputStream in = TestcaseRecordInputStream.create(changingFlagSimpleData);
String actual = in.readCompressedUnicode(18);
assertEquals("Multilingual - \u591A\u8A00\u8A9E", actual);
+
+ in.mark(10);
+ in.reset();
}
@Test
- void testReadString() {
+ void testReadString() throws IOException {
byte[] changingFlagFullData = HexRead.readFromString(""
+ "AA AA " // fake SID
+ "12 00 " // first rec len 18 (15 + next 3 bytes)
@@ -92,6 +99,9 @@ final class TestRecordInputStream {
RecordInputStream in = TestcaseRecordInputStream.create(changingFlagFullData);
String actual = in.readString();
assertEquals("Multilingual - \u591A\u8A00\u8A9E", actual);
+
+ in.mark(10);
+ in.reset();
}
@ParameterizedTest
diff --git a/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestWorkbook.java b/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestWorkbook.java
index 7e16d69df6..ceea6b1b88 100644
--- a/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestWorkbook.java
+++ b/poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestWorkbook.java
@@ -537,6 +537,7 @@ public abstract class BaseTestWorkbook {
/**
* Tests that all the unicode capable string fields can be set, written and then read back
*/
+ @SuppressWarnings("UnnecessaryUnicodeEscape")
@Test
protected void unicodeInAll() throws IOException {
try (Workbook wb1 = _testDataProvider.createWorkbook()) {
@@ -850,7 +851,7 @@ public abstract class BaseTestWorkbook {
wb.removeSheetAt(0);
wb.removeSheetAt(2);
- // ensure that sheets are moved up and removed sheets are not found any more
+ // ensure that sheets are moved up and removed sheets are not found anymore
assertEquals(-1, wb.getSheetIndex(sheet1));
assertEquals(0, wb.getSheetIndex(sheet2));
assertEquals(1, wb.getSheetIndex(sheet3));
@@ -936,4 +937,20 @@ public abstract class BaseTestWorkbook {
);
}
}
+
+ @Test
+ void testSheetNameDifferOnlyLowercaseUppercase() throws IOException {
+ try (Workbook wb = _testDataProvider.createWorkbook()) {
+ wb.createSheet("abc");
+ assertEquals(1, wb.getNumberOfSheets());
+
+ assertThrows(IllegalArgumentException.class,
+ () -> wb.createSheet("ABC"));
+ assertEquals(1, wb.getNumberOfSheets());
+
+ Sheet sheet = wb.getSheet("abc");
+ assertNotNull(sheet);
+ assertEquals("abc", sheet.getSheetName());
+ }
+ }
}