Author: nick Date: Tue Dec 11 05:03:53 2007 New Revision: 603233 URL: http://svn.apache.org/viewvc?rev=603233&view=rev Log: Support for the Trim function, and a little enhancement to the formula evaluation test
Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Trim.java poi/trunk/src/scratchpad/testcases/org/apache/poi/hssf/data/FormulaEvalTestData.xls poi/trunk/src/scratchpad/testcases/org/apache/poi/hssf/record/formula/eval/TestEverything.java Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Trim.java URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Trim.java?rev=603233&r1=603232&r2=603233&view=diff ============================================================================== --- poi/trunk/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Trim.java (original) +++ poi/trunk/src/scratchpad/src/org/apache/poi/hssf/record/formula/functions/Trim.java Tue Dec 11 05:03:53 2007 @@ -14,12 +14,62 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* - * Created on May 15, 2005 - * - */ package org.apache.poi.hssf.record.formula.functions; -public class Trim extends NotImplementedFunction { +import org.apache.poi.hssf.record.formula.eval.BlankEval; +import org.apache.poi.hssf.record.formula.eval.ErrorEval; +import org.apache.poi.hssf.record.formula.eval.Eval; +import org.apache.poi.hssf.record.formula.eval.NumberEval; +import org.apache.poi.hssf.record.formula.eval.StringEval; +import org.apache.poi.hssf.record.formula.eval.StringValueEval; +import org.apache.poi.hssf.record.formula.eval.ValueEval; + +/** + * An implementation of the TRIM function: + * Removes leading and trailing spaces from value if evaluated operand + * value is string. + * @author Manda Wilson < wilson at c bio dot msk cc dot org > + */ +public class Trim extends TextFunction { + /** + * Removes leading and trailing spaces from value if evaluated + * operand value is string. + * Returns StringEval only if evaluated operand is of type string + * (and is not blank or null) or number. If evaluated operand is + * of type string and is blank or null, or if evaluated operand is + * of type blank, returns BlankEval. Otherwise returns ErrorEval. + * + * @see org.apache.poi.hssf.record.formula.eval.Eval + */ + public Eval evaluate(Eval[] operands, int srcCellRow, short srcCellCol) { + Eval retval = ErrorEval.VALUE_INVALID; + String str = null; + + switch (operands.length) { + default: + break; + case 1: + ValueEval veval = singleOperandEvaluate(operands[0], srcCellRow, srcCellCol); + if (veval instanceof StringValueEval) { + StringValueEval sve = (StringValueEval) veval; + str = sve.getStringValue(); + if (str == null || str.trim().equals("")) { + return BlankEval.INSTANCE; + } + } + else if (veval instanceof NumberEval) { + NumberEval neval = (NumberEval) veval; + str = neval.getStringValue(); + } + else if (veval instanceof BlankEval) { + return BlankEval.INSTANCE; + } + } + + if (str != null) { + retval = new StringEval(str.trim()); + } + return retval; + } } Modified: poi/trunk/src/scratchpad/testcases/org/apache/poi/hssf/data/FormulaEvalTestData.xls URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hssf/data/FormulaEvalTestData.xls?rev=603233&r1=603232&r2=603233&view=diff ============================================================================== Binary files - no diff available. Modified: poi/trunk/src/scratchpad/testcases/org/apache/poi/hssf/record/formula/eval/TestEverything.java URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hssf/record/formula/eval/TestEverything.java?rev=603233&r1=603232&r2=603233&view=diff ============================================================================== --- poi/trunk/src/scratchpad/testcases/org/apache/poi/hssf/record/formula/eval/TestEverything.java (original) +++ poi/trunk/src/scratchpad/testcases/org/apache/poi/hssf/record/formula/eval/TestEverything.java Tue Dec 11 05:03:53 2007 @@ -30,20 +30,28 @@ public static TestSuite suite() throws Exception { TestSuite suite = new TestSuite("Tests for OperationEval concrete implementation classes."); - suite.addTest(new GenericFormulaTestCase("D23")); - suite.addTest(new GenericFormulaTestCase("D27")); - suite.addTest(new GenericFormulaTestCase("D31")); - suite.addTest(new GenericFormulaTestCase("D35")); - suite.addTest(new GenericFormulaTestCase("D39")); - suite.addTest(new GenericFormulaTestCase("D43")); - suite.addTest(new GenericFormulaTestCase("D47")); - suite.addTest(new GenericFormulaTestCase("D51")); - suite.addTest(new GenericFormulaTestCase("D55")); - suite.addTest(new GenericFormulaTestCase("D59")); - suite.addTest(new GenericFormulaTestCase("D63")); - suite.addTest(new GenericFormulaTestCase("D67")); - suite.addTest(new GenericFormulaTestCase("D71")); - suite.addTest(new GenericFormulaTestCase("D75")); + suite.addTest(new GenericFormulaTestCase("D23")); // Add + suite.addTest(new GenericFormulaTestCase("D27")); // ConcatEval + suite.addTest(new GenericFormulaTestCase("D31")); // DivideEval + suite.addTest(new GenericFormulaTestCase("D35")); // EqualEval + suite.addTest(new GenericFormulaTestCase("D39")); // GreaterEqualEval + suite.addTest(new GenericFormulaTestCase("D43")); // GreaterThanEval + suite.addTest(new GenericFormulaTestCase("D47")); // LessEqualEval + suite.addTest(new GenericFormulaTestCase("D51")); // LessThanEval + suite.addTest(new GenericFormulaTestCase("D55")); // MultiplyEval + suite.addTest(new GenericFormulaTestCase("D59")); // NotEqualEval + suite.addTest(new GenericFormulaTestCase("D63")); // PowerEval + suite.addTest(new GenericFormulaTestCase("D67")); // SubtractEval + suite.addTest(new GenericFormulaTestCase("D71")); // UnaryMinusEval + suite.addTest(new GenericFormulaTestCase("D75")); // UnaryPlusEval + + suite.addTest(new GenericFormulaTestCase("D249")); // Concatenate + suite.addTest(new GenericFormulaTestCase("D741")); // Int + suite.addTest(new GenericFormulaTestCase("D1393")); // Trim + suite.addTest(new GenericFormulaTestCase("D1421")); // Upper + + // Add newly implemented formula functions here + return suite; } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]