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.

FormatTrackingHSSFListener.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.eventusermodel;
  16. import java.text.NumberFormat;
  17. import java.util.ArrayList;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. import java.util.Locale;
  21. import java.util.Map;
  22. import org.apache.logging.log4j.LogManager;
  23. import org.apache.logging.log4j.Logger;
  24. import org.apache.poi.hssf.record.CellValueRecordInterface;
  25. import org.apache.poi.hssf.record.ExtendedFormatRecord;
  26. import org.apache.poi.hssf.record.FormatRecord;
  27. import org.apache.poi.hssf.record.FormulaRecord;
  28. import org.apache.poi.hssf.record.NumberRecord;
  29. import org.apache.poi.hssf.record.Record;
  30. import org.apache.poi.hssf.usermodel.HSSFDataFormat;
  31. import org.apache.poi.hssf.usermodel.HSSFDataFormatter;
  32. import org.apache.poi.util.LocaleUtil;
  33. import static org.apache.logging.log4j.util.Unbox.box;
  34. /**
  35. * A proxy HSSFListener that keeps track of the document formatting records, and
  36. * provides an easy way to look up the format strings used by cells from their
  37. * ids.
  38. */
  39. public class FormatTrackingHSSFListener implements HSSFListener {
  40. private static final Logger LOG = LogManager.getLogger(FormatTrackingHSSFListener.class);
  41. private final HSSFListener _childListener;
  42. private final HSSFDataFormatter _formatter;
  43. private final NumberFormat _defaultFormat;
  44. private final Map<Integer, FormatRecord> _customFormatRecords = new HashMap<>();
  45. private final List<ExtendedFormatRecord> _xfRecords = new ArrayList<>();
  46. /**
  47. * Creates a format tracking wrapper around the given listener, using
  48. * the {@link Locale#getDefault() default locale} for the formats.
  49. *
  50. * @param childListener the listener to be wrapped
  51. */
  52. public FormatTrackingHSSFListener(HSSFListener childListener) {
  53. this(childListener, LocaleUtil.getUserLocale());
  54. }
  55. /**
  56. * Creates a format tracking wrapper around the given listener, using
  57. * the given locale for the formats.
  58. *
  59. * @param childListener the listener to be wrapped
  60. * @param locale the locale for the formats
  61. */
  62. public FormatTrackingHSSFListener(
  63. HSSFListener childListener, Locale locale) {
  64. _childListener = childListener;
  65. _formatter = new HSSFDataFormatter(locale);
  66. _defaultFormat = NumberFormat.getInstance(locale);
  67. }
  68. protected int getNumberOfCustomFormats() {
  69. return _customFormatRecords.size();
  70. }
  71. protected int getNumberOfExtendedFormats() {
  72. return _xfRecords.size();
  73. }
  74. /**
  75. * Process this record ourselves, and then pass it on to our child listener
  76. */
  77. @Override
  78. public void processRecord(Record record) {
  79. // Handle it ourselves
  80. processRecordInternally(record);
  81. // Now pass on to our child
  82. _childListener.processRecord(record);
  83. }
  84. /**
  85. * Process the record ourselves, but do not pass it on to the child
  86. * Listener.
  87. *
  88. * @param record the record to be processed
  89. */
  90. public void processRecordInternally(Record record) {
  91. if (record instanceof FormatRecord) {
  92. FormatRecord fr = (FormatRecord) record;
  93. _customFormatRecords.put(Integer.valueOf(fr.getIndexCode()), fr);
  94. }
  95. if (record instanceof ExtendedFormatRecord) {
  96. ExtendedFormatRecord xr = (ExtendedFormatRecord) record;
  97. _xfRecords.add(xr);
  98. }
  99. }
  100. /**
  101. * Formats the given numeric of date cells contents as a String, in as
  102. * close as we can to the way that Excel would do so. Uses the various
  103. * format records to manage this.
  104. *
  105. * TODO - move this to a central class in such a way that hssf.usermodel can
  106. * make use of it too
  107. *
  108. * @param cell the cell
  109. *
  110. * @return the given numeric of date cells contents as a String
  111. */
  112. public String formatNumberDateCell(CellValueRecordInterface cell) {
  113. double value;
  114. if (cell instanceof NumberRecord) {
  115. value = ((NumberRecord) cell).getValue();
  116. } else if (cell instanceof FormulaRecord) {
  117. value = ((FormulaRecord) cell).getValue();
  118. } else {
  119. throw new IllegalArgumentException("Unsupported CellValue Record passed in " + cell);
  120. }
  121. // Get the built in format, if there is one
  122. int formatIndex = getFormatIndex(cell);
  123. String formatString = getFormatString(cell);
  124. if (formatString == null) {
  125. return _defaultFormat.format(value);
  126. }
  127. // Format, using the nice new
  128. // HSSFDataFormatter to do the work for us
  129. return _formatter.formatRawCellContents(value, formatIndex, formatString);
  130. }
  131. /**
  132. * Returns the format string, eg $##.##, for the given number format index.
  133. *
  134. * @param formatIndex the format index
  135. *
  136. * @return the format string
  137. */
  138. public String getFormatString(int formatIndex) {
  139. String format = null;
  140. if (formatIndex >= HSSFDataFormat.getNumberOfBuiltinBuiltinFormats()) {
  141. FormatRecord tfr = _customFormatRecords.get(Integer.valueOf(formatIndex));
  142. if (tfr == null) {
  143. LOG.atError().log("Requested format at index {}, but it wasn't found", box(formatIndex));
  144. } else {
  145. format = tfr.getFormatString();
  146. }
  147. } else {
  148. format = HSSFDataFormat.getBuiltinFormat((short) formatIndex);
  149. }
  150. return format;
  151. }
  152. /**
  153. * Returns the format string, eg $##.##, used by your cell
  154. *
  155. * @param cell the cell
  156. *
  157. * @return the format string
  158. */
  159. public String getFormatString(CellValueRecordInterface cell) {
  160. int formatIndex = getFormatIndex(cell);
  161. if (formatIndex == -1) {
  162. // Not found
  163. return null;
  164. }
  165. return getFormatString(formatIndex);
  166. }
  167. /**
  168. * Returns the index of the format string, used by your cell, or -1 if none found
  169. *
  170. * @param cell the cell
  171. *
  172. * @return the index of the format string
  173. */
  174. public int getFormatIndex(CellValueRecordInterface cell) {
  175. ExtendedFormatRecord xfr = _xfRecords.get(cell.getXFIndex());
  176. if (xfr == null) {
  177. LOG.atError().log("Cell {},{} uses XF with index {}, but we don't have that", box(cell.getRow()),box(cell.getColumn()),box(cell.getXFIndex()));
  178. return -1;
  179. }
  180. return xfr.getFormatIndex();
  181. }
  182. }