KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > biff > BaseCompoundFile


1 /*********************************************************************
2 *
3 * Copyright (C) 2002 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;
21
22 import common.Assert;
23
24 /**
25  * Contains the common data for a compound file
26  */

27 public abstract class BaseCompoundFile
28 {
29   /**
30    * The identifier at the beginning of every OLE file
31    */

32   protected static final byte[] IDENTIFIER = new byte[]
33     {(byte) 0xd0,
34      (byte) 0xcf,
35      (byte) 0x11,
36      (byte) 0xe0,
37      (byte) 0xa1,
38      (byte) 0xb1,
39      (byte) 0x1a,
40      (byte) 0xe1};
41   /**
42    */

43   protected static final int NUM_BIG_BLOCK_DEPOT_BLOCKS_POS = 0x2c;
44   /**
45    */

46   protected static final int SMALL_BLOCK_DEPOT_BLOCK_POS = 0x3c;
47   /**
48    */

49   protected static final int NUM_SMALL_BLOCK_DEPOT_BLOCKS_POS = 0x40;
50   /**
51    */

52   protected static final int ROOT_START_BLOCK_POS = 0x30;
53   /**
54    */

55   protected static final int BIG_BLOCK_SIZE = 0x200;
56   /**
57    */

58   protected static final int SMALL_BLOCK_SIZE = 0x40;
59   /**
60    */

61   protected static final int EXTENSION_BLOCK_POS = 0x44;
62   /**
63    */

64   protected static final int NUM_EXTENSION_BLOCK_POS = 0x48;
65   /**
66    */

67   protected static final int PROPERTY_STORAGE_BLOCK_SIZE = 0x80;
68   /**
69    */

70   protected static final int BIG_BLOCK_DEPOT_BLOCKS_POS = 0x4c;
71   /**
72    */

73   protected static final int SMALL_BLOCK_THRESHOLD = 0x1000;
74
75   // property storage offsets
76
/**
77      */

78     private static final int SIZE_OF_NAME_POS = 0x40;
79     /**
80      */

81     private static final int TYPE_POS = 0x42;
82     /**
83     */

84     private static final int COLOUR_POS = 0x43;
85     /**
86      */

87     private static final int PREVIOUS_POS = 0x44;
88     /**
89      */

90     private static final int NEXT_POS = 0x48;
91     /**
92      */

93     private static final int CHILD_POS = 0x4c;
94     /**
95      */

96     private static final int START_BLOCK_POS = 0x74;
97     /**
98      */

99     private static final int SIZE_POS = 0x78;
100
101   /**
102    * The standard property sets
103    */

104   public final static String JavaDoc ROOT_ENTRY_NAME = "Root Entry";
105   public final static String JavaDoc WORKBOOK_NAME = "Workbook";
106   public final static String JavaDoc SUMMARY_INFORMATION_NAME =
107     "\u0005SummaryInformation";
108   public final static String JavaDoc DOCUMENT_SUMMARY_INFORMATION_NAME =
109     "\u0005DocumentSummaryInformation";
110   public final static String JavaDoc COMP_OBJ_NAME =
111     "\u0001CompObj";
112   public final static String JavaDoc[] STANDARD_PROPERTY_SETS =
113     new String JavaDoc[] {ROOT_ENTRY_NAME, WORKBOOK_NAME,
114                   SUMMARY_INFORMATION_NAME,
115                   DOCUMENT_SUMMARY_INFORMATION_NAME};
116
117   /**
118    * Property storage types
119    */

120   public final static int NONE_PS_TYPE = 0;
121   public final static int DIRECTORY_PS_TYPE = 1;
122   public final static int FILE_PS_TYPE = 2;
123   public final static int ROOT_ENTRY_PS_TYPE = 5;
124
125
126   /**
127    * Inner class to represent the property storage sets. Access is public
128    * to allow access from the PropertySetsReader demo utility
129    */

130   public class PropertyStorage
131   {
132     /**
133      * The name of this property set
134      */

135     public String JavaDoc name;
136     /**
137      * The type of the property set
138      */

139     public int type;
140     /**
141      * The colour of the property set
142      */

143     public int colour;
144     /**
145      * The block number in the stream which this property sets starts at
146      */

147     public int startBlock;
148     /**
149      * The size, in bytes, of this property set
150      */

151     public int size;
152     /**
153      * The previous property set
154      */

155     public int previous;
156     /**
157      * The next property set
158      */

159     public int next;
160     /**
161      * The child for this property set
162      */

163     public int child;
164
165     /**
166      * The data that created this set
167      */

168     public byte[] data;
169
170     /**
171      * Constructs a property set
172      *
173      * @param d the bytes
174      */

175     public PropertyStorage(byte[] d)
176     {
177       data = d;
178       int nameSize = IntegerHelper.getInt(data[SIZE_OF_NAME_POS],
179                                           data[SIZE_OF_NAME_POS + 1]);
180       type = data[TYPE_POS];
181       colour = data[COLOUR_POS];
182
183       startBlock = IntegerHelper.getInt
184         (data[START_BLOCK_POS],
185          data[START_BLOCK_POS + 1],
186          data[START_BLOCK_POS + 2],
187          data[START_BLOCK_POS + 3]);
188       size = IntegerHelper.getInt
189         (data[SIZE_POS],
190          data[SIZE_POS + 1],
191          data[SIZE_POS + 2],
192          data[SIZE_POS + 3]);
193       previous = IntegerHelper.getInt
194         (data[PREVIOUS_POS],
195          data[PREVIOUS_POS+1],
196          data[PREVIOUS_POS+2],
197          data[PREVIOUS_POS+3]);
198       next = IntegerHelper.getInt
199         (data[NEXT_POS],
200          data[NEXT_POS+1],
201          data[NEXT_POS+2],
202          data[NEXT_POS+3]);
203       child = IntegerHelper.getInt
204         (data[CHILD_POS],
205          data[CHILD_POS+1],
206          data[CHILD_POS+2],
207          data[CHILD_POS+3]);
208
209       int chars = 0;
210       if (nameSize > 2)
211       {
212         chars = (nameSize - 1) / 2;
213       }
214
215       StringBuffer JavaDoc n = new StringBuffer JavaDoc("");
216       for (int i = 0; i < chars ; i++)
217       {
218         n.append( (char) data[i * 2]);
219       }
220
221       name = n.toString();
222     }
223
224     /**
225      * Constructs an empty property set. Used when writing the file
226      *
227      * @param name the property storage name
228      */

229     public PropertyStorage(String JavaDoc name)
230     {
231       data = new byte[PROPERTY_STORAGE_BLOCK_SIZE];
232
233       Assert.verify(name.length() < 32);
234
235       IntegerHelper.getTwoBytes((name.length() + 1) * 2,
236                                 data,
237                                 SIZE_OF_NAME_POS);
238       // add one to the name length to allow for the null character at
239
// the end
240
for (int i = 0; i < name.length(); i++)
241       {
242         data[i * 2] = (byte) name.charAt(i);
243       }
244     }
245
246     /**
247      * Sets the type
248      *
249      * @param t the type
250      */

251     public void setType(int t)
252     {
253       type = t;
254       data[TYPE_POS] = (byte) t;
255     }
256
257     /**
258      * Sets the number of the start block
259      *
260      * @param sb the number of the start block
261      */

262     public void setStartBlock(int sb)
263     {
264       startBlock = sb;
265       IntegerHelper.getFourBytes(sb, data, START_BLOCK_POS);
266     }
267
268     /**
269      * Sets the size of the file
270      *
271      * @param s the size
272      */

273     public void setSize(int s)
274     {
275       size = s;
276       IntegerHelper.getFourBytes(s, data, SIZE_POS);
277     }
278
279     /**
280      * Sets the previous block
281      *
282      * @param prev the previous block
283      */

284     public void setPrevious(int prev)
285     {
286       previous = prev;
287       IntegerHelper.getFourBytes(prev, data, PREVIOUS_POS);
288     }
289
290     /**
291      * Sets the next block
292      *
293      * @param nxt the next block
294      */

295     public void setNext(int nxt)
296     {
297       next = nxt;
298       IntegerHelper.getFourBytes(next, data, NEXT_POS);
299     }
300
301     /**
302      * Sets the child
303      *
304      * @param dir the child
305      */

306     public void setChild(int dir)
307     {
308       child = dir;
309       IntegerHelper.getFourBytes(child, data, CHILD_POS);
310     }
311
312     /**
313      * Sets the colour
314      *
315      * @param col colour
316      */

317     public void setColour(int col)
318     {
319       colour = col == 0 ? 0 : 1;
320       data[COLOUR_POS] = (byte) colour;
321     }
322
323   }
324
325   /**
326    * Constructor
327    */

328   protected BaseCompoundFile()
329   {
330   }
331
332 }
333
334
335
336
337
Popular Tags