Author: centic
Date: Fri Apr 24 20:58:38 2020
New Revision: 1876948

URL: http://svn.apache.org/viewvc?rev=1876948&view=rev
Log:
Bug 63294: Add some more methods to allow to use CellType everywhere

Modified:
    poi/trunk/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java
    poi/trunk/src/java/org/apache/poi/hssf/record/FormulaRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/record/FormulaSpecialCachedValue.java
    poi/trunk/src/java/org/apache/poi/hssf/record/OldFormulaRecord.java
    poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
    poi/trunk/src/testcases/org/apache/poi/hssf/record/TestFormulaRecord.java

Modified: 
poi/trunk/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java?rev=1876948&r1=1876947&r2=1876948&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java 
(original)
+++ poi/trunk/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java Fri 
Apr 24 20:58:38 2020
@@ -50,10 +50,10 @@ import org.apache.poi.util.IOUtils;
 
 /**
  * A text extractor for old Excel files, which are too old for
- *  HSSFWorkbook to handle. This includes Excel 95, and very old 
+ *  HSSFWorkbook to handle. This includes Excel 95, and very old
  *  (pre-OLE2) Excel files, such as Excel 4 files.
  * <p>
- * Returns much (but not all) of the textual content of the file, 
+ * Returns much (but not all) of the textual content of the file,
  *  suitable for indexing by something like Apache Lucene, or used
  *  by Apache Tika, but not really intended for display to the user.
  * </p>
@@ -113,7 +113,7 @@ public class OldExcelExtractor implement
     }
 
     private void open(InputStream biffStream) throws IOException {
-        BufferedInputStream bis = (biffStream instanceof BufferedInputStream) 
+        BufferedInputStream bis = (biffStream instanceof BufferedInputStream)
             ? (BufferedInputStream)biffStream
             : new BufferedInputStream(biffStream, 8);
 
@@ -150,7 +150,7 @@ public class OldExcelExtractor implement
         if (book == null) {
             throw new IOException("No Excel 5/95 Book stream found");
         }
-        
+
         ris = new RecordInputStream(directory.createDocumentInputStream(book));
         prepare();
     }
@@ -165,13 +165,13 @@ public class OldExcelExtractor implement
         System.out.println(extractor.getText());
         extractor.close();
     }
-    
+
     private void prepare() {
         if (! ris.hasNextRecord()) {
             throw new IllegalArgumentException("File contains no records!");
-        } 
+        }
         ris.nextRecord();
-        
+
         // Work out what version we're dealing with
         int bofSid = ris.getSid();
         switch (bofSid) {
@@ -188,9 +188,9 @@ public class OldExcelExtractor implement
                 biffVersion = 5;
                 break;
             default:
-                throw new IllegalArgumentException("File does not begin with a 
BOF, found sid of " + bofSid); 
+                throw new IllegalArgumentException("File does not begin with a 
BOF, found sid of " + bofSid);
         }
-        
+
         // Get the type
         BOFRecord bof = new BOFRecord(ris);
         fileType = bof.getType();
@@ -198,18 +198,18 @@ public class OldExcelExtractor implement
 
     /**
      * The Biff version, largely corresponding to the Excel version
-     * 
+     *
      * @return the Biff version
      */
     public int getBiffVersion() {
         return biffVersion;
     }
-    
+
     /**
      * The kind of the file, one of {@link BOFRecord#TYPE_WORKSHEET},
      *  {@link BOFRecord#TYPE_CHART}, {@link BOFRecord#TYPE_EXCEL_4_MACRO}
      *  or {@link BOFRecord#TYPE_WORKSPACE_FILE}
-     * 
+     *
      * @return the file type
      */
     public int getFileType() {
@@ -219,12 +219,12 @@ public class OldExcelExtractor implement
     /**
      * Retrieves the text contents of the file, as best we can
      *  for these old file formats
-     * 
+     *
      * @return the text contents of the file
      */
     public String getText() {
         StringBuilder text = new StringBuilder();
-        
+
         // To track formats and encodings
         CodepageRecord codepage = null;
         // TODO track the XFs and Format Strings
@@ -245,7 +245,7 @@ public class OldExcelExtractor implement
                     text.append(shr.getSheetname());
                     text.append('\n');
                     break;
-            
+
                 case OldLabelRecord.biff2_sid:
                 case OldLabelRecord.biff345_sid:
                     OldLabelRecord lr = new OldLabelRecord(ris);
@@ -260,7 +260,7 @@ public class OldExcelExtractor implement
                     text.append(sr.getString());
                     text.append('\n');
                     break;
-                    
+
                 case NumberRecord.sid:
                     NumberRecord nr = new NumberRecord(ris);
                     handleNumericCell(text, nr.getValue());
@@ -271,12 +271,12 @@ public class OldExcelExtractor implement
                     // Biff 2 and 5+ share the same SID, due to a bug...
                     if (biffVersion == 5) {
                         FormulaRecord fr = new FormulaRecord(ris);
-                        if (fr.getCachedResultType() == 
CellType.NUMERIC.getCode()) {
+                        if (fr.getCachedResultTypeEnum() == CellType.NUMERIC) {
                             handleNumericCell(text, fr.getValue());
                         }
                     } else {
                         OldFormulaRecord fr = new OldFormulaRecord(ris);
-                        if (fr.getCachedResultType() == 
CellType.NUMERIC.getCode()) {
+                        if (fr.getCachedResultTypeEnum() == CellType.NUMERIC) {
                             handleNumericCell(text, fr.getValue());
                         }
                     }
@@ -285,11 +285,11 @@ public class OldExcelExtractor implement
                     RKRecord rr = new RKRecord(ris);
                     handleNumericCell(text, rr.getRKNumber());
                     break;
-                    
+
                 case CodepageRecord.sid:
                     codepage = new CodepageRecord(ris);
                     break;
-                    
+
                 default:
                     ris.readFully(IOUtils.safelyAllocate(ris.remaining(), 
MAX_RECORD_LENGTH));
             }

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/FormulaRecord.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/FormulaRecord.java?rev=1876948&r1=1876947&r2=1876948&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/FormulaRecord.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/FormulaRecord.java Fri Apr 24 
20:58:38 2020
@@ -120,6 +120,10 @@ public final class FormulaRecord extends
                                specialCachedValue.getTypeCode() == 
FormulaSpecialCachedValue.STRING;
        }
 
+       /**
+        * @deprecated POI 4.1.3, will be removed in 5.0, use 
getCachedResultTypeEnum until switch to enum is fully done
+        */
+       @Deprecated
        public int getCachedResultType() {
                if (specialCachedValue == null) {
                        return CellType.NUMERIC.getCode();
@@ -127,6 +131,18 @@ public final class FormulaRecord extends
                return specialCachedValue.getValueType();
        }
 
+       /**
+        * Returns the type of the cached result
+        * @return A CellType
+        * @since POI 4.1.3
+        */
+       public CellType getCachedResultTypeEnum() {
+               if (specialCachedValue == null) {
+                       return CellType.NUMERIC;
+               }
+               return specialCachedValue.getValueTypeEnum();
+       }
+
        public boolean getCachedBooleanValue() {
                return specialCachedValue.getBooleanValue();
        }

Modified: 
poi/trunk/src/java/org/apache/poi/hssf/record/FormulaSpecialCachedValue.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/FormulaSpecialCachedValue.java?rev=1876948&r1=1876947&r2=1876948&view=diff
==============================================================================
--- 
poi/trunk/src/java/org/apache/poi/hssf/record/FormulaSpecialCachedValue.java 
(original)
+++ 
poi/trunk/src/java/org/apache/poi/hssf/record/FormulaSpecialCachedValue.java 
Fri Apr 24 20:58:38 2020
@@ -144,6 +144,10 @@ public final class FormulaSpecialCachedV
         return getClass().getName() + '[' + formatValue() + ']';
     }
 
+    /**
+     * @deprecated POI 4.1.3, will be removed in 5.0, use getValueTypeEnum 
until switch to enum is fully done
+     */
+    @Deprecated
     public int getValueType() {
         int typeCode = getTypeCode();
         switch (typeCode) {
@@ -157,6 +161,25 @@ public final class FormulaSpecialCachedV
         }
         throw new IllegalStateException("Unexpected type id (" + typeCode + 
")");
     }
+
+    /**
+     * Returns the type of the cached value
+     * @return A CellType
+     * @since POI 4.1.3
+     */
+    public CellType getValueTypeEnum() {
+        int typeCode = getTypeCode();
+        switch (typeCode) {
+            case EMPTY: // is this correct?
+            case STRING:
+                return CellType.STRING;
+            case BOOLEAN:
+                return CellType.BOOLEAN;
+            case ERROR_CODE:
+                return CellType.ERROR;
+        }
+        throw new IllegalStateException("Unexpected type id (" + typeCode + 
")");
+    }
 
     public boolean getBooleanValue() {
         if (getTypeCode() != BOOLEAN) {

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/OldFormulaRecord.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/OldFormulaRecord.java?rev=1876948&r1=1876947&r2=1876948&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/OldFormulaRecord.java 
(original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/OldFormulaRecord.java Fri Apr 
24 20:58:38 2020
@@ -64,6 +64,10 @@ public final class OldFormulaRecord exte
         field_6_parsed_expr = Formula.read(expression_len, ris, 
nBytesAvailable);
     }
 
+    /**
+     * @deprecated POI 4.1.3, will be removed in 5.0, use 
getCachedResultTypeEnum until switch to enum is fully done
+     */
+    @Deprecated
     public int getCachedResultType() {
         if (specialCachedValue == null) {
             return CellType.NUMERIC.getCode();
@@ -71,6 +75,18 @@ public final class OldFormulaRecord exte
         return specialCachedValue.getValueType();
     }
 
+    /**
+     * Returns the type of the cached result
+     * @return A CellType
+     * @since POI 4.1.3
+     */
+    public CellType getCachedResultTypeEnum() {
+        if (specialCachedValue == null) {
+            return CellType.NUMERIC;
+        }
+        return specialCachedValue.getValueTypeEnum();
+    }
+
     public boolean getCachedBooleanValue() {
         return specialCachedValue.getBooleanValue();
     }

Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java?rev=1876948&r1=1876947&r2=1876948&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFCell.java Fri Apr 24 
20:58:38 2020
@@ -653,7 +653,7 @@ public class HSSFCell extends CellBase {
         return new IllegalStateException(msg);
     }
     private static void checkFormulaCachedValueType(CellType expectedTypeCode, 
FormulaRecord fr) {
-        CellType cachedValueType = CellType.forInt(fr.getCachedResultType());
+        CellType cachedValueType = fr.getCachedResultTypeEnum();
         if (cachedValueType != expectedTypeCode) {
             throw typeMismatch(expectedTypeCode, cachedValueType, true);
         }
@@ -879,7 +879,7 @@ public class HSSFCell extends CellBase {
         }
         FormulaRecordAggregate fra = ((FormulaRecordAggregate)_record);
         FormulaRecord fr = fra.getFormulaRecord();
-        switch (CellType.forInt(fr.getCachedResultType())) {
+        switch (fr.getCachedResultTypeEnum()) {
             case BOOLEAN:
                 return fr.getCachedBooleanValue() ? "TRUE" : "FALSE";
             case STRING:
@@ -1174,8 +1174,8 @@ public class HSSFCell extends CellBase {
         if (_cellType != CellType.FORMULA) {
             throw new IllegalStateException("Only formula cells have cached 
results");
         }
-        int code = 
((FormulaRecordAggregate)_record).getFormulaRecord().getCachedResultType();
-        return CellType.forInt(code);
+
+        return 
((FormulaRecordAggregate)_record).getFormulaRecord().getCachedResultTypeEnum();
     }
 
     /**

Modified: 
poi/trunk/src/testcases/org/apache/poi/hssf/record/TestFormulaRecord.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/TestFormulaRecord.java?rev=1876948&r1=1876947&r2=1876948&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/record/TestFormulaRecord.java 
(original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/record/TestFormulaRecord.java 
Fri Apr 24 20:58:38 2020
@@ -85,7 +85,9 @@ public final class TestFormulaRecord {
                FormulaRecord record = new 
FormulaRecord(TestcaseRecordInputStream.create(FormulaRecord.sid, formulaByte));
                assertEquals("Row", 0, record.getRow());
                assertEquals("Column", 0, record.getColumn());
+               //noinspection deprecation
                assertEquals(CellType.ERROR.getCode(), 
record.getCachedResultType());
+               assertEquals(CellType.ERROR, record.getCachedResultTypeEnum());
 
                byte[] output = record.serialize();
                assertEquals("Output size", 33, output.length); //includes 
sid+recordlength



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to