1 2 17 18 19 20 package org.apache.poi.hssf.record; 21 22 23 24 import org.apache.poi.util.*; 25 26 import java.util.List ; 27 import java.util.Iterator ; 28 import java.util.ArrayList ; 29 30 35 public class ObjRecord 36 extends Record 37 { 38 public final static short sid = 0x5D; 39 private List subrecords; 40 41 44 45 public ObjRecord() 46 { 47 subrecords = new ArrayList (2); 48 } 49 50 58 59 public ObjRecord(short id, short size, byte [] data) 60 { 61 super(id, size, data); 62 } 63 64 73 public ObjRecord(short id, short size, byte[] data, int offset) 74 { 75 super(id, size, data, offset); 76 } 77 78 83 protected void validateSid(short id) 84 { 85 if (id != sid) 86 { 87 throw new RecordFormatException("Not an OBJ record"); 88 } 89 } 90 91 protected void fillFields(byte [] data, short size, int offset) 92 { 93 subrecords = new ArrayList (); 94 int pos = offset; 95 while (pos - offset < size) 96 { 97 short subRecordSid = LittleEndian.getShort(data, pos); 98 short subRecordSize = LittleEndian.getShort(data, pos + 2); 99 Record subRecord = SubRecord.createSubRecord(subRecordSid, subRecordSize, data, pos + 4); 100 subrecords.add(subRecord); 101 pos += subRecord.getRecordSize(); 102 } 103 104 } 105 106 public String toString() 107 { 108 StringBuffer buffer = new StringBuffer (); 109 110 buffer.append("[OBJ]\n"); 111 for ( Iterator iterator = subrecords.iterator(); iterator.hasNext(); ) 112 { 113 Record record = (Record) iterator.next(); 114 buffer.append("SUBRECORD: " + record.toString()); 115 } 116 buffer.append("[/OBJ]\n"); 117 return buffer.toString(); 118 } 119 120 public int serialize(int offset, byte[] data) 121 { 122 int pos = 0; 123 124 LittleEndian.putShort(data, 0 + offset, sid); 125 LittleEndian.putShort(data, 2 + offset, (short)(getRecordSize() - 4)); 126 127 pos = offset + 4; 128 for ( Iterator iterator = subrecords.iterator(); iterator.hasNext(); ) 129 { 130 Record record = (Record) iterator.next(); 131 pos += record.serialize(pos, data); 132 } 133 134 return getRecordSize(); 135 } 136 137 140 public int getRecordSize() 141 { 142 int size = 0; 143 for ( Iterator iterator = subrecords.iterator(); iterator.hasNext(); ) 144 { 145 Record record = (Record) iterator.next(); 146 size += record.getRecordSize(); 147 } 148 return 4 + size; 149 } 150 151 public short getSid() 152 { 153 return sid; 154 } 155 156 public List getSubRecords() 157 { 158 return subrecords; 159 } 160 161 public void clearSubRecords() 162 { 163 subrecords.clear(); 164 } 165 166 public void addSubRecord(int index, Object element) 167 { 168 subrecords.add( index, element ); 169 } 170 171 public boolean addSubRecord(Object o) 172 { 173 return subrecords.add( o ); 174 } 175 176 public Object clone() 177 { 178 ObjRecord rec = new ObjRecord(); 179 180 for ( Iterator iterator = subrecords.iterator(); iterator.hasNext(); ) 181 rec.addSubRecord(( (Record) iterator.next() ).clone()); 182 183 return rec; 184 } 185 186 } 188 189 190 191 | Popular Tags |