]> source.dussan.org Git - poi.git/commitdiff
Bug 65312: Make RecordType.byName work for type "formula"
authorDominik Stadler <centic@apache.org>
Thu, 30 Dec 2021 23:03:40 +0000 (23:03 +0000)
committerDominik Stadler <centic@apache.org>
Thu, 30 Dec 2021 23:03:40 +0000 (23:03 +0000)
Also avoid NullPointerException when parsing unexpected record types
and check for bounds in byId()

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1896553 13f79535-47bb-0310-9956-ffa450edef68

poi/src/main/java/org/apache/poi/ss/usermodel/ConditionalFormattingThreshold.java
poi/src/test/java/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java

index 69cc1887c58d98eba5a6856f63b6e4d6f2d10a2c..00b8f89ddc0b6cb2ac20c64eb1739413696f2450 100644 (file)
@@ -19,7 +19,6 @@
 
 package org.apache.poi.ss.usermodel;
 
-
 /**
  * The Threshold / CFVO / Conditional Formatting Value Object.
  * <p>This defines how to calculate the ranges for a conditional
@@ -28,17 +27,27 @@ package org.apache.poi.ss.usermodel;
  */
 public interface ConditionalFormattingThreshold {
     enum RangeType {
+        // IDs should match the values documented
+        // in the spec for HSSF, e.g. at
+        // https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-xls/3cc68999-c2fc-4a57-92a5-94c0720779e9
+
         /** Number / Parameter */
         NUMBER(1, "num"),
+
         /** The minimum value from the range */
         MIN(2, "min"),
+
         /** The maximum value from the range */
         MAX(3, "max"),
+
         /** Percent of the way from the mi to the max value in the range */
         PERCENT(4, "percent"),
+
         /** The minimum value of the cell that is in X percentile of the range */
         PERCENTILE(5, "percentile"),
+
         UNALLOCATED(6, null),
+
         /** Formula result */
         FORMULA(7, "formula");
 
@@ -52,12 +61,23 @@ public interface ConditionalFormattingThreshold {
         }
 
         public static RangeType byId(int id) {
+            // id is mapped to ordinal()+1
+            if (id <= 0 || id > values().length) {
+                return null;
+            }
+
             return values()[id-1]; // 1-based IDs
         }
+
         public static RangeType byName(String name) {
             for (RangeType t : values()) {
-                if (t.name.equals(name)) return t;
+                if (t.name == null && name == null) {
+                    return t;
+                } else if (t.name != null && t.name.equals(name)) {
+                    return t;
+                }
             }
+
             return null;
         }
 
index eec63048e65f751be9d89a42cc554d61646a9a7f..e58138c663350442f8f765334b439fda0dfc2e0d 100644 (file)
@@ -1385,4 +1385,19 @@ public abstract class BaseTestConditionalFormatting {
             }
         }
     }
+
+    @Test
+    void testRangeType() {
+        for (RangeType rangeType : RangeType.values()) {
+            assertEquals(rangeType, RangeType.byName(rangeType.name));
+            assertEquals(rangeType, RangeType.byId(rangeType.id));
+        }
+
+        assertEquals(RangeType.UNALLOCATED, RangeType.byName(null));
+        assertNull(RangeType.byName("some other name"));
+
+        assertNull(RangeType.byId(-1));
+        assertNull(RangeType.byId(0));
+        assertNull(RangeType.byId(99));
+    }
 }
\ No newline at end of file