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

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