KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > biff > drawing > EscherRecordData


1 /*********************************************************************
2 *
3 * Copyright (C) 2003 Andrew Khan
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ***************************************************************************/

19
20 package jxl.biff.drawing;
21
22 import jxl.biff.IntegerHelper;
23
24 /**
25  * A single record from an Escher stream. Basically this a container for
26  * the header data for each Escher record
27  */

28 final class EscherRecordData
29 {
30   /**
31    * The byte position of this record in the escher stream
32    */

33   private int pos;
34
35   /**
36    * The instance value
37    */

38   private int instance;
39
40   /**
41    * The version value
42    */

43   private int version;
44
45   /**
46    * The record id
47    */

48   private int recordId;
49
50   /**
51    * The length of the record, excluding the 8 byte header
52    */

53   private int length;
54
55   /**
56    * The length of the stream
57    */

58   private int streamLength;
59
60   /**
61    * Indicates whether this record is a container
62    */

63   private boolean container;
64
65   /**
66    * The type of this record
67    */

68   private EscherRecordType type;
69
70   /**
71    * A handle back to the drawing group, which contains the entire escher
72    * stream byte data
73    */

74   private EscherStream escherStream;
75
76   /**
77    * Constructor
78    */

79   public EscherRecordData(EscherStream dg, int p)
80   {
81     escherStream = dg;
82     pos = p;
83     byte[] data = escherStream.getData();
84
85     streamLength = data.length;
86
87     // First two bytes contain instance and version
88
int value = IntegerHelper.getInt(data[pos], data[pos+1]);
89     
90     // Instance value is the first 12 bits
91
instance = (value & 0xfff0) >> 4;
92     
93     // Version is the last four bits
94
version = value & 0xf;
95
96     // Bytes 2 and 3 are the record id
97
recordId = IntegerHelper.getInt(data[pos+2], data[pos+3]);
98
99     // Length is bytes 4,5,6 and 7
100
length = IntegerHelper.getInt(data[pos+4], data[pos+5],
101                                   data[pos+6], data[pos+7]);
102     
103     if (version == 0x0f)
104     {
105       container = true;
106     }
107     else
108     {
109       container = false;
110     }
111   }
112
113   /**
114    * Constructor
115    *
116    * @param t the type of the escher record
117    */

118   public EscherRecordData(EscherRecordType t)
119   {
120     type = t;
121     recordId = type.getValue();
122   }
123
124   /**
125    * Determines whether this record is a container
126    *
127    * @return TRUE if this is a container, FALSE otherwise
128    */

129   public boolean isContainer()
130   {
131     return container;
132   }
133
134   /**
135    * Accessor for the length, excluding the 8 byte header
136    *
137    * @return the length excluding the 8 byte header
138    */

139   public int getLength()
140   {
141     return length;
142   }
143
144   /**
145    * Accessor for the record id
146    *
147    * @return the record id
148    */

149   public int getRecordId()
150   {
151     return recordId;
152   }
153
154   /**
155    * Accessor for the drawing group stream
156    *
157    * @return the drawing group stream
158    */

159   EscherStream getDrawingGroup()
160   {
161     return escherStream;
162   }
163
164   /**
165    * Gets the position in the stream
166    *
167    * @return the position in the stream
168    */

169   int getPos()
170   {
171     return pos;
172   }
173
174   /**
175    * Gets the escher type of this record
176    */

177   EscherRecordType getType()
178   {
179     if (type == null)
180     {
181       type = EscherRecordType.getType(recordId);
182     }
183
184     return type;
185   }
186
187   /**
188    * Gets the instance value
189    *
190    * @return the instance value
191    */

192   int getInstance()
193   {
194     return instance;
195   }
196
197   /**
198    * Sets whether or not this is a container - called when writing
199    * out an escher stream
200    *
201    * @param c TRUE if this is a container, FALSE otherwise
202    */

203   void setContainer(boolean c)
204   {
205     container = c;
206   }
207
208   /**
209    * Called from the subclass when writing to set the instance value
210    *
211    * @param inst the instance
212    */

213   void setInstance(int inst)
214   {
215     instance = inst;
216   }
217
218   /**
219    * Called when writing to set the length of this record
220    *
221    * @param l the length
222    */

223   void setLength(int l)
224   {
225     length = l;
226   }
227
228   /**
229    * Called when writing to set the version of this record
230    *
231    * @param v the version
232    */

233   void setVersion(int v)
234   {
235     version = v;
236   }
237
238   /**
239    * Adds the 8 byte header data on the value data passed in, returning
240    * the modified data
241    *
242    * @param d the value data
243    * @return the value data with the header information
244    */

245   byte[] setHeaderData(byte[] d)
246   {
247     byte[] data = new byte[d.length + 8];
248     System.arraycopy(d, 0, data, 8, d.length);
249
250     if (container)
251     {
252       version = 0x0f;
253     }
254
255     // First two bytes contain instance and version
256
int value = instance << 4;
257     value |= version;
258     IntegerHelper.getTwoBytes(value, data, 0);
259
260     // Bytes 2 and 3 are the record id
261
IntegerHelper.getTwoBytes(recordId, data, 2);
262
263     // Length is bytes 4,5,6 and 7
264
IntegerHelper.getFourBytes(d.length, data, 4);
265     
266     return data;
267   }
268
269   /**
270    * Accessor for the header stream
271    *
272    * @return the escher stream
273    */

274   EscherStream getEscherStream()
275   {
276     return escherStream;
277   }
278
279   /**
280    * Gets the data that was read in, excluding the header data
281    *
282    * @return the value data that was read in
283    */

284   byte[] getBytes()
285   {
286     byte[] d = new byte[length];
287     System.arraycopy(escherStream.getData(), pos+8, d, 0, length);
288     return d;
289   }
290
291   /**
292    * Accessor for the stream length
293    *
294    * @return the stream length
295    */

296   int getStreamLength()
297   {
298     return streamLength;
299   }
300 }
301
Popular Tags