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 6.4KB

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