diff --git a/build.xml b/build.xml index 523e328be..45274b63f 100644 --- a/build.xml +++ b/build.xml @@ -544,6 +544,7 @@ under the License. + diff --git a/src/java/org/apache/poi/hssf/record/BoolErrRecord.java b/src/java/org/apache/poi/hssf/record/BoolErrRecord.java index 9a9ea554e..f2b9d928a 100644 --- a/src/java/org/apache/poi/hssf/record/BoolErrRecord.java +++ b/src/java/org/apache/poi/hssf/record/BoolErrRecord.java @@ -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() { diff --git a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Cell.java b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Cell.java index 39113f59c..1a250b26d 100644 --- a/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Cell.java +++ b/src/ooxml/interfaces-jdk15/org/apache/poi/ss/usermodel/Cell.java @@ -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) diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java index 96ddd9567..1534e567e 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFCell.java @@ -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) { diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index dd6e5eb2c..59e4e26b9 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -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 c = CLASS.getConstructor(InputStream.class); - XSSFModel model = null; - + public InputStream getContents(PackagePart corePart) throws IOException, InvalidFormatException { PackageRelationshipCollection prc = corePart.getRelationshipsByType(REL); Iterator 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(); - try { - model = c.newInstance(is); - } finally { - is.close(); - } + 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 c = CLASS.getConstructor(InputStream.class); + XSSFModel model = null; + + InputStream inp = getContents(corePart); + if(inp != null) { + try { + model = c.newInstance(inp); + } finally { + inp.close(); + } } return model; } + /** * Save, with the default name * @return The internal reference ID it was saved at, normally then used as an r:id diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java index c53e184b2..f3e78e38a 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFCell.java @@ -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 {