01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: /*
18: * Created on May 22, 2005
19: *
20: */
21: package org.apache.poi.hssf.record.formula.functions;
22:
23: import org.apache.poi.hssf.record.formula.eval.AreaEval;
24: import org.apache.poi.hssf.record.formula.eval.BlankEval;
25: import org.apache.poi.hssf.record.formula.eval.ErrorEval;
26: import org.apache.poi.hssf.record.formula.eval.Eval;
27: import org.apache.poi.hssf.record.formula.eval.RefEval;
28: import org.apache.poi.hssf.record.formula.eval.StringValueEval;
29: import org.apache.poi.hssf.record.formula.eval.ValueEval;
30:
31: /**
32: * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
33: *
34: */
35: public abstract class TextFunction implements Function {
36:
37: protected static final String EMPTY_STRING = "";
38:
39: protected ValueEval singleOperandEvaluate(Eval eval, int srcRow,
40: short srcCol) {
41: ValueEval retval;
42: if (eval instanceof AreaEval) {
43: AreaEval ae = (AreaEval) eval;
44: if (ae.contains(srcRow, srcCol)) { // circular ref!
45: retval = ErrorEval.CIRCULAR_REF_ERROR;
46: } else if (ae.isRow()) {
47: if (ae.containsColumn(srcCol)) {
48: ValueEval ve = ae.getValueAt(ae.getFirstRow(),
49: srcCol);
50: retval = attemptXlateToText(ve);
51: } else {
52: retval = ErrorEval.VALUE_INVALID;
53: }
54: } else if (ae.isColumn()) {
55: if (ae.containsRow(srcRow)) {
56: ValueEval ve = ae.getValueAt(srcRow, ae
57: .getFirstColumn());
58: retval = attemptXlateToText(ve);
59: } else {
60: retval = ErrorEval.VALUE_INVALID;
61: }
62: } else {
63: retval = ErrorEval.VALUE_INVALID;
64: }
65: } else {
66: retval = attemptXlateToText((ValueEval) eval);
67: }
68: return retval;
69: }
70:
71: /**
72: * converts from Different ValueEval types to StringEval.
73: * Note: AreaEvals are not handled, if arg is an AreaEval,
74: * the returned value is ErrorEval.VALUE_INVALID
75: * @param ve
76: */
77: protected ValueEval attemptXlateToText(ValueEval ve) {
78: ValueEval retval;
79: if (ve instanceof StringValueEval) {
80: retval = ve;
81: } else if (ve instanceof RefEval) {
82: RefEval re = (RefEval) ve;
83: ValueEval ive = re.getInnerValueEval();
84: if (ive instanceof StringValueEval) {
85: retval = ive;
86: } else if (ive instanceof BlankEval) {
87: retval = ive;
88: } else {
89: retval = ErrorEval.VALUE_INVALID;
90: }
91: } else if (ve instanceof BlankEval) {
92: retval = ve;
93: } else {
94: retval = ErrorEval.VALUE_INVALID;
95: }
96: return retval;
97: }
98: }
|