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.

FormatRecord.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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.Arrays;
  17. import java.util.Map;
  18. import java.util.function.Supplier;
  19. import org.apache.logging.log4j.LogManager;
  20. import org.apache.logging.log4j.Logger;
  21. import org.apache.poi.hssf.model.InternalWorkbook;
  22. import org.apache.poi.util.GenericRecordUtil;
  23. import org.apache.poi.util.LittleEndianConsts;
  24. import org.apache.poi.util.LittleEndianOutput;
  25. import org.apache.poi.util.StringUtil;
  26. import static org.apache.logging.log4j.util.Unbox.box;
  27. /**
  28. * Describes a number format -- those goofy strings like $(#,###)
  29. */
  30. public final class FormatRecord extends StandardRecord {
  31. private static final Logger LOG = LogManager.getLogger(FormatRecord.class);
  32. public static final short sid = 0x041E;
  33. private final int field_1_index_code;
  34. private final boolean field_3_hasMultibyte;
  35. private final String field_4_formatstring;
  36. private FormatRecord(FormatRecord other) {
  37. super(other);
  38. field_1_index_code = other.field_1_index_code;
  39. field_3_hasMultibyte = other.field_3_hasMultibyte;
  40. field_4_formatstring = other.field_4_formatstring;
  41. }
  42. public FormatRecord(int indexCode, String fs) {
  43. field_1_index_code = indexCode;
  44. field_4_formatstring = fs;
  45. field_3_hasMultibyte = StringUtil.hasMultibyte(fs);
  46. }
  47. public FormatRecord(RecordInputStream in) {
  48. field_1_index_code = in.readShort();
  49. int field_3_unicode_len = in.readUShort();
  50. field_3_hasMultibyte = (in.readByte() & 0x01) != 0;
  51. if (field_3_hasMultibyte) {
  52. field_4_formatstring = readStringCommon(in, field_3_unicode_len, false);
  53. } else {
  54. field_4_formatstring = readStringCommon(in, field_3_unicode_len, true);
  55. }
  56. }
  57. /**
  58. * get the format index code (for built in formats)
  59. *
  60. * @return the format index code
  61. * @see InternalWorkbook
  62. */
  63. public int getIndexCode() {
  64. return field_1_index_code;
  65. }
  66. /**
  67. * get the format string
  68. *
  69. * @return the format string
  70. */
  71. public String getFormatString() {
  72. return field_4_formatstring;
  73. }
  74. public void serialize(LittleEndianOutput out) {
  75. String formatString = getFormatString();
  76. out.writeShort(getIndexCode());
  77. out.writeShort(formatString.length());
  78. out.writeByte(field_3_hasMultibyte ? 0x01 : 0x00);
  79. if ( field_3_hasMultibyte ) {
  80. StringUtil.putUnicodeLE( formatString, out);
  81. } else {
  82. StringUtil.putCompressedUnicode( formatString, out);
  83. }
  84. }
  85. protected int getDataSize() {
  86. return 5 // 2 shorts + 1 byte
  87. + getFormatString().length() * (field_3_hasMultibyte ? 2 : 1);
  88. }
  89. public short getSid() {
  90. return sid;
  91. }
  92. @Override
  93. public FormatRecord copy() {
  94. return new FormatRecord(this);
  95. }
  96. private static String readStringCommon(RecordInputStream ris, int requestedLength, boolean pIsCompressedEncoding) {
  97. //custom copy of ris.readUnicodeLEString to allow for extra bytes at the end
  98. // Sanity check to detect garbage string lengths
  99. if (requestedLength < 0 || requestedLength > 0x100000) { // 16 million chars?
  100. throw new IllegalArgumentException("Bad requested string length (" + requestedLength + ")");
  101. }
  102. char[] buf;
  103. int availableChars = pIsCompressedEncoding ? ris.remaining() : ris.remaining() / LittleEndianConsts.SHORT_SIZE;
  104. //everything worked out. Great!
  105. if (requestedLength == availableChars) {
  106. buf = new char[requestedLength];
  107. } else {
  108. //sometimes in older Excel 97 .xls files,
  109. //the requested length is wrong.
  110. //Read all available characters.
  111. buf = new char[availableChars];
  112. }
  113. for (int i = 0; i < buf.length; i++) {
  114. char ch;
  115. if (pIsCompressedEncoding) {
  116. ch = (char) ris.readUByte();
  117. } else {
  118. ch = (char) ris.readShort();
  119. }
  120. buf[i] = ch;
  121. }
  122. //TIKA-2154's file shows that even in a unicode string
  123. //there can be a remaining byte (without proper final '00')
  124. //that should be read as a byte
  125. if (ris.available() == 1) {
  126. char[] tmp = Arrays.copyOf(buf, buf.length+1);
  127. tmp[buf.length] = (char)ris.readUByte();
  128. buf = tmp;
  129. }
  130. if (ris.available() > 0) {
  131. LOG.atInfo().log("FormatRecord has {} unexplained bytes. Silently skipping", box(ris.available()));
  132. //swallow what's left
  133. while (ris.available() > 0) {
  134. ris.readByte();
  135. }
  136. }
  137. return new String(buf);
  138. }
  139. @Override
  140. public HSSFRecordTypes getGenericRecordType() {
  141. return HSSFRecordTypes.FORMAT;
  142. }
  143. @Override
  144. public Map<String, Supplier<?>> getGenericProperties() {
  145. return GenericRecordUtil.getGenericProperties(
  146. "indexCode", this::getIndexCode,
  147. "unicode", () -> field_3_hasMultibyte,
  148. "formatString", this::getFormatString
  149. );
  150. }
  151. }