formats = BuiltinFormats.getBuiltinFormats();
- for(int key : formats.keySet()) {
- builtinFormats.add(key, formats.get(key));
- }
- return builtinFormats;
- }
+ /**
+ * get the format index that matches the given format string
+ * Automatically converts "text" to excel's format string to represent text.
+ * @param format string matching a built in format
+ * @return index of format or -1 if undefined.
+ */
+ public static short getBuiltinFormat(String format) {
+ return (short) BuiltinFormats.getBuiltinFormat(format);
+ }
- public static List getBuiltinFormats()
- {
- return builtinFormats;
- }
+ /**
+ * Get the format index that matches the given format
+ * string, creating a new format entry if required.
+ * Aliases text to the proper format as required.
+ * @param format string matching a built in format
+ * @return index of format.
+ */
+ public short getFormat(String pFormat) {
- /**
- * get the format index that matches the given format string
- * Automatically converts "text" to excel's format string to represent text.
- * @param format string matching a built in format
- * @return index of format or -1 if undefined.
- */
+ String format;
+ if (pFormat.toUpperCase().equals("TEXT")) {
+ format = "@";
+ } else {
+ format = pFormat;
+ }
- public static short getBuiltinFormat( String format )
- {
- return (short)BuiltinFormats.getBuiltinFormat(format);
- }
+ if (!_movedBuiltins) {
+ for (int i=0; i<_builtinFormats.length; i++) {
+ if (_formats.size() < i + 1) {
+ _formats.setSize(i + 1);
+ }
+ _formats.set(i, _builtinFormats[i]);
+ }
+ _movedBuiltins = true;
+ }
+ ListIterator i;
+ i = _formats.listIterator();
+ int ind;
+ while (i.hasNext()) {
+ ind = i.nextIndex();
+ if (format.equals(i.next())) {
+ return (short) ind;
+ }
+ }
- /**
- * Get the format index that matches the given format
- * string, creating a new format entry if required.
- * Aliases text to the proper format as required.
- * @param format string matching a built in format
- * @return index of format.
- */
- public short getFormat(String format) {
- ListIterator i;
- int ind;
+ ind = _workbook.getFormat(format, true);
+ if (_formats.size() <= ind)
+ _formats.setSize(ind + 1);
+ _formats.set(ind, format);
- if (format.toUpperCase().equals("TEXT"))
- format = "@";
+ return (short) ind;
+ }
- if (!movedBuiltins) {
- i = builtinFormats.listIterator();
- while (i.hasNext()) {
- ind = i.nextIndex();
- if (formats.size() < ind + 1) {
- formats.setSize(ind + 1);
- }
+ /**
+ * get the format string that matches the given format index
+ * @param index of a format
+ * @return string represented at index of format or null if there is not a format at that index
+ */
+ public String getFormat(short index) {
+ if (_movedBuiltins) {
+ return _formats.get(index);
+ }
+ if (_builtinFormats.length > index && _builtinFormats[index] != null) {
+ return _builtinFormats[index];
+ }
+ return _formats.get(index);
+ }
- formats.set(ind, i.next());
- }
- movedBuiltins = true;
- }
- i = formats.listIterator();
- while (i.hasNext()) {
- ind = i.nextIndex();
- if (format.equals(i.next()))
- return (short) ind;
- }
+ /**
+ * get the format string that matches the given format index
+ * @param index of a built in format
+ * @return string represented at index of format or null if there is not a builtin format at that index
+ */
+ public static String getBuiltinFormat(short index) {
+ return BuiltinFormats.getBuiltinFormat(index);
+ }
- ind = workbook.getFormat(format, true);
- if (formats.size() <= ind)
- formats.setSize(ind + 1);
- formats.set(ind, format);
-
- return (short) ind;
- }
-
- /**
- * get the format string that matches the given format index
- * @param index of a format
- * @return string represented at index of format or null if there is not a format at that index
- */
-
- public String getFormat( short index )
- {
- if ( movedBuiltins )
- return (String) formats.get( index );
- else
- return (String) ( builtinFormats.size() > index
- && builtinFormats.get( index ) != null
- ? builtinFormats.get( index ) : formats.get( index ) );
- }
-
- /**
- * get the format string that matches the given format index
- * @param index of a built in format
- * @return string represented at index of format or null if there is not a builtin format at that index
- */
-
- public static String getBuiltinFormat( short index )
- {
- return BuiltinFormats.getBuiltinFormat(index);
- }
-
- /**
- * get the number of builtin and reserved builtinFormats
- * @return number of builtin and reserved builtinFormats
- */
-
- public static int getNumberOfBuiltinBuiltinFormats()
- {
- return BuiltinFormats.getBuiltinFormats().size();
- }
+ /**
+ * get the number of built-in and reserved builtinFormats
+ * @return number of built-in and reserved builtinFormats
+ */
+ public static int getNumberOfBuiltinBuiltinFormats() {
+ return _builtinFormats.length;
+ }
}
diff --git a/src/java/org/apache/poi/ss/usermodel/BuiltinFormats.java b/src/java/org/apache/poi/ss/usermodel/BuiltinFormats.java
index f07fd96b6a..433572cb3f 100755
--- a/src/java/org/apache/poi/ss/usermodel/BuiltinFormats.java
+++ b/src/java/org/apache/poi/ss/usermodel/BuiltinFormats.java
@@ -1,133 +1,190 @@
-/* ====================================================================
- 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.usermodel;
-
-import java.util.Map;
-import java.util.LinkedHashMap;
-
-/**
- * Utility to identify builtin formats
- *
- * @author Yegor Kozlov
- */
-public class BuiltinFormats {
- /**
- * The first user-defined format starts at 164.
- */
- public static final int FIRST_USER_DEFINED_FORMAT_INDEX = 164;
-
- private final static Map formats;
-
- static {
- formats = new LinkedHashMap();
- formats.put( 0, "General" );
- formats.put( 1, "0" );
- formats.put( 2, "0.00" );
- formats.put( 3, "#,##0" );
- formats.put( 4, "#,##0.00" );
- formats.put( 5, "($#,##0_);($#,##0)" );
- formats.put( 6, "($#,##0_);[Red]($#,##0)" );
- formats.put( 7, "($#,##0.00);($#,##0.00)" );
- formats.put( 8, "($#,##0.00_);[Red]($#,##0.00)" );
- formats.put( 9, "0%" );
- formats.put( 0xa, "0.00%" );
- formats.put( 0xb, "0.00E+00" );
- formats.put( 0xc, "# ?/?" );
- formats.put( 0xd, "# ??/??" );
- formats.put( 0xe, "m/d/yy" );
- formats.put( 0xf, "d-mmm-yy" );
- formats.put( 0x10, "d-mmm" );
- formats.put( 0x11, "mmm-yy" );
- formats.put( 0x12, "h:mm AM/PM" );
- formats.put( 0x13, "h:mm:ss AM/PM" );
- formats.put( 0x14, "h:mm" );
- formats.put( 0x15, "h:mm:ss" );
- formats.put( 0x16, "m/d/yy h:mm" );
-
- // 0x17 - 0x24 reserved for international and undocumented
- formats.put( 0x17, "0x17" );
- formats.put( 0x18, "0x18" );
- formats.put( 0x19, "0x19" );
- formats.put( 0x1a, "0x1a" );
- formats.put( 0x1b, "0x1b" );
- formats.put( 0x1c, "0x1c" );
- formats.put( 0x1d, "0x1d" );
- formats.put( 0x1e, "0x1e" );
- formats.put( 0x1f, "0x1f" );
- formats.put( 0x20, "0x20" );
- formats.put( 0x21, "0x21" );
- formats.put( 0x22, "0x22" );
- formats.put( 0x23, "0x23" );
- formats.put( 0x24, "0x24" );
-
- // 0x17 - 0x24 reserved for international and undocumented
- formats.put( 0x25, "(#,##0_);(#,##0)" );
- formats.put( 0x26, "(#,##0_);[Red](#,##0)" );
- formats.put( 0x27, "(#,##0.00_);(#,##0.00)" );
- formats.put( 0x28, "(#,##0.00_);[Red](#,##0.00)" );
- formats.put( 0x29, "_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)" );
- formats.put( 0x2a, "_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)" );
- formats.put( 0x2b, "_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)" );
- formats.put( 0x2c,
- "_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)" );
- formats.put( 0x2d, "mm:ss" );
- formats.put( 0x2e, "[h]:mm:ss" );
- formats.put( 0x2f, "mm:ss.0" );
- formats.put( 0x30, "##0.0E+0" );
- formats.put( 0x31, "@" );
- }
-
- /**
- * Get the index-formatString map with built-in data formats
- *
- * @return built-in data formats
- */
- public static Map getBuiltinFormats(){
- return formats;
- }
-
- /**
- * Get the format string that matches the given format index
- *
- * @param index of a built in format
- * @return string represented at index of format or null if there is not a builtin format at that index
- */
- public static String getBuiltinFormat(int index){
- return formats.get(index);
- }
-
- /**
- * Get the format index that matches the given format string
- *
- * Automatically converts "text" to excel's format string to represent text.
- *
- * @param fmt string matching a built-in format
- * @return index of format or -1 if undefined.
- */
- public static int getBuiltinFormat(String fmt){
- int idx = -1;
- if (fmt.toUpperCase().equals("TEXT")) fmt = "@";
-
- for(int key : formats.keySet()) {
- if(fmt.equals(formats.get(key))) {
- idx = key;
- break;
- }
- }
- return idx;
- }
-}
+/* ====================================================================
+ 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.usermodel;
+
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Utility to identify built-in formats. The following is a list of the formats as
+ * returned by this class.
+ *
+ * 0, "General"
+ * 1, "0"
+ * 2, "0.00"
+ * 3, "#,##0"
+ * 4, "#,##0.00"
+ * 5, "($#,##0_);($#,##0)"
+ * 6, "($#,##0_);[Red]($#,##0)"
+ * 7, "($#,##0.00);($#,##0.00)"
+ * 8, "($#,##0.00_);[Red]($#,##0.00)"
+ * 9, "0%"
+ * 0xa, "0.00%"
+ * 0xb, "0.00E+00"
+ * 0xc, "# ?/?"
+ * 0xd, "# ??/??"
+ * 0xe, "m/d/yy"
+ * 0xf, "d-mmm-yy"
+ * 0x10, "d-mmm"
+ * 0x11, "mmm-yy"
+ * 0x12, "h:mm AM/PM"
+ * 0x13, "h:mm:ss AM/PM"
+ * 0x14, "h:mm"
+ * 0x15, "h:mm:ss"
+ * 0x16, "m/d/yy h:mm"
+ *
+ * // 0x17 - 0x24 reserved for international and undocumented
+ * 0x25, "(#,##0_);(#,##0)"
+ * 0x26, "(#,##0_);[Red](#,##0)"
+ * 0x27, "(#,##0.00_);(#,##0.00)"
+ * 0x28, "(#,##0.00_);[Red](#,##0.00)"
+ * 0x29, "_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)"
+ * 0x2a, "_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)"
+ * 0x2b, "_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)"
+ * 0x2c, "_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)"
+ * 0x2d, "mm:ss"
+ * 0x2e, "[h]:mm:ss"
+ * 0x2f, "mm:ss.0"
+ * 0x30, "##0.0E+0"
+ * 0x31, "@" - This is text format.
+ * 0x31 "text" - Alias for "@"
+ *
+ *
+ * @author Yegor Kozlov
+ */
+public final class BuiltinFormats {
+ /**
+ * The first user-defined format starts at 164.
+ */
+ public static final int FIRST_USER_DEFINED_FORMAT_INDEX = 164;
+
+ private final static String[] _formats;
+
+ static {
+ List m = new ArrayList();
+ putFormat(m, 0, "General");
+ putFormat(m, 1, "0");
+ putFormat(m, 2, "0.00");
+ putFormat(m, 3, "#,##0");
+ putFormat(m, 4, "#,##0.00");
+ putFormat(m, 5, "($#,##0_);($#,##0)");
+ putFormat(m, 6, "($#,##0_);[Red]($#,##0)");
+ putFormat(m, 7, "($#,##0.00);($#,##0.00)");
+ putFormat(m, 8, "($#,##0.00_);[Red]($#,##0.00)");
+ putFormat(m, 9, "0%");
+ putFormat(m, 0xa, "0.00%");
+ putFormat(m, 0xb, "0.00E+00");
+ putFormat(m, 0xc, "# ?/?");
+ putFormat(m, 0xd, "# ??/??");
+ putFormat(m, 0xe, "m/d/yy");
+ putFormat(m, 0xf, "d-mmm-yy");
+ putFormat(m, 0x10, "d-mmm");
+ putFormat(m, 0x11, "mmm-yy");
+ putFormat(m, 0x12, "h:mm AM/PM");
+ putFormat(m, 0x13, "h:mm:ss AM/PM");
+ putFormat(m, 0x14, "h:mm");
+ putFormat(m, 0x15, "h:mm:ss");
+ putFormat(m, 0x16, "m/d/yy h:mm");
+
+ // 0x17 - 0x24 reserved for international and undocumented
+ for (int i=0x17; i<=0x24; i++) {
+ // TODO - one junit relies on these values which seems incorrect
+ putFormat(m, i, "reserved-0x" + Integer.toHexString(i));
+ }
+
+ putFormat(m, 0x25, "(#,##0_);(#,##0)");
+ putFormat(m, 0x26, "(#,##0_);[Red](#,##0)");
+ putFormat(m, 0x27, "(#,##0.00_);(#,##0.00)");
+ putFormat(m, 0x28, "(#,##0.00_);[Red](#,##0.00)");
+ putFormat(m, 0x29, "_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)");
+ putFormat(m, 0x2a, "_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)");
+ putFormat(m, 0x2b, "_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)");
+ putFormat(m, 0x2c, "_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)");
+ putFormat(m, 0x2d, "mm:ss");
+ putFormat(m, 0x2e, "[h]:mm:ss");
+ putFormat(m, 0x2f, "mm:ss.0");
+ putFormat(m, 0x30, "##0.0E+0");
+ putFormat(m, 0x31, "@");
+ String[] ss = new String[m.size()];
+ m.toArray(ss);
+ _formats = ss;
+ }
+ private static void putFormat(List m, int index, String value) {
+ if (m.size() != index) {
+ throw new IllegalStateException("index " + index + " is wrong");
+ }
+ m.add(value);
+ }
+
+
+ /**
+ * @deprecated (May 2009) use {@link #getAll()}
+ */
+ public static Map getBuiltinFormats() {
+ Map result = new LinkedHashMap();
+ for (int i=0; i<_formats.length; i++) {
+ result.put(new Integer(i), _formats[i]);
+ }
+ return result;
+ }
+
+ /**
+ * @return array of built-in data formats
+ */
+ public static String[] getAll() {
+ return _formats.clone();
+ }
+
+ /**
+ * Get the format string that matches the given format index
+ *
+ * @param index of a built in format
+ * @return string represented at index of format or null
if there is not a built-in format at that index
+ */
+ public static String getBuiltinFormat(int index) {
+ if (index < 0 || index >=_formats.length) {
+ return null;
+ }
+ return _formats[index];
+ }
+
+ /**
+ * Get the format index that matches the given format string
+ *
+ * Automatically converts "text" to excel's format string to represent text.
+ *
+ * @param fmt string matching a built-in format
+ * @return index of format or -1 if undefined.
+ */
+ public static int getBuiltinFormat(String pFmt) {
+ String fmt;
+ if (pFmt.equalsIgnoreCase("TEXT")) {
+ fmt = "@";
+ } else {
+ fmt = pFmt;
+ }
+
+ for(int i =0; i< _formats.length; i++) {
+ if(fmt.equals(_formats[i])) {
+ return i;
+ }
+ }
+ return -1;
+ }
+}
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDataFormatter.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDataFormatter.java
index 718d909b66..48583e8946 100644
--- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDataFormatter.java
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFDataFormatter.java
@@ -22,6 +22,7 @@ import java.text.Format;
import java.util.Iterator;
import org.apache.poi.hssf.HSSFTestDataSamples;
+import org.apache.poi.ss.usermodel.Cell;
import junit.framework.TestCase;
@@ -80,8 +81,8 @@ public final class TestHSSFDataFormatter extends TestCase {
"(#,##0.00_);(#,##0.00)",
"($#,##0.00_);[Red]($#,##0.00)",
"$#,##0.00",
- "[$�-809]#,##0.00",
- "[$�-2] #,##0.00",
+ "[$-809]#,##0.00", // international format
+ "[$-2]#,##0.00", // international format
"0000.00000%",
"0.000E+00",
"0.00E+00",
@@ -170,10 +171,10 @@ public final class TestHSSFDataFormatter extends TestCase {
public void testGetFormattedCellValueHSSFCell() {
// Valid date formats -- cell values should be date formatted & not "555.555"
HSSFRow row = wb.getSheetAt(0).getRow(0);
- Iterator it = row.cellIterator();
+ Iterator it = row.cellIterator();
log("==== VALID DATE FORMATS ====");
while (it.hasNext()) {
- HSSFCell cell = (HSSFCell) it.next();
+ Cell cell = it.next();
log(formatter.formatCellValue(cell));
// should not be equal to "555.555"
@@ -256,13 +257,13 @@ public final class TestHSSFDataFormatter extends TestCase {
*/
public void testSetDefaultNumberFormat() {
HSSFRow row = wb.getSheetAt(0).getRow(2);
- Iterator it = row.cellIterator();
+ Iterator it = row.cellIterator();
Format defaultFormat = new DecimalFormat("Balance $#,#00.00 USD;Balance -$#,#00.00 USD");
formatter.setDefaultNumberFormat(defaultFormat);
log("\n==== DEFAULT NUMBER FORMAT ====");
while (it.hasNext()) {
- HSSFCell cell = (HSSFCell) it.next();
+ Cell cell = it.next();
cell.setCellValue(cell.getNumericCellValue() * Math.random() / 1000000 - 1000);
log(formatter.formatCellValue(cell));
assertTrue(formatter.formatCellValue(cell).startsWith("Balance "));
@@ -274,18 +275,18 @@ public final class TestHSSFDataFormatter extends TestCase {
* A format of "@" means use the general format
*/
public void testGeneralAtFormat() {
- HSSFWorkbook workbook = HSSFTestDataSamples.openSampleWorkbook("47154.xls");
- HSSFSheet sheet = workbook.getSheetAt(0);
- HSSFRow row = sheet.getRow(0);
- HSSFCell cellA1 = row.getCell(0);
-
- assertEquals(HSSFCell.CELL_TYPE_NUMERIC, cellA1.getCellType());
- assertEquals(2345.0, cellA1.getNumericCellValue(), 0.0001);
- assertEquals("@", cellA1.getCellStyle().getDataFormatString());
+ HSSFWorkbook workbook = HSSFTestDataSamples.openSampleWorkbook("47154.xls");
+ HSSFSheet sheet = workbook.getSheetAt(0);
+ HSSFRow row = sheet.getRow(0);
+ HSSFCell cellA1 = row.getCell(0);
- HSSFDataFormatter f = new HSSFDataFormatter();
-
- assertEquals("2345", f.formatCellValue(cellA1));
+ assertEquals(HSSFCell.CELL_TYPE_NUMERIC, cellA1.getCellType());
+ assertEquals(2345.0, cellA1.getNumericCellValue(), 0.0001);
+ assertEquals("@", cellA1.getCellStyle().getDataFormatString());
+
+ HSSFDataFormatter f = new HSSFDataFormatter();
+
+ assertEquals("2345", f.formatCellValue(cellA1));
}
private static void log(String msg) {
| |