]> source.dussan.org Git - poi.git/commitdiff
Improve how XSSFCell does error stuff
authorNick Burch <nick@apache.org>
Sun, 30 Mar 2008 16:25:52 +0000 (16:25 +0000)
committerNick Burch <nick@apache.org>
Sun, 30 Mar 2008 16:25:52 +0000 (16:25 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@642769 13f79535-47bb-0310-9956-ffa450edef68

build.xml
src/java/org/apache/poi/hssf/record/BoolErrRecord.java
src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Cell.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java

index 523e328bef99c08399bb642f0a726e9b85e50633..45274b63fb7bafc46f41edbca73e7d0d4021caf1 100644 (file)
--- a/build.xml
+++ b/build.xml
@@ -544,6 +544,7 @@ under the License.
       <classpath>
         <path refid="ooxml.classpath"/>
         <pathelement path="${ooxml.output.dir}"/>
+        <pathelement path="${main.output.test.dir}"/>
         <pathelement location="${junit.jar1.dir}"/>
       </classpath>
     </javac>
index 9a9ea554e1acfcfdfafe78830ab61d1500df5fc9..f2b9d928ab47de0341728fa5d8f450fbd3f661cd 100644 (file)
@@ -46,14 +46,6 @@ public class BoolErrRecord
     private byte              field_4_bBoolErr;
     private byte              field_5_fError;
     
-    public static final byte NULL = 0;
-    public static final byte DIV0 = 7;
-    public static final byte VALUE = 15;
-    public static final byte REF = 23;
-    public static final byte NAME = 29;
-    public static final byte NUM = 36;
-    public static final byte NA = 42;
-
     /** Creates new BoolErrRecord */
     public BoolErrRecord()
     {
index 39113f59c74454267f50ab6f0ac5e11373641cba..1a250b26d8563ae698312da145d4feac2950f4af 100644 (file)
@@ -70,6 +70,26 @@ public interface Cell {
      */
 
     public final static int CELL_TYPE_ERROR = 5;
+    
+    public final static class CELL_ERROR_TYPE {
+       private final byte type;
+       private final String repr;
+       private CELL_ERROR_TYPE(int type, String repr) {
+               this.type = (byte)type;
+               this.repr = repr;
+       }
+       
+       public byte getType() { return type; }
+       public String getStringRepr() { return repr; }
+    }
+    public static final CELL_ERROR_TYPE ERROR_NULL  = new CELL_ERROR_TYPE(0, "#NULL!");
+    public static final CELL_ERROR_TYPE ERROR_DIV0  = new CELL_ERROR_TYPE(7, "#DIV/0!");
+    public static final CELL_ERROR_TYPE ERROR_VALUE = new CELL_ERROR_TYPE(15, "#VALUE!");
+    public static final CELL_ERROR_TYPE ERROR_REF   = new CELL_ERROR_TYPE(23, "#REF!");
+    public static final CELL_ERROR_TYPE ERROR_NAME  = new CELL_ERROR_TYPE(29, "#NAME?");
+    public static final CELL_ERROR_TYPE ERROR_NUM   = new CELL_ERROR_TYPE(36, "#NUM!");
+    public static final CELL_ERROR_TYPE ERROR_NA    = new CELL_ERROR_TYPE(42, "#N/A");
+
 
     /**
      * set the cell's number within the row (0 based)
index 96ddd9567d03f72ba7482f85b18400a0dff32eb0..1534e567ea33a12761e47b3b5c87412f13619135 100644 (file)
@@ -20,7 +20,6 @@ package org.apache.poi.xssf.usermodel;
 import java.util.Calendar;
 import java.util.Date;
 
-import org.apache.poi.hssf.record.BoolErrRecord;
 import org.apache.poi.hssf.usermodel.HSSFDateUtil;
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.CellStyle;
@@ -28,6 +27,8 @@ import org.apache.poi.ss.usermodel.Comment;
 import org.apache.poi.ss.usermodel.RichTextString;
 import org.apache.poi.ss.usermodel.SharedStringSource;
 import org.apache.poi.ss.usermodel.StylesSource;
+import org.apache.poi.util.POILogFactory;
+import org.apache.poi.util.POILogger;
 import org.apache.poi.xssf.util.CellReference;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCellFormula;
@@ -44,6 +45,8 @@ public class XSSFCell implements Cell {
     private SharedStringSource sharedStringSource;
     private StylesSource stylesSource;
     
+    private POILogger logger = POILogFactory.getLogger(XSSFCell.class);
+    
     /**
      * Create a new XSSFCell. This method is protected to be used only by
      * tests.
@@ -154,7 +157,7 @@ public class XSSFCell implements Cell {
     }
     /**
      * Returns the error type, in the same way that
-     *  HSSFCell does. See {@link BoolErrRecord} for details
+     *  HSSFCell does. See {@link Cell} for details
      */
     public byte getErrorCellValue() {
         if (STCellType.E != cell.getT()) { 
@@ -162,25 +165,25 @@ public class XSSFCell implements Cell {
         }
         if (this.cell.isSetV()) {
                String errS = this.cell.getV();
-               if(errS.equals("#NULL!")) {
-                       return BoolErrRecord.NULL;
+               if(errS.equals(Cell.ERROR_NULL.getStringRepr())) {
+                       return Cell.ERROR_NULL.getType();
                }
-               if(errS.equals("#DIV/0!")) {
-                       return BoolErrRecord.DIV0;
+               if(errS.equals(Cell.ERROR_DIV0.getStringRepr())) {
+                       return Cell.ERROR_DIV0.getType();
                }
-               if(errS.equals("#VALUE!")) {
-                       return BoolErrRecord.VALUE;
+               if(errS.equals(Cell.ERROR_VALUE.getStringRepr())) {
+                       return Cell.ERROR_VALUE.getType();
                }
-               if(errS.equals("#REF!")) {
-                       return BoolErrRecord.REF;
+               if(errS.equals(Cell.ERROR_REF.getStringRepr())) {
+                       return Cell.ERROR_REF.getType();
                }
-               if(errS.equals("#NAME?")) {
-                       return BoolErrRecord.NAME;
+               if(errS.equals(Cell.ERROR_NAME.getStringRepr())) {
+                       return Cell.ERROR_NAME.getType();
                }
-               if(errS.equals("#NUM!")) {
-                       return BoolErrRecord.NUM;
+               if(errS.equals(Cell.ERROR_NUM.getStringRepr())) {
+                       return Cell.ERROR_NUM.getType();
                }
-               return BoolErrRecord.NA;
+               return Cell.ERROR_NA.getType();
         }
         return 0;
     }
@@ -224,13 +227,32 @@ public class XSSFCell implements Cell {
     }
 
     public void setCellErrorValue(byte value) {
+       if(value == Cell.ERROR_DIV0.getType()) {
+                       setCellErrorValue(Cell.ERROR_DIV0);
+       } else if(value == Cell.ERROR_NA.getType()) {
+               setCellErrorValue(Cell.ERROR_NA);
+       } else if(value == Cell.ERROR_NAME.getType()) {
+               setCellErrorValue(Cell.ERROR_NAME);
+       } else if(value == Cell.ERROR_NULL.getType()) {
+               setCellErrorValue(Cell.ERROR_NULL);
+       } else if(value == Cell.ERROR_NUM.getType()) {
+               setCellErrorValue(Cell.ERROR_NUM);
+       } else if(value == Cell.ERROR_REF.getType()) {
+               setCellErrorValue(Cell.ERROR_REF);
+       } else if(value == Cell.ERROR_VALUE.getType()) {
+               setCellErrorValue(Cell.ERROR_VALUE);
+       } else {
+               logger.log(POILogger.WARN, "Unknown error type " + value + " specified, treating as #N/A");
+                       setCellErrorValue(Cell.ERROR_NA);
+       }
+    }
+    public void setCellErrorValue(CELL_ERROR_TYPE errorType) {
         if ((this.cell.getT() != STCellType.E) && (this.cell.getT() != STCellType.STR))
         {
             this.cell.setT(STCellType.E);
         }
-        this.cell.setV(String.valueOf(value));
+        this.cell.setV( errorType.getStringRepr() );
     }
-
     
    
     public void setCellFormula(String formula) {
index dd6e5eb2c9856af45072f92cd6c36c5062c97007..59e4e26b9928592ea812de0e6c98e15d8ff06148 100644 (file)
@@ -120,12 +120,10 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
                public String getDefaultFileName() { return DEFAULT_NAME; }
 
                /**
-                * Load, off the specified core part
+                * Fetches the InputStream to read the contents, based
+                *  of the specified core part
                 */
-               private XSSFModel load(PackagePart corePart) throws Exception {
-                       Constructor<? extends XSSFModel> c = CLASS.getConstructor(InputStream.class);
-                       XSSFModel model = null;
-                       
+               public InputStream getContents(PackagePart corePart) throws IOException, InvalidFormatException {
             PackageRelationshipCollection prc =
                corePart.getRelationshipsByType(REL);
             Iterator<PackageRelationship> it = prc.iterator();
@@ -133,17 +131,30 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook {
                 PackageRelationship rel = it.next();
                 PackagePartName relName = PackagingURIHelper.createPartName(rel.getTargetURI());
                 PackagePart part = corePart.getPackage().getPart(relName);
-                InputStream is = part.getInputStream();
+                return part.getInputStream();
+            } else {
+               log.log(POILogger.WARN, "No part " + DEFAULT_NAME + " found");
+               return null;
+            }
+               }
+               /**
+                * Load, off the specified core part
+                */
+               public XSSFModel load(PackagePart corePart) throws Exception {
+                       Constructor<? extends XSSFModel> c = CLASS.getConstructor(InputStream.class);
+                       XSSFModel model = null;
+                       
+                       InputStream inp = getContents(corePart);
+                       if(inp != null) {
                 try {
-                       model = c.newInstance(is);
+                       model = c.newInstance(inp);
                 } finally {
-                       is.close();
+                       inp.close();
                 }
-            } else {
-               log.log(POILogger.WARN, "No part " + DEFAULT_NAME + " found");
             }
             return model;
                }
+               
                /**
                 * Save, with the default name
                 * @return The internal reference ID it was saved at, normally then used as an r:id
index c53e184b2dee5363c7252cd0d3c4e58865804f77..f3e78e38aa11adb803a03627d23737ba8a26ea29 100644 (file)
@@ -109,10 +109,18 @@ public class TestXSSFCell extends TestCase {
     public void testSetGetError() throws Exception {
         XSSFRow row = createParentObjects();
         XSSFCell cell = new XSSFCell(row);
-        cell.setCellErrorValue((byte)255);
+        
+        cell.setCellErrorValue((byte)0);
         assertEquals(Cell.CELL_TYPE_ERROR, cell.getCellType());
-
-        assertEquals((byte)255, cell.getErrorCellValue());
+        assertEquals((byte)0, cell.getErrorCellValue());
+        
+        cell.setCellValue(2.2);
+        assertEquals(Cell.CELL_TYPE_NUMERIC, cell.getCellType());
+        
+        cell.setCellErrorValue(Cell.ERROR_NAME);
+        assertEquals(Cell.CELL_TYPE_ERROR, cell.getCellType());
+        assertEquals(Cell.ERROR_NAME.getType(), cell.getErrorCellValue());
+        assertEquals(Cell.ERROR_NAME.getStringRepr(), cell.getErrorCellString());
     }
     
     public void testSetGetFormula() throws Exception {