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

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