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

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