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.

HSSFDataFormat.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.hssf.usermodel;
  16. import java.util.Arrays;
  17. import java.util.Iterator;
  18. import java.util.List;
  19. import java.util.Vector;
  20. import org.apache.poi.hssf.model.InternalWorkbook;
  21. import org.apache.poi.hssf.record.FormatRecord;
  22. import org.apache.poi.ss.usermodel.BuiltinFormats;
  23. import org.apache.poi.ss.usermodel.DataFormat;
  24. /**
  25. * Identifies both built-in and user defined formats within a workbook.<p>
  26. * See {@link BuiltinFormats} for a list of supported built-in formats.<p>
  27. *
  28. * <b>International Formats</b><br>
  29. * Since version 2003 Excel has supported international formats. These are denoted
  30. * with a prefix "[$-xxx]" (where xxx is a 1-7 digit hexadecimal number).
  31. * See the Microsoft article
  32. * <a href="http://office.microsoft.com/assistance/hfws.aspx?AssetID=HA010346351033&CTT=6&Origin=EC010272491033">
  33. * Creating international number formats
  34. * </a> for more details on these codes.
  35. */
  36. public final class HSSFDataFormat implements DataFormat {
  37. private static final String[] _builtinFormats = BuiltinFormats.getAll();
  38. private final Vector<String> _formats = new Vector<>();
  39. private final InternalWorkbook _workbook;
  40. private boolean _movedBuiltins; // Flag to see if need to
  41. // check the built in list
  42. // or if the regular list
  43. // has all entries.
  44. /**
  45. * Constructs a new data formatter. It takes a workbook to have
  46. * access to the workbooks format records.
  47. * @param workbook the workbook the formats are tied to.
  48. */
  49. HSSFDataFormat(InternalWorkbook workbook) {
  50. _workbook = workbook;
  51. Iterator<FormatRecord> i = workbook.getFormats().iterator();
  52. while (i.hasNext()) {
  53. FormatRecord r = i.next();
  54. ensureFormatsSize(r.getIndexCode());
  55. _formats.set(r.getIndexCode(), r.getFormatString());
  56. }
  57. }
  58. public static List<String> getBuiltinFormats() {
  59. return Arrays.asList(_builtinFormats);
  60. }
  61. /**
  62. * get the format index that matches the given format string<p>
  63. * Automatically converts "text" to excel's format string to represent text.
  64. * @param format string matching a built in format
  65. * @return index of format or -1 if undefined.
  66. */
  67. public static short getBuiltinFormat(String format) {
  68. return (short) BuiltinFormats.getBuiltinFormat(format);
  69. }
  70. /**
  71. * Get the format index that matches the given format
  72. * string, creating a new format entry if required.
  73. * Aliases text to the proper format as required.
  74. * @param pFormat string matching a built in format
  75. * @return index of format.
  76. */
  77. public short getFormat(String pFormat) {
  78. // Normalise the format string
  79. String format;
  80. if (pFormat.equalsIgnoreCase("TEXT")) {
  81. format = "@";
  82. } else {
  83. format = pFormat;
  84. }
  85. // Merge in the built in formats if we haven't already
  86. if (!_movedBuiltins) {
  87. for (int i=0; i<_builtinFormats.length; i++) {
  88. ensureFormatsSize(i);
  89. if (_formats.get(i) == null) {
  90. _formats.set(i, _builtinFormats[i]);
  91. } else {
  92. // The workbook overrides this default format
  93. }
  94. }
  95. _movedBuiltins = true;
  96. }
  97. // See if we can find it
  98. for(int i=0; i<_formats.size(); i++) {
  99. if(format.equals(_formats.get(i))) {
  100. return (short)i;
  101. }
  102. }
  103. // We can't find it, so add it as a new one
  104. short index = _workbook.getFormat(format, true);
  105. ensureFormatsSize(index);
  106. _formats.set(index, format);
  107. return index;
  108. }
  109. /**
  110. * get the format string that matches the given format index
  111. * @param index of a format
  112. * @return string represented at index of format or null if there is not a format at that index
  113. */
  114. public String getFormat(short index) {
  115. if (_movedBuiltins) {
  116. return _formats.get(index);
  117. }
  118. if(index == -1) {
  119. // YK: formatIndex can be -1, for example, for cell in column Y in test-data/spreadsheet/45322.xls
  120. // return null for those
  121. return null;
  122. }
  123. String fmt = _formats.size() > index ? _formats.get(index) : null;
  124. if (_builtinFormats.length > index && _builtinFormats[index] != null) {
  125. // It's in the built in range
  126. if (fmt != null) {
  127. // It's been overriden, use that value
  128. return fmt;
  129. } else {
  130. // Standard built in format
  131. return _builtinFormats[index];
  132. }
  133. }
  134. return fmt;
  135. }
  136. /**
  137. * get the format string that matches the given format index
  138. * @param index of a built in format
  139. * @return string represented at index of format or null if there is not a builtin format at that index
  140. */
  141. public static String getBuiltinFormat(short index) {
  142. return BuiltinFormats.getBuiltinFormat(index);
  143. }
  144. /**
  145. * get the number of built-in and reserved builtinFormats
  146. * @return number of built-in and reserved builtinFormats
  147. */
  148. public static int getNumberOfBuiltinBuiltinFormats() {
  149. return _builtinFormats.length;
  150. }
  151. /**
  152. * Ensures that the formats list can hold entries
  153. * up to and including the entry with this index
  154. */
  155. private void ensureFormatsSize(int index) {
  156. if(_formats.size() <= index) {
  157. _formats.setSize(index+1);
  158. }
  159. }
  160. }