KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > ddf > EscherDggRecord


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

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 JavaDoc;
25 import java.util.*;
26
27 /**
28  * This record defines the drawing groups used for a particular sheet.
29  */

30 public class EscherDggRecord
31     extends EscherRecord
32 {
33     public static final short RECORD_ID = (short) 0xF006;
34     public static final String JavaDoc RECORD_DESCRIPTION = "MsofbtDgg";
35
36     private int field_1_shapeIdMax;
37 // private int field_2_numIdClusters; // for some reason the number of clusters is actually the real number + 1
38
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     /**
70      * This method deserializes the record from a byte array.
71      *
72      * @param data The byte array containing the escher record information
73      * @param offset The starting offset into <code>data</code>.
74      * @param recordFactory May be null since this is not a container record.
75      * @return The number of bytes read from the byte array.
76      */

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     /**
99      * This method serializes this escher record into a byte array.
100      *
101      * @param offset The offset into <code>data</code> to start writing the record data to.
102      * @param data The byte array to serialize to.
103      * @param listener A listener to retrieve start and end callbacks. Use a <code>NullEscherSerailizationListener</code> to ignore these events.
104      * @return The number of bytes written.
105      *
106      * @see NullEscherSerializationListener
107      */

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     /**
132      * Returns the number of bytes that are required to serialize this record.
133      *
134      * @return Number of bytes
135      */

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     /**
147      * The short name for this record
148      */

149     public String JavaDoc getRecordName()
150     {
151         return "Dgg";
152     }
153
154     public String JavaDoc toString()
155     {
156         String JavaDoc nl = System.getProperty("line.separator");
157
158 // String extraData;
159
// ByteArrayOutputStream b = new ByteArrayOutputStream();
160
// try
161
// {
162
// HexDump.dump(this.remainingData, 0, b, 0);
163
// extraData = b.toString();
164
// }
165
// catch ( Exception e )
166
// {
167
// extraData = "error";
168
// }
169
StringBuffer JavaDoc field_5_string = new StringBuffer JavaDoc();
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     /**
196      * The maximum is actually the next available. shape id.
197      */

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 JavaDoc o1, Object JavaDoc 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