Added toString methods formatAsString to CellValue. Changed deprecation on CellValue.getRichTextStringValue

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@694881 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Josh Micich 2008-09-13 05:43:41 +00:00
parent a0769d4082
commit 0c881e7c99
4 changed files with 93 additions and 91 deletions

View File

@ -179,7 +179,7 @@ public class HSSFFormulaEvaluator {
case HSSFCell.CELL_TYPE_NUMERIC: case HSSFCell.CELL_TYPE_NUMERIC:
return new CellValue(cell.getNumericCellValue()); return new CellValue(cell.getNumericCellValue());
case HSSFCell.CELL_TYPE_STRING: case HSSFCell.CELL_TYPE_STRING:
return new CellValue(cell.getRichStringCellValue()); return new CellValue(cell.getRichStringCellValue().getString());
} }
throw new IllegalStateException("Bad cell type (" + cell.getCellType() + ")"); throw new IllegalStateException("Bad cell type (" + cell.getCellType() + ")");
} }
@ -270,7 +270,7 @@ public class HSSFFormulaEvaluator {
cell.setCellValue(cv.getNumberValue()); cell.setCellValue(cv.getNumberValue());
break; break;
case HSSFCell.CELL_TYPE_STRING: case HSSFCell.CELL_TYPE_STRING:
cell.setCellValue(cv.getRichTextStringValue()); cell.setCellValue(new HSSFRichTextString(cv.getStringValue()));
break; break;
case HSSFCell.CELL_TYPE_BLANK: case HSSFCell.CELL_TYPE_BLANK:
// never happens - blanks eventually get translated to zero // never happens - blanks eventually get translated to zero
@ -325,7 +325,7 @@ public class HSSFFormulaEvaluator {
} }
if (eval instanceof StringEval) { if (eval instanceof StringEval) {
StringEval ne = (StringEval) eval; StringEval ne = (StringEval) eval;
return new CellValue(new HSSFRichTextString(ne.getStringValue())); return new CellValue(ne.getStringValue());
} }
if (eval instanceof ErrorEval) { if (eval instanceof ErrorEval) {
return CellValue.getError(((ErrorEval)eval).getErrorCode()); return CellValue.getError(((ErrorEval)eval).getErrorCode());
@ -423,7 +423,7 @@ public class HSSFFormulaEvaluator {
throw new IllegalStateException("evaluation stack not empty"); throw new IllegalStateException("evaluation stack not empty");
} }
value = dereferenceValue(value, srcRowNum, srcColNum); value = dereferenceValue(value, srcRowNum, srcColNum);
if (value instanceof BlankEval) { if (value == BlankEval.INSTANCE) {
// Note Excel behaviour here. A blank final final value is converted to zero. // Note Excel behaviour here. A blank final final value is converted to zero.
return NumberEval.ZERO; return NumberEval.ZERO;
// Formulas _never_ evaluate to blank. If a formula appears to have evaluated to // Formulas _never_ evaluate to blank. If a formula appears to have evaluated to
@ -597,15 +597,15 @@ public class HSSFFormulaEvaluator {
private final int _cellType; private final int _cellType;
private final double _numberValue; private final double _numberValue;
private final boolean _booleanValue; private final boolean _booleanValue;
private final HSSFRichTextString _richTextStringValue; private final String _textValue;
private final int _errorCode; private final int _errorCode;
private CellValue(int cellType, double numberValue, boolean booleanValue, private CellValue(int cellType, double numberValue, boolean booleanValue,
HSSFRichTextString textValue, int errorCode) { String textValue, int errorCode) {
_cellType = cellType; _cellType = cellType;
_numberValue = numberValue; _numberValue = numberValue;
_booleanValue = booleanValue; _booleanValue = booleanValue;
_richTextStringValue = textValue; _textValue = textValue;
_errorCode = errorCode; _errorCode = errorCode;
} }
@ -616,7 +616,7 @@ public class HSSFFormulaEvaluator {
/* package*/ static CellValue valueOf(boolean booleanValue) { /* package*/ static CellValue valueOf(boolean booleanValue) {
return booleanValue ? TRUE : FALSE; return booleanValue ? TRUE : FALSE;
} }
/* package*/ CellValue(HSSFRichTextString stringValue) { /* package*/ CellValue(String stringValue) {
this(HSSFCell.CELL_TYPE_STRING, 0.0, false, stringValue, 0); this(HSSFCell.CELL_TYPE_STRING, 0.0, false, stringValue, 0);
} }
/* package*/ static CellValue getError(int errorCode) { /* package*/ static CellValue getError(int errorCode) {
@ -637,12 +637,10 @@ public class HSSFFormulaEvaluator {
return _numberValue; return _numberValue;
} }
/** /**
* @return Returns the stringValue. This method is deprecated, use * @return Returns the stringValue.
* getRichTextStringValue instead
* @deprecated
*/ */
public String getStringValue() { public String getStringValue() {
return _richTextStringValue.getString(); return _textValue;
} }
/** /**
* @return Returns the cellType. * @return Returns the cellType.
@ -658,9 +656,31 @@ public class HSSFFormulaEvaluator {
} }
/** /**
* @return Returns the richTextStringValue. * @return Returns the richTextStringValue.
* @deprecated (Sep 2008) Text formatting is lost during formula evaluation. Use {@link #getStringValue()}
*/ */
public HSSFRichTextString getRichTextStringValue() { public HSSFRichTextString getRichTextStringValue() {
return _richTextStringValue; return new HSSFRichTextString(_textValue);
}
public String toString() {
StringBuffer sb = new StringBuffer(64);
sb.append(getClass().getName()).append(" [");
sb.append(formatAsString());
sb.append("]");
return sb.toString();
}
public String formatAsString() {
switch (_cellType) {
case HSSFCell.CELL_TYPE_NUMERIC:
return String.valueOf(_numberValue);
case HSSFCell.CELL_TYPE_STRING:
return '"' + _textValue + '"';
case HSSFCell.CELL_TYPE_BOOLEAN:
return _booleanValue ? "TRUE" : "FALSE";
case HSSFCell.CELL_TYPE_ERROR:
return ErrorEval.getText(_errorCode);
}
return "<error unexpected cell type " + _cellType + ">";
} }
} }

View File

@ -132,7 +132,7 @@ public final class TestFormulasFromSpreadsheet extends TestCase {
break; break;
case HSSFCell.CELL_TYPE_STRING: case HSSFCell.CELL_TYPE_STRING:
assertEquals(msg, HSSFCell.CELL_TYPE_STRING, actual.getCellType()); assertEquals(msg, HSSFCell.CELL_TYPE_STRING, actual.getCellType());
assertEquals(msg, expected.getRichStringCellValue().getString(), actual.getRichTextStringValue().getString()); assertEquals(msg, expected.getRichStringCellValue().getString(), actual.getStringValue());
break; break;
} }
} }

View File

@ -95,7 +95,7 @@ public final class TestIndexFunctionFromSpreadsheet extends TestCase {
assertEquals(expected.getNumericCellValue(), actual.getNumberValue(), 0.0); assertEquals(expected.getNumericCellValue(), actual.getNumberValue(), 0.0);
break; break;
case HSSFCell.CELL_TYPE_STRING: case HSSFCell.CELL_TYPE_STRING:
assertEquals(msg, expected.getRichStringCellValue().getString(), actual.getRichTextStringValue().getString()); assertEquals(msg, expected.getRichStringCellValue().getString(), actual.getStringValue());
break; break;
} }
} }
@ -103,7 +103,7 @@ public final class TestIndexFunctionFromSpreadsheet extends TestCase {
private static AssertionFailedError wrongTypeError(String msgPrefix, HSSFCell expectedCell, CellValue actualValue) { private static AssertionFailedError wrongTypeError(String msgPrefix, HSSFCell expectedCell, CellValue actualValue) {
return new AssertionFailedError(msgPrefix + " Result type mismatch. Evaluated result was " return new AssertionFailedError(msgPrefix + " Result type mismatch. Evaluated result was "
+ formatValue(actualValue) + actualValue.formatAsString()
+ " but the expected result was " + " but the expected result was "
+ formatValue(expectedCell) + formatValue(expectedCell)
); );
@ -121,7 +121,7 @@ public final class TestIndexFunctionFromSpreadsheet extends TestCase {
if(actual.getCellType() != HSSFCell.CELL_TYPE_ERROR) { if(actual.getCellType() != HSSFCell.CELL_TYPE_ERROR) {
throw new AssertionFailedError(msgPrefix + " Expected cell error (" throw new AssertionFailedError(msgPrefix + " Expected cell error ("
+ ErrorEval.getText(expectedErrorCode) + ") but actual value was " + ErrorEval.getText(expectedErrorCode) + ") but actual value was "
+ formatValue(actual)); + actual.formatAsString());
} }
if(expectedErrorCode != actual.getErrorValue()) { if(expectedErrorCode != actual.getErrorValue()) {
throw new AssertionFailedError(msgPrefix + " Expected cell error code (" throw new AssertionFailedError(msgPrefix + " Expected cell error code ("
@ -142,15 +142,6 @@ public final class TestIndexFunctionFromSpreadsheet extends TestCase {
} }
throw new RuntimeException("Unexpected cell type of expected value (" + expecedCell.getCellType() + ")"); throw new RuntimeException("Unexpected cell type of expected value (" + expecedCell.getCellType() + ")");
} }
private static String formatValue(CellValue actual) {
switch (actual.getCellType()) {
case HSSFCell.CELL_TYPE_BLANK: return "<blank>";
case HSSFCell.CELL_TYPE_BOOLEAN: return String.valueOf(actual.getBooleanValue());
case HSSFCell.CELL_TYPE_NUMERIC: return String.valueOf(actual.getNumberValue());
case HSSFCell.CELL_TYPE_STRING: return actual.getRichTextStringValue().getString();
}
throw new RuntimeException("Unexpected cell type of evaluated value (" + actual.getCellType() + ")");
}
protected void setUp() { protected void setUp() {

View File

@ -112,12 +112,12 @@ public final class TestLookupFunctionsFromSpreadsheet extends TestCase {
assertEquals(msg, expected.getBooleanCellValue(), actual.getBooleanValue()); assertEquals(msg, expected.getBooleanCellValue(), actual.getBooleanValue());
break; break;
case HSSFCell.CELL_TYPE_FORMULA: // will never be used, since we will call method after formula evaluation case HSSFCell.CELL_TYPE_FORMULA: // will never be used, since we will call method after formula evaluation
throw new AssertionFailedError("Cannot expect formula as result of formula evaluation: " + msg); throw new IllegalStateException("Cannot expect formula as result of formula evaluation: " + msg);
case HSSFCell.CELL_TYPE_NUMERIC: case HSSFCell.CELL_TYPE_NUMERIC:
assertEquals(expected.getNumericCellValue(), actual.getNumberValue(), 0.0); assertEquals(expected.getNumericCellValue(), actual.getNumberValue(), 0.0);
break; break;
case HSSFCell.CELL_TYPE_STRING: case HSSFCell.CELL_TYPE_STRING:
assertEquals(msg, expected.getRichStringCellValue().getString(), actual.getRichTextStringValue().getString()); assertEquals(msg, expected.getRichStringCellValue().getString(), actual.getStringValue());
break; break;
} }
} }
@ -125,7 +125,7 @@ public final class TestLookupFunctionsFromSpreadsheet extends TestCase {
private static AssertionFailedError wrongTypeError(String msgPrefix, HSSFCell expectedCell, CellValue actualValue) { private static AssertionFailedError wrongTypeError(String msgPrefix, HSSFCell expectedCell, CellValue actualValue) {
return new AssertionFailedError(msgPrefix + " Result type mismatch. Evaluated result was " return new AssertionFailedError(msgPrefix + " Result type mismatch. Evaluated result was "
+ formatValue(actualValue) + actualValue.formatAsString()
+ " but the expected result was " + " but the expected result was "
+ formatValue(expectedCell) + formatValue(expectedCell)
); );
@ -143,7 +143,7 @@ public final class TestLookupFunctionsFromSpreadsheet extends TestCase {
if(actual.getCellType() != HSSFCell.CELL_TYPE_ERROR) { if(actual.getCellType() != HSSFCell.CELL_TYPE_ERROR) {
throw new AssertionFailedError(msgPrefix + " Expected cell error (" throw new AssertionFailedError(msgPrefix + " Expected cell error ("
+ ErrorEval.getText(expectedErrorCode) + ") but actual value was " + ErrorEval.getText(expectedErrorCode) + ") but actual value was "
+ formatValue(actual)); + actual.formatAsString());
} }
if(expectedErrorCode != actual.getErrorValue()) { if(expectedErrorCode != actual.getErrorValue()) {
throw new AssertionFailedError(msgPrefix + " Expected cell error code (" throw new AssertionFailedError(msgPrefix + " Expected cell error code ("
@ -164,18 +164,9 @@ public final class TestLookupFunctionsFromSpreadsheet extends TestCase {
} }
throw new RuntimeException("Unexpected cell type of expected value (" + expecedCell.getCellType() + ")"); throw new RuntimeException("Unexpected cell type of expected value (" + expecedCell.getCellType() + ")");
} }
private static String formatValue(CellValue actual) {
switch (actual.getCellType()) {
case HSSFCell.CELL_TYPE_BLANK: return "<blank>";
case HSSFCell.CELL_TYPE_BOOLEAN: return String.valueOf(actual.getBooleanValue());
case HSSFCell.CELL_TYPE_NUMERIC: return String.valueOf(actual.getNumberValue());
case HSSFCell.CELL_TYPE_STRING: return actual.getRichTextStringValue().getString();
}
throw new RuntimeException("Unexpected cell type of evaluated value (" + actual.getCellType() + ")");
}
protected void setUp() throws Exception { protected void setUp() {
_sheetFailureCount = 0; _sheetFailureCount = 0;
_sheetSuccessCount = 0; _sheetSuccessCount = 0;
_evaluationFailureCount = 0; _evaluationFailureCount = 0;