From 0e53beb61eb53ffeec2e5f35d2e4421cad7e7737 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Mon, 18 Jan 2010 14:07:38 +0000 Subject: [PATCH] New debugging class, useful for when figuring out how to split on continue records, where continue records lie etc git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@900397 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/hssf/dev/RecordLister.java | 202 ++++++++++++++++++ 1 file changed, 202 insertions(+) create mode 100644 src/java/org/apache/poi/hssf/dev/RecordLister.java diff --git a/src/java/org/apache/poi/hssf/dev/RecordLister.java b/src/java/org/apache/poi/hssf/dev/RecordLister.java new file mode 100644 index 000000000..4cd7d246b --- /dev/null +++ b/src/java/org/apache/poi/hssf/dev/RecordLister.java @@ -0,0 +1,202 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.hssf.dev; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; + +import org.apache.poi.hssf.record.ContinueRecord; +import org.apache.poi.hssf.record.Record; +import org.apache.poi.hssf.record.RecordFactory; +import org.apache.poi.hssf.record.RecordInputStream; +import org.apache.poi.poifs.filesystem.POIFSFileSystem; + +/** + * This is a low-level debugging class, which simply prints + * out what records come in what order. + * Most people will want to use {@link BiffViewer} or + * {@link EFBiffViewer}, but this can be handy when + * trying to make sense of {@link ContinueRecord} + * special cases. + * + * Output is of the form: + * SID - Length - Type (if known) + * byte0 byte1 byte2 byte3 .... byte(n-4) byte(n-3) byte(n-2) byte(n-1) + */ +public class RecordLister +{ + String file; + + public RecordLister() + { + } + + public void run() + throws IOException + { + FileInputStream fin = new FileInputStream(file); + POIFSFileSystem poifs = new POIFSFileSystem(fin); + InputStream din = poifs.createDocumentInputStream("Workbook"); + RecordInputStream rinp = new RecordInputStream(din); + + while(rinp.hasNextRecord()) { + int sid = rinp.getNextSid(); + rinp.nextRecord(); + + int size = rinp.available(); + Class clz = RecordFactory.getRecordClass(sid); + + System.out.print( + formatSID(sid) + + " - " + + formatSize(size) + + " bytes" + ); + if(clz != null) { + System.out.print(" \t"); + System.out.print(clz.getName().replace("org.apache.poi.hssf.record.", "")); + } + System.out.println(); + + byte[] data = rinp.readRemainder(); + if(data.length > 0) { + System.out.print(" "); + System.out.println( formatData(data) ); + } + } + } + + private static String formatSID(int sid) { + String hex = Integer.toHexString(sid); + String dec = Integer.toString(sid); + + StringBuffer s = new StringBuffer(); + s.append("0x"); + for(int i=hex.length(); i<4; i++) { + s.append('0'); + } + s.append(hex); + + s.append(" ("); + for(int i=dec.length(); i<4; i++) { + s.append('0'); + } + s.append(dec); + s.append(")"); + + return s.toString(); + } + private static String formatSize(int size) { + String hex = Integer.toHexString(size); + String dec = Integer.toString(size); + + StringBuffer s = new StringBuffer(); + for(int i=hex.length(); i<3; i++) { + s.append('0'); + } + s.append(hex); + + s.append(" ("); + for(int i=dec.length(); i<3; i++) { + s.append('0'); + } + s.append(dec); + s.append(")"); + + return s.toString(); + } + private static String formatData(byte[] data) { + if(data == null || data.length == 0) + return ""; + + // If possible, do first 4 and last 4 bytes + StringBuffer s = new StringBuffer(); + if(data.length > 9) { + s.append(byteToHex(data[0])); + s.append(' '); + s.append(byteToHex(data[1])); + s.append(' '); + s.append(byteToHex(data[2])); + s.append(' '); + s.append(byteToHex(data[3])); + s.append(' '); + + s.append(" .... "); + + s.append(' '); + s.append(byteToHex(data[data.length-4])); + s.append(' '); + s.append(byteToHex(data[data.length-3])); + s.append(' '); + s.append(byteToHex(data[data.length-2])); + s.append(' '); + s.append(byteToHex(data[data.length-1])); + } else { + for(int i=0; i