poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1658190 - in /poi/trunk/src: java/org/apache/poi/ss/formula/eval/ErrorEval.java java/org/apache/poi/ss/usermodel/FormulaError.java ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
Date Sun, 08 Feb 2015 15:37:15 GMT
Author: nick
Date: Sun Feb  8 15:37:14 2015
New Revision: 1658190

URL: http://svn.apache.org/r1658190
Log:
Finish converting ErrorEval to only use the FormulaError constants, and then finish unit test
for 57535

Modified:
    poi/trunk/src/java/org/apache/poi/ss/formula/eval/ErrorEval.java
    poi/trunk/src/java/org/apache/poi/ss/usermodel/FormulaError.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java

Modified: poi/trunk/src/java/org/apache/poi/ss/formula/eval/ErrorEval.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/eval/ErrorEval.java?rev=1658190&r1=1658189&r2=1658190&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/eval/ErrorEval.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/eval/ErrorEval.java Sun Feb  8 15:37:14 2015
@@ -17,13 +17,17 @@
 
 package org.apache.poi.ss.formula.eval;
 
-import org.apache.poi.ss.usermodel.ErrorConstants;
+import java.util.HashMap;
+import java.util.Map;
+
 import org.apache.poi.ss.usermodel.FormulaError;
 
 /**
  * Evaluations for formula errors
  */
 public final class ErrorEval implements ValueEval {
+    private static final Map<FormulaError,ErrorEval> evals = new HashMap<FormulaError,
ErrorEval>();
+    
     /** <b>#NULL!</b>  - Intersection of two cell ranges is empty */
     public static final ErrorEval NULL_INTERSECTION = new ErrorEval(FormulaError.NULL);
     /** <b>#DIV/0!</b> - Division by zero */
@@ -40,29 +44,23 @@ public final class ErrorEval implements
     public static final ErrorEval NA = new ErrorEval(FormulaError.NA);
 
     // POI internal error codes
-    private static final int CIRCULAR_REF_ERROR_CODE = 0xFFFFFFC4;
-    private static final int FUNCTION_NOT_IMPLEMENTED_CODE = 0xFFFFFFE2;
+    public static final ErrorEval FUNCTION_NOT_IMPLEMENTED = new ErrorEval(FormulaError.FUNCTION_NOT_IMPLEMENTED);
 
     // Note - Excel does not seem to represent this condition with an error code
-    public static final ErrorEval CIRCULAR_REF_ERROR = new ErrorEval(CIRCULAR_REF_ERROR_CODE);
+    public static final ErrorEval CIRCULAR_REF_ERROR = new ErrorEval(FormulaError.CIRCULAR_REF);
 
     /**
      * Translates an Excel internal error code into the corresponding POI ErrorEval instance
      * @param errorCode
      */
     public static ErrorEval valueOf(int errorCode) {
-        switch(errorCode) {
-            case ErrorConstants.ERROR_NULL:  return NULL_INTERSECTION;
-            case ErrorConstants.ERROR_DIV_0: return DIV_ZERO;
-            case ErrorConstants.ERROR_VALUE: return VALUE_INVALID;
-            case ErrorConstants.ERROR_REF:   return REF_INVALID;
-            case ErrorConstants.ERROR_NAME:  return NAME_INVALID;
-            case ErrorConstants.ERROR_NUM:   return NUM_ERROR;
-            case ErrorConstants.ERROR_NA:    return NA;
-            // non-std errors (conditions modelled as errors by POI)
-            case CIRCULAR_REF_ERROR_CODE:        return CIRCULAR_REF_ERROR;
+        FormulaError error = FormulaError.forInt(errorCode);
+        ErrorEval eval = evals.get(error);
+        if (eval != null) {
+            return eval;
+        } else {
+            throw new RuntimeException("Unhandled error type " + eval + " for code " + errorCode);
         }
-        throw new RuntimeException("Unexpected error code (" + errorCode + ")");
     }
 
     /**
@@ -72,36 +70,28 @@ public final class ErrorEval implements
      */
     public static String getText(int errorCode) {
         if(FormulaError.isValidCode(errorCode)) {
-            return FormulaError.forInt((byte)errorCode).getString();
-        }
-        // It is desirable to make these (arbitrary) strings look clearly different from
any other
-        // value expression that might appear in a formula.  In addition these error strings
should
-        // look unlike the standard Excel errors.  Hence tilde ('~') was used.
-        switch(errorCode) {
-            case CIRCULAR_REF_ERROR_CODE: return "~CIRCULAR~REF~";
-            case FUNCTION_NOT_IMPLEMENTED_CODE: return "~FUNCTION~NOT~IMPLEMENTED~";
+            return FormulaError.forInt(errorCode).getString();
         }
+        // Give a special string, based on ~, to make clear this isn't a standard Excel error
         return "~non~std~err(" + errorCode + ")~";
     }
 
-    private int _errorCode;
-    /**
-     * @param errorCode an 8-bit value
-     */
-    private ErrorEval(int errorCode) {
-        _errorCode = errorCode;
-    }
+    private FormulaError _error;
     private ErrorEval(FormulaError error) {
-        _errorCode = error.getCode();
+        _error = error;
+        evals.put(error, this);
     }
 
     public int getErrorCode() {
-        return _errorCode;
+        return _error.getLongCode();
+    }
+    public String getErrorString() {
+        return _error.getString();
     }
     public String toString() {
         StringBuffer sb = new StringBuffer(64);
         sb.append(getClass().getName()).append(" [");
-        sb.append(getText(_errorCode));
+        sb.append(_error.getString());
         sb.append("]");
         return sb.toString();
     }

Modified: poi/trunk/src/java/org/apache/poi/ss/usermodel/FormulaError.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/usermodel/FormulaError.java?rev=1658190&r1=1658189&r2=1658190&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/usermodel/FormulaError.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/usermodel/FormulaError.java Sun Feb  8 15:37:14 2015
@@ -93,7 +93,23 @@ public enum FormulaError {
      * </p>
      * This error value can be produced by calling the function NA
      */
-    NA(0x2A, "#N/A");
+    NA(0x2A, "#N/A"),
+    
+    // These are POI-specific error codes
+    // It is desirable to make these (arbitrary) strings look clearly different from any
other
+    // value expression that might appear in a formula.  In addition these error strings
should
+    // look unlike the standard Excel errors.  Hence tilde ('~') was used.
+    
+    /**
+     * POI specific code to indicate that there is a circular reference
+     *  in the formula
+     */
+    CIRCULAR_REF(0xFFFFFFC4, "~CIRCULAR~REF~"),
+    /**
+     * POI specific code to indicate that the funcition required is
+     *  not implemented in POI
+     */
+    FUNCTION_NOT_IMPLEMENTED(0xFFFFFFE2, "~FUNCTION~NOT~IMPLEMENTED~");
 
     private final byte type;
     private final int longType;
@@ -151,6 +167,7 @@ public enum FormulaError {
     }
     public static FormulaError forInt(int type){
         FormulaError err = imap.get(type);
+        if(err == null) err = bmap.get((byte)type);
         if(err == null) throw new IllegalArgumentException("Unknown error type: " + type);
         return err;
     }

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java?rev=1658190&r1=1658189&r2=1658190&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java Sun Feb
 8 15:37:14 2015
@@ -2151,7 +2151,7 @@ public final class TestXSSFBugs extends
         CellValue value = evaluator.evaluate(cell);
         assertEquals(Cell.CELL_TYPE_ERROR, value.getCellType());
         assertEquals(-60, value.getErrorValue());
-        // TODO Fix this
-//        assertEquals("", FormulaError.forInt(value.getErrorValue()).toString());
+        assertEquals("~CIRCULAR~REF~", FormulaError.forInt(value.getErrorValue()).getString());
+        assertEquals("CIRCULAR_REF", FormulaError.forInt(value.getErrorValue()).toString());
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message