File indexing completed on 2024-06-16 04:26:00

0001 package mso.generator;
0002 
0003 import java.io.FileInputStream;
0004 import java.io.FileNotFoundException;
0005 import java.io.FileOutputStream;
0006 import java.io.IOException;
0007 import java.io.PrintStream;
0008 import java.util.Iterator;
0009 import java.util.Map;
0010 
0011 import javax.xml.parsers.DocumentBuilderFactory;
0012 
0013 import mso.generator.utils.ParserGeneratorUtils;
0014 import mso.javaparser.LEInputStream;
0015 
0016 import org.apache.poi.poifs.filesystem.DirectoryEntry;
0017 import org.apache.poi.poifs.filesystem.DocumentEntry;
0018 import org.apache.poi.poifs.filesystem.DocumentInputStream;
0019 import org.apache.poi.poifs.filesystem.Entry;
0020 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
0021 import org.w3c.dom.Document;
0022 
0023 public class PPTStructurePrinter {
0024 
0025     /**
0026      * @param args
0027      */
0028     public static void main(String[] args) throws Exception {
0029         final String xmlfilename = "src/mso.xml";
0030         String testfile = "/tmp/mp00_RomanBulletsnumber.ppt";
0031 
0032         final Document dom = DocumentBuilderFactory.newInstance()
0033                 .newDocumentBuilder().parse(xmlfilename);
0034         Map<Integer, String> recordTypeNames = ParserGeneratorUtils
0035                 .getRecordTypeNames(dom);
0036         PrintStream out = System.out;
0037         out = new PrintStream(new FileOutputStream("/tmp/out"));
0038         parse(testfile, recordTypeNames, out);
0039     }
0040 
0041     public static void parse(String filepath,
0042             Map<Integer, String> recordTypeNames, PrintStream out)
0043                     throws IOException {
0044         POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filepath));
0045         DirectoryEntry root = fs.getRoot();
0046         try {
0047             root = (DirectoryEntry) root.getEntry("PP97_DUALSTORAGE");
0048         } catch (FileNotFoundException e) {
0049         }
0050         parse(root, recordTypeNames, out);
0051     }
0052 
0053     private static void parse(DirectoryEntry dir,
0054             Map<Integer, String> recordTypeNames, PrintStream out)
0055                     throws IOException {
0056         for (Iterator<Entry> iter = dir.getEntries(); iter.hasNext();) {
0057             Entry entry = (Entry) iter.next();
0058             if (entry instanceof DirectoryEntry) {
0059                 out.println("found directory entry: " + entry.getName());
0060             } else if (entry instanceof DocumentEntry) {
0061                 DocumentEntry e = (DocumentEntry) entry;
0062                 DocumentInputStream in = new DocumentInputStream(e);
0063                 byte datain[] = new byte[e.getSize()];
0064                 if (datain.length != in.read(datain, 0, datain.length)) {
0065                     in.close();
0066                     throw new IOException("could not read all data");
0067                 }
0068                 in.close();
0069                 LEInputStream le = new LEInputStream(datain);
0070 
0071                 out.println("found document entry: " + entry.getName()
0072                         + " of length " + e.getSize());
0073                 try {
0074                     while (le.getPosition() != e.getSize()) {
0075                         printStructure(le, 0, recordTypeNames, out);
0076                     }
0077                 } catch (IOException ex) {
0078                     out.println("abrupt end");
0079                 }
0080             }
0081         }
0082     }
0083 
0084     private static void printStructure(LEInputStream in, int depth,
0085             Map<Integer, String> recordTypeNames, PrintStream out)
0086                     throws IOException {
0087         int recVer = in.readuint4();
0088         int recInstance = in.readuint12();
0089         int recType = in.readuint16();
0090         int recLen = in.readuint32();
0091 
0092         String hexinstance = Integer.toHexString(recInstance).toUpperCase();
0093         String hextype = Integer.toHexString(recType).toUpperCase();
0094 
0095         String t = "\t\t";
0096         String name = recordTypeNames.containsKey(recType)
0097                 ? recordTypeNames.get(recType) : "";
0098         out.println(depth + t + recVer + t + hexinstance + t + hextype + t
0099                 + recLen + t + in.getPosition() + t + name);
0100         if (recVer == 0xF && recType != 0x428) {
0101             int end = in.getPosition() + recLen;
0102             while (in.getPosition() != end) {
0103                 if (in.getPosition() > end) {
0104                     String msg = in.getPosition() + " > " + end;
0105                     out.println(msg);
0106                     throw new IOException(msg);
0107                 }
0108                 printStructure(in, depth + 1, recordTypeNames, out);
0109             }
0110         } else {
0111             for (int i = 0; i < recLen; ++i) {
0112                 in.readuint8();
0113             }
0114         }
0115     }
0116 }