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

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