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: package org.apache.poi.hssf.record.formula.functions;
18:
19: import org.apache.poi.hssf.record.formula.eval.BlankEval;
20: import org.apache.poi.hssf.record.formula.eval.ErrorEval;
21: import org.apache.poi.hssf.record.formula.eval.Eval;
22: import org.apache.poi.hssf.record.formula.eval.NumberEval;
23: import org.apache.poi.hssf.record.formula.eval.StringEval;
24: import org.apache.poi.hssf.record.formula.eval.StringValueEval;
25: import org.apache.poi.hssf.record.formula.eval.ValueEval;
26:
27: /**
28: * An implementation of the TRIM function:
29: * Removes leading and trailing spaces from value if evaluated operand
30: * value is string.
31: * @author Manda Wilson < wilson at c bio dot msk cc dot org >
32: */
33: public class Trim extends TextFunction {
34:
35: /**
36: * Removes leading and trailing spaces from value if evaluated
37: * operand value is string.
38: * Returns StringEval only if evaluated operand is of type string
39: * (and is not blank or null) or number. If evaluated operand is
40: * of type string and is blank or null, or if evaluated operand is
41: * of type blank, returns BlankEval. Otherwise returns ErrorEval.
42: *
43: * @see org.apache.poi.hssf.record.formula.eval.Eval
44: */
45: public Eval evaluate(Eval[] operands, int srcCellRow,
46: short srcCellCol) {
47: Eval retval = ErrorEval.VALUE_INVALID;
48: String str = null;
49:
50: switch (operands.length) {
51: default:
52: break;
53: case 1:
54: ValueEval veval = singleOperandEvaluate(operands[0],
55: srcCellRow, srcCellCol);
56: if (veval instanceof StringValueEval) {
57: StringValueEval sve = (StringValueEval) veval;
58: str = sve.getStringValue();
59: if (str == null || str.trim().equals("")) {
60: return BlankEval.INSTANCE;
61: }
62: } else if (veval instanceof NumberEval) {
63: NumberEval neval = (NumberEval) veval;
64: str = neval.getStringValue();
65: } else if (veval instanceof BlankEval) {
66: return BlankEval.INSTANCE;
67: }
68: }
69:
70: if (str != null) {
71: retval = new StringEval(str.trim());
72: }
73: return retval;
74: }
75: }
|