View | Details | Raw Unified | Return to bug 57010
Collapse All | Expand All

(-)a/src/java/org/apache/poi/ss/formula/eval/FunctionEval.java (-1 / +1 lines)
Lines 207-213 public final class FunctionEval { Link Here
207
        retval[111] = TextFunction.CHAR;
207
        retval[111] = TextFunction.CHAR;
208
        retval[112] = TextFunction.LOWER;
208
        retval[112] = TextFunction.LOWER;
209
        retval[113] = TextFunction.UPPER;
209
        retval[113] = TextFunction.UPPER;
210
210
        retval[114] = TextFunction.PROPER;
211
        retval[115] = TextFunction.LEFT;
211
        retval[115] = TextFunction.LEFT;
212
        retval[116] = TextFunction.RIGHT;
212
        retval[116] = TextFunction.RIGHT;
213
        retval[117] = TextFunction.EXACT;
213
        retval[117] = TextFunction.EXACT;
(-)a/src/java/org/apache/poi/ss/formula/functions/TextFunction.java (+28 lines)
Lines 17-22 Link Here
17
17
18
package org.apache.poi.ss.formula.functions;
18
package org.apache.poi.ss.formula.functions;
19
19
20
import java.util.regex.Pattern;
21
20
import org.apache.poi.ss.formula.eval.BoolEval;
22
import org.apache.poi.ss.formula.eval.BoolEval;
21
import org.apache.poi.ss.formula.eval.ErrorEval;
23
import org.apache.poi.ss.formula.eval.ErrorEval;
22
import org.apache.poi.ss.formula.eval.EvaluationException;
24
import org.apache.poi.ss.formula.eval.EvaluationException;
Lines 112-117 public abstract class TextFunction implements Function { Link Here
112
			return new StringEval(arg.toUpperCase());
114
			return new StringEval(arg.toUpperCase());
113
		}
115
		}
114
	};
116
	};
117
118
	/**
119
	 * Implementation of the PROPER function:
120
     * Normalizes all words (separated by non-word characters) by
121
     * making the first letter upper and the rest lower case.
122
	 */
123
	public static final Function PROPER = new SingleArgTextFunc() {
124
	    final Pattern nonAlphabeticPattern = Pattern.compile("\\P{IsAlphabetic}");
125
		protected ValueEval evaluate(String text) {
126
			StringBuilder sb = new StringBuilder();
127
			boolean shouldMakeUppercase = true;
128
			String lowercaseText = text.toLowerCase();
129
			String uppercaseText = text.toUpperCase();
130
			for(int i = 0; i < text.length(); ++i) {
131
				if (shouldMakeUppercase) {
132
					sb.append(uppercaseText.charAt(i));
133
				}
134
				else {
135
					sb.append(lowercaseText.charAt(i));
136
				}
137
				shouldMakeUppercase = nonAlphabeticPattern.matcher(text.subSequence(i, i + 1)).matches();
138
			}
139
			return new StringEval(sb.toString());
140
		}
141
	};
142
115
	/**
143
	/**
116
	 * An implementation of the TRIM function:
144
	 * An implementation of the TRIM function:
117
	 * Removes leading and trailing spaces from value if evaluated operand
145
	 * Removes leading and trailing spaces from value if evaluated operand
(-)a/src/testcases/org/apache/poi/ss/formula/functions/TestProper.java (+59 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Licensed to the Apache Software Foundation (ASF) under one or more
3
   contributor license agreements.  See the NOTICE file distributed with
4
   this work for additional information regarding copyright ownership.
5
   The ASF licenses this file to You under the Apache License, Version 2.0
6
   (the "License"); you may not use this file except in compliance with
7
   the License.  You may obtain a copy of the License at
8
9
       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
package org.apache.poi.ss.formula.functions;
19
20
import junit.framework.AssertionFailedError;
21
import junit.framework.TestCase;
22
23
import org.apache.poi.hssf.usermodel.HSSFCell;
24
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
25
import org.apache.poi.hssf.usermodel.HSSFSheet;
26
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
27
import org.apache.poi.ss.usermodel.Cell;
28
import org.apache.poi.ss.usermodel.CellValue;
29
30
public final class TestProper extends TestCase {
31
    private HSSFCell cell11;
32
    private HSSFFormulaEvaluator evaluator;
33
34
    public void setUp() {
35
        HSSFWorkbook wb = new HSSFWorkbook();
36
        HSSFSheet sheet = wb.createSheet("new sheet");
37
        cell11 = sheet.createRow(0).createCell(0);
38
        cell11.setCellType(HSSFCell.CELL_TYPE_FORMULA);
39
        evaluator = new HSSFFormulaEvaluator(wb);
40
    }
41
42
    public void testValid() {
43
        confirm("PROPER(\"hi there\")", "Hi There");
44
        confirm("PROPER(\"what's up\")", "What'S Up");
45
        confirm("PROPER(\"I DON'T TH!NK SO!\")", "I Don'T Th!Nk So!");
46
        confirm("PROPER(\"drÜbö'ä éloş|ëè \")", "Drübö'Ä Éloş|Ëè ");
47
    }
48
49
    private void confirm(String formulaText, String expectedResult) {
50
        cell11.setCellFormula(formulaText);
51
        evaluator.clearAllCachedResultValues();
52
        CellValue cv = evaluator.evaluate(cell11);
53
        if (cv.getCellType() != Cell.CELL_TYPE_STRING) {
54
            throw new AssertionFailedError("Wrong result type: " + cv.formatAsString());
55
        }
56
        String actualValue = cv.getStringValue();
57
        assertEquals(expectedResult, actualValue);
58
    }
59
}

Return to bug 57010