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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 org.apache.poi.util.HexDump;
  17. import org.apache.poi.util.LittleEndianInput;
  18. import org.apache.poi.util.LittleEndianOutput;
  19. import org.apache.poi.util.StringUtil;
  20. /**
  21. * Title: NAMECMT Record (0x0894)
  22. * <p/>
  23. * Description: Defines a comment associated with a specified name.
  24. * <P>
  25. * REFERENCE:
  26. * <P>
  27. *
  28. * @author Andrew Shirley (aks at corefiling.co.uk)
  29. */
  30. public final class NameCommentRecord extends StandardRecord {
  31. public final static short sid = 0x0894;
  32. private final short field_1_record_type;
  33. private final short field_2_frt_cell_ref_flag;
  34. private final long field_3_reserved;
  35. //private short field_4_name_length;
  36. //private short field_5_comment_length;
  37. private String field_6_name_text;
  38. private String field_7_comment_text;
  39. public NameCommentRecord(final String name, final String comment) {
  40. field_1_record_type = 0;
  41. field_2_frt_cell_ref_flag = 0;
  42. field_3_reserved = 0;
  43. field_6_name_text = name;
  44. field_7_comment_text = comment;
  45. }
  46. @Override
  47. public void serialize(final LittleEndianOutput out) {
  48. final int field_4_name_length = field_6_name_text.length();
  49. final int field_5_comment_length = field_7_comment_text.length();
  50. out.writeShort(field_1_record_type);
  51. out.writeShort(field_2_frt_cell_ref_flag);
  52. out.writeLong(field_3_reserved);
  53. out.writeShort(field_4_name_length);
  54. out.writeShort(field_5_comment_length);
  55. boolean isNameMultiByte = StringUtil.hasMultibyte(field_6_name_text);
  56. out.writeByte(isNameMultiByte ? 1 : 0);
  57. if (isNameMultiByte) {
  58. StringUtil.putUnicodeLE(field_6_name_text, out);
  59. } else {
  60. StringUtil.putCompressedUnicode(field_6_name_text, out);
  61. }
  62. boolean isCommentMultiByte = StringUtil.hasMultibyte(field_7_comment_text);
  63. out.writeByte(isCommentMultiByte ? 1 : 0);
  64. if (isCommentMultiByte) {
  65. StringUtil.putUnicodeLE(field_7_comment_text, out);
  66. } else {
  67. StringUtil.putCompressedUnicode(field_7_comment_text, out);
  68. }
  69. }
  70. @Override
  71. protected int getDataSize() {
  72. return 18 // 4 shorts + 1 long + 2 spurious 'nul's
  73. + (StringUtil.hasMultibyte(field_6_name_text) ? field_6_name_text.length()*2 : field_6_name_text.length())
  74. + (StringUtil.hasMultibyte(field_7_comment_text) ? field_7_comment_text.length()*2 : field_7_comment_text.length());
  75. }
  76. /**
  77. * @param ris the RecordInputstream to read the record from
  78. */
  79. public NameCommentRecord(final RecordInputStream ris) {
  80. final LittleEndianInput in = ris;
  81. field_1_record_type = in.readShort();
  82. field_2_frt_cell_ref_flag = in.readShort();
  83. field_3_reserved = in.readLong();
  84. final int field_4_name_length = in.readShort();
  85. final int field_5_comment_length = in.readShort();
  86. if (in.readByte() == 0) {
  87. field_6_name_text = StringUtil.readCompressedUnicode(in, field_4_name_length);
  88. } else {
  89. field_6_name_text = StringUtil.readUnicodeLE(in, field_4_name_length);
  90. }
  91. if (in.readByte() == 0) {
  92. field_7_comment_text = StringUtil.readCompressedUnicode(in, field_5_comment_length);
  93. } else {
  94. field_7_comment_text = StringUtil.readUnicodeLE(in, field_5_comment_length);
  95. }
  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. @Override
  105. public String toString() {
  106. final StringBuffer sb = new StringBuffer();
  107. sb.append("[NAMECMT]\n");
  108. sb.append(" .record type = ").append(HexDump.shortToHex(field_1_record_type)).append("\n");
  109. sb.append(" .frt cell ref flag = ").append(HexDump.byteToHex(field_2_frt_cell_ref_flag)).append("\n");
  110. sb.append(" .reserved = ").append(field_3_reserved).append("\n");
  111. sb.append(" .name length = ").append(field_6_name_text.length()).append("\n");
  112. sb.append(" .comment length = ").append(field_7_comment_text.length()).append("\n");
  113. sb.append(" .name = ").append(field_6_name_text).append("\n");
  114. sb.append(" .comment = ").append(field_7_comment_text).append("\n");
  115. sb.append("[/NAMECMT]\n");
  116. return sb.toString();
  117. }
  118. /**
  119. * @return the name of the NameRecord to which this comment applies.
  120. */
  121. public String getNameText() {
  122. return field_6_name_text;
  123. }
  124. /**
  125. * Updates the name we're associated with, normally used
  126. * when renaming that Name
  127. */
  128. public void setNameText(String newName) {
  129. field_6_name_text = newName;
  130. }
  131. /**
  132. * @return the text of the comment.
  133. */
  134. public String getCommentText() {
  135. return field_7_comment_text;
  136. }
  137. public void setCommentText(String comment) {
  138. field_7_comment_text = comment;
  139. }
  140. public short getRecordType() {
  141. return field_1_record_type;
  142. }
  143. }