001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /*
018: * Created on May 15, 2005
019: *
020: */
021: package org.apache.poi.hssf.record.formula.functions;
022:
023: import org.apache.poi.hssf.record.formula.eval.BoolEval;
024: import org.apache.poi.hssf.record.formula.eval.ErrorEval;
025: import org.apache.poi.hssf.record.formula.eval.Eval;
026: import org.apache.poi.hssf.record.formula.eval.NumberEval;
027: import org.apache.poi.hssf.record.formula.eval.RefEval;
028: import org.apache.poi.hssf.record.formula.eval.StringEval;
029: import org.apache.poi.hssf.record.formula.eval.ValueEval;
030:
031: /**
032: * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
033: *
034: */
035: public class Left extends TextFunction {
036:
037: public Eval evaluate(Eval[] operands, int srcCellRow,
038: short srcCellCol) {
039: Eval retval = ErrorEval.VALUE_INVALID;
040: int index = 1;
041: switch (operands.length) {
042: default:
043: break;
044: case 2:
045: Eval indexEval = operands[1];
046: index = evaluateAsInteger(indexEval);
047: if (index < 0) {
048: break;
049: }
050: case 1:
051: ValueEval veval = singleOperandEvaluate(operands[0],
052: srcCellRow, srcCellCol);
053: String str = null;
054: if (veval instanceof StringEval) {
055: StringEval stringEval = (StringEval) veval;
056: str = stringEval.getStringValue();
057: } else if (veval instanceof BoolEval) {
058: BoolEval beval = (BoolEval) veval;
059: str = beval.getBooleanValue() ? "TRUE" : "FALSE";
060: } else if (veval instanceof NumberEval) {
061: NumberEval neval = (NumberEval) veval;
062: str = neval.getStringValue();
063: }
064: if (null != str) {
065: str = str.substring(0, Math.min(str.length(), index));
066: retval = new StringEval(str);
067: }
068: }
069: return retval;
070: }
071:
072: protected int evaluateAsInteger(Eval eval) {
073: int numval = -1;
074: if (eval instanceof NumberEval) {
075: NumberEval neval = (NumberEval) eval;
076: double d = neval.getNumberValue();
077: numval = (int) d;
078: } else if (eval instanceof StringEval) {
079: StringEval seval = (StringEval) eval;
080: String s = seval.getStringValue();
081: try {
082: double d = Double.parseDouble(s);
083: numval = (int) d;
084: } catch (Exception e) {
085: }
086: } else if (eval instanceof BoolEval) {
087: BoolEval beval = (BoolEval) eval;
088: numval = beval.getBooleanValue() ? 1 : 0;
089: } else if (eval instanceof RefEval) {
090: numval = evaluateAsInteger(xlateRefEval((RefEval) eval));
091: }
092: return numval;
093: }
094:
095: protected Eval xlateRefEval(RefEval reval) {
096: Eval retval = reval.getInnerValueEval();
097:
098: if (retval instanceof RefEval) {
099: retval = xlateRefEval((RefEval) retval);
100: }
101: return retval;
102: }
103: }
|