File indexing completed on 2024-12-15 04:19:55
0001 package mso.javaparser; 0002 0003 import java.io.ByteArrayOutputStream; 0004 import java.io.FileInputStream; 0005 import java.io.FileNotFoundException; 0006 import java.io.FileOutputStream; 0007 import java.io.IOException; 0008 import java.io.OutputStream; 0009 import java.util.Iterator; 0010 0011 import org.apache.poi.poifs.filesystem.DirectoryEntry; 0012 import org.apache.poi.poifs.filesystem.DocumentEntry; 0013 import org.apache.poi.poifs.filesystem.DocumentInputStream; 0014 import org.apache.poi.poifs.filesystem.Entry; 0015 import org.apache.poi.poifs.filesystem.POIFSFileSystem; 0016 import org.eclipse.jdt.annotation.NonNullByDefault; 0017 0018 @NonNullByDefault 0019 public class MSOParser { 0020 public void parse(String filepath) throws IOException { 0021 GeneratedMsoParser parser = new GeneratedMsoParser(); 0022 POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filepath)); 0023 DirectoryEntry root = fs.getRoot(); 0024 try { 0025 root = (DirectoryEntry) root.getEntry("PP97_DUALSTORAGE"); 0026 } catch (FileNotFoundException e) { 0027 } 0028 0029 parseDirectory(parser, root); 0030 } 0031 0032 private void parseDirectory(GeneratedMsoParser parser, DirectoryEntry de) 0033 throws IOException { 0034 for (Iterator<Entry> iter = de.getEntries(); iter.hasNext();) { 0035 Entry entry = (Entry) iter.next(); 0036 if (entry instanceof DirectoryEntry) { 0037 System.out.println("found dir entry: " + entry.getName()); 0038 } else if (entry instanceof DocumentEntry) { 0039 DocumentEntry e = (DocumentEntry) entry; 0040 save("/tmp", e); 0041 System.out.println( 0042 "found entry: " + e.getName() + " " + e.getSize()); 0043 DocumentInputStream in = new DocumentInputStream(e); 0044 byte datain[] = new byte[e.getSize()]; 0045 if (datain.length != in.read(datain, 0, datain.length)) { 0046 in.close(); 0047 throw new IOException("could not read all data"); 0048 } 0049 LEInputStream lei = new LEInputStream(datain); 0050 String name = e.getName(); 0051 Object mso = parser.parse(name, lei); 0052 if (lei.getPosition() != lei.getSize()) { 0053 // try { 0054 in.close(); 0055 throw new IOException("trailing data in stream " 0056 + e.getName() + ": " 0057 + (lei.getSize() - lei.getPosition()) + " bytes"); 0058 // } catch (Exception ex) { 0059 // System.out.println(ex.getMessage()); 0060 // } 0061 } 0062 in.close(); 0063 ByteArrayOutputStream out = new ByteArrayOutputStream(); 0064 LEOutputStream leo = new LEOutputStream(out); 0065 parser.serialize(name, mso, leo); 0066 leo.close(); 0067 out.close(); 0068 byte dataout[] = out.toByteArray(); 0069 writeData("/tmp/datain", datain); 0070 writeData("/tmp/dataout", dataout); 0071 int diffpos = 0; 0072 while (diffpos < datain.length 0073 && datain[diffpos] == dataout[diffpos]) 0074 diffpos++; 0075 if (diffpos < datain.length || diffpos < dataout.length) { 0076 System.err.println("in: " + e.getSize() + " out: " 0077 + dataout.length + " diff at: " + diffpos); 0078 throw new IOException("missing data when writing stream."); 0079 } 0080 } 0081 } 0082 } 0083 0084 private void writeData(String name, byte data[]) throws IOException { 0085 OutputStream out = new FileOutputStream(name); 0086 out.write(data); 0087 out.close(); 0088 } 0089 0090 public void save(String dir, DocumentEntry e) throws IOException { 0091 FileOutputStream f = new FileOutputStream(dir + "/" + e.getName()); 0092 DocumentInputStream in = new DocumentInputStream(e); 0093 while (in.available() > 0) { 0094 f.write(in.read()); 0095 } 0096 f.close(); 0097 in.close(); 0098 } 0099 0100 static boolean test() { 0101 int i = 3; 0102 System.out.println(i << 3); 0103 return true; 0104 } 0105 }