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.2KB

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