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.

SSTRecord.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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.util.Iterator;
  17. import java.util.Map;
  18. import java.util.function.Supplier;
  19. import org.apache.poi.hssf.record.common.UnicodeString;
  20. import org.apache.poi.hssf.record.cont.ContinuableRecord;
  21. import org.apache.poi.hssf.record.cont.ContinuableRecordOutput;
  22. import org.apache.poi.util.GenericRecordUtil;
  23. import org.apache.poi.util.IntMapper;
  24. /**
  25. * Static String Table Record (0x00FC)<p>
  26. *
  27. * This holds all the strings for LabelSSTRecords.
  28. *
  29. * @see org.apache.poi.hssf.record.LabelSSTRecord
  30. * @see org.apache.poi.hssf.record.ContinueRecord
  31. */
  32. public final class SSTRecord extends ContinuableRecord {
  33. public static final short sid = 0x00FC;
  34. private static final UnicodeString EMPTY_STRING = new UnicodeString("");
  35. /**
  36. * union of strings in the SST and EXTSST
  37. */
  38. private int field_1_num_strings;
  39. /**
  40. * according to docs ONLY SST
  41. */
  42. private int field_2_num_unique_strings;
  43. private final IntMapper<UnicodeString> field_3_strings;
  44. private final SSTDeserializer deserializer;
  45. /**
  46. * Offsets from the beginning of the SST record (even across continuations)
  47. */
  48. private int[] bucketAbsoluteOffsets;
  49. /**
  50. * Offsets relative the start of the current SST or continue record
  51. */
  52. private int[] bucketRelativeOffsets;
  53. public SSTRecord() {
  54. field_1_num_strings = 0;
  55. field_2_num_unique_strings = 0;
  56. field_3_strings = new IntMapper<>();
  57. deserializer = new SSTDeserializer(field_3_strings);
  58. }
  59. public SSTRecord(SSTRecord other) {
  60. super(other);
  61. field_1_num_strings = other.field_1_num_strings;
  62. field_2_num_unique_strings = other.field_2_num_unique_strings;
  63. field_3_strings = other.field_3_strings.copy();
  64. deserializer = new SSTDeserializer(field_3_strings);
  65. bucketAbsoluteOffsets = (other.bucketAbsoluteOffsets == null) ? null : other.bucketAbsoluteOffsets.clone();
  66. bucketRelativeOffsets = (other.bucketRelativeOffsets == null) ? null : other.bucketRelativeOffsets.clone();
  67. }
  68. /**
  69. * Add a string.
  70. *
  71. * @param string string to be added
  72. *
  73. * @return the index of that string in the table
  74. */
  75. public int addString(UnicodeString string)
  76. {
  77. field_1_num_strings++;
  78. UnicodeString ucs = ( string == null ) ? EMPTY_STRING
  79. : string;
  80. int rval;
  81. int index = field_3_strings.getIndex(ucs);
  82. if ( index != -1 ) {
  83. rval = index;
  84. } else {
  85. // This is a new string -- we didn't see it among the
  86. // strings we've already collected
  87. rval = field_3_strings.size();
  88. field_2_num_unique_strings++;
  89. SSTDeserializer.addToStringTable( field_3_strings, ucs );
  90. }
  91. return rval;
  92. }
  93. /**
  94. * @return number of strings
  95. */
  96. public int getNumStrings()
  97. {
  98. return field_1_num_strings;
  99. }
  100. /**
  101. * @return number of unique strings
  102. */
  103. public int getNumUniqueStrings()
  104. {
  105. return field_2_num_unique_strings;
  106. }
  107. /**
  108. * Get a particular string by its index
  109. *
  110. * @param id index into the array of strings
  111. *
  112. * @return the desired string
  113. */
  114. public UnicodeString getString(int id ) {
  115. return field_3_strings.get( id );
  116. }
  117. @Override
  118. public short getSid() {
  119. return sid;
  120. }
  121. /**
  122. * Fill the fields from the data
  123. * <P>
  124. * The data consists of sets of string data. This string data is
  125. * arranged as follows:
  126. * <pre>{@code
  127. * short string_length; // length of string data
  128. * byte string_flag; // flag specifying special string
  129. * // handling
  130. * short run_count; // optional count of formatting runs
  131. * int extend_length; // optional extension length
  132. * char[] string_data; // string data, can be byte[] or
  133. * // short[] (length of array is
  134. * // string_length)
  135. * int[] formatting_runs; // optional formatting runs (length of
  136. * // array is run_count)
  137. * byte[] extension; // optional extension (length of array
  138. * // is extend_length)
  139. * }</pre>
  140. * <P>
  141. * The string_flag is bit mapped as follows:
  142. * <TABLE>
  143. * <caption>string_flag mapping</caption>
  144. * <TR>
  145. * <TH>Bit number</TH>
  146. * <TH>Meaning if 0</TH>
  147. * <TH>Meaning if 1</TH>
  148. * <TR>
  149. * <TR>
  150. * <TD>0</TD>
  151. * <TD>string_data is byte[]</TD>
  152. * <TD>string_data is short[]</TD>
  153. * <TR>
  154. * <TR>
  155. * <TD>1</TD>
  156. * <TD>Should always be 0</TD>
  157. * <TD>string_flag is defective</TD>
  158. * <TR>
  159. * <TR>
  160. * <TD>2</TD>
  161. * <TD>extension is not included</TD>
  162. * <TD>extension is included</TD>
  163. * <TR>
  164. * <TR>
  165. * <TD>3</TD>
  166. * <TD>formatting run data is not included</TD>
  167. * <TD>formatting run data is included</TD>
  168. * <TR>
  169. * <TR>
  170. * <TD>4</TD>
  171. * <TD>Should always be 0</TD>
  172. * <TD>string_flag is defective</TD>
  173. * <TR>
  174. * <TR>
  175. * <TD>5</TD>
  176. * <TD>Should always be 0</TD>
  177. * <TD>string_flag is defective</TD>
  178. * <TR>
  179. * <TR>
  180. * <TD>6</TD>
  181. * <TD>Should always be 0</TD>
  182. * <TD>string_flag is defective</TD>
  183. * <TR>
  184. * <TR>
  185. * <TD>7</TD>
  186. * <TD>Should always be 0</TD>
  187. * <TD>string_flag is defective</TD>
  188. * <TR>
  189. * </TABLE>
  190. * <P>
  191. * We can handle eating the overhead associated with bits 2 or 3
  192. * (or both) being set, but we have no idea what to do with the
  193. * associated data. The UnicodeString class can handle the byte[]
  194. * vs short[] nature of the actual string data
  195. *
  196. * @param in the RecordInputStream to read the record from
  197. */
  198. public SSTRecord(RecordInputStream in) {
  199. // this method is ALWAYS called after construction -- using
  200. // the nontrivial constructor, of course -- so this is where
  201. // we initialize our fields
  202. field_1_num_strings = in.readInt();
  203. field_2_num_unique_strings = in.readInt();
  204. field_3_strings = new IntMapper<>();
  205. deserializer = new SSTDeserializer(field_3_strings);
  206. // Bug 57456: some Excel Sheets send 0 as field=1, but have some random number in field_2,
  207. // we should not try to read the strings in this case.
  208. if(field_1_num_strings == 0) {
  209. field_2_num_unique_strings = 0;
  210. return;
  211. }
  212. deserializer.manufactureStrings( field_2_num_unique_strings, in );
  213. }
  214. /**
  215. * @return an iterator of the strings we hold. All instances are
  216. * UnicodeStrings
  217. */
  218. Iterator<UnicodeString> getStrings()
  219. {
  220. return field_3_strings.iterator();
  221. }
  222. /**
  223. * @return count of the strings we hold.
  224. */
  225. int countStrings() {
  226. return field_3_strings.size();
  227. }
  228. @Override
  229. protected void serialize(ContinuableRecordOutput out) {
  230. SSTSerializer serializer = new SSTSerializer(field_3_strings, getNumStrings(), getNumUniqueStrings() );
  231. serializer.serialize(out);
  232. bucketAbsoluteOffsets = serializer.getBucketAbsoluteOffsets();
  233. bucketRelativeOffsets = serializer.getBucketRelativeOffsets();
  234. }
  235. /**
  236. * Creates an extended string record based on the current contents of
  237. * the current SST record. The offset within the stream to the SST record
  238. * is required because the extended string record points directly to the
  239. * strings in the SST record.
  240. * <p>
  241. * NOTE: THIS FUNCTION MUST ONLY BE CALLED AFTER THE SST RECORD HAS BEEN
  242. * SERIALIZED.
  243. *
  244. * @param sstOffset The offset in the stream to the start of the
  245. * SST record.
  246. * @return The new SST record.
  247. */
  248. public ExtSSTRecord createExtSSTRecord(int sstOffset) {
  249. if (bucketAbsoluteOffsets == null || bucketRelativeOffsets == null) {
  250. throw new IllegalStateException("SST record has not yet been serialized.");
  251. }
  252. ExtSSTRecord extSST = new ExtSSTRecord();
  253. extSST.setNumStringsPerBucket((short)8);
  254. int[] absoluteOffsets = bucketAbsoluteOffsets.clone();
  255. int[] relativeOffsets = bucketRelativeOffsets.clone();
  256. for ( int i = 0; i < absoluteOffsets.length; i++ ) {
  257. absoluteOffsets[i] += sstOffset;
  258. }
  259. extSST.setBucketOffsets(absoluteOffsets, relativeOffsets);
  260. return extSST;
  261. }
  262. /**
  263. * Calculates the size in bytes of the EXTSST record as it would be if the
  264. * record was serialized.
  265. *
  266. * @return The size of the ExtSST record in bytes.
  267. */
  268. public int calcExtSSTRecordSize() {
  269. return ExtSSTRecord.getRecordSizeForStrings(field_3_strings.size());
  270. }
  271. @Override
  272. public SSTRecord copy() {
  273. return new SSTRecord(this);
  274. }
  275. @Override
  276. public HSSFRecordTypes getGenericRecordType() {
  277. return HSSFRecordTypes.SST;
  278. }
  279. @Override
  280. public Map<String, Supplier<?>> getGenericProperties() {
  281. return GenericRecordUtil.getGenericProperties(
  282. "numStrings", this::getNumStrings,
  283. "numUniqueStrings", this::getNumUniqueStrings,
  284. "strings", field_3_strings::getElements,
  285. "bucketAbsoluteOffsets", () -> bucketAbsoluteOffsets,
  286. "bucketRelativeOffsets", () -> bucketRelativeOffsets
  287. );
  288. }
  289. }