package org.apache.poi.hslf.dev;
+import java.io.ByteArrayOutputStream;
import java.io.File;
-import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import org.apache.poi.hslf.record.RecordTypes;
-import org.apache.poi.poifs.filesystem.DocumentEntry;
-import org.apache.poi.poifs.filesystem.DocumentInputStream;
+import org.apache.poi.poifs.filesystem.DirectoryNode;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
+import org.apache.poi.util.IOUtils;
import org.apache.poi.util.LittleEndian;
/**
* Utility class which dumps raw contents of a ppt file into XML format
- *
- * @author Yegor Kozlov
*/
public final class PPTXMLDump {
- public static final int HEADER_SIZE = 8; //size of the record header
- public static final int PICT_HEADER_SIZE = 25; //size of the picture header
- public final static String PPDOC_ENTRY = "PowerPoint Document";
- public final static String PICTURES_ENTRY = "Pictures";
- public final static String CR = System.getProperty("line.separator");
+ private static final int HEADER_SIZE = 8; //size of the record header
+ private static final int PICT_HEADER_SIZE = 25; //size of the picture header
+ private static final String PPDOC_ENTRY = "PowerPoint Document";
+ private static final String PICTURES_ENTRY = "Pictures";
+ private static final String CR = System.getProperty("line.separator");
- protected Writer out;
- protected byte[] docstream;
- protected byte[] pictstream;
- protected boolean hexHeader = true;
+ private Writer out;
+ private byte[] docstream;
+ private byte[] pictstream;
+ private boolean hexHeader = true;
public PPTXMLDump(File ppt) throws IOException {
NPOIFSFileSystem fs = new NPOIFSFileSystem(ppt, true);
- DocumentInputStream is = null;
-
try {
- //read the document entry from OLE file system
- DocumentEntry entry = (DocumentEntry)fs.getRoot().getEntry(PPDOC_ENTRY);
- docstream = new byte[entry.getSize()];
- is = fs.createDocumentInputStream(PPDOC_ENTRY);
- is.read(docstream);
- is.close();
-
- entry = (DocumentEntry)fs.getRoot().getEntry(PICTURES_ENTRY);
- pictstream = new byte[entry.getSize()];
- is = fs.createDocumentInputStream(PICTURES_ENTRY);
- is.read(pictstream);
- } catch(FileNotFoundException e){
- //silently catch errors if the presentation does not contain pictures
+ docstream = readEntry(fs, PPDOC_ENTRY);
+ pictstream = readEntry(fs, PICTURES_ENTRY);
} finally {
- if (is != null) is.close();
fs.close();
}
}
+ private static byte[] readEntry(NPOIFSFileSystem fs, String entry)
+ throws IOException {
+ DirectoryNode dn = fs.getRoot();
+ if (!dn.hasEntry(entry)) {
+ return null;
+ }
+ InputStream is = dn.createDocumentInputStream(entry);
+ try {
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ IOUtils.copy(is, bos);
+ return bos.toByteArray();
+ } finally {
+ is.close();
+ }
+ }
+
/**
* Dump the structure of the supplied PPT file into XML
* @param outWriter <code>Writer</code> to write out
public void dump(byte[] data, int offset, int length, int padding) throws IOException {
int pos = offset;
while (pos <= (offset + length - HEADER_SIZE)){
- if (pos < 0) break;
+ if (pos < 0) {
+ break;
+ }
//read record header
int info = LittleEndian.getUShort(data, pos);