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.

NameCommentRecord.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 org.apache.poi.util.GenericRecordUtil;
  19. import org.apache.poi.util.LittleEndianOutput;
  20. import org.apache.poi.util.StringUtil;
  21. /**
  22. * Defines a comment associated with a specified name.
  23. */
  24. public final class NameCommentRecord extends StandardRecord {
  25. public static final short sid = 0x0894;
  26. private final short field_1_record_type;
  27. private final short field_2_frt_cell_ref_flag;
  28. private final long field_3_reserved;
  29. //private short field_4_name_length;
  30. //private short field_5_comment_length;
  31. private String field_6_name_text;
  32. private String field_7_comment_text;
  33. public NameCommentRecord(NameCommentRecord other) {
  34. field_1_record_type = other.field_1_record_type;
  35. field_2_frt_cell_ref_flag = other.field_2_frt_cell_ref_flag;
  36. field_3_reserved = other.field_3_reserved;
  37. field_6_name_text = other.field_6_name_text;
  38. field_7_comment_text = other.field_7_comment_text;
  39. }
  40. public NameCommentRecord(final String name, final String comment) {
  41. field_1_record_type = 0;
  42. field_2_frt_cell_ref_flag = 0;
  43. field_3_reserved = 0;
  44. field_6_name_text = name;
  45. field_7_comment_text = comment;
  46. }
  47. /**
  48. * @param ris the RecordInputstream to read the record from
  49. */
  50. public NameCommentRecord(final RecordInputStream ris) {
  51. field_1_record_type = ris.readShort();
  52. field_2_frt_cell_ref_flag = ris.readShort();
  53. field_3_reserved = ris.readLong();
  54. final int field_4_name_length = ris.readShort();
  55. final int field_5_comment_length = ris.readShort();
  56. if (ris.readByte() == 0) {
  57. field_6_name_text = StringUtil.readCompressedUnicode(ris, field_4_name_length);
  58. } else {
  59. field_6_name_text = StringUtil.readUnicodeLE(ris, field_4_name_length);
  60. }
  61. if (ris.readByte() == 0) {
  62. field_7_comment_text = StringUtil.readCompressedUnicode(ris, field_5_comment_length);
  63. } else {
  64. field_7_comment_text = StringUtil.readUnicodeLE(ris, field_5_comment_length);
  65. }
  66. }
  67. @Override
  68. public void serialize(final LittleEndianOutput out) {
  69. final int field_4_name_length = field_6_name_text.length();
  70. final int field_5_comment_length = field_7_comment_text.length();
  71. out.writeShort(field_1_record_type);
  72. out.writeShort(field_2_frt_cell_ref_flag);
  73. out.writeLong(field_3_reserved);
  74. out.writeShort(field_4_name_length);
  75. out.writeShort(field_5_comment_length);
  76. boolean isNameMultiByte = StringUtil.hasMultibyte(field_6_name_text);
  77. out.writeByte(isNameMultiByte ? 1 : 0);
  78. if (isNameMultiByte) {
  79. StringUtil.putUnicodeLE(field_6_name_text, out);
  80. } else {
  81. StringUtil.putCompressedUnicode(field_6_name_text, out);
  82. }
  83. boolean isCommentMultiByte = StringUtil.hasMultibyte(field_7_comment_text);
  84. out.writeByte(isCommentMultiByte ? 1 : 0);
  85. if (isCommentMultiByte) {
  86. StringUtil.putUnicodeLE(field_7_comment_text, out);
  87. } else {
  88. StringUtil.putCompressedUnicode(field_7_comment_text, out);
  89. }
  90. }
  91. @Override
  92. protected int getDataSize() {
  93. return 18 // 4 shorts + 1 long + 2 spurious 'nul's
  94. + (StringUtil.hasMultibyte(field_6_name_text) ? field_6_name_text.length() * 2 : field_6_name_text.length())
  95. + (StringUtil.hasMultibyte(field_7_comment_text) ? field_7_comment_text.length() * 2 : field_7_comment_text.length());
  96. }
  97. /**
  98. * return the non static version of the id for this record.
  99. */
  100. @Override
  101. public short getSid() {
  102. return sid;
  103. }
  104. /**
  105. * @return the name of the NameRecord to which this comment applies.
  106. */
  107. public String getNameText() {
  108. return field_6_name_text;
  109. }
  110. /**
  111. * Updates the name we're associated with, normally used
  112. * when renaming that Name
  113. *
  114. * @param newName the new name
  115. */
  116. public void setNameText(String newName) {
  117. field_6_name_text = newName;
  118. }
  119. /**
  120. * @return the text of the comment.
  121. */
  122. public String getCommentText() {
  123. return field_7_comment_text;
  124. }
  125. public void setCommentText(String comment) {
  126. field_7_comment_text = comment;
  127. }
  128. public short getRecordType() {
  129. return field_1_record_type;
  130. }
  131. @Override
  132. public NameCommentRecord copy() {
  133. return new NameCommentRecord(this);
  134. }
  135. @Override
  136. public HSSFRecordTypes getGenericRecordType() {
  137. return HSSFRecordTypes.NAME_COMMENT;
  138. }
  139. @Override
  140. public Map<String, Supplier<?>> getGenericProperties() {
  141. return GenericRecordUtil.getGenericProperties(
  142. "recordType", this::getRecordType,
  143. "frtCellRefFlag", () -> field_2_frt_cell_ref_flag,
  144. "reserved", () -> field_3_reserved,
  145. "name", this::getNameText,
  146. "comment", this::getCommentText
  147. );
  148. }
  149. }