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.

StyleRecord.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.BitField;
  17. import org.apache.poi.util.BitFieldFactory;
  18. import org.apache.poi.util.HexDump;
  19. import org.apache.poi.util.LittleEndianOutput;
  20. import org.apache.poi.util.StringUtil;
  21. /**
  22. * Title: Style Record (0x0293)<p/>
  23. * Description: Describes a builtin to the gui or user defined style<P>
  24. * REFERENCE: PG 390 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
  25. * @author Andrew C. Oliver (acoliver at apache dot org)
  26. * @author aviks : string fixes for UserDefined Style
  27. */
  28. public final class StyleRecord extends StandardRecord {
  29. public final static short sid = 0x0293;
  30. private static final BitField styleIndexMask = BitFieldFactory.getInstance(0x0FFF);
  31. private static final BitField isBuiltinFlag = BitFieldFactory.getInstance(0x8000);
  32. /** shared by both user defined and built-in styles */
  33. private int field_1_xf_index;
  34. // only for built in styles
  35. private int field_2_builtin_style;
  36. private int field_3_outline_style_level;
  37. // only for user defined styles
  38. private boolean field_3_stringHasMultibyte;
  39. private String field_4_name;
  40. /**
  41. * creates a new style record, initially set to 'built-in'
  42. */
  43. public StyleRecord() {
  44. field_1_xf_index = isBuiltinFlag.set(field_1_xf_index);
  45. }
  46. public StyleRecord(RecordInputStream in) {
  47. field_1_xf_index = in.readShort();
  48. if (isBuiltin()) {
  49. field_2_builtin_style = in.readByte();
  50. field_3_outline_style_level = in.readByte();
  51. } else {
  52. int field_2_name_length = in.readShort();
  53. if(in.remaining() < 1) {
  54. // Some files from Crystal Reports lack the is16BitUnicode byte
  55. // the remaining fields, which is naughty
  56. if (field_2_name_length != 0) {
  57. throw new RecordFormatException("Ran out of data reading style record");
  58. }
  59. // guess this is OK if the string length is zero
  60. field_4_name = "";
  61. } else {
  62. field_3_stringHasMultibyte = in.readByte() != 0x00;
  63. if (field_3_stringHasMultibyte) {
  64. field_4_name = StringUtil.readUnicodeLE(in, field_2_name_length);
  65. } else {
  66. field_4_name = StringUtil.readCompressedUnicode(in, field_2_name_length);
  67. }
  68. }
  69. }
  70. }
  71. /**
  72. * set the actual index of the style extended format record
  73. * @param xfIndex of the xf record
  74. */
  75. public void setXFIndex(int xfIndex) {
  76. field_1_xf_index = styleIndexMask.setValue(field_1_xf_index, xfIndex);
  77. }
  78. /**
  79. * get the actual index of the style extended format record
  80. * @see #getXFIndex()
  81. * @return index of the xf record
  82. */
  83. public int getXFIndex() {
  84. return styleIndexMask.getValue(field_1_xf_index);
  85. }
  86. /**
  87. * set the style's name
  88. * @param name of the style
  89. */
  90. public void setName(String name) {
  91. field_4_name = name;
  92. field_3_stringHasMultibyte = StringUtil.hasMultibyte(name);
  93. field_1_xf_index = isBuiltinFlag.clear(field_1_xf_index);
  94. }
  95. /**
  96. * if this is a builtin style set the number of the built in style
  97. * @param builtinStyleId style number (0-7)
  98. *
  99. */
  100. public void setBuiltinStyle(int builtinStyleId) {
  101. field_1_xf_index = isBuiltinFlag.set(field_1_xf_index);
  102. field_2_builtin_style = builtinStyleId;
  103. }
  104. /**
  105. * set the row or column level of the style (if builtin 1||2)
  106. */
  107. public void setOutlineStyleLevel(int level) {
  108. field_3_outline_style_level = level & 0x00FF;
  109. }
  110. public boolean isBuiltin(){
  111. return isBuiltinFlag.isSet(field_1_xf_index);
  112. }
  113. /**
  114. * get the style's name
  115. * @return name of the style
  116. */
  117. public String getName() {
  118. return field_4_name;
  119. }
  120. public String toString() {
  121. StringBuffer sb = new StringBuffer();
  122. sb.append("[STYLE]\n");
  123. sb.append(" .xf_index_raw =").append(HexDump.shortToHex(field_1_xf_index)).append("\n");
  124. sb.append(" .type =").append(isBuiltin() ? "built-in" : "user-defined").append("\n");
  125. sb.append(" .xf_index =").append(HexDump.shortToHex(getXFIndex())).append("\n");
  126. if (isBuiltin()){
  127. sb.append(" .builtin_style=").append(HexDump.byteToHex(field_2_builtin_style)).append("\n");
  128. sb.append(" .outline_level=").append(HexDump.byteToHex(field_3_outline_style_level)).append("\n");
  129. } else {
  130. sb.append(" .name =").append(getName()).append("\n");
  131. }
  132. sb.append("[/STYLE]\n");
  133. return sb.toString();
  134. }
  135. protected int getDataSize() {
  136. if (isBuiltin()) {
  137. return 4; // short, byte, byte
  138. }
  139. return 2 // short xf index
  140. + 3 // str len + flag
  141. + field_4_name.length() * (field_3_stringHasMultibyte ? 2 : 1);
  142. }
  143. public void serialize(LittleEndianOutput out) {
  144. out.writeShort(field_1_xf_index);
  145. if (isBuiltin()) {
  146. out.writeByte(field_2_builtin_style);
  147. out.writeByte(field_3_outline_style_level);
  148. } else {
  149. out.writeShort(field_4_name.length());
  150. out.writeByte(field_3_stringHasMultibyte ? 0x01 : 0x00);
  151. if (field_3_stringHasMultibyte) {
  152. StringUtil.putUnicodeLE(getName(), out);
  153. } else {
  154. StringUtil.putCompressedUnicode(getName(), out);
  155. }
  156. }
  157. }
  158. public short getSid() {
  159. return sid;
  160. }
  161. }