1 19 20 package jxl.demo; 21 22 import java.io.OutputStream ; 23 import java.io.BufferedWriter ; 24 import java.io.OutputStreamWriter ; 25 import java.io.FileInputStream ; 26 import java.io.IOException ; 27 28 import java.util.HashMap ; 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 40 class BiffDump 41 { 42 private BufferedWriter writer; 43 private BiffRecordReader reader; 44 45 private HashMap recordNames; 46 47 private int xfIndex; 48 private int fontIndex; 49 private int bofs; 50 51 private static final int bytesPerLine = 16; 52 53 61 public BiffDump(java.io.File file, OutputStream os) 62 throws IOException , BiffException 63 { 64 writer = new BufferedWriter (new OutputStreamWriter (os)); 65 FileInputStream fis = new FileInputStream (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 80 private void buildNameHash() 81 { 82 recordNames = new HashMap (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 195 private void dump() throws IOException 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 211 private boolean writeRecord(Record r) 212 throws IOException 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 buf = new StringBuffer (); 239 240 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 (); 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 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 334 private void writeSixDigitValue(int pos, StringBuffer buf) 335 { 336 String 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 348 private void writeByte(byte val, StringBuffer buf) 349 { 350 String 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 |