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.

FeatRecord.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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.Map;
  17. import java.util.function.Supplier;
  18. import java.util.stream.Stream;
  19. import org.apache.poi.hssf.record.common.FeatFormulaErr2;
  20. import org.apache.poi.hssf.record.common.FeatProtection;
  21. import org.apache.poi.hssf.record.common.FeatSmartTag;
  22. import org.apache.poi.hssf.record.common.FtrHeader;
  23. import org.apache.poi.hssf.record.common.SharedFeature;
  24. import org.apache.poi.ss.util.CellRangeAddress;
  25. import org.apache.poi.util.GenericRecordUtil;
  26. import org.apache.poi.util.LittleEndianOutput;
  27. import org.apache.poi.util.POILogFactory;
  28. import org.apache.poi.util.POILogger;
  29. /**
  30. * Title: Feat (Feature) Record
  31. * <P>
  32. * This record specifies Shared Features data. It is normally paired
  33. * up with a {@link FeatHdrRecord}.
  34. */
  35. public final class FeatRecord extends StandardRecord {
  36. private static final POILogger LOG = POILogFactory.getLogger(FeatRecord.class);
  37. public static final short sid = 0x0868;
  38. // SIDs from newer versions
  39. public static final short v11_sid = 0x0872;
  40. public static final short v12_sid = 0x0878;
  41. private final FtrHeader futureHeader;
  42. /** See SHAREDFEATURES_* on {@link FeatHdrRecord} */
  43. private int isf_sharedFeatureType;
  44. private byte reserved1; // Should always be zero
  45. private long reserved2; // Should always be zero
  46. /** Only matters if type is ISFFEC2 */
  47. private long cbFeatData;
  48. private int reserved3; // Should always be zero
  49. private CellRangeAddress[] cellRefs;
  50. /**
  51. * Contents depends on isf_sharedFeatureType :
  52. * ISFPROTECTION -> FeatProtection
  53. * ISFFEC2 -> FeatFormulaErr2
  54. * ISFFACTOID -> FeatSmartTag
  55. */
  56. private SharedFeature sharedFeature;
  57. public FeatRecord() {
  58. futureHeader = new FtrHeader();
  59. futureHeader.setRecordType(sid);
  60. }
  61. public FeatRecord(FeatRecord other) {
  62. super(other);
  63. futureHeader = other.futureHeader.copy();
  64. isf_sharedFeatureType = other.isf_sharedFeatureType;
  65. reserved1 = other.reserved1;
  66. reserved2 = other.reserved2;
  67. cbFeatData = other.cbFeatData;
  68. reserved3 = other.reserved3;
  69. cellRefs = (other.cellRefs == null) ? null :
  70. Stream.of(other.cellRefs).map(CellRangeAddress::copy).toArray(CellRangeAddress[]::new);
  71. sharedFeature = (other.sharedFeature == null) ? null : other.sharedFeature.copy();
  72. }
  73. public FeatRecord(RecordInputStream in) {
  74. futureHeader = new FtrHeader(in);
  75. isf_sharedFeatureType = in.readShort();
  76. reserved1 = in.readByte();
  77. reserved2 = in.readInt();
  78. int cref = in.readUShort();
  79. cbFeatData = in.readInt();
  80. reserved3 = in.readShort();
  81. cellRefs = new CellRangeAddress[cref];
  82. for(int i=0; i<cellRefs.length; i++) {
  83. cellRefs[i] = new CellRangeAddress(in);
  84. }
  85. switch(isf_sharedFeatureType) {
  86. case FeatHdrRecord.SHAREDFEATURES_ISFPROTECTION:
  87. sharedFeature = new FeatProtection(in);
  88. break;
  89. case FeatHdrRecord.SHAREDFEATURES_ISFFEC2:
  90. sharedFeature = new FeatFormulaErr2(in);
  91. break;
  92. case FeatHdrRecord.SHAREDFEATURES_ISFFACTOID:
  93. sharedFeature = new FeatSmartTag(in);
  94. break;
  95. default:
  96. LOG.log( POILogger.ERROR, "Unknown Shared Feature ", isf_sharedFeatureType, " found!");
  97. }
  98. }
  99. public short getSid() {
  100. return sid;
  101. }
  102. public void serialize(LittleEndianOutput out) {
  103. futureHeader.serialize(out);
  104. out.writeShort(isf_sharedFeatureType);
  105. out.writeByte(reserved1);
  106. out.writeInt((int)reserved2);
  107. out.writeShort(cellRefs.length);
  108. out.writeInt((int)cbFeatData);
  109. out.writeShort(reserved3);
  110. for (CellRangeAddress cellRef : cellRefs) {
  111. cellRef.serialize(out);
  112. }
  113. sharedFeature.serialize(out);
  114. }
  115. protected int getDataSize() {
  116. return 12 + 2+1+4+2+4+2+
  117. (cellRefs.length * CellRangeAddress.ENCODED_SIZE)
  118. +sharedFeature.getDataSize();
  119. }
  120. public int getIsf_sharedFeatureType() {
  121. return isf_sharedFeatureType;
  122. }
  123. public long getCbFeatData() {
  124. return cbFeatData;
  125. }
  126. public void setCbFeatData(long cbFeatData) {
  127. this.cbFeatData = cbFeatData;
  128. }
  129. public CellRangeAddress[] getCellRefs() {
  130. return cellRefs;
  131. }
  132. public void setCellRefs(CellRangeAddress[] cellRefs) {
  133. this.cellRefs = cellRefs;
  134. }
  135. public SharedFeature getSharedFeature() {
  136. return sharedFeature;
  137. }
  138. public void setSharedFeature(SharedFeature feature) {
  139. this.sharedFeature = feature;
  140. if(feature instanceof FeatProtection) {
  141. isf_sharedFeatureType = FeatHdrRecord.SHAREDFEATURES_ISFPROTECTION;
  142. }
  143. if(feature instanceof FeatFormulaErr2) {
  144. isf_sharedFeatureType = FeatHdrRecord.SHAREDFEATURES_ISFFEC2;
  145. }
  146. if(feature instanceof FeatSmartTag) {
  147. isf_sharedFeatureType = FeatHdrRecord.SHAREDFEATURES_ISFFACTOID;
  148. }
  149. if(isf_sharedFeatureType == FeatHdrRecord.SHAREDFEATURES_ISFFEC2) {
  150. cbFeatData = sharedFeature.getDataSize();
  151. } else {
  152. cbFeatData = 0;
  153. }
  154. }
  155. @Override
  156. public FeatRecord copy() {
  157. return new FeatRecord(this);
  158. }
  159. @Override
  160. public HSSFRecordTypes getGenericRecordType() {
  161. return HSSFRecordTypes.FEAT;
  162. }
  163. @Override
  164. public Map<String, Supplier<?>> getGenericProperties() {
  165. return GenericRecordUtil.getGenericProperties(
  166. "futureHeader", () -> futureHeader,
  167. "isf_sharedFeatureType", this::getIsf_sharedFeatureType,
  168. "reserved1", () -> reserved1,
  169. "reserved2", () -> reserved2,
  170. "cbFeatData", this::getCbFeatData,
  171. "reserved3", () -> reserved3,
  172. "cellRefs", this::getCellRefs,
  173. "sharedFeature", this::getSharedFeature
  174. );
  175. }
  176. }