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.

RecordFactory.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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.record;
  16. import java.io.InputStream;
  17. import java.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.List;
  20. import org.apache.poi.util.IOUtils;
  21. import org.apache.poi.util.RecordFormatException;
  22. /**
  23. * Title: Record Factory<p>
  24. * Description: Takes a stream and outputs an array of Record objects.
  25. *
  26. * @see org.apache.poi.hssf.eventmodel.EventRecordFactory
  27. */
  28. public final class RecordFactory {
  29. private static final int NUM_RECORDS = 512;
  30. // how many records we read at max by default (can be adjusted via IOUtils)
  31. //increased to 5 million due to https://bz.apache.org/bugzilla/show_bug.cgi?id=65887
  32. private static final int DEFAULT_MAX_NUMBER_OF_RECORDS = 5_000_000;
  33. private static int MAX_NUMBER_OF_RECORDS = DEFAULT_MAX_NUMBER_OF_RECORDS;
  34. public static void setMaxNumberOfRecords(int maxNumberOfRecords) {
  35. MAX_NUMBER_OF_RECORDS = maxNumberOfRecords;
  36. }
  37. public static int getMaxNumberOfRecords() {
  38. return MAX_NUMBER_OF_RECORDS;
  39. }
  40. private RecordFactory() {}
  41. /**
  42. * Debug / diagnosis method<p>
  43. *
  44. * Gets the POI implementation class for a given {@code sid}. Only a subset of the BIFF
  45. * records are actually interpreted by POI. A few others are known but not interpreted
  46. * (see {@link UnknownRecord#getBiffName(int)}).
  47. *
  48. * @param sid the record sid
  49. *
  50. * @return the POI implementation class for the specified record {@code sid}.
  51. * {@code null} if the specified record is not interpreted by POI.
  52. */
  53. public static Class<? extends Record> getRecordClass(int sid) {
  54. return HSSFRecordTypes.forSID(sid).clazz;
  55. }
  56. /**
  57. * create a record, if there are MUL records than multiple records
  58. * are returned digested into the non-mul form.
  59. *
  60. * @param in the RecordInputStream to read from
  61. * @return the extracted records
  62. */
  63. public static org.apache.poi.hssf.record.Record [] createRecord(RecordInputStream in) {
  64. Record record = createSingleRecord(in);
  65. if (record instanceof DBCellRecord) {
  66. // Not needed by POI. Regenerated from scratch by POI when spreadsheet is written
  67. return new Record[] { null, };
  68. }
  69. if (record instanceof RKRecord) {
  70. return new Record[] { convertToNumberRecord((RKRecord) record), };
  71. }
  72. if (record instanceof MulRKRecord) {
  73. return convertRKRecords((MulRKRecord)record);
  74. }
  75. return new Record[] { record, };
  76. }
  77. public static org.apache.poi.hssf.record.Record createSingleRecord(RecordInputStream in) {
  78. HSSFRecordTypes rec = HSSFRecordTypes.forSID(in.getSid());
  79. if (!rec.isParseable()) {
  80. rec = HSSFRecordTypes.UNKNOWN;
  81. }
  82. return rec.recordConstructor.apply(in);
  83. }
  84. /**
  85. * RK record is a slightly smaller alternative to NumberRecord
  86. * POI likes NumberRecord better
  87. *
  88. * @param rk the RK record to convert
  89. * @return the NumberRecord
  90. */
  91. public static NumberRecord convertToNumberRecord(RKRecord rk) {
  92. NumberRecord num = new NumberRecord();
  93. num.setColumn(rk.getColumn());
  94. num.setRow(rk.getRow());
  95. num.setXFIndex(rk.getXFIndex());
  96. num.setValue(rk.getRKNumber());
  97. return num;
  98. }
  99. /**
  100. * Converts a {@link MulRKRecord} into an equivalent array of {@link NumberRecord NumberRecords}
  101. *
  102. * @param mrk the MulRKRecord to convert
  103. * @return the equivalent array of {@link NumberRecord NumberRecords}
  104. */
  105. public static NumberRecord[] convertRKRecords(MulRKRecord mrk) {
  106. int numColumns = mrk.getNumColumns();
  107. if (numColumns < 0) {
  108. throw new RecordFormatException("Cannot create RKRecords with negative number of columns: " + numColumns);
  109. }
  110. NumberRecord[] mulRecs = new NumberRecord[numColumns];
  111. for (int k = 0; k < numColumns; k++) {
  112. NumberRecord nr = new NumberRecord();
  113. nr.setColumn((short) (k + mrk.getFirstColumn()));
  114. nr.setRow(mrk.getRow());
  115. nr.setXFIndex(mrk.getXFAt(k));
  116. nr.setValue(mrk.getRKNumberAt(k));
  117. mulRecs[k] = nr;
  118. }
  119. return mulRecs;
  120. }
  121. /**
  122. * Converts a {@link MulBlankRecord} into an equivalent array of {@link BlankRecord BlankRecords}
  123. *
  124. * @param mbk the MulBlankRecord to convert
  125. * @return the equivalent array of {@link BlankRecord BlankRecords}
  126. */
  127. public static BlankRecord[] convertBlankRecords(MulBlankRecord mbk) {
  128. BlankRecord[] mulRecs = new BlankRecord[mbk.getNumColumns()];
  129. for (int k = 0; k < mbk.getNumColumns(); k++) {
  130. BlankRecord br = new BlankRecord();
  131. br.setColumn((short) (k + mbk.getFirstColumn()));
  132. br.setRow(mbk.getRow());
  133. br.setXFIndex(mbk.getXFAt(k));
  134. mulRecs[k] = br;
  135. }
  136. return mulRecs;
  137. }
  138. /**
  139. * @return an array of all the SIDS for all known records
  140. */
  141. public static short[] getAllKnownRecordSIDs() {
  142. int[] intSid = Arrays.stream(HSSFRecordTypes.values()).mapToInt(HSSFRecordTypes::getSid).toArray();
  143. short[] shortSid = new short[intSid.length];
  144. for (int i=0; i<intSid.length; i++) {
  145. shortSid[i] = (short)intSid[i];
  146. }
  147. return shortSid;
  148. }
  149. /**
  150. * Create an array of records from an input stream
  151. *
  152. * @param in the InputStream from which the records will be obtained
  153. *
  154. * @return an array of Records created from the InputStream
  155. *
  156. * @throws org.apache.poi.util.RecordFormatException on error processing the InputStream
  157. */
  158. public static List<org.apache.poi.hssf.record.Record> createRecords(InputStream in) throws RecordFormatException {
  159. List<org.apache.poi.hssf.record.Record> records = new ArrayList<>(NUM_RECORDS);
  160. RecordFactoryInputStream recStream = new RecordFactoryInputStream(in, true);
  161. Record record;
  162. while ((record = recStream.nextRecord())!=null) {
  163. records.add(record);
  164. IOUtils.safelyAllocateCheck(records.size(), MAX_NUMBER_OF_RECORDS);
  165. }
  166. return records;
  167. }
  168. }