You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BuiltinFormats.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.usermodel;
  16. import java.util.ArrayList;
  17. import java.util.LinkedHashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. /**
  21. * Utility to identify built-in formats. The following is a list of the formats as
  22. * returned by this class.<p/>
  23. *<p/>
  24. * 0, "General"<br/>
  25. * 1, "0"<br/>
  26. * 2, "0.00"<br/>
  27. * 3, "#,##0"<br/>
  28. * 4, "#,##0.00"<br/>
  29. * 5, "$#,##0_);($#,##0)"<br/>
  30. * 6, "$#,##0_);[Red]($#,##0)"<br/>
  31. * 7, "$#,##0.00);($#,##0.00)"<br/>
  32. * 8, "$#,##0.00_);[Red]($#,##0.00)"<br/>
  33. * 9, "0%"<br/>
  34. * 0xa, "0.00%"<br/>
  35. * 0xb, "0.00E+00"<br/>
  36. * 0xc, "# ?/?"<br/>
  37. * 0xd, "# ??/??"<br/>
  38. * 0xe, "m/d/yy"<br/>
  39. * 0xf, "d-mmm-yy"<br/>
  40. * 0x10, "d-mmm"<br/>
  41. * 0x11, "mmm-yy"<br/>
  42. * 0x12, "h:mm AM/PM"<br/>
  43. * 0x13, "h:mm:ss AM/PM"<br/>
  44. * 0x14, "h:mm"<br/>
  45. * 0x15, "h:mm:ss"<br/>
  46. * 0x16, "m/d/yy h:mm"<br/>
  47. *<p/>
  48. * // 0x17 - 0x24 reserved for international and undocumented
  49. * 0x25, "#,##0_);(#,##0)"<br/>
  50. * 0x26, "#,##0_);[Red](#,##0)"<br/>
  51. * 0x27, "#,##0.00_);(#,##0.00)"<br/>
  52. * 0x28, "#,##0.00_);[Red](#,##0.00)"<br/>
  53. * 0x29, "_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)"<br/>
  54. * 0x2a, "_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)"<br/>
  55. * 0x2b, "_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)"<br/>
  56. * 0x2c, "_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)"<br/>
  57. * 0x2d, "mm:ss"<br/>
  58. * 0x2e, "[h]:mm:ss"<br/>
  59. * 0x2f, "mm:ss.0"<br/>
  60. * 0x30, "##0.0E+0"<br/>
  61. * 0x31, "@" - This is text format.<br/>
  62. * 0x31 "text" - Alias for "@"<br/>
  63. * <p/>
  64. *
  65. * @author Yegor Kozlov
  66. *
  67. * Modified 6/17/09 by Stanislav Shor - positive formats don't need starting '('
  68. *
  69. */
  70. public final class BuiltinFormats {
  71. /**
  72. * The first user-defined format starts at 164.
  73. */
  74. public static final int FIRST_USER_DEFINED_FORMAT_INDEX = 164;
  75. private final static String[] _formats;
  76. static {
  77. List<String> m = new ArrayList<String>();
  78. putFormat(m, 0, "General");
  79. putFormat(m, 1, "0");
  80. putFormat(m, 2, "0.00");
  81. putFormat(m, 3, "#,##0");
  82. putFormat(m, 4, "#,##0.00");
  83. putFormat(m, 5, "$#,##0_);($#,##0)");
  84. putFormat(m, 6, "$#,##0_);[Red]($#,##0)");
  85. putFormat(m, 7, "$#,##0.00_);($#,##0.00)");
  86. putFormat(m, 8, "$#,##0.00_);[Red]($#,##0.00)");
  87. putFormat(m, 9, "0%");
  88. putFormat(m, 0xa, "0.00%");
  89. putFormat(m, 0xb, "0.00E+00");
  90. putFormat(m, 0xc, "# ?/?");
  91. putFormat(m, 0xd, "# ??/??");
  92. putFormat(m, 0xe, "m/d/yy");
  93. putFormat(m, 0xf, "d-mmm-yy");
  94. putFormat(m, 0x10, "d-mmm");
  95. putFormat(m, 0x11, "mmm-yy");
  96. putFormat(m, 0x12, "h:mm AM/PM");
  97. putFormat(m, 0x13, "h:mm:ss AM/PM");
  98. putFormat(m, 0x14, "h:mm");
  99. putFormat(m, 0x15, "h:mm:ss");
  100. putFormat(m, 0x16, "m/d/yy h:mm");
  101. // 0x17 - 0x24 reserved for international and undocumented
  102. for (int i=0x17; i<=0x24; i++) {
  103. // TODO - one junit relies on these values which seems incorrect
  104. putFormat(m, i, "reserved-0x" + Integer.toHexString(i));
  105. }
  106. putFormat(m, 0x25, "#,##0_);(#,##0)");
  107. putFormat(m, 0x26, "#,##0_);[Red](#,##0)");
  108. putFormat(m, 0x27, "#,##0.00_);(#,##0.00)");
  109. putFormat(m, 0x28, "#,##0.00_);[Red](#,##0.00)");
  110. putFormat(m, 0x29, "_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)");
  111. putFormat(m, 0x2a, "_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)");
  112. putFormat(m, 0x2b, "_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)");
  113. putFormat(m, 0x2c, "_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)");
  114. putFormat(m, 0x2d, "mm:ss");
  115. putFormat(m, 0x2e, "[h]:mm:ss");
  116. putFormat(m, 0x2f, "mm:ss.0");
  117. putFormat(m, 0x30, "##0.0E+0");
  118. putFormat(m, 0x31, "@");
  119. String[] ss = new String[m.size()];
  120. m.toArray(ss);
  121. _formats = ss;
  122. }
  123. private static void putFormat(List<String> m, int index, String value) {
  124. if (m.size() != index) {
  125. throw new IllegalStateException("index " + index + " is wrong");
  126. }
  127. m.add(value);
  128. }
  129. /**
  130. * @deprecated (May 2009) use {@link #getAll()}
  131. */
  132. public static Map<Integer, String> getBuiltinFormats() {
  133. Map<Integer, String> result = new LinkedHashMap<Integer, String>();
  134. for (int i=0; i<_formats.length; i++) {
  135. result.put(new Integer(i), _formats[i]);
  136. }
  137. return result;
  138. }
  139. /**
  140. * @return array of built-in data formats
  141. */
  142. public static String[] getAll() {
  143. return _formats.clone();
  144. }
  145. /**
  146. * Get the format string that matches the given format index
  147. *
  148. * @param index of a built in format
  149. * @return string represented at index of format or <code>null</code> if there is not a built-in format at that index
  150. */
  151. public static String getBuiltinFormat(int index) {
  152. if (index < 0 || index >=_formats.length) {
  153. return null;
  154. }
  155. return _formats[index];
  156. }
  157. /**
  158. * Get the format index that matches the given format string
  159. * <p/>
  160. * Automatically converts "text" to excel's format string to represent text.
  161. * </p>
  162. * @param fmt string matching a built-in format
  163. * @return index of format or -1 if undefined.
  164. */
  165. public static int getBuiltinFormat(String pFmt) {
  166. String fmt;
  167. if (pFmt.equalsIgnoreCase("TEXT")) {
  168. fmt = "@";
  169. } else {
  170. fmt = pFmt;
  171. }
  172. for(int i =0; i< _formats.length; i++) {
  173. if(fmt.equals(_formats[i])) {
  174. return i;
  175. }
  176. }
  177. return -1;
  178. }
  179. }