2002-01-30 21:22:28 -05:00
|
|
|
/* ====================================================================
|
2006-12-22 14:18:16 -05:00
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
(the "License"); you may not use this file except in compliance with
|
|
|
|
the License. You may obtain a copy of the License at
|
2004-04-09 09:05:39 -04:00
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
==================================================================== */
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
package org.apache.poi.hssf.record;
|
|
|
|
|
2003-07-27 15:15:16 -04:00
|
|
|
import org.apache.poi.util.BitField;
|
2005-09-01 23:48:31 -04:00
|
|
|
import org.apache.poi.util.BitFieldFactory;
|
2008-10-23 20:58:49 -04:00
|
|
|
import org.apache.poi.util.HexDump;
|
2008-11-18 19:06:06 -05:00
|
|
|
import org.apache.poi.util.LittleEndianOutput;
|
2008-08-06 02:27:39 -04:00
|
|
|
import org.apache.poi.util.StringUtil;
|
2002-01-30 21:22:28 -05:00
|
|
|
|
|
|
|
/**
|
2016-06-13 19:08:13 -04:00
|
|
|
* Title: Style Record (0x0293)<p>
|
|
|
|
* Description: Describes a builtin to the gui or user defined style<p>
|
|
|
|
* REFERENCE: PG 390 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)
|
2002-01-30 21:22:28 -05:00
|
|
|
*/
|
2008-11-18 19:06:06 -05:00
|
|
|
public final class StyleRecord extends StandardRecord {
|
2008-10-23 20:58:49 -04:00
|
|
|
public final static short sid = 0x0293;
|
|
|
|
|
|
|
|
private static final BitField styleIndexMask = BitFieldFactory.getInstance(0x0FFF);
|
|
|
|
private static final BitField isBuiltinFlag = BitFieldFactory.getInstance(0x8000);
|
|
|
|
|
|
|
|
/** shared by both user defined and built-in styles */
|
|
|
|
private int field_1_xf_index;
|
|
|
|
|
|
|
|
// only for built in styles
|
|
|
|
private int field_2_builtin_style;
|
|
|
|
private int field_3_outline_style_level;
|
|
|
|
|
|
|
|
// only for user defined styles
|
2008-11-18 19:06:06 -05:00
|
|
|
private boolean field_3_stringHasMultibyte;
|
2008-10-23 20:58:49 -04:00
|
|
|
private String field_4_name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* creates a new style record, initially set to 'built-in'
|
|
|
|
*/
|
|
|
|
public StyleRecord() {
|
2016-03-12 06:37:12 -05:00
|
|
|
field_1_xf_index = isBuiltinFlag.set(0);
|
2008-10-23 20:58:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public StyleRecord(RecordInputStream in) {
|
|
|
|
field_1_xf_index = in.readShort();
|
|
|
|
if (isBuiltin()) {
|
|
|
|
field_2_builtin_style = in.readByte();
|
|
|
|
field_3_outline_style_level = in.readByte();
|
|
|
|
} else {
|
|
|
|
int field_2_name_length = in.readShort();
|
2013-08-23 06:44:39 -04:00
|
|
|
|
2008-10-23 20:58:49 -04:00
|
|
|
if(in.remaining() < 1) {
|
|
|
|
// Some files from Crystal Reports lack the is16BitUnicode byte
|
|
|
|
// the remaining fields, which is naughty
|
|
|
|
if (field_2_name_length != 0) {
|
|
|
|
throw new RecordFormatException("Ran out of data reading style record");
|
|
|
|
}
|
|
|
|
// guess this is OK if the string length is zero
|
|
|
|
field_4_name = "";
|
|
|
|
} else {
|
2013-08-23 06:44:39 -04:00
|
|
|
|
2008-11-18 19:06:06 -05:00
|
|
|
field_3_stringHasMultibyte = in.readByte() != 0x00;
|
|
|
|
if (field_3_stringHasMultibyte) {
|
2008-10-23 20:58:49 -04:00
|
|
|
field_4_name = StringUtil.readUnicodeLE(in, field_2_name_length);
|
|
|
|
} else {
|
|
|
|
field_4_name = StringUtil.readCompressedUnicode(in, field_2_name_length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the actual index of the style extended format record
|
|
|
|
* @param xfIndex of the xf record
|
|
|
|
*/
|
|
|
|
public void setXFIndex(int xfIndex) {
|
|
|
|
field_1_xf_index = styleIndexMask.setValue(field_1_xf_index, xfIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the actual index of the style extended format record
|
2013-08-23 06:44:39 -04:00
|
|
|
* @see #getXFIndex()
|
2008-10-23 20:58:49 -04:00
|
|
|
* @return index of the xf record
|
|
|
|
*/
|
|
|
|
public int getXFIndex() {
|
|
|
|
return styleIndexMask.getValue(field_1_xf_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the style's name
|
|
|
|
* @param name of the style
|
|
|
|
*/
|
|
|
|
public void setName(String name) {
|
|
|
|
field_4_name = name;
|
2008-11-18 19:06:06 -05:00
|
|
|
field_3_stringHasMultibyte = StringUtil.hasMultibyte(name);
|
2008-10-23 20:58:49 -04:00
|
|
|
field_1_xf_index = isBuiltinFlag.clear(field_1_xf_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* if this is a builtin style set the number of the built in style
|
|
|
|
* @param builtinStyleId style number (0-7)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public void setBuiltinStyle(int builtinStyleId) {
|
|
|
|
field_1_xf_index = isBuiltinFlag.set(field_1_xf_index);
|
|
|
|
field_2_builtin_style = builtinStyleId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set the row or column level of the style (if builtin 1||2)
|
|
|
|
*/
|
|
|
|
public void setOutlineStyleLevel(int level) {
|
|
|
|
field_3_outline_style_level = level & 0x00FF;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isBuiltin(){
|
|
|
|
return isBuiltinFlag.isSet(field_1_xf_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get the style's name
|
|
|
|
* @return name of the style
|
|
|
|
*/
|
|
|
|
public String getName() {
|
|
|
|
return field_4_name;
|
|
|
|
}
|
|
|
|
|
2013-08-23 06:44:39 -04:00
|
|
|
@Override
|
2008-10-23 20:58:49 -04:00
|
|
|
public String toString() {
|
2016-03-12 06:37:12 -05:00
|
|
|
StringBuilder sb = new StringBuilder();
|
2008-10-23 20:58:49 -04:00
|
|
|
|
|
|
|
sb.append("[STYLE]\n");
|
|
|
|
sb.append(" .xf_index_raw =").append(HexDump.shortToHex(field_1_xf_index)).append("\n");
|
|
|
|
sb.append(" .type =").append(isBuiltin() ? "built-in" : "user-defined").append("\n");
|
|
|
|
sb.append(" .xf_index =").append(HexDump.shortToHex(getXFIndex())).append("\n");
|
|
|
|
if (isBuiltin()){
|
|
|
|
sb.append(" .builtin_style=").append(HexDump.byteToHex(field_2_builtin_style)).append("\n");
|
|
|
|
sb.append(" .outline_level=").append(HexDump.byteToHex(field_3_outline_style_level)).append("\n");
|
|
|
|
} else {
|
|
|
|
sb.append(" .name =").append(getName()).append("\n");
|
|
|
|
}
|
|
|
|
sb.append("[/STYLE]\n");
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
|
2013-08-23 06:44:39 -04:00
|
|
|
|
|
|
|
@Override
|
2008-10-30 18:13:56 -04:00
|
|
|
protected int getDataSize() {
|
2008-10-23 20:58:49 -04:00
|
|
|
if (isBuiltin()) {
|
|
|
|
return 4; // short, byte, byte
|
|
|
|
}
|
2013-08-23 06:44:39 -04:00
|
|
|
return 2 // short xf index
|
|
|
|
+ 3 // str len + flag
|
2008-11-18 19:06:06 -05:00
|
|
|
+ field_4_name.length() * (field_3_stringHasMultibyte ? 2 : 1);
|
2008-10-23 20:58:49 -04:00
|
|
|
}
|
|
|
|
|
2013-08-23 06:44:39 -04:00
|
|
|
@Override
|
2008-11-18 19:06:06 -05:00
|
|
|
public void serialize(LittleEndianOutput out) {
|
|
|
|
out.writeShort(field_1_xf_index);
|
2008-10-23 20:58:49 -04:00
|
|
|
if (isBuiltin()) {
|
2008-11-18 19:06:06 -05:00
|
|
|
out.writeByte(field_2_builtin_style);
|
|
|
|
out.writeByte(field_3_outline_style_level);
|
2008-10-23 20:58:49 -04:00
|
|
|
} else {
|
2008-11-18 19:06:06 -05:00
|
|
|
out.writeShort(field_4_name.length());
|
|
|
|
out.writeByte(field_3_stringHasMultibyte ? 0x01 : 0x00);
|
2008-12-16 00:44:32 -05:00
|
|
|
if (field_3_stringHasMultibyte) {
|
|
|
|
StringUtil.putUnicodeLE(getName(), out);
|
|
|
|
} else {
|
|
|
|
StringUtil.putCompressedUnicode(getName(), out);
|
|
|
|
}
|
2008-10-23 20:58:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-23 06:44:39 -04:00
|
|
|
@Override
|
2008-11-18 19:06:06 -05:00
|
|
|
public short getSid() {
|
2008-10-23 20:58:49 -04:00
|
|
|
return sid;
|
|
|
|
}
|
2002-01-30 21:22:28 -05:00
|
|
|
}
|