]> source.dussan.org Git - poi.git/commitdiff
Bugzilla 55036 - Dec2HEx formula support
authorYegor Kozlov <yegor@apache.org>
Sun, 2 Jun 2013 15:13:47 +0000 (15:13 +0000)
committerYegor Kozlov <yegor@apache.org>
Sun, 2 Jun 2013 15:13:47 +0000 (15:13 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1488729 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/usermodel/HSSFRow.java
src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
src/java/org/apache/poi/ss/formula/functions/Dec2Hex.java [new file with mode: 0644]
src/java/org/apache/poi/util/Configurator.java [new file with mode: 0644]
src/testcases/org/apache/poi/ss/formula/functions/BaseTestFunctionsFromSpreadsheet.java
src/testcases/org/apache/poi/ss/formula/functions/TestDec2Hex.java [new file with mode: 0644]

index 402397e542f9ad442b03070f967c3218df603253..8dfe8e722482623d4ad02390713876e0b041c548 100644 (file)
@@ -27,6 +27,7 @@ import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.CellStyle;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.SpreadsheetVersion;
+import org.apache.poi.util.Configurator;
 
 /**
  * High level representation of a row of a spreadsheet.
@@ -39,7 +40,7 @@ import org.apache.poi.ss.SpreadsheetVersion;
 public final class HSSFRow implements Row {
 
     // used for collections
-    public final static int INITIAL_CAPACITY = 5;
+    public final static int INITIAL_CAPACITY = Configurator.getIntValue("HSSFRow.ColInitialCapacity", 5);
 
     private int rowNum;
     private HSSFCell[] cells;
index 5e53c022ada28ea1d3b3b94027f2b0dfe307494c..e0e3503edbe86ba4f1d9efd40e512342ce39a9b7 100644 (file)
@@ -50,6 +50,7 @@ import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.ss.util.CellReference;
 import org.apache.poi.ss.util.SSCellRange;
 import org.apache.poi.ss.util.SheetUtil;
+import org.apache.poi.util.Configurator;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
 
@@ -74,7 +75,7 @@ public final class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet {
      * rows.  It is currently set to 20.  If you generate larger sheets you may benefit
      * by setting this to a higher number and recompiling a custom edition of HSSFSheet.
      */
-    public final static int INITIAL_CAPACITY = 20;
+    public final static int INITIAL_CAPACITY = Configurator.getIntValue("HSSFSheet.RowInitialCapacity", 20);
 
     /**
      * reference to the low level {@link InternalSheet} object
index 8f2a33f6b56af521454d52b56879abd7aece943c..9d1873b3c3b8665583c237e1d29c42da1c6fd0bf 100644 (file)
@@ -29,6 +29,7 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.regex.Pattern;
 
+import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.poi.POIDocument;
 import org.apache.poi.ddf.EscherBSERecord;
 import org.apache.poi.ddf.EscherBitmapBlip;
@@ -54,9 +55,9 @@ import org.apache.poi.ss.formula.udf.UDFFinder;
 import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.ss.util.WorkbookUtil;
+import org.apache.poi.util.Configurator;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
-import org.apache.commons.codec.digest.DigestUtils;
 import org.apache.poi.ss.formula.udf.IndexedUDFFinder;
 
 
@@ -93,7 +94,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
      * since you're never allowed to have more or less than three sheets!
      */
 
-    public final static int INITIAL_CAPACITY = 3;
+    public final static int INITIAL_CAPACITY = Configurator.getIntValue("HSSFWorkbook.SheetInitialCapacity",3);
 
     /**
      * this is the reference to the low level Workbook object
diff --git a/src/java/org/apache/poi/ss/formula/functions/Dec2Hex.java b/src/java/org/apache/poi/ss/formula/functions/Dec2Hex.java
new file mode 100644 (file)
index 0000000..85d5df5
--- /dev/null
@@ -0,0 +1,122 @@
+/* ====================================================================\r
+   Licensed to the Apache Software Foundation (ASF) under one or more\r
+   contributor license agreements.  See the NOTICE file distributed with\r
+   this work for additional information regarding copyright ownership.\r
+   The ASF licenses this file to You under the Apache License, Version 2.0\r
+   (the "License"); you may not use this file except in compliance with\r
+   the License.  You may obtain a copy of the License at\r
+\r
+       http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.ss.formula.functions;\r
+\r
+import org.apache.poi.ss.formula.eval.*;\r
+\r
+/**\r
+ * Implementation for Excel DELTA() function.<p/>\r
+ * <p/>\r
+ * <b>Syntax</b>:<br/> <b>DEC2HEX  </b>(<b>number</b>,<b>places</b> )<br/>\r
+ * <p/>\r
+ * Converts a decimal number to hexadecimal.\r
+ *\r
+ * The decimal integer you want to convert. If number is negative, places is ignored\r
+ * and this function returns a 10-character (40-bit) hexadecimal number in which the\r
+ * most significant bit is the sign bit. The remaining 39 bits are magnitude bits.\r
+ * Negative numbers are represented using two's-complement notation.\r
+ *\r
+ * <ul>\r
+ * <li>If number < -549,755,813,888 or if number > 549,755,813,887, this function returns the #NUM! error value.</li>\r
+ * <li>If number is nonnumeric, this function returns the #VALUE! error value.</li>\r
+ * </ul>\r
+ *\r
+ * <h2>places</h2>\r
+ *\r
+ * The number of characters to use. The places argument is useful for padding the\r
+ * return value with leading 0s (zeros).\r
+ *\r
+ * <ul>\r
+ * <li>If this argument is omitted, this function uses the minimum number of characters necessary.</li>\r
+ * <li>If this function requires more than places characters, it returns the #NUM! error value.</li>\r
+ * <li>If this argument is nonnumeric, this function returns the #VALUE! error value.</li>\r
+ * <li>If this argument is negative, this function returns the #NUM! error value.</li>\r
+ * <li>If this argument contains a decimal value, this function ignores the numbers to the right side of the decimal point.</li>\r
+ * </ul>\r
+ *\r
+ * @author cedric dot walter @ gmail dot com\r
+ */\r
+public final class Dec2Hex extends Var1or2ArgFunction {\r
+\r
+    private final static long MIN_VALUE = new Long("-549755813888").longValue();\r
+    private final static long MAX_VALUE = new Long("549755813887").longValue();\r
+    private final static int DEFAULT_PLACES_VALUE = 10;;\r
+\r
+    @Override\r
+    public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval number, ValueEval places) {\r
+        ValueEval veText1;\r
+        try {\r
+            veText1 = OperandResolver.getSingleValue(number, srcRowIndex, srcColumnIndex);\r
+        } catch (EvaluationException e) {\r
+            return e.getErrorEval();\r
+        }\r
+        String strText1 = OperandResolver.coerceValueToString(veText1);\r
+        Double number1 = OperandResolver.parseDouble(strText1);\r
+\r
+        //If this number argument is non numeric, this function returns the #VALUE! error value.\r
+        if (number1 == null) {\r
+            return ErrorEval.VALUE_INVALID;\r
+        }\r
+\r
+        //If number < -549,755,813,888 or if number > 549,755,813,887, this function returns the #NUM! error value.\r
+        if (number1.longValue() < MIN_VALUE || number1.longValue() > MAX_VALUE)  {\r
+            return ErrorEval.NUM_ERROR;\r
+        }\r
+\r
+        int placesNumber = 0;\r
+        if (number1 < 0) {\r
+            placesNumber = DEFAULT_PLACES_VALUE;\r
+        } else  {\r
+            ValueEval placesValueEval;\r
+            try {\r
+                placesValueEval = OperandResolver.getSingleValue(places, srcRowIndex, srcColumnIndex);\r
+            } catch (EvaluationException e) {\r
+                return e.getErrorEval();\r
+            }\r
+            String placesStr = OperandResolver.coerceValueToString(placesValueEval);\r
+            Double placesNumberDouble = OperandResolver.parseDouble(placesStr);\r
+\r
+            //non numeric value\r
+            if (placesNumberDouble == null) {\r
+                return ErrorEval.VALUE_INVALID;\r
+            }\r
+\r
+            //If this argument contains a decimal value, this function ignores the numbers to the right side of the decimal point.\r
+            placesNumber = placesNumberDouble.intValue();\r
+\r
+            if (placesNumber < 0)  {\r
+                return ErrorEval.NUM_ERROR;\r
+            }\r
+        }\r
+\r
+        String hex = "";\r
+        if (placesNumber != 0) {\r
+            hex = String.format("%0"+placesNumber+"X", number1.intValue() & 0x0FFFFF);\r
+        }\r
+        else {\r
+            hex = Integer.toHexString(number1.intValue());\r
+        }\r
+\r
+        return new StringEval(hex.toUpperCase());\r
+    }\r
+\r
+    @Override\r
+    public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) {\r
+        return this.evaluate(srcRowIndex, srcColumnIndex, arg0, new StringEval(String.valueOf(DEFAULT_PLACES_VALUE)));\r
+    }\r
+}
\ No newline at end of file
diff --git a/src/java/org/apache/poi/util/Configurator.java b/src/java/org/apache/poi/util/Configurator.java
new file mode 100644 (file)
index 0000000..7269030
--- /dev/null
@@ -0,0 +1,40 @@
+package org.apache.poi.util;\r
+\r
+/* ====================================================================\r
+   Licensed to the Apache Software Foundation (ASF) under one or more\r
+   contributor license agreements.  See the NOTICE file distributed with\r
+   this work for additional information regarding copyright ownership.\r
+   The ASF licenses this file to You under the Apache License, Version 2.0\r
+   (the "License"); you may not use this file except in compliance with\r
+   the License.  You may obtain a copy of the License at\r
+\r
+       http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+==================================================================== */\r
+\r
+/**\r
+ *\r
+ * @author Cedric Walter (cedric.walter at innoveo.com)\r
+ */\r
+public class Configurator {\r
+\r
+    private static POILogger logger = POILogFactory.getLogger(Configurator.class);\r
+\r
+    public static int getIntValue(String systemProperty, int defaultValue) {\r
+        int result = defaultValue;\r
+        String property = System.getProperty(systemProperty);\r
+        try {\r
+            result = Integer.valueOf(property);\r
+        } catch (Exception e) {\r
+            logger.log(POILogger.ERROR, "System property -D"+systemProperty +" do not contains a valid integer " + property);\r
+        }\r
+        return result;\r
+    }\r
+\r
+\r
+}\r
index 920593c83f6a6dcfff06dbacbe537e5bcae583e9..6993e15f0f0c1ef62a0479cd56801f105a2177e6 100644 (file)
@@ -226,7 +226,7 @@ public abstract class BaseTestFunctionsFromSpreadsheet extends TestCase {
             HSSFCell expectedValueCell = r.getCell(SS.COLUMN_INDEX_EXPECTED_RESULT);\r
             String rowComment = getRowCommentColumnValue(r);\r
 \r
-            String msgPrefix = formatTestCaseDetails(sheetName, r.getRowNum(), c, currentGroupComment, rowComment);\r
+            String msgPrefix = formatTestCaseDetails(this.getFilename(),sheetName, r.getRowNum(), c, currentGroupComment, rowComment);\r
             try {\r
                 confirmExpectedResult(msgPrefix, expectedValueCell, actualValue);\r
                 _evaluationSuccessCount ++;\r
@@ -250,10 +250,13 @@ public abstract class BaseTestFunctionsFromSpreadsheet extends TestCase {
     }\r
 \r
 \r
-    private static String formatTestCaseDetails(String sheetName, int rowIndex, HSSFCell c, String currentGroupComment,\r
+    private static String formatTestCaseDetails(String filename, String sheetName, int rowIndex, HSSFCell c, String currentGroupComment,\r
                                                 String rowComment) {\r
 \r
         StringBuffer sb = new StringBuffer();\r
+\r
+        sb.append("In ").append(filename).append(" ");\r
+\r
         CellReference cr = new CellReference(sheetName, rowIndex, c.getColumnIndex(), false, false);\r
         sb.append(cr.formatAsString());\r
         sb.append(" {=").append(c.getCellFormula()).append("}");\r
diff --git a/src/testcases/org/apache/poi/ss/formula/functions/TestDec2Hex.java b/src/testcases/org/apache/poi/ss/formula/functions/TestDec2Hex.java
new file mode 100644 (file)
index 0000000..8ff090c
--- /dev/null
@@ -0,0 +1,79 @@
+/* ====================================================================
+   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.TestCase;
+import org.apache.poi.ss.formula.eval.ErrorEval;
+import org.apache.poi.ss.formula.eval.StringEval;
+import org.apache.poi.ss.formula.eval.ValueEval;
+
+/**
+ * Tests for {@link Dec2Hex}
+ *
+ * @author cedric dot walter @ gmail dot com
+ */
+public final class TestDec2Hex extends TestCase {
+
+       private static ValueEval invokeValue(String number1, String number2) {
+               ValueEval[] args = new ValueEval[] { new StringEval(number1), new StringEval(number2), };
+               return new Dec2Hex().evaluate(args, -1, -1);
+       }
+
+    private static ValueEval invokeValue(String number1) {
+               ValueEval[] args = new ValueEval[] { new StringEval(number1), };
+               return new Dec2Hex().evaluate(args, -1, -1);
+       }
+
+       private static void confirmValue(String msg, String number1, String number2, String expected) {
+               ValueEval result = invokeValue(number1, number2);
+               assertEquals(StringEval.class, result.getClass());
+               assertEquals(msg, expected, ((StringEval) result).getStringValue());
+       }
+
+    private static void confirmValue(String msg, String number1, String expected) {
+               ValueEval result = invokeValue(number1);
+               assertEquals(StringEval.class, result.getClass());
+               assertEquals(msg, expected, ((StringEval) result).getStringValue());
+       }
+
+    private static void confirmValueError(String msg, String number1, String number2, ErrorEval numError) {
+        ValueEval result = invokeValue(number1, number2);
+        assertEquals(ErrorEval.class, result.getClass());
+        assertEquals(msg, numError, result);
+    }
+
+       public void testBasic() {
+               confirmValue("Converts decimal 100 to hexadecimal with 0 characters (64)", "100","0", "64");
+               confirmValue("Converts decimal 100 to hexadecimal with 4 characters (0064)", "100","4", "0064");
+               confirmValue("Converts decimal 100 to hexadecimal with 5 characters (0064)", "100","5", "00064");
+               confirmValue("Converts decimal 100 to hexadecimal with 10 (default) characters", "100","10", "0000000064");
+               confirmValue("If argument places contains a decimal value, dec2hex ignores the numbers to the right side of the decimal point.", "100","10.0", "0000000064");
+
+               confirmValue("Converts decimal -54 to hexadecimal, 0 is ignored", "-54", "0",  "00000FFFCA");
+               confirmValue("Converts decimal -54 to hexadecimal, 2 is ignored","-54", "2",  "00000FFFCA");
+               confirmValue("places is optionnal","-54", "00000FFFCA");
+       }
+
+    public void testErrors() {
+        confirmValueError("Out of range min number","-549755813889","0", ErrorEval.NUM_ERROR);
+        confirmValueError("Out of range max number","549755813888","0", ErrorEval.NUM_ERROR);
+
+        confirmValueError("negative places not allowed","549755813888","-10", ErrorEval.NUM_ERROR);
+        confirmValueError("non number places not allowed","ABCDEF","0", ErrorEval.VALUE_INVALID);
+    }
+}