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.

HeaderFooterBase.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. * Common header/footer base class
  23. */
  24. public abstract class HeaderFooterBase extends StandardRecord {
  25. private boolean field_2_hasMultibyte;
  26. private String field_3_text;
  27. protected HeaderFooterBase(String text) {
  28. setText(text);
  29. }
  30. protected HeaderFooterBase(HeaderFooterBase other) {
  31. super(other);
  32. field_2_hasMultibyte = other.field_2_hasMultibyte;
  33. field_3_text = other.field_3_text;
  34. }
  35. protected HeaderFooterBase(RecordInputStream in) {
  36. if (in.remaining() > 0) {
  37. int field_1_footer_len = in.readShort();
  38. //61287 -- if the footer_len == 0, there may not be a multibyte flag
  39. if (field_1_footer_len == 0) {
  40. field_3_text = "";
  41. if (in.remaining() == 0) {
  42. return;
  43. }
  44. }
  45. field_2_hasMultibyte = in.readByte() != 0x00;
  46. if (field_2_hasMultibyte) {
  47. field_3_text = in.readUnicodeLEString(field_1_footer_len);
  48. } else {
  49. field_3_text = in.readCompressedUnicode(field_1_footer_len);
  50. }
  51. } else {
  52. // Note - this is unusual for BIFF records in general, but normal for header / footer records:
  53. // when the text is empty string, the whole record is empty (just the 4 byte BIFF header)
  54. field_3_text = "";
  55. }
  56. }
  57. /**
  58. * set the footer string
  59. *
  60. * @param text string to display
  61. */
  62. public final void setText(String text) {
  63. if (text == null) {
  64. throw new IllegalArgumentException("text must not be null");
  65. }
  66. field_2_hasMultibyte = StringUtil.hasMultibyte(text);
  67. field_3_text = text;
  68. // Check it'll fit into the space in the record
  69. if (getDataSize() > RecordInputStream.MAX_RECORD_DATA_SIZE) {
  70. throw new IllegalArgumentException("Header/Footer string too long (limit is "
  71. + RecordInputStream.MAX_RECORD_DATA_SIZE + " bytes)");
  72. }
  73. }
  74. /**
  75. * get the length of the footer string
  76. *
  77. * @return length of the footer string
  78. */
  79. private int getTextLength() {
  80. return field_3_text.length();
  81. }
  82. public final String getText() {
  83. return field_3_text;
  84. }
  85. public final void serialize(LittleEndianOutput out) {
  86. if (getTextLength() > 0) {
  87. out.writeShort(getTextLength());
  88. out.writeByte(field_2_hasMultibyte ? 0x01 : 0x00);
  89. if (field_2_hasMultibyte) {
  90. StringUtil.putUnicodeLE(field_3_text, out);
  91. } else {
  92. StringUtil.putCompressedUnicode(field_3_text, out);
  93. }
  94. }
  95. }
  96. protected final int getDataSize() {
  97. if (getTextLength() < 1) {
  98. return 0;
  99. }
  100. return 3 + getTextLength() * (field_2_hasMultibyte ? 2 : 1);
  101. }
  102. @Override
  103. public abstract HeaderFooterBase copy();
  104. @Override
  105. public Map<String, Supplier<?>> getGenericProperties() {
  106. return GenericRecordUtil.getGenericProperties("text", this::getText);
  107. }
  108. }