KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hdf > extractor > NewOleFile


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
19
20 package org.apache.poi.hdf.extractor;
21
22 import java.io.*;
23 import java.util.*;
24
25 /**
26  * Comment me
27  *
28  * @author Ryan Ackley
29  */

30
31 public class NewOleFile extends RandomAccessFile
32 {
33     private byte[] LAOLA_ID_ARRAY = new byte[]{(byte)0xd0, (byte)0xcf, (byte)0x11,
34                                                (byte)0xe0, (byte)0xa1, (byte)0xb1,
35                                                (byte)0x1a, (byte)0xe1};
36     private int _num_bbd_blocks;
37     private int _root_startblock;
38     private int _sbd_startblock;
39     private long _size;
40     private int[] _bbd_list;
41     protected int[] _big_block_depot;
42     protected int[] _small_block_depot;
43     Hashtable _propertySetsHT = new Hashtable();
44     Vector _propertySetsV = new Vector();
45
46     public NewOleFile(String JavaDoc fileName, String JavaDoc mode) throws FileNotFoundException
47     {
48         super(fileName, mode);
49         try
50         {
51             init();
52         }
53         catch(Throwable JavaDoc e)
54         {
55             e.printStackTrace();
56         }
57     }
58
59     private void init() throws IOException
60     {
61
62         for(int x = 0; x < LAOLA_ID_ARRAY.length; x++)
63         {
64             if(LAOLA_ID_ARRAY[x] != readByte())
65             {
66                 throw new IOException("Not an OLE file");
67             }
68         }
69         _size = length();
70         _num_bbd_blocks = readInt(0x2c);
71         _root_startblock = readInt(0x30);
72         _sbd_startblock = readInt(0x3c);
73         _bbd_list = new int[_num_bbd_blocks];
74         //populate bbd_list. If _num_bbd_blocks > 109 I have to do it
75
//differently
76
if(_num_bbd_blocks <= 109)
77         {
78             seek(0x4c);
79             for(int x = 0; x < _num_bbd_blocks; x++)
80             {
81                 _bbd_list[x] = readIntLE();
82             }
83         }
84         else
85         {
86             populateBbdList();
87         }
88         //populate the big block depot
89
_big_block_depot = new int[_num_bbd_blocks * 128];
90         int counter = 0;
91         for(int x = 0; x < _num_bbd_blocks; x++)
92         {
93             byte[] bigBlock = new byte[512];
94             int offset = (_bbd_list[x] + 1) * 512;
95             seek(offset);
96             for(int y = 0; y < 128; y++)
97             {
98                 _big_block_depot[counter++] = readIntLE();
99             }
100         }
101         _small_block_depot = createSmallBlockDepot();
102         int[] rootChain = readChain(_big_block_depot, _root_startblock);
103         initializePropertySets(rootChain);
104
105     }
106     public static void main(String JavaDoc args[])
107     {
108       try
109       {
110           NewOleFile file = new NewOleFile(args[0], "r");
111       }
112       catch(Exception JavaDoc e)
113       {
114       }
115     }
116     protected int[] readChain(int[] blockChain, int startBlock) throws IOException
117     {
118
119         int[] tempChain = new int[blockChain.length];
120         tempChain[0] = startBlock;
121         int x = 1;
122         for(;;x++)
123         {
124             int nextVal = blockChain[tempChain[x-1]];
125             if(nextVal != -2)
126             {
127                 tempChain[x] = nextVal;
128             }
129             else
130             {
131                 break;
132             }
133         }
134         int[] newChain = new int[x];
135         System.arraycopy(tempChain, 0, newChain, 0, x);
136
137         return newChain;
138     }
139     private void initializePropertySets(int[] rootChain) throws IOException
140     {
141         for(int x = 0; x < rootChain.length; x++)
142         {
143             int offset = (rootChain[x] + 1) * 512;
144             seek(offset);
145             for(int y = 0; y < 4; y++)
146             {
147                 //read the block the makes up the property set
148
byte[] propArray = new byte[128];
149                 read(propArray);
150
151                 //parse the byte array for properties
152
int nameSize = Utils.convertBytesToShort(propArray[0x41], propArray[0x40])/2 - 1;
153                 if(nameSize > 0)
154                 {
155                     StringBuffer JavaDoc nameBuffer = new StringBuffer JavaDoc(nameSize);
156                     for(int z = 0; z < nameSize; z++)
157                     {
158                         nameBuffer.append((char)propArray[z*2]);
159                     }
160                     int type = propArray[0x42];
161                     int previous_pps = Utils.convertBytesToInt(propArray[0x47], propArray[0x46], propArray[0x45], propArray[0x44]);
162                     int next_pps = Utils.convertBytesToInt(propArray[0x4b], propArray[0x4a], propArray[0x49], propArray[0x48]);
163                     int pps_dir = Utils.convertBytesToInt(propArray[0x4f], propArray[0x4e], propArray[0x4d], propArray[0x4c]);
164                     int pps_sb = Utils.convertBytesToInt(propArray[0x77], propArray[0x76], propArray[0x75], propArray[0x74]);
165                     int pps_size = Utils.convertBytesToInt(propArray[0x7b], propArray[0x7a], propArray[0x79], propArray[0x78]);
166
167                     PropertySet propSet = new PropertySet(nameBuffer.toString(),
168                                                           type, previous_pps, next_pps,
169                                                           pps_dir, pps_sb, pps_size,
170                                                           (x*4) + y);
171                     _propertySetsHT.put(nameBuffer.toString(), propSet);
172                     _propertySetsV.add(propSet);
173                 }
174             }
175         }
176
177     }
178     private int[] createSmallBlockDepot() throws IOException
179     {
180
181         int[] sbd_list = readChain(_big_block_depot, _sbd_startblock);
182         int[] small_block_depot = new int[sbd_list.length * 128];
183
184         for(int x = 0; x < sbd_list.length && sbd_list[x] != -2; x++)
185         {
186             int offset = ((sbd_list[x] + 1) * 512);
187             seek(offset);
188             for(int y = 0; y < 128; y++)
189             {
190                 small_block_depot[y] = readIntLE();
191             }
192         }
193         return small_block_depot;
194     }
195
196     private void populateBbdList() throws IOException
197     {
198       seek(0x4c);
199       for(int x = 0; x < 109; x++)
200       {
201           _bbd_list[x] = readIntLE();
202       }
203       int pos = 109;
204       int remainder = _num_bbd_blocks - 109;
205       seek(0x48);
206       int numLists = readIntLE();
207       seek(0x44);
208       int firstList = readIntLE();
209
210       firstList = (firstList + 1) * 512;
211
212       for(int y = 0; y < numLists; y++)
213       {
214         int size = Math.min(127, remainder);
215         for(int z = 0; z < size; z++)
216         {
217           seek(firstList + (z * 4));
218           _bbd_list[pos++] = readIntLE();
219         }
220         if(size == 127)
221         {
222           seek(firstList + (127 * 4));
223           firstList = readIntLE();
224           firstList = (firstList + 1) * 512;
225           remainder -= 127;
226         }
227       }
228
229     }
230     private int readInt(long offset) throws IOException
231     {
232         seek(offset);
233         return readIntLE();
234     }
235     private int readIntLE() throws IOException
236     {
237         byte[] intBytes = new byte[4];
238         read(intBytes);
239         return Utils.convertBytesToInt(intBytes[3], intBytes[2], intBytes[1], intBytes[0]);
240     }
241
242
243
244
245
246 }
247
Popular Tags