You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ce...@apache.org on 2014/10/20 21:57:11 UTC
svn commit: r1633215 - in /poi/trunk/src:
java/org/apache/poi/ss/formula/eval/FunctionEval.java
java/org/apache/poi/ss/formula/functions/TextFunction.java
testcases/org/apache/poi/ss/formula/functions/TestProper.java
Author: centic
Date: Mon Oct 20 19:57:11 2014
New Revision: 1633215
URL: http://svn.apache.org/r1633215
Log:
Bug 57010: Add implementation of function PROPER
Added:
poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestProper.java
Modified:
poi/trunk/src/java/org/apache/poi/ss/formula/eval/FunctionEval.java
poi/trunk/src/java/org/apache/poi/ss/formula/functions/TextFunction.java
Modified: poi/trunk/src/java/org/apache/poi/ss/formula/eval/FunctionEval.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/eval/FunctionEval.java?rev=1633215&r1=1633214&r2=1633215&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/eval/FunctionEval.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/eval/FunctionEval.java Mon Oct 20 19:57:11 2014
@@ -207,7 +207,7 @@ public final class FunctionEval {
retval[111] = TextFunction.CHAR;
retval[112] = TextFunction.LOWER;
retval[113] = TextFunction.UPPER;
-
+ retval[114] = TextFunction.PROPER;
retval[115] = TextFunction.LEFT;
retval[116] = TextFunction.RIGHT;
retval[117] = TextFunction.EXACT;
Modified: poi/trunk/src/java/org/apache/poi/ss/formula/functions/TextFunction.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/formula/functions/TextFunction.java?rev=1633215&r1=1633214&r2=1633215&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/formula/functions/TextFunction.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/formula/functions/TextFunction.java Mon Oct 20 19:57:11 2014
@@ -17,6 +17,8 @@
package org.apache.poi.ss.formula.functions;
+import java.util.regex.Pattern;
+
import org.apache.poi.ss.formula.eval.BoolEval;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.EvaluationException;
@@ -112,6 +114,32 @@ public abstract class TextFunction imple
return new StringEval(arg.toUpperCase());
}
};
+
+ /**
+ * Implementation of the PROPER function:
+ * Normalizes all words (separated by non-word characters) by
+ * making the first letter upper and the rest lower case.
+ */
+ public static final Function PROPER = new SingleArgTextFunc() {
+ final Pattern nonAlphabeticPattern = Pattern.compile("\\P{IsL}");
+ protected ValueEval evaluate(String text) {
+ StringBuilder sb = new StringBuilder();
+ boolean shouldMakeUppercase = true;
+ String lowercaseText = text.toLowerCase();
+ String uppercaseText = text.toUpperCase();
+ for(int i = 0; i < text.length(); ++i) {
+ if (shouldMakeUppercase) {
+ sb.append(uppercaseText.charAt(i));
+ }
+ else {
+ sb.append(lowercaseText.charAt(i));
+ }
+ shouldMakeUppercase = nonAlphabeticPattern.matcher(text.subSequence(i, i + 1)).matches();
+ }
+ return new StringEval(sb.toString());
+ }
+ };
+
/**
* An implementation of the TRIM function:
* Removes leading and trailing spaces from value if evaluated operand
Added: poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestProper.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestProper.java?rev=1633215&view=auto
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestProper.java (added)
+++ poi/trunk/src/testcases/org/apache/poi/ss/formula/functions/TestProper.java Mon Oct 20 19:57:11 2014
@@ -0,0 +1,86 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.ss.formula.functions;
+
+import junit.framework.AssertionFailedError;
+import junit.framework.TestCase;
+
+import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellValue;
+import org.apache.poi.ss.usermodel.FormulaEvaluator;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.xssf.usermodel.XSSFCell;
+import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+
+public final class TestProper extends TestCase {
+ private Cell cell11;
+ private FormulaEvaluator evaluator;
+
+ public void testValidHSSF() {
+ HSSFWorkbook wb = new HSSFWorkbook();
+ evaluator = new HSSFFormulaEvaluator(wb);
+
+ confirm(wb);
+ }
+
+ public void testValidXSSF() {
+ XSSFWorkbook wb = new XSSFWorkbook();
+ evaluator = new XSSFFormulaEvaluator(wb);
+
+ confirm(wb);
+ }
+
+ private void confirm(Workbook wb) {
+ Sheet sheet = wb.createSheet("new sheet");
+ cell11 = sheet.createRow(0).createCell(0);
+ cell11.setCellType(XSSFCell.CELL_TYPE_FORMULA);
+
+ confirm("PROPER(\"hi there\")", "Hi There");
+ confirm("PROPER(\"what's up\")", "What'S Up");
+ confirm("PROPER(\"I DON'T TH!NK SO!\")", "I Don'T Th!Nk So!");
+ confirm("PROPER(\"drÃbö'ä éloÅ|ëè \")", "Drübö'à ÃloÅ|Ãè ");
+ confirm("PROPER(\"hi123 the123re\")", "Hi123 The123Re");
+ confirm("PROPER(\"-\")", "-");
+ confirm("PROPER(\"!§$\")", "!§$");
+ confirm("PROPER(\"/&%\")", "/&%");
+
+ // also test longer string
+ StringBuilder builder = new StringBuilder("A");
+ StringBuilder expected = new StringBuilder("A");
+ for(int i = 1;i < 254;i++) {
+ builder.append((char)(65 + (i % 26)));
+ expected.append((char)(97 + (i % 26)));
+ }
+ confirm("PROPER(\"" + builder.toString() + "\")", expected.toString());
+ }
+
+ private void confirm(String formulaText, String expectedResult) {
+ cell11.setCellFormula(formulaText);
+ evaluator.clearAllCachedResultValues();
+ CellValue cv = evaluator.evaluate(cell11);
+ if (cv.getCellType() != Cell.CELL_TYPE_STRING) {
+ throw new AssertionFailedError("Wrong result type: " + cv.formatAsString());
+ }
+ String actualValue = cv.getStringValue();
+ assertEquals(expectedResult, actualValue);
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org