1 2 17 18 package org.apache.poi.ddf; 19 20 import org.apache.poi.util.LittleEndian; 21 22 import java.io.PrintWriter ; 23 import java.util.Collections ; 24 import java.util.List ; 25 26 32 abstract public class EscherRecord 33 { 34 private short options; 35 private short recordId; 36 37 40 public EscherRecord() 41 { 42 } 43 44 49 protected int fillFields( byte[] data, EscherRecordFactory f ) 50 { 51 return fillFields( data, 0, f ); 52 } 53 54 64 public abstract int fillFields( byte[] data, int offset, EscherRecordFactory recordFactory ); 65 66 75 protected int readHeader( byte[] data, int offset ) 76 { 77 EscherRecordHeader header = EscherRecordHeader.readHeader(data, offset); 78 options = header.getOptions(); 79 recordId = header.getRecordId(); 80 return header.getRemainingBytes(); 81 } 82 83 88 public boolean isContainerRecord() 89 { 90 return (options & (short)0x000f) == (short)0x000f; 91 } 92 93 96 public short getOptions() 97 { 98 return options; 99 } 100 101 105 public void setOptions( short options ) 106 { 107 this.options = options; 108 } 109 110 117 public byte[] serialize() 118 { 119 byte[] retval = new byte[getRecordSize()]; 120 121 serialize( 0, retval ); 122 return retval; 123 } 124 125 135 public int serialize( int offset, byte[] data) 136 { 137 return serialize( offset, data, new NullEscherSerializationListener() ); 138 } 139 140 151 public abstract int serialize( int offset, byte[] data, EscherSerializationListener listener ); 152 153 159 abstract public int getRecordSize(); 160 161 166 public short getRecordId() 167 { 168 return recordId; 169 } 170 171 174 public void setRecordId( short recordId ) 175 { 176 this.recordId = recordId; 177 } 178 179 186 public List getChildRecords() { return Collections.EMPTY_LIST; } 187 188 194 public void setChildRecords( List childRecords ) { throw new IllegalArgumentException ("This record does not support child records."); } 195 196 199 public Object clone() 200 { 201 throw new RuntimeException ( "The class " + getClass().getName() + " needs to define a clone method" ); 202 } 203 204 207 public EscherRecord getChild( int index ) 208 { 209 return (EscherRecord) getChildRecords().get(index); 210 } 211 212 219 public void display(PrintWriter w, int indent) 220 { 221 for (int i = 0; i < indent * 4; i++) w.print(' '); 222 w.println(getRecordName()); 223 } 224 225 228 public abstract String getRecordName(); 229 230 235 public short getInstance() 236 { 237 return (short) ( options >> 4 ); 238 } 239 240 243 static class EscherRecordHeader 244 { 245 private short options; 246 private short recordId; 247 private int remainingBytes; 248 249 private EscherRecordHeader() 250 { 251 } 252 253 public static EscherRecordHeader readHeader( byte[] data, int offset ) 254 { 255 EscherRecordHeader header = new EscherRecordHeader(); 256 header.options = LittleEndian.getShort(data, offset); 257 header.recordId = LittleEndian.getShort(data, offset + 2); 258 header.remainingBytes = LittleEndian.getInt( data, offset + 4 ); 259 return header; 260 } 261 262 263 public short getOptions() 264 { 265 return options; 266 } 267 268 public short getRecordId() 269 { 270 return recordId; 271 } 272 273 public int getRemainingBytes() 274 { 275 return remainingBytes; 276 } 277 278 public String toString() 279 { 280 return "EscherRecordHeader{" + 281 "options=" + options + 282 ", recordId=" + recordId + 283 ", remainingBytes=" + remainingBytes + 284 "}"; 285 } 286 287 288 } 289 290 } 291 | Popular Tags |