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.

BoundSheetRecord.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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.Arrays;
  17. import java.util.List;
  18. import java.util.Map;
  19. import java.util.function.Supplier;
  20. import org.apache.poi.ss.util.WorkbookUtil;
  21. import org.apache.poi.util.BitField;
  22. import org.apache.poi.util.BitFieldFactory;
  23. import org.apache.poi.util.GenericRecordUtil;
  24. import org.apache.poi.util.LittleEndian;
  25. import org.apache.poi.util.LittleEndianConsts;
  26. import org.apache.poi.util.LittleEndianOutput;
  27. import org.apache.poi.util.StringUtil;
  28. /**
  29. * Defines a sheet within a workbook. Basically stores the sheet name and
  30. * tells where the Beginning of file record is within the HSSF file.
  31. */
  32. public final class BoundSheetRecord extends StandardRecord {
  33. public static final short sid = 0x0085;
  34. private static final BitField hiddenFlag = BitFieldFactory.getInstance(0x01);
  35. private static final BitField veryHiddenFlag = BitFieldFactory.getInstance(0x02);
  36. private int field_1_position_of_BOF;
  37. private int field_2_option_flags;
  38. private int field_4_isMultibyteUnicode;
  39. private String field_5_sheetname;
  40. public BoundSheetRecord(String sheetname) {
  41. field_2_option_flags = 0;
  42. setSheetname(sheetname);
  43. }
  44. public BoundSheetRecord(BoundSheetRecord other) {
  45. super(other);
  46. field_1_position_of_BOF = other.field_1_position_of_BOF;
  47. field_2_option_flags = other.field_2_option_flags;
  48. field_4_isMultibyteUnicode = other.field_4_isMultibyteUnicode;
  49. field_5_sheetname = other.field_5_sheetname;
  50. }
  51. /**
  52. * UTF8: sid + len + bof + flags + len(str) + unicode + str 2 + 2 + 4 + 2 +
  53. * 1 + 1 + len(str)
  54. *
  55. * UNICODE: sid + len + bof + flags + len(str) + unicode + str 2 + 2 + 4 + 2 +
  56. * 1 + 1 + 2 * len(str)
  57. *
  58. * @param in the record stream to read from
  59. */
  60. public BoundSheetRecord(RecordInputStream in) {
  61. byte[] buf = new byte[LittleEndianConsts.INT_SIZE];
  62. in.readPlain(buf, 0, buf.length);
  63. field_1_position_of_BOF = LittleEndian.getInt(buf);
  64. field_2_option_flags = in.readUShort();
  65. int field_3_sheetname_length = in.readUByte();
  66. field_4_isMultibyteUnicode = in.readByte();
  67. if (isMultibyte()) {
  68. field_5_sheetname = in.readUnicodeLEString(field_3_sheetname_length);
  69. } else {
  70. field_5_sheetname = in.readCompressedUnicode(field_3_sheetname_length);
  71. }
  72. }
  73. /**
  74. * set the offset in bytes of the Beginning of File Marker within the HSSF
  75. * Stream part of the POIFS file
  76. *
  77. * @param pos offset in bytes
  78. */
  79. public void setPositionOfBof(int pos) {
  80. field_1_position_of_BOF = pos;
  81. }
  82. /**
  83. * Set the sheetname for this sheet. (this appears in the tabs at the bottom)
  84. * @param sheetName the name of the sheet
  85. * @see org.apache.poi.ss.util.WorkbookUtil#createSafeSheetName(String nameProposal)
  86. * for a safe way to create valid names
  87. * @throws IllegalArgumentException if sheet name will cause excel to crash.
  88. */
  89. public void setSheetname(String sheetName) {
  90. WorkbookUtil.validateSheetName(sheetName);
  91. field_5_sheetname = sheetName;
  92. field_4_isMultibyteUnicode = StringUtil.hasMultibyte(sheetName) ? 1 : 0;
  93. }
  94. /**
  95. * get the offset in bytes of the Beginning of File Marker within the HSSF Stream part of the POIFS file
  96. *
  97. * @return offset in bytes
  98. */
  99. public int getPositionOfBof() {
  100. return field_1_position_of_BOF;
  101. }
  102. private boolean isMultibyte() {
  103. return (field_4_isMultibyteUnicode & 0x01) != 0;
  104. }
  105. /**
  106. * get the sheetname for this sheet. (this appears in the tabs at the bottom)
  107. * @return sheetname the name of the sheet
  108. */
  109. public String getSheetname() {
  110. return field_5_sheetname;
  111. }
  112. protected int getDataSize() {
  113. return 8 + field_5_sheetname.length() * (isMultibyte() ? 2 : 1);
  114. }
  115. public void serialize(LittleEndianOutput out) {
  116. out.writeInt(getPositionOfBof());
  117. out.writeShort(field_2_option_flags);
  118. String name = field_5_sheetname;
  119. out.writeByte(name.length());
  120. out.writeByte(field_4_isMultibyteUnicode);
  121. if (isMultibyte()) {
  122. StringUtil.putUnicodeLE(name, out);
  123. } else {
  124. StringUtil.putCompressedUnicode(name, out);
  125. }
  126. }
  127. public short getSid() {
  128. return sid;
  129. }
  130. /**
  131. * Is the sheet hidden? Different from very hidden
  132. *
  133. * @return {@code true} if hidden
  134. */
  135. public boolean isHidden() {
  136. return hiddenFlag.isSet(field_2_option_flags);
  137. }
  138. /**
  139. * Is the sheet hidden? Different from very hidden
  140. *
  141. * @param hidden {@code true} if hidden
  142. */
  143. public void setHidden(boolean hidden) {
  144. field_2_option_flags = hiddenFlag.setBoolean(field_2_option_flags, hidden);
  145. }
  146. /**
  147. * Is the sheet very hidden? Different from (normal) hidden
  148. *
  149. * @return {@code true} if very hidden
  150. */
  151. public boolean isVeryHidden() {
  152. return veryHiddenFlag.isSet(field_2_option_flags);
  153. }
  154. /**
  155. * Is the sheet very hidden? Different from (normal) hidden
  156. *
  157. * @param veryHidden {@code true} if very hidden
  158. */
  159. public void setVeryHidden(boolean veryHidden) {
  160. field_2_option_flags = veryHiddenFlag.setBoolean(field_2_option_flags, veryHidden);
  161. }
  162. /**
  163. * Converts a List of {@link BoundSheetRecord}s to an array and sorts by the position of their
  164. * BOFs.
  165. *
  166. * @param boundSheetRecords the boundSheetRecord list to arrayify
  167. *
  168. * @return the sorted boundSheetRecords
  169. */
  170. public static BoundSheetRecord[] orderByBofPosition(List<BoundSheetRecord> boundSheetRecords) {
  171. BoundSheetRecord[] bsrs = new BoundSheetRecord[boundSheetRecords.size()];
  172. boundSheetRecords.toArray(bsrs);
  173. Arrays.sort(bsrs, BoundSheetRecord::compareRecords);
  174. return bsrs;
  175. }
  176. private static int compareRecords(BoundSheetRecord bsr1, BoundSheetRecord bsr2) {
  177. return bsr1.getPositionOfBof() - bsr2.getPositionOfBof();
  178. }
  179. @Override
  180. public BoundSheetRecord copy() {
  181. return new BoundSheetRecord(this);
  182. }
  183. @Override
  184. public HSSFRecordTypes getGenericRecordType() {
  185. return HSSFRecordTypes.BOUND_SHEET;
  186. }
  187. @Override
  188. public Map<String, Supplier<?>> getGenericProperties() {
  189. return GenericRecordUtil.getGenericProperties(
  190. "bof", this::getPositionOfBof,
  191. "optionFlags", () -> field_2_option_flags,
  192. "multiByte", this::isMultibyte,
  193. "sheetName", this::getSheetname,
  194. "hidden", this::isHidden,
  195. "veryHidden", this::isVeryHidden
  196. );
  197. }
  198. }