1 16 17 18 package org.apache.poi.hssf.record; 19 20 import org.apache.poi.ddf.DefaultEscherRecordFactory; 21 import org.apache.poi.ddf.EscherRecord; 22 import org.apache.poi.ddf.EscherRecordFactory; 23 import org.apache.poi.ddf.NullEscherSerializationListener; 24 import org.apache.poi.util.LittleEndian; 25 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 37 public abstract class AbstractEscherHolderRecord 38 extends Record 39 { 40 private static final boolean DESERIALISE = System.getProperty("poi.deserialize.escher") != null; 41 42 private List escherRecords; 43 private byte[] rawData; 44 45 46 public AbstractEscherHolderRecord() 47 { 48 escherRecords = new ArrayList (); 49 } 50 51 59 60 public AbstractEscherHolderRecord(short id, short size, byte [] data) 61 { 62 super(id, size, data); 63 64 } 65 66 75 76 public AbstractEscherHolderRecord(short id, short size, byte [] data, int offset) 77 { 78 super(id, size, data, offset); 79 80 } 81 82 87 protected void validateSid(short id) 88 { 89 if (id != getSid()) 90 { 91 throw new RecordFormatException("Not a Bar record"); 92 } 93 } 94 95 protected void fillFields(byte [] data, short size, int offset) 96 { 97 escherRecords = new ArrayList (); 98 if (! DESERIALISE ) 99 { 100 rawData = new byte[size]; 101 System.arraycopy(data, offset, rawData, 0, size); 102 } 103 else 104 { 105 EscherRecordFactory recordFactory = new DefaultEscherRecordFactory(); 106 int pos = offset; 107 while ( pos < offset + size ) 108 { 109 EscherRecord r = recordFactory.createRecord(data, pos); 110 int bytesRead = r.fillFields(data, pos, recordFactory ); 111 escherRecords.add(r); 112 pos += bytesRead; 113 } 114 } 115 } 116 117 public String toString() 118 { 119 StringBuffer buffer = new StringBuffer (); 120 121 final String nl = System.getProperty("line.separator"); 122 buffer.append('[' + getRecordName() + ']' + nl); 123 for ( Iterator iterator = escherRecords.iterator(); iterator.hasNext(); ) 124 { 125 EscherRecord r = (EscherRecord) iterator.next(); 126 buffer.append(r.toString()); 127 } 128 buffer.append("[/" + getRecordName() + ']' + nl); 129 130 return buffer.toString(); 131 } 132 133 protected abstract String getRecordName(); 134 135 public int serialize(int offset, byte[] data) 136 { 137 LittleEndian.putShort( data, 0 + offset, getSid() ); 138 LittleEndian.putShort( data, 2 + offset, (short) ( getRecordSize() - 4 ) ); 139 if ( escherRecords.size() == 0 && rawData != null ) 140 { 141 System.arraycopy( rawData, 0, data, offset + 4, rawData.length ); 142 } 143 else 144 { 145 int pos = offset + 4; 146 for ( Iterator iterator = escherRecords.iterator(); iterator.hasNext(); ) 147 { 148 EscherRecord r = (EscherRecord) iterator.next(); 149 pos += r.serialize( pos, data, new NullEscherSerializationListener() ); 150 } 151 } 152 return getRecordSize(); 153 } 154 155 180 183 public int getRecordSize() 184 { 185 if (escherRecords.size() == 0 && rawData != null) 186 { 187 return rawData.length + 4; 188 } 189 else 190 { 191 int size = 4; 192 for ( Iterator iterator = escherRecords.iterator(); iterator.hasNext(); ) 193 { 194 EscherRecord r = (EscherRecord) iterator.next(); 195 size += r.getRecordSize(); 196 } 197 return size; 198 } 199 } 200 201 225 public abstract short getSid(); 226 227 public Object clone() 228 { 229 throw new IllegalStateException ("Not implemented yet."); 230 } 231 232 public void addEscherRecord(int index, EscherRecord element) 233 { 234 escherRecords.add( index, element ); 235 } 236 237 public boolean addEscherRecord(EscherRecord element) 238 { 239 return escherRecords.add( element ); 240 } 241 242 public List getEscherRecords() 243 { 244 return escherRecords; 245 } 246 247 public void clearEscherRecords() 248 { 249 escherRecords.clear(); 250 } 251 252 253 public EscherRecord getEscherRecord(int index) 254 { 255 return (EscherRecord) escherRecords.get(index); 256 } 257 258 259 } 261 262 263 264 | Popular Tags |