1 2 /* ==================================================================== 3 * The Apache Software License, Version 1.1 4 * 5 * Copyright (c) 2002 The Apache Software Foundation. All rights 6 * reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. The end-user documentation included with the redistribution, 21 * if any, must include the following acknowledgment: 22 * "This product includes software developed by the 23 * Apache Software Foundation (http://www.apache.org/)." 24 * Alternately, this acknowledgment may appear in the software itself, 25 * if and wherever such third-party acknowledgments normally appear. 26 * 27 * 4. The names "Apache" and "Apache Software Foundation" and 28 * "Apache POI" must not be used to endorse or promote products 29 * derived from this software without prior written permission. For 30 * written permission, please contact apache@apache.org. 31 * 32 * 5. Products derived from this software may not be called "Apache", 33 * "Apache POI", nor may "Apache" appear in their name, without 34 * prior written permission of the Apache Software Foundation. 35 * 36 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 37 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 38 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 39 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 42 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 43 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 44 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 45 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 46 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 47 * SUCH DAMAGE. 48 * ==================================================================== 49 * 50 * This software consists of voluntary contributions made by many 51 * individuals on behalf of the Apache Software Foundation. For more 52 * information on the Apache Software Foundation, please see 53 * <http://www.apache.org/>. 54 */ 55 56 package org.apache.poi.hssf.record; 57 58 import org.apache.poi.util.LittleEndian; 59 import org.apache.poi.util.StringUtil; 60 61 /** 62 * Title: Format Record<P> 63 * Description: describes a number format -- those goofy strings like $(#,###)<P> 64 * 65 * REFERENCE: PG 317 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P> 66 * @author Andrew C. Oliver (acoliver at apache dot org) 67 * @version 2.0-pre 68 */ 69 70 public class FormatRecord 71 extends Record 72 { 73 public final static short sid = 0x41e; 74 private short field_1_index_code; 75 private byte field_2_formatstring_len; 76 private short field_3_unicode_len; // unicode string length 77 private boolean field_3_unicode_flag; // it is not undocumented - it is unicode flag 78 private String field_4_formatstring; 79 80 public FormatRecord() 81 { 82 } 83 84 /** 85 * Constructs a Format record and sets its fields appropriately. 86 * 87 * @param id id must be 0x41e or an exception will be throw upon validation 88 * @param size the size of the data area of the record 89 * @param data data of the record (should not contain sid/len) 90 */ 91 92 public FormatRecord(short id, short size, byte [] data) 93 { 94 super(id, size, data); 95 } 96 97 /** 98 * Constructs a Format record and sets its fields appropriately. 99 * 100 * @param id id must be 0x41e or an exception will be throw upon validation 101 * @param size the size of the data area of the record 102 * @param data data of the record (should not contain sid/len) 103 * @param offset of the record's data 104 */ 105 106 public FormatRecord(short id, short size, byte [] data, int offset) 107 { 108 super(id, size, data, offset); 109 } 110 111 protected void validateSid(short id) 112 { 113 if (id != sid) 114 { 115 throw new RecordFormatException("NOT A FORMAT RECORD"); 116 } 117 } 118 119 protected void fillFields(byte [] data, short size, int offset) 120 { 121 field_1_index_code = LittleEndian.getShort(data, 0 + offset); 122 // field_2_formatstring_len = data[ 2 + offset ]; 123 field_3_unicode_len = LittleEndian.getShort( data, 2 + offset ); 124 field_3_unicode_flag = ( data[ 4 + offset ] & (byte)0x01 ) != 0; 125 126 127 if ( field_3_unicode_flag ) { 128 // unicode 129 field_4_formatstring = StringUtil.getFromUnicodeHigh( data, 5 + offset, field_3_unicode_len ); 130 } 131 else { 132 // not unicode 133 field_4_formatstring = new String(data, 5 + offset, field_3_unicode_len ); 134 } 135 } 136 137 /** 138 * set the format index code (for built in formats) 139 * 140 * @param index the format index code 141 * @see org.apache.poi.hssf.model.Workbook 142 */ 143 144 public void setIndexCode(short index) 145 { 146 field_1_index_code = index; 147 } 148 149 /** 150 * set the format string length 151 * 152 * @param len the length of the format string 153 * @see #setFormatString(String) 154 */ 155 156 public void setFormatStringLength(byte len) 157 { 158 field_2_formatstring_len = len; 159 } 160 161 /** 162 * set the format string 163 * 164 * @param fs the format string 165 * @see #setFormatStringLength(byte) 166 */ 167 168 public void setFormatString(String fs) 169 { 170 field_4_formatstring = fs; 171 } 172 173 /** 174 * get the format index code (for built in formats) 175 * 176 * @return the format index code 177 * @see org.apache.poi.hssf.model.Workbook 178 */ 179 180 public short getIndexCode() 181 { 182 return field_1_index_code; 183 } 184 185 /** 186 * get the format string length 187 * 188 * @return the length of the format string 189 * @see #getFormatString() 190 */ 191 192 public byte getFormatStringLength() 193 { 194 return field_2_formatstring_len; 195 } 196 197 /** 198 * get the format string 199 * 200 * @return the format string 201 * @see #getFormatStringLength() 202 */ 203 204 public String getFormatString() 205 { 206 return field_4_formatstring; 207 } 208 209 public String toString() 210 { 211 StringBuffer buffer = new StringBuffer(); 212 213 buffer.append("[FORMAT]\n"); 214 buffer.append(" .indexcode = ") 215 .append(Integer.toHexString(getIndexCode())).append("\n"); 216 /* 217 buffer.append(" .formatstringlen = ") 218 .append(Integer.toHexString(getFormatStringLength())) 219 .append("\n"); 220 */ 221 buffer.append(" .unicode length = ") 222 .append(Integer.toHexString(field_3_unicode_len)).append("\n"); 223 buffer.append(" .isUnicode = ") 224 .append( field_3_unicode_flag ).append("\n"); 225 buffer.append(" .formatstring = ").append(getFormatString()) 226 .append("\n"); 227 buffer.append("[/FORMAT]\n"); 228 return buffer.toString(); 229 } 230 231 public int serialize(int offset, byte [] data) 232 { 233 LittleEndian.putShort(data, 0 + offset, sid); 234 LittleEndian.putShort(data, 2 + offset, (short)( 2 + 2 + 1 + ( (field_3_unicode_flag) 235 ? 2 * field_3_unicode_len 236 : field_3_unicode_len ) ) ); 237 // index + len + flag + format string length 238 LittleEndian.putShort(data, 4 + offset, getIndexCode()); 239 LittleEndian.putShort(data, 6 + offset, field_3_unicode_len); 240 data[ 8 + offset ] = (byte)( (field_3_unicode_flag) ? 0x01 : 0x00 ); 241 242 if ( field_3_unicode_flag ) { 243 // unicode 244 StringUtil.putUncompressedUnicode( getFormatString(), data, 9 + offset ); 245 } 246 else { 247 // not unicode 248 StringUtil.putCompressedUnicode( getFormatString(), data, 9 + offset ); 249 } 250 251 return getRecordSize(); 252 } 253 254 public int getRecordSize() 255 { 256 return 9 + ( ( field_3_unicode_flag ) ? 2 * field_3_unicode_len : field_3_unicode_len ); 257 } 258 259 public short getSid() 260 { 261 return this.sid; 262 } 263 } 264