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.

BOFRecord.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.Collections;
  17. import java.util.LinkedHashMap;
  18. import java.util.Map;
  19. import java.util.function.Supplier;
  20. import org.apache.poi.util.LittleEndianOutput;
  21. /**
  22. * Somewhat of a misnomer, its used for the beginning of a set of records that
  23. * have a particular purpose or subject. Used in sheets and workbooks.
  24. */
  25. public final class BOFRecord extends StandardRecord {
  26. /**
  27. * for BIFF8 files the BOF is 0x809. For earlier versions see
  28. * {@link #biff2_sid} {@link #biff3_sid} {@link #biff4_sid}
  29. * {@link #biff5_sid}
  30. */
  31. public static final short sid = 0x809;
  32. // SIDs from earlier BIFF versions
  33. public static final short biff2_sid = 0x009;
  34. public static final short biff3_sid = 0x209;
  35. public static final short biff4_sid = 0x409;
  36. public static final short biff5_sid = 0x809;
  37. /** suggested default (0x0600 - BIFF8) */
  38. public static final int VERSION = 0x0600;
  39. /** suggested default 0x10d3 */
  40. public static final int BUILD = 0x10d3;
  41. /** suggested default 0x07CC (1996) */
  42. public static final int BUILD_YEAR = 0x07CC; // 1996
  43. /** suggested default for a normal sheet (0x41) */
  44. public static final int HISTORY_MASK = 0x41;
  45. public static final int TYPE_WORKBOOK = 0x05;
  46. public static final int TYPE_VB_MODULE = 0x06;
  47. public static final int TYPE_WORKSHEET = 0x10;
  48. public static final int TYPE_CHART = 0x20;
  49. public static final int TYPE_EXCEL_4_MACRO = 0x40;
  50. public static final int TYPE_WORKSPACE_FILE = 0x100;
  51. private int field_1_version;
  52. private int field_2_type;
  53. private int field_3_build;
  54. private int field_4_year;
  55. private int field_5_history;
  56. private int field_6_rversion;
  57. /**
  58. * Constructs an empty BOFRecord with no fields set.
  59. */
  60. public BOFRecord() {}
  61. public BOFRecord(BOFRecord other) {
  62. super(other);
  63. field_1_version = other.field_1_version;
  64. field_2_type = other.field_2_type;
  65. field_3_build = other.field_3_build;
  66. field_4_year = other.field_4_year;
  67. field_5_history = other.field_5_history;
  68. field_6_rversion = other.field_6_rversion;
  69. }
  70. private BOFRecord(int type) {
  71. field_1_version = VERSION;
  72. field_2_type = type;
  73. field_3_build = BUILD;
  74. field_4_year = BUILD_YEAR;
  75. field_5_history = 0x01;
  76. field_6_rversion = VERSION;
  77. }
  78. public static BOFRecord createSheetBOF() {
  79. return new BOFRecord(TYPE_WORKSHEET);
  80. }
  81. public BOFRecord(RecordInputStream in) {
  82. field_1_version = in.readShort();
  83. field_2_type = in.readShort();
  84. // Some external tools don't generate all of
  85. // the remaining fields
  86. if (in.remaining() >= 2) {
  87. field_3_build = in.readShort();
  88. }
  89. if (in.remaining() >= 2) {
  90. field_4_year = in.readShort();
  91. }
  92. if (in.remaining() >= 4) {
  93. field_5_history = in.readInt();
  94. }
  95. if (in.remaining() >= 4) {
  96. field_6_rversion = in.readInt();
  97. }
  98. }
  99. /**
  100. * Version number - for BIFF8 should be 0x06
  101. * @see #VERSION
  102. * @param version version to be set
  103. */
  104. public void setVersion(int version) {
  105. field_1_version = version;
  106. }
  107. /**
  108. * type of object this marks
  109. * @see #TYPE_WORKBOOK
  110. * @see #TYPE_VB_MODULE
  111. * @see #TYPE_WORKSHEET
  112. * @see #TYPE_CHART
  113. * @see #TYPE_EXCEL_4_MACRO
  114. * @see #TYPE_WORKSPACE_FILE
  115. * @param type type to be set
  116. */
  117. public void setType(int type) {
  118. field_2_type = type;
  119. }
  120. /**
  121. * build that wrote this file
  122. * @see #BUILD
  123. * @param build build number to set
  124. */
  125. public void setBuild(int build) {
  126. field_3_build = build;
  127. }
  128. /**
  129. * Year of the build that wrote this file
  130. * @see #BUILD_YEAR
  131. * @param year build year to set
  132. */
  133. public void setBuildYear(int year) {
  134. field_4_year = year;
  135. }
  136. /**
  137. * set the history bit mask (not very useful)
  138. * @see #HISTORY_MASK
  139. * @param bitmask bitmask to set for the history
  140. */
  141. public void setHistoryBitMask(int bitmask) {
  142. field_5_history = bitmask;
  143. }
  144. /**
  145. * set the minimum version required to read this file
  146. *
  147. * @see #VERSION
  148. * @param version version to set
  149. */
  150. public void setRequiredVersion(int version) {
  151. field_6_rversion = version;
  152. }
  153. /**
  154. * Version number - for BIFF8 should be 0x06
  155. * @see #VERSION
  156. * @return version number of the generator of this file
  157. */
  158. public int getVersion() {
  159. return field_1_version;
  160. }
  161. /**
  162. * type of object this marks
  163. * @see #TYPE_WORKBOOK
  164. * @see #TYPE_VB_MODULE
  165. * @see #TYPE_WORKSHEET
  166. * @see #TYPE_CHART
  167. * @see #TYPE_EXCEL_4_MACRO
  168. * @see #TYPE_WORKSPACE_FILE
  169. * @return type of object
  170. */
  171. public int getType() {
  172. return field_2_type;
  173. }
  174. /**
  175. * get the build that wrote this file
  176. * @see #BUILD
  177. * @return short build number of the generator of this file
  178. */
  179. public int getBuild() {
  180. return field_3_build;
  181. }
  182. /**
  183. * Year of the build that wrote this file
  184. * @see #BUILD_YEAR
  185. * @return short build year of the generator of this file
  186. */
  187. public int getBuildYear() {
  188. return field_4_year;
  189. }
  190. /**
  191. * get the history bit mask (not very useful)
  192. * @see #HISTORY_MASK
  193. * @return int bitmask showing the history of the file (who cares!)
  194. */
  195. public int getHistoryBitMask() {
  196. return field_5_history;
  197. }
  198. /**
  199. * get the minimum version required to read this file
  200. *
  201. * @see #VERSION
  202. * @return int least version that can read the file
  203. */
  204. public int getRequiredVersion() {
  205. return field_6_rversion;
  206. }
  207. private String getTypeName() {
  208. switch(field_2_type) {
  209. case TYPE_CHART: return "chart";
  210. case TYPE_EXCEL_4_MACRO: return "excel 4 macro";
  211. case TYPE_VB_MODULE: return "vb module";
  212. case TYPE_WORKBOOK: return "workbook";
  213. case TYPE_WORKSHEET: return "worksheet";
  214. case TYPE_WORKSPACE_FILE: return "workspace file";
  215. }
  216. return "#error unknown type#";
  217. }
  218. public void serialize(LittleEndianOutput out) {
  219. out.writeShort(getVersion());
  220. out.writeShort(getType());
  221. out.writeShort(getBuild());
  222. out.writeShort(getBuildYear());
  223. out.writeInt(getHistoryBitMask());
  224. out.writeInt(getRequiredVersion());
  225. }
  226. protected int getDataSize() {
  227. return 16;
  228. }
  229. public short getSid(){
  230. return sid;
  231. }
  232. @Override
  233. public BOFRecord copy() {
  234. return new BOFRecord(this);
  235. }
  236. @Override
  237. public HSSFRecordTypes getGenericRecordType() {
  238. return HSSFRecordTypes.BOF;
  239. }
  240. @Override
  241. public Map<String, Supplier<?>> getGenericProperties() {
  242. final Map<String,Supplier<?>> m = new LinkedHashMap<>();
  243. m.put("version", this::getVersion);
  244. m.put("type", this::getType);
  245. m.put("typeName", this::getTypeName);
  246. m.put("build", this::getBuild);
  247. m.put("buildYear", this::getBuildYear);
  248. m.put("history", this::getHistoryBitMask);
  249. m.put("requiredVersion", this::getRequiredVersion);
  250. return Collections.unmodifiableMap(m);
  251. }
  252. }