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.AreaEval;
024: import org.apache.poi.hssf.record.formula.eval.BlankEval;
025: import org.apache.poi.hssf.record.formula.eval.ErrorEval;
026: import org.apache.poi.hssf.record.formula.eval.Eval;
027: import org.apache.poi.hssf.record.formula.eval.NumberEval;
028: import org.apache.poi.hssf.record.formula.eval.RefEval;
029: import org.apache.poi.hssf.record.formula.eval.StringValueEval;
030: import org.apache.poi.hssf.record.formula.eval.ValueEval;
031:
032: /**
033: * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
034: *
035: */
036: public class Len extends TextFunction {
037:
038: public Eval evaluate(Eval[] operands, int srcCellRow,
039: short srcCellCol) {
040: ValueEval retval = null;
041: String s = null;
042:
043: switch (operands.length) {
044: default:
045: retval = ErrorEval.VALUE_INVALID;
046: break;
047: case 1:
048: ValueEval ve = singleOperandEvaluate(operands[0],
049: srcCellRow, srcCellCol);
050: if (ve instanceof StringValueEval) {
051: StringValueEval sve = (StringValueEval) ve;
052: s = sve.getStringValue();
053: } else if (ve instanceof RefEval) {
054: RefEval re = (RefEval) ve;
055: ValueEval ive = re.getInnerValueEval();
056: if (ive instanceof BlankEval) {
057: s = re.isEvaluated() ? "0" : null;
058: } else if (ive instanceof StringValueEval) {
059: s = ((StringValueEval) ive).getStringValue();
060: } else if (ive instanceof BlankEval) {
061: } else {
062: retval = ErrorEval.VALUE_INVALID;
063: }
064: } else if (ve instanceof BlankEval) {
065: } else {
066: retval = ErrorEval.VALUE_INVALID;
067: break;
068: }
069: }
070:
071: if (retval == null) {
072: s = (s == null) ? EMPTY_STRING : s;
073: retval = new NumberEval(s.length());
074: }
075:
076: return retval;
077: }
078:
079: protected ValueEval singleOperandEvaluate(Eval eval, int srcRow,
080: short srcCol) {
081: ValueEval retval;
082: if (eval instanceof AreaEval) {
083: AreaEval ae = (AreaEval) eval;
084: if (ae.contains(srcRow, srcCol)) { // circular ref!
085: retval = ErrorEval.CIRCULAR_REF_ERROR;
086: } else if (ae.isRow()) {
087: if (ae.containsColumn(srcCol)) {
088: ValueEval ve = ae.getValueAt(ae.getFirstRow(),
089: srcCol);
090: retval = attemptXlateToText(ve);
091: } else {
092: retval = ErrorEval.VALUE_INVALID;
093: }
094: } else if (ae.isColumn()) {
095: if (ae.containsRow(srcRow)) {
096: ValueEval ve = ae.getValueAt(srcRow, ae
097: .getFirstColumn());
098: retval = attemptXlateToText(ve);
099: } else {
100: retval = ErrorEval.VALUE_INVALID;
101: }
102: } else {
103: retval = ErrorEval.VALUE_INVALID;
104: }
105: } else {
106: retval = attemptXlateToText((ValueEval) eval);
107: }
108: return retval;
109: }
110:
111: /**
112: * converts from Different ValueEval types to StringEval.
113: * Note: AreaEvals are not handled, if arg is an AreaEval,
114: * the returned value is ErrorEval.VALUE_INVALID
115: * @param ve
116: */
117: protected ValueEval attemptXlateToText(ValueEval ve) {
118: ValueEval retval;
119: if (ve instanceof StringValueEval || ve instanceof RefEval) {
120: retval = ve;
121: } else if (ve instanceof BlankEval) {
122: retval = ve;
123: } else {
124: retval = ErrorEval.VALUE_INVALID;
125: }
126: return retval;
127: }
128: }
|