1 2 17 18 package org.apache.poi.ddf; 19 20 import org.apache.poi.util.HexDump; 21 import org.apache.poi.util.LittleEndian; 22 import org.apache.poi.hssf.record.RecordFormatException; 23 24 import java.lang.reflect.Array ; 25 import java.util.*; 26 27 30 public class EscherDggRecord 31 extends EscherRecord 32 { 33 public static final short RECORD_ID = (short) 0xF006; 34 public static final String RECORD_DESCRIPTION = "MsofbtDgg"; 35 36 private int field_1_shapeIdMax; 37 private int field_3_numShapesSaved; 39 private int field_4_drawingsSaved; 40 private FileIdCluster[] field_5_fileIdClusters; 41 42 public static class FileIdCluster 43 { 44 public FileIdCluster( int drawingGroupId, int numShapeIdsUsed ) 45 { 46 this.field_1_drawingGroupId = drawingGroupId; 47 this.field_2_numShapeIdsUsed = numShapeIdsUsed; 48 } 49 50 private int field_1_drawingGroupId; 51 private int field_2_numShapeIdsUsed; 52 53 public int getDrawingGroupId() 54 { 55 return field_1_drawingGroupId; 56 } 57 58 public int getNumShapeIdsUsed() 59 { 60 return field_2_numShapeIdsUsed; 61 } 62 63 public void incrementShapeId( ) 64 { 65 this.field_2_numShapeIdsUsed++; 66 } 67 } 68 69 77 public int fillFields( byte[] data, int offset, EscherRecordFactory recordFactory ) 78 { 79 int bytesRemaining = readHeader( data, offset ); 80 int pos = offset + 8; 81 int size = 0; 82 field_1_shapeIdMax = LittleEndian.getInt( data, pos + size );size+=4; 83 int field_2_numIdClusters = LittleEndian.getInt( data, pos + size );size+=4; 84 field_3_numShapesSaved = LittleEndian.getInt( data, pos + size );size+=4; 85 field_4_drawingsSaved = LittleEndian.getInt( data, pos + size );size+=4; 86 field_5_fileIdClusters = new FileIdCluster[field_2_numIdClusters-1]; 87 for (int i = 0; i < field_2_numIdClusters-1; i++) 88 { 89 field_5_fileIdClusters[i] = new FileIdCluster(LittleEndian.getInt( data, pos + size ), LittleEndian.getInt( data, pos + size + 4 )); 90 size += 8; 91 } 92 bytesRemaining -= size; 93 if (bytesRemaining != 0) 94 throw new RecordFormatException("Expecting no remaining data but got " + bytesRemaining + " byte(s)."); 95 return 8 + size + bytesRemaining; 96 } 97 98 108 public int serialize( int offset, byte[] data, EscherSerializationListener listener ) 109 { 110 listener.beforeRecordSerialize( offset, getRecordId(), this ); 111 112 int pos = offset; 113 LittleEndian.putShort( data, pos, getOptions() ); pos += 2; 114 LittleEndian.putShort( data, pos, getRecordId() ); pos += 2; 115 int remainingBytes = getRecordSize() - 8; 116 LittleEndian.putInt( data, pos, remainingBytes ); pos += 4; 117 LittleEndian.putInt( data, pos, field_1_shapeIdMax ); pos += 4; 118 LittleEndian.putInt( data, pos, getNumIdClusters() ); pos += 4; 119 LittleEndian.putInt( data, pos, field_3_numShapesSaved ); pos += 4; 120 LittleEndian.putInt( data, pos, field_4_drawingsSaved ); pos += 4; 121 for ( int i = 0; i < field_5_fileIdClusters.length; i++ ) 122 { 123 LittleEndian.putInt( data, pos, field_5_fileIdClusters[i].field_1_drawingGroupId ); pos += 4; 124 LittleEndian.putInt( data, pos, field_5_fileIdClusters[i].field_2_numShapeIdsUsed ); pos += 4; 125 } 126 127 listener.afterRecordSerialize( pos, getRecordId(), getRecordSize(), this ); 128 return getRecordSize(); 129 } 130 131 136 public int getRecordSize() 137 { 138 return 8 + 16 + (8 * field_5_fileIdClusters.length); 139 } 140 141 public short getRecordId() 142 { 143 return RECORD_ID; 144 } 145 146 149 public String getRecordName() 150 { 151 return "Dgg"; 152 } 153 154 public String toString() 155 { 156 String nl = System.getProperty("line.separator"); 157 158 StringBuffer field_5_string = new StringBuffer (); 170 for ( int i = 0; i < field_5_fileIdClusters.length; i++ ) 171 { 172 field_5_string.append(" DrawingGroupId").append(i+1).append(": "); 173 field_5_string.append(field_5_fileIdClusters[i].field_1_drawingGroupId); 174 field_5_string.append(nl); 175 field_5_string.append(" NumShapeIdsUsed").append(i+1).append(": "); 176 field_5_string.append(field_5_fileIdClusters[i].field_2_numShapeIdsUsed); 177 field_5_string.append(nl); 178 } 179 return getClass().getName() + ":" + nl + 180 " RecordId: 0x" + HexDump.toHex(RECORD_ID) + nl + 181 " Options: 0x" + HexDump.toHex(getOptions()) + nl + 182 " ShapeIdMax: " + field_1_shapeIdMax + nl + 183 " NumIdClusters: " + getNumIdClusters() + nl + 184 " NumShapesSaved: " + field_3_numShapesSaved + nl + 185 " DrawingsSaved: " + field_4_drawingsSaved + nl + 186 "" + field_5_string.toString(); 187 188 } 189 190 public int getShapeIdMax() 191 { 192 return field_1_shapeIdMax; 193 } 194 195 198 public void setShapeIdMax( int field_1_shapeIdMax ) 199 { 200 this.field_1_shapeIdMax = field_1_shapeIdMax; 201 } 202 203 public int getNumIdClusters() 204 { 205 return field_5_fileIdClusters.length + 1; 206 } 207 208 public int getNumShapesSaved() 209 { 210 return field_3_numShapesSaved; 211 } 212 213 public void setNumShapesSaved( int field_3_numShapesSaved ) 214 { 215 this.field_3_numShapesSaved = field_3_numShapesSaved; 216 } 217 218 public int getDrawingsSaved() 219 { 220 return field_4_drawingsSaved; 221 } 222 223 public void setDrawingsSaved( int field_4_drawingsSaved ) 224 { 225 this.field_4_drawingsSaved = field_4_drawingsSaved; 226 } 227 228 public FileIdCluster[] getFileIdClusters() 229 { 230 return field_5_fileIdClusters; 231 } 232 233 public void setFileIdClusters( FileIdCluster[] field_5_fileIdClusters ) 234 { 235 this.field_5_fileIdClusters = field_5_fileIdClusters; 236 } 237 238 public void addCluster( int dgId, int numShapedUsed ) 239 { 240 List clusters = new ArrayList(Arrays.asList(field_5_fileIdClusters)); 241 clusters.add(new FileIdCluster(dgId, numShapedUsed)); 242 Collections.sort(clusters, new Comparator() 243 { 244 public int compare( Object o1, Object o2 ) 245 { 246 FileIdCluster f1 = (FileIdCluster) o1; 247 FileIdCluster f2 = (FileIdCluster) o2; 248 if (f1.getDrawingGroupId() == f2.getDrawingGroupId()) 249 return 0; 250 if (f1.getDrawingGroupId() < f2.getDrawingGroupId()) 251 return -1; 252 else 253 return +1; 254 } 255 } ); 256 field_5_fileIdClusters = (FileIdCluster[]) clusters.toArray( new FileIdCluster[clusters.size()] ); 257 } 258 } 259 | Popular Tags |