KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > demo > BiffDump


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.demo;
21
22 import java.io.OutputStream JavaDoc;
23 import java.io.BufferedWriter JavaDoc;
24 import java.io.OutputStreamWriter JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 import java.util.HashMap JavaDoc;
29
30 import jxl.WorkbookSettings;
31 import jxl.biff.Type;
32 import jxl.read.biff.File;
33 import jxl.read.biff.BiffRecordReader;
34 import jxl.read.biff.BiffException;
35 import jxl.read.biff.Record;
36
37 /**
38  * Generates a biff dump of the specified excel file
39  */

40 class BiffDump
41 {
42   private BufferedWriter JavaDoc writer;
43   private BiffRecordReader reader;
44
45   private HashMap JavaDoc recordNames;
46
47   private int xfIndex;
48   private int fontIndex;
49   private int bofs;
50
51   private static final int bytesPerLine = 16;
52
53   /**
54    * Constructor
55    *
56    * @param file the file
57    * @param os the output stream
58    * @exception IOException
59    * @exception BiffException
60    */

61   public BiffDump(java.io.File JavaDoc file, OutputStream JavaDoc os)
62     throws IOException JavaDoc, BiffException
63   {
64     writer = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(os));
65     FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
66     File f = new File(fis, new WorkbookSettings());
67     reader = new BiffRecordReader(f);
68
69     buildNameHash();
70     dump();
71
72     writer.flush();
73     writer.close();
74     fis.close();
75   }
76
77   /**
78    * Builds the hashmap of record names
79    */

80   private void buildNameHash()
81   {
82     recordNames = new HashMap JavaDoc(50);
83
84     recordNames.put(Type.BOF, "BOF");
85     recordNames.put(Type.EOF, "EOF");
86     recordNames.put(Type.FONT, "FONT");
87     recordNames.put(Type.SST, "SST");
88     recordNames.put(Type.LABELSST, "LABELSST");
89     recordNames.put(Type.WRITEACCESS, "WRITEACCESS");
90     recordNames.put(Type.FORMULA, "FORMULA");
91     recordNames.put(Type.FORMULA2, "FORMULA");
92     recordNames.put(Type.XF, "XF");
93     recordNames.put(Type.MULRK, "MULRK");
94     recordNames.put(Type.NUMBER, "NUMBER");
95     recordNames.put(Type.BOUNDSHEET, "BOUNDSHEET");
96     recordNames.put(Type.CONTINUE, "CONTINUE");
97     recordNames.put(Type.FORMAT, "FORMAT");
98     recordNames.put(Type.EXTERNSHEET, "EXTERNSHEET");
99     recordNames.put(Type.INDEX, "INDEX");
100     recordNames.put(Type.DIMENSION, "DIMENSION");
101     recordNames.put(Type.ROW, "ROW");
102     recordNames.put(Type.DBCELL, "DBCELL");
103     recordNames.put(Type.BLANK, "BLANK");
104     recordNames.put(Type.MULBLANK, "MULBLANK");
105     recordNames.put(Type.RK, "RK");
106     recordNames.put(Type.RK2, "RK");
107     recordNames.put(Type.COLINFO, "COLINFO");
108     recordNames.put(Type.LABEL, "LABEL");
109     recordNames.put(Type.SHAREDFORMULA, "SHAREDFORMULA");
110     recordNames.put(Type.CODEPAGE, "CODEPAGE");
111     recordNames.put(Type.WINDOW1, "WINDOW1");
112     recordNames.put(Type.WINDOW2, "WINDOW2");
113     recordNames.put(Type.MERGEDCELLS, "MERGEDCELLS");
114     recordNames.put(Type.HLINK, "HLINK");
115     recordNames.put(Type.HEADER, "HEADER");
116     recordNames.put(Type.FOOTER, "FOOTER");
117     recordNames.put(Type.INTERFACEHDR, "INTERFACEHDR");
118     recordNames.put(Type.MMS, "MMS");
119     recordNames.put(Type.INTERFACEEND, "INTERFACEEND");
120     recordNames.put(Type.DSF, "DSF");
121     recordNames.put(Type.FNGROUPCOUNT, "FNGROUPCOUNT");
122     recordNames.put(Type.COUNTRY, "COUNTRY");
123     recordNames.put(Type.TABID, "TABID");
124     recordNames.put(Type.PROTECT, "PROTECT");
125     recordNames.put(Type.SCENPROTECT, "SCENPROTECT");
126     recordNames.put(Type.OBJPROTECT, "OBJPROTECT");
127     recordNames.put(Type.WINDOWPROTECT, "WINDOWPROTECT");
128     recordNames.put(Type.PASSWORD, "PASSWORD");
129     recordNames.put(Type.PROT4REV, "PROT4REV");
130     recordNames.put(Type.PROT4REVPASS, "PROT4REVPASS");
131     recordNames.put(Type.BACKUP, "BACKUP");
132     recordNames.put(Type.HIDEOBJ, "HIDEOBJ");
133     recordNames.put(Type.NINETEENFOUR, "1904");
134     recordNames.put(Type.PRECISION, "PRECISION");
135     recordNames.put(Type.BOOKBOOL, "BOOKBOOL");
136     recordNames.put(Type.STYLE, "STYLE");
137     recordNames.put(Type.EXTSST, "EXTSST");
138     recordNames.put(Type.REFRESHALL, "REFRESHALL");
139     recordNames.put(Type.CALCMODE, "CALCMODE");
140     recordNames.put(Type.CALCCOUNT, "CALCCOUNT");
141     recordNames.put(Type.NAME, "NAME");
142     recordNames.put(Type.MSODRAWINGGROUP, "MSODRAWINGGROUP");
143     recordNames.put(Type.MSODRAWING, "MSODRAWING");
144     recordNames.put(Type.OBJ, "OBJ");
145     recordNames.put(Type.USESELFS, "USESELFS");
146     recordNames.put(Type.SUPBOOK, "SUPBOOK");
147     recordNames.put(Type.LEFTMARGIN, "LEFTMARGIN");
148     recordNames.put(Type.RIGHTMARGIN, "RIGHTMARGIN");
149     recordNames.put(Type.TOPMARGIN, "TOPMARGIN");
150     recordNames.put(Type.BOTTOMMARGIN, "BOTTOMMARGIN");
151     recordNames.put(Type.HCENTER, "HCENTER");
152     recordNames.put(Type.VCENTER, "VCENTER");
153     recordNames.put(Type.ITERATION, "ITERATION");
154     recordNames.put(Type.DELTA, "DELTA");
155     recordNames.put(Type.SAVERECALC, "SAVERECALC");
156     recordNames.put(Type.PRINTHEADERS, "PRINTHEADERS");
157     recordNames.put(Type.PRINTGRIDLINES, "PRINTGRIDLINES");
158     recordNames.put(Type.SETUP, "SETUP");
159     recordNames.put(Type.SELECTION, "SELECTION");
160     recordNames.put(Type.STRING, "STRING");
161     recordNames.put(Type.FONTX, "FONTX");
162     recordNames.put(Type.IFMT, "IFMT");
163     recordNames.put(Type.WSBOOL, "WSBOOL");
164     recordNames.put(Type.GRIDSET, "GRIDSET");
165     recordNames.put(Type.REFMODE, "REFMODE");
166     recordNames.put(Type.GUTS, "GUTS");
167     recordNames.put(Type.EXTERNNAME, "EXTERNNAME");
168     recordNames.put(Type.FBI, "FBI");
169     recordNames.put(Type.CRN, "CRN");
170     recordNames.put(Type.HORIZONTALPAGEBREAKS, "HORIZONTALPAGEBREAKS");
171     recordNames.put(Type.DEFAULTROWHEIGHT, "DEFAULTROWHEIGHT");
172     recordNames.put(Type.TEMPLATE, "TEMPLATE");
173     recordNames.put(Type.PANE, "PANE");
174     recordNames.put(Type.SCL, "SCL");
175     recordNames.put(Type.PALETTE, "PALETTE");
176     recordNames.put(Type.PLS, "PLS");
177     recordNames.put(Type.OBJPROJ, "OBJPROJ");
178     recordNames.put(Type.DEFCOLWIDTH, "DEFCOLWIDTH");
179     recordNames.put(Type.ARRAY, "ARRAY");
180     recordNames.put(Type.WEIRD1, "WEIRD1");
181     recordNames.put(Type.BOOLERR, "BOOLERR");
182     recordNames.put(Type.SORT, "SORT");
183     recordNames.put(Type.BUTTONPROPERTYSET, "BUTTONPROPERTYSET");
184     recordNames.put(Type.NOTE, "NOTE");
185     recordNames.put(Type.TXO, "TXO");
186     recordNames.put(Type.DV, "DV");
187     recordNames.put(Type.DVAL, "DVAL");
188
189     
190     recordNames.put(Type.UNKNOWN, "???");
191   }
192   /**
193    * Dumps out the contents of the excel file
194    */

195   private void dump() throws IOException JavaDoc
196   {
197     Record r = null;
198     boolean cont = true;
199     while (reader.hasNext() && cont)
200     {
201       r = reader.next();
202       cont = writeRecord(r);
203     }
204   }
205
206   /**
207    * Writes out the biff record
208    * @param r
209    * @exception IOException if an error occurs
210    */

211   private boolean writeRecord(Record r)
212     throws IOException JavaDoc
213   {
214     boolean cont = true;
215     int pos = reader.getPos();
216     int code = r.getCode();
217
218     if (bofs == 0)
219     {
220       cont = (r.getType() == Type.BOF);
221     }
222
223     if (!cont)
224     {
225       return cont;
226     }
227
228     if (r.getType() == Type.BOF)
229     {
230       bofs++;
231     }
232     
233     if (r.getType() == Type.EOF)
234     {
235       bofs--;
236     }
237
238     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
239
240     // Write out the record header
241
writeSixDigitValue(pos, buf);
242     buf.append(" [");
243     buf.append(recordNames.get(r.getType()));
244     buf.append("]");
245     buf.append(" (0x");
246     buf.append(Integer.toHexString(code));
247     buf.append(")");
248
249     if (code == Type.XF.value)
250     {
251       buf.append(" (0x");
252       buf.append(Integer.toHexString(xfIndex));
253       buf.append(")");
254       xfIndex++;
255     }
256
257     if (code == Type.FONT.value)
258     {
259       if (fontIndex == 4)
260       {
261         fontIndex++;
262       }
263
264       buf.append(" (0x");
265       buf.append(Integer.toHexString(fontIndex));
266       buf.append(")");
267       fontIndex++;
268     }
269
270     writer.write(buf.toString());
271     writer.newLine();
272
273     byte[] standardData = new byte[4];
274     standardData[0] = (byte) (code & 0xff);
275     standardData[1] = (byte) ((code & 0xff00) >> 8);
276     standardData[2] = (byte) (r.getLength() & 0xff);
277     standardData[3] = (byte) ((r.getLength() & 0xff00) >> 8);
278     byte[] recordData = r.getData();
279     byte[] data = new byte[standardData.length + recordData.length];
280     System.arraycopy(standardData, 0, data, 0, standardData.length);
281     System.arraycopy(recordData, 0, data,
282                      standardData.length, recordData.length);
283
284     int byteCount = 0;
285     int lineBytes = 0;
286
287     while (byteCount < data.length)
288     {
289       buf = new StringBuffer JavaDoc();
290       writeSixDigitValue(pos+byteCount, buf);
291       buf.append(" ");
292
293       lineBytes = Math.min(bytesPerLine, data.length - byteCount);
294
295       for (int i = 0; i < lineBytes ; i++)
296       {
297         writeByte(data[i+byteCount], buf);
298         buf.append(' ');
299       }
300
301       // Perform any padding
302
if (lineBytes < bytesPerLine)
303       {
304         for(int i = 0; i < bytesPerLine - lineBytes; i++)
305         {
306           buf.append(" ");
307         }
308       }
309
310       buf.append(" ");
311
312       for (int i = 0 ; i < lineBytes; i++)
313       {
314         char c = (char) data[i+byteCount];
315         if (c < ' ' || c > 'z')
316         {
317           c = '.';
318         }
319         buf.append(c);
320       }
321
322       byteCount+= lineBytes;
323
324       writer.write(buf.toString());
325       writer.newLine();
326     }
327
328     return cont;
329   }
330
331   /**
332    * Writes the string passed in as a minimum of four digits
333    */

334   private void writeSixDigitValue(int pos, StringBuffer JavaDoc buf)
335   {
336     String JavaDoc val = Integer.toHexString(pos);
337
338     for (int i = 6; i > val.length() ; i--)
339     {
340       buf.append('0');
341     }
342     buf.append(val);
343   }
344
345   /**
346    * Writes the string passed in as a minimum of four digits
347    */

348   private void writeByte(byte val, StringBuffer JavaDoc buf)
349   {
350     String JavaDoc sv = Integer.toHexString((val & 0xff));
351
352     if (sv.length() == 1)
353     {
354       buf.append('0');
355     }
356     buf.append(sv);
357   }
358 }
359
Popular Tags