You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RecordLister.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.dev;
  16. import java.io.File;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import org.apache.poi.hssf.record.ContinueRecord;
  20. import org.apache.poi.hssf.record.Record;
  21. import org.apache.poi.hssf.record.RecordFactory;
  22. import org.apache.poi.hssf.record.RecordInputStream;
  23. import org.apache.poi.poifs.filesystem.POIFSFileSystem;
  24. /**
  25. * This is a low-level debugging class, which simply prints
  26. * out what records come in what order.
  27. * Most people will want to use {@link BiffViewer} or
  28. * {@link EFBiffViewer}, but this can be handy when
  29. * trying to make sense of {@link ContinueRecord}
  30. * special cases.
  31. *
  32. * Output is of the form:
  33. * SID - Length - Type (if known)
  34. * byte0 byte1 byte2 byte3 .... byte(n-4) byte(n-3) byte(n-2) byte(n-1)
  35. */
  36. public class RecordLister
  37. {
  38. String file;
  39. public RecordLister()
  40. {
  41. }
  42. public void run() throws IOException {
  43. try (POIFSFileSystem fs = new POIFSFileSystem(new File(file), true);
  44. InputStream din = BiffViewer.getPOIFSInputStream(fs)) {
  45. RecordInputStream rinp = new RecordInputStream(din);
  46. while (rinp.hasNextRecord()) {
  47. int sid = rinp.getNextSid();
  48. rinp.nextRecord();
  49. int size = rinp.available();
  50. Class<? extends Record> clz = RecordFactory.getRecordClass(sid);
  51. System.out.print(
  52. formatSID(sid) +
  53. " - " +
  54. formatSize(size) +
  55. " bytes"
  56. );
  57. if (clz != null) {
  58. System.out.print(" \t");
  59. System.out.print(clz.getName().replace("org.apache.poi.hssf.record.", ""));
  60. }
  61. System.out.println();
  62. byte[] data = rinp.readRemainder();
  63. if (data.length > 0) {
  64. System.out.print(" ");
  65. System.out.println(formatData(data));
  66. }
  67. }
  68. }
  69. }
  70. private static String formatSID(int sid) {
  71. String hex = Integer.toHexString(sid);
  72. String dec = Integer.toString(sid);
  73. StringBuilder s = new StringBuilder();
  74. s.append("0x");
  75. for(int i=hex.length(); i<4; i++) {
  76. s.append('0');
  77. }
  78. s.append(hex);
  79. s.append(" (");
  80. for(int i=dec.length(); i<4; i++) {
  81. s.append('0');
  82. }
  83. s.append(dec);
  84. s.append(")");
  85. return s.toString();
  86. }
  87. private static String formatSize(int size) {
  88. String hex = Integer.toHexString(size);
  89. String dec = Integer.toString(size);
  90. StringBuilder s = new StringBuilder();
  91. for(int i=hex.length(); i<3; i++) {
  92. s.append('0');
  93. }
  94. s.append(hex);
  95. s.append(" (");
  96. for(int i=dec.length(); i<3; i++) {
  97. s.append('0');
  98. }
  99. s.append(dec);
  100. s.append(")");
  101. return s.toString();
  102. }
  103. private static String formatData(byte[] data) {
  104. if(data == null || data.length == 0)
  105. return "";
  106. // If possible, do first 4 and last 4 bytes
  107. StringBuilder s = new StringBuilder();
  108. if(data.length > 9) {
  109. s.append(byteToHex(data[0]));
  110. s.append(' ');
  111. s.append(byteToHex(data[1]));
  112. s.append(' ');
  113. s.append(byteToHex(data[2]));
  114. s.append(' ');
  115. s.append(byteToHex(data[3]));
  116. s.append(' ');
  117. s.append(" .... ");
  118. s.append(' ');
  119. s.append(byteToHex(data[data.length-4]));
  120. s.append(' ');
  121. s.append(byteToHex(data[data.length-3]));
  122. s.append(' ');
  123. s.append(byteToHex(data[data.length-2]));
  124. s.append(' ');
  125. s.append(byteToHex(data[data.length-1]));
  126. } else {
  127. for (byte aData : data) {
  128. s.append(byteToHex(aData));
  129. s.append(' ');
  130. }
  131. }
  132. return s.toString();
  133. }
  134. private static String byteToHex(byte b) {
  135. int i = b;
  136. if(i<0) {
  137. i += 256;
  138. }
  139. String s = Integer.toHexString(i);
  140. if(i < 16) {
  141. return "0" + s;
  142. }
  143. return s;
  144. }
  145. public void setFile(String file)
  146. {
  147. this.file = file;
  148. }
  149. public static void main(String [] args) throws IOException
  150. {
  151. if ((args.length == 1) && !args[ 0 ].equals("--help"))
  152. {
  153. RecordLister viewer = new RecordLister();
  154. viewer.setFile(args[ 0 ]);
  155. viewer.run();
  156. }
  157. else
  158. {
  159. System.out.println("RecordLister");
  160. System.out.println(
  161. "Outputs the summary of the records in file order");
  162. System.out
  163. .println("usage: java org.apache.poi.hssf.dev.RecordLister "
  164. + "filename");
  165. }
  166. }
  167. }