public static byte[] safelyAllocate(long length, int maxLength) {
if (length < 0L) {
- throw new RecordFormatException("Can't allocate an array of length < 0");
+ throw new RecordFormatException("Can't allocate an array of length < 0, but had " + length + " and " + maxLength);
}
if (length > (long)Integer.MAX_VALUE) {
throw new RecordFormatException("Can't allocate an array > "+Integer.MAX_VALUE);
System.exit(1);
}
- HSLFSlideShowImpl ss = new HSLFSlideShowImpl(args[0]);
+ try (HSLFSlideShowImpl ss = new HSLFSlideShowImpl(args[0])) {
- // Find PPDrawings at any second level position
- Record[] records = ss.getRecords();
- for(int i=0; i<records.length; i++) {
- Record[] children = records[i].getChildRecords();
- if(children != null && children.length != 0) {
- for(int j=0; j<children.length; j++) {
- if(children[j] instanceof PPDrawing) {
- System.out.println("Found PPDrawing at " + j + " in top level record " + i + " (" + records[i].getRecordType() + ")" );
+ // Find PPDrawings at any second level position
+ Record[] records = ss.getRecords();
+ for (int i = 0; i < records.length; i++) {
+ Record[] children = records[i].getChildRecords();
+ if (children != null && children.length != 0) {
+ for (int j = 0; j < children.length; j++) {
+ if (children[j] instanceof PPDrawing) {
+ System.out.println("Found PPDrawing at " + j + " in top level record " + i + " (" + records[i].getRecordType() + ")");
- // Look for EscherTextboxWrapper's
- PPDrawing ppd = (PPDrawing)children[j];
- EscherTextboxWrapper[] wrappers = ppd.getTextboxWrappers();
- System.out.println(" Has " + wrappers.length + " textbox wrappers within");
+ // Look for EscherTextboxWrapper's
+ PPDrawing ppd = (PPDrawing) children[j];
+ EscherTextboxWrapper[] wrappers = ppd.getTextboxWrappers();
+ System.out.println(" Has " + wrappers.length + " textbox wrappers within");
- // Loop over the wrappers, showing what they contain
- for(int k=0; k<wrappers.length; k++) {
- EscherTextboxWrapper tbw = wrappers[k];
- System.out.println(" " + k + " has " + tbw.getChildRecords().length + " PPT atoms within");
+ // Loop over the wrappers, showing what they contain
+ for (int k = 0; k < wrappers.length; k++) {
+ EscherTextboxWrapper tbw = wrappers[k];
+ System.out.println(" " + k + " has " + tbw.getChildRecords().length + " PPT atoms within");
- // Loop over the records, printing the text
- Record[] pptatoms = tbw.getChildRecords();
- for(int l=0; l<pptatoms.length; l++) {
- String text = null;
- if(pptatoms[l] instanceof TextBytesAtom) {
- TextBytesAtom tba = (TextBytesAtom)pptatoms[l];
- text = tba.getText();
- }
- if(pptatoms[l] instanceof TextCharsAtom) {
- TextCharsAtom tca = (TextCharsAtom)pptatoms[l];
- text = tca.getText();
- }
+ // Loop over the records, printing the text
+ Record[] pptatoms = tbw.getChildRecords();
+ for (Record pptatom : pptatoms) {
+ String text = null;
+ if (pptatom instanceof TextBytesAtom) {
+ TextBytesAtom tba = (TextBytesAtom) pptatom;
+ text = tba.getText();
+ }
+ if (pptatom instanceof TextCharsAtom) {
+ TextCharsAtom tca = (TextCharsAtom) pptatom;
+ text = tca.getText();
+ }
- if(text != null) {
- text = text.replace('\r','\n');
- System.out.println(" ''" + text + "''");
+ if (text != null) {
+ text = text.replace('\r', '\n');
+ System.out.println(" ''" + text + "''");
+ }
}
}
}
}
}
}
-
- ss.close();
}
}
private boolean hexHeader = true;
public PPTXMLDump(File ppt) throws IOException {
- POIFSFileSystem fs = new POIFSFileSystem(ppt, true);
- try {
+ try (POIFSFileSystem fs = new POIFSFileSystem(ppt, true)) {
docstream = readEntry(fs, HSLFSlideShow.POWERPOINT_DOCUMENT);
pictstream = readEntry(fs, PICTURES_ENTRY);
- } finally {
- fs.close();
}
}
if (!dn.hasEntry(entry)) {
return null;
}
- InputStream is = dn.createDocumentInputStream(entry);
- try {
+ try (InputStream is = dn.createDocumentInputStream(entry)) {
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
- * @throws java.io.IOException
+ * @throws java.io.IOException If writing to the writer fails
*/
public void dump(Writer outWriter) throws IOException {
this.out = outWriter;
//dump the structure of the powerpoint document
write(out, "<PowerPointDocument>" + CR, padding);
padding++;
- dump(docstream, 0, docstream.length, padding);
+ if(docstream != null) {
+ dump(docstream, 0, docstream.length, padding);
+ }
padding--;
write(out, "</PowerPointDocument>" + CR, padding);
padding--;
* @param offset offset from the beginning of the document
* @param length of the document
* @param padding used for formatting results
- * @throws java.io.IOException
+ * @throws java.io.IOException If writing out information fails
*/
public void dump(byte[] data, int offset, int length, int padding) throws IOException {
int pos = offset;
* Dumps the Pictures OLE stream into XML.
*
* @param data from the Pictures OLE data stream
- * @param padding
- * @throws java.io.IOException
+ * @param padding How many leading blanks to add in the output
+ * @throws java.io.IOException If writing out information fails
*/
public void dumpPictures(byte[] data, int padding) throws IOException {
int pos = 0;
while (pos < data.length) {
byte[] header = new byte[PICT_HEADER_SIZE];
+ if(data.length - pos < header.length) {
+ // corrupt file, cannot read header
+ return;
+ }
System.arraycopy(data, pos, header, 0, header.length);
int size = LittleEndian.getInt(header, 4) - 17;
+ if(size < 0) {
+ // corrupt file, negative image size
+ return;
+ }
byte[] pictdata = IOUtils.safelyAllocate(size, MAX_RECORD_LENGTH);
System.arraycopy(data, pos + PICT_HEADER_SIZE, pictdata, 0, pictdata.length);
pos += PICT_HEADER_SIZE + size;
padding--;
write(out, "</picture>" + CR, padding);
padding--;
-
}
}
(byte) '4', (byte) '5', (byte) '6', (byte) '7',
(byte) '8', (byte) '9', (byte) 'A', (byte) 'B',
(byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F'};
-
}
}
HSLFSlideShowImpl ss = new HSLFSlideShowImpl(args[0]);
- System.out.println("");
+ System.out.println();
// Find either Slides or Notes
Record[] records = ss.getRecords();
System.out.println("Found Slide at " + i);
System.out.println(" Slide's master ID is " + sa.getMasterID());
System.out.println(" Slide's notes ID is " + sa.getNotesID());
- System.out.println("");
+ System.out.println();
}
if(r instanceof Notes) {
Notes n = (Notes)r;
NotesAtom na = n.getNotesAtom();
System.out.println("Found Notes at " + i);
System.out.println(" Notes ID is " + na.getSlideID());
- System.out.println("");
+ System.out.println();
}
}
// Grab any records that interest us
Document document = null;
- for(int i=0; i<latestRecords.length; i++) {
- if(latestRecords[i] instanceof Document) {
- document = (Document)latestRecords[i];
+ for (Record latestRecord : latestRecords) {
+ if (latestRecord instanceof Document) {
+ document = (Document) latestRecord;
}
}
- System.out.println("");
+ System.out.println();
// Look for SlidePersistAtoms, and report what they have to
// say about possible slide IDs
SlideListWithText[] slwts = document.getSlideListWithTexts();
- for(int i=0; i<slwts.length; i++) {
- Record[] cr = slwts[i].getChildRecords();
- for(int j=0; j<cr.length; j++) {
- if(cr[j] instanceof SlidePersistAtom) {
- SlidePersistAtom spa = (SlidePersistAtom)cr[j];
+ for (SlideListWithText slwt : slwts) {
+ Record[] cr = slwt.getChildRecords();
+ for (Record record : cr) {
+ if (record instanceof SlidePersistAtom) {
+ SlidePersistAtom spa = (SlidePersistAtom) record;
System.out.println("SlidePersistAtom knows about slide:");
System.out.println("\t" + spa.getRefID());
System.out.println("\t" + spa.getSlideIdentifier());
}
}
- System.out.println("");
+ System.out.println();
// Look for latest core records that are slides or notes
for(int i=0; i<latestRecords.length; i++) {
System.out.println("\tNotes ID is " + sa.getNotesID());
}
}
- System.out.println("");
+ System.out.println();
for(int i=0; i<latestRecords.length; i++) {
if(latestRecords[i] instanceof Notes) {
Notes n = (Notes)latestRecords[i];
}
}
- System.out.println("");
+ System.out.println();
// Find any persist ones first
int pos = 0;
- for(int i=0; i<records.length; i++) {
- Record r = records[i];
-
- if(r.getRecordType() == 6001l) {
+ for (Record r : records) {
+ if (r.getRecordType() == 6001L) {
// PersistPtrFullBlock
System.out.println("Found PersistPtrFullBlock at " + pos + " (" + Integer.toHexString(pos) + ")");
}
- if(r.getRecordType() == 6002l) {
+ if (r.getRecordType() == 6002L) {
// PersistPtrIncrementalBlock
System.out.println("Found PersistPtrIncrementalBlock at " + pos + " (" + Integer.toHexString(pos) + ")");
- PersistPtrHolder pph = (PersistPtrHolder)r;
+ PersistPtrHolder pph = (PersistPtrHolder) r;
// Check the sheet offsets
int[] sheetIDs = pph.getKnownSlideIDs();
- Map<Integer,Integer> sheetOffsets = pph.getSlideLocationsLookup();
- for(int j=0; j<sheetIDs.length; j++) {
- Integer id = sheetIDs[j];
+ Map<Integer, Integer> sheetOffsets = pph.getSlideLocationsLookup();
+ for (Integer id : sheetIDs) {
Integer offset = sheetOffsets.get(id);
System.out.println(" Knows about sheet " + id);
System.out.println(" The record at that pos is of type " + atPos.getRecordType());
System.out.println(" The record at that pos has class " + atPos.getClass().getName());
- if(! (atPos instanceof PositionDependentRecord)) {
+ if (!(atPos instanceof PositionDependentRecord)) {
System.out.println(" ** The record class isn't position aware! **");
}
}
ss.close();
- System.out.println("");
+ System.out.println();
}
* dump of what it contains
*/
public static void main(String[] args) throws IOException {
- String filename = "";
boolean verbose = false;
boolean escher = false;
return;
}
- filename = args[ndx];
+ String filename = args[ndx];
SlideShowRecordDumper foo = new SlideShowRecordDumper(System.out,
filename, verbose, escher);
foo.printDump();
+
+ foo.doc.close();
}
public static void printUsage() {
}
public String makeHex(int number, int padding) {
- String hex = Integer.toHexString(number).toUpperCase(Locale.ROOT);
+ StringBuilder hex = new StringBuilder(Integer.toHexString(number).toUpperCase(Locale.ROOT));
while (hex.length() < padding) {
- hex = "0" + hex;
+ hex.insert(0, "0");
}
- return hex;
+ return hex.toString();
}
public String reverseHex(String s) {
for (Record child : etw.getChildRecords()) {
if (child instanceof StyleTextPropAtom) {
// need preceding Text[Chars|Bytes]Atom to initialize the data structure
- String text = null;
+ String text;
if (prevChild instanceof TextCharsAtom) {
text = ((TextCharsAtom)prevChild).getText();
} else if (prevChild instanceof TextBytesAtom) {
public void walkTree(int depth, int pos, Record[] records, int indent) throws IOException {
String ind = tabs.substring(0, indent);
- for (int i = 0; i < records.length; i++) {
- Record r = records[i];
+ for (Record r : records) {
if (r == null) {
ps.println(ind + "At position " + pos + " (" + makeHex(pos, 6) + "):");
ps.println(ind + "Warning! Null record found.");
String hexType = makeHex((int) r.getRecordType(), 4);
String rHexType = reverseHex(hexType);
- // Grab the hslf.record type
- Class<? extends Record> c = r.getClass();
- String cname = c.toString();
- if(cname.startsWith("class ")) {
- cname = cname.substring(6);
- }
- if(cname.startsWith("org.apache.poi.hslf.record.")) {
- cname = cname.substring(27);
- }
+ // Grab the hslf.record type
+ Class<? extends Record> c = r.getClass();
+ String cname = c.toString();
+ if (cname.startsWith("class ")) {
+ cname = cname.substring(6);
+ }
+ if (cname.startsWith("org.apache.poi.hslf.record.")) {
+ cname = cname.substring(27);
+ }
- // Display the record
- ps.println(ind + "At position " + pos + " (" + makeHex(pos,6) + "):");
- ps.println(ind + " Record is of type " + cname);
- ps.println(ind + " Type is " + r.getRecordType() + " (" + hexType + " -> " + rHexType + " )");
- ps.println(ind + " Len is " + (len-8) + " (" + makeHex((len-8),8) + "), on disk len is " + len );
+ // Display the record
+ ps.println(ind + "At position " + pos + " (" + makeHex(pos, 6) + "):");
+ ps.println(ind + " Record is of type " + cname);
+ ps.println(ind + " Type is " + r.getRecordType() + " (" + hexType + " -> " + rHexType + " )");
+ ps.println(ind + " Len is " + (len - 8) + " (" + makeHex((len - 8), 8) + "), on disk len is " + len);
- // print additional information for drawings and atoms
- if (optEscher && cname.equals("PPDrawing")) {
- DefaultEscherRecordFactory factory = new HSLFEscherRecordFactory();
+ // print additional information for drawings and atoms
+ if (optEscher && cname.equals("PPDrawing")) {
+ DefaultEscherRecordFactory factory = new HSLFEscherRecordFactory();
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- r.writeOut(baos);
- byte[] b = baos.toByteArray();
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ r.writeOut(baos);
+ byte[] b = baos.toByteArray();
- EscherRecord er = factory.createRecord(b, 0);
- er.fillFields(b, 0, factory);
+ EscherRecord er = factory.createRecord(b, 0);
+ er.fillFields(b, 0, factory);
- printEscherRecord( er, indent+1 );
+ printEscherRecord(er, indent + 1);
- } else if(optVerbose && r.getChildRecords() == null) {
- String recData = getPrintableRecordContents(r);
- ps.println(ind + recData );
- }
+ } else if (optVerbose && r.getChildRecords() == null) {
+ String recData = getPrintableRecordContents(r);
+ ps.println(ind + recData);
+ }
- ps.println();
+ ps.println();
- // If it has children, show them
- if(r.getChildRecords() != null) {
- walkTree((depth+3),pos+8,r.getChildRecords(), indent+1);
- }
+ // If it has children, show them
+ if (r.getChildRecords() != null) {
+ walkTree((depth + 3), pos + 8, r.getChildRecords(), indent + 1);
+ }
- // Wind on the position marker
- pos += len;
- }
+ // Wind on the position marker
+ pos += len;
+ }
}
}
// Find the documents, and then their SLWT
Record[] records = ss.getRecords();
- for(int i=0; i<records.length; i++) {
- if(records[i].getRecordType() == 1000l) {
- Record docRecord = records[i];
- Record[] docChildren = docRecord.getChildRecords();
- for(int j=0; j<docChildren.length; j++) {
- if(docChildren[j] instanceof SlideListWithText) {
- Record[] slwtChildren = docChildren[j].getChildRecords();
+ for (Record record : records) {
+ if (record.getRecordType() == 1000L) {
+ Record[] docChildren = record.getChildRecords();
+ for (Record docChild : docChildren) {
+ if (docChild instanceof SlideListWithText) {
+ Record[] slwtChildren = docChild.getChildRecords();
int lastTextLen = -1;
- for(int k=0; k<slwtChildren.length; k++) {
- if(slwtChildren[k] instanceof TextCharsAtom) {
- lastTextLen = ((TextCharsAtom)slwtChildren[k]).getText().length();
+ for (Record slwtChild : slwtChildren) {
+ if (slwtChild instanceof TextCharsAtom) {
+ lastTextLen = ((TextCharsAtom) slwtChild).getText().length();
}
- if(slwtChildren[k] instanceof TextBytesAtom) {
- lastTextLen = ((TextBytesAtom)slwtChildren[k]).getText().length();
+ if (slwtChild instanceof TextBytesAtom) {
+ lastTextLen = ((TextBytesAtom) slwtChild).getText().length();
}
- if(slwtChildren[k] instanceof StyleTextPropAtom) {
- StyleTextPropAtom stpa = (StyleTextPropAtom)slwtChildren[k];
+ if (slwtChild instanceof StyleTextPropAtom) {
+ StyleTextPropAtom stpa = (StyleTextPropAtom) slwtChild;
stpa.setParentTextSize(lastTextLen);
showStyleTextPropAtom(stpa);
}
// Create the slideshow object, for normal working with
HSLFSlideShowImpl ss = new HSLFSlideShowImpl(args[0]);
fileContents = ss.getUnderlyingBytes();
- System.out.println("");
+ System.out.println();
// Find any persist ones first
int pos = 0;
pos += baos.size();
}
- System.out.println("");
+ System.out.println();
pos = 0;
// Now look for UserEditAtoms
pos += baos.size();
}
- System.out.println("");
+ System.out.println();
// Query the CurrentUserAtom
System.out.println("Checking Current User Atom");
System.out.println(" Thinks the CurrentEditOffset is " + cua.getCurrentEditOffset());
- System.out.println("");
+ System.out.println();
ss.close();
}
--- /dev/null
+/* ====================================================================
+ 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.hslf.dev;
+
+import org.apache.poi.POIDataSamples;
+import org.apache.poi.hslf.exceptions.EncryptedPowerPointFileException;
+import org.apache.poi.hslf.exceptions.OldPowerPointFormatException;
+import org.apache.poi.util.IOUtils;
+import org.apache.poi.util.LocaleUtil;
+import org.apache.poi.util.NullOutputStream;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import java.io.File;
+import java.io.OutputStream;
+import java.io.PrintStream;
+import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+
+import static org.junit.Assert.assertNotNull;
+
+@RunWith(Parameterized.class)
+public abstract class BasePPTIteratingTest {
+ protected static final OutputStream NULL_OUTPUT_STREAM = new NullOutputStream();
+
+ protected static final Set<String> OLD_FILES = new HashSet<>();
+ static {
+ OLD_FILES.add("PPT95.ppt");
+ OLD_FILES.add("pp40only.ppt");
+ }
+
+ protected static final Set<String> ENCRYPTED_FILES = new HashSet<>();
+ static {
+ ENCRYPTED_FILES.add("cryptoapi-proc2356.ppt");
+ ENCRYPTED_FILES.add("Password_Protected-np-hello.ppt");
+ ENCRYPTED_FILES.add("Password_Protected-56-hello.ppt");
+ ENCRYPTED_FILES.add("Password_Protected-hello.ppt");
+ }
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ protected static final Map<String,Class<? extends Throwable>> EXCLUDED =
+ new HashMap<>();
+
+ @Parameterized.Parameters(name="{index}: {0}")
+ public static Iterable<Object[]> files() {
+ String dataDirName = System.getProperty(POIDataSamples.TEST_PROPERTY);
+ if(dataDirName == null) {
+ dataDirName = "test-data";
+ }
+
+ List<Object[]> files = new ArrayList<>();
+ findFile(files, dataDirName + "/slideshow");
+
+ return files;
+ }
+
+ private final PrintStream save = System.out;
+
+ @Before
+ public void setUpBase() throws UnsupportedEncodingException {
+ // set a higher max allocation limit as some test-files require more
+ IOUtils.setByteArrayMaxOverride(5*1024*1024);
+
+ // redirect standard out during the test to avoid spamming the console with output
+ System.setOut(new PrintStream(NULL_OUTPUT_STREAM, true, LocaleUtil.CHARSET_1252.name()));
+ }
+
+ @After
+ public void tearDownBase() {
+ System.setOut(save);
+
+ // reset
+ IOUtils.setByteArrayMaxOverride(-1);
+ }
+
+ private static void findFile(List<Object[]> list, String dir) {
+ String[] files = new File(dir).list((arg0, arg1) -> arg1.toLowerCase(Locale.ROOT).endsWith(".ppt"));
+
+ assertNotNull("Did not find any ppt files in directory " + dir, files);
+
+ for(String file : files) {
+ list.add(new Object[] { new File(dir, file) });
+ }
+ }
+
+ @Parameterized.Parameter
+ public File file;
+
+ @Test
+ public void testAllFiles() throws Exception {
+ String fileName = file.getName();
+ if (EXCLUDED.containsKey(fileName)) {
+ thrown.expect(EXCLUDED.get(fileName));
+ }
+
+ try {
+ runOneFile(file);
+ } catch (OldPowerPointFormatException e) {
+ // expected for some files
+ if(!OLD_FILES.contains(file.getName())) {
+ throw e;
+ }
+ } catch (EncryptedPowerPointFileException e) {
+ // expected for some files
+ if(!ENCRYPTED_FILES.contains(file.getName())) {
+ throw e;
+ }
+ }
+ }
+
+ abstract void runOneFile(File pFile) throws Exception;
+}
--- /dev/null
+/* ====================================================================
+ 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.hslf.dev;
+
+import org.apache.poi.EmptyFileException;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+
+import static org.junit.Assert.fail;
+
+public class TestPPDrawingTextListing extends BasePPTIteratingTest {
+ @Test
+ public void testMain() throws IOException {
+ // calls System.exit(): PPDrawingTextListing.main(new String[0]);
+
+ try {
+ PPDrawingTextListing.main(new String[]{"invalidfile"});
+ fail("Should catch exception here");
+ } catch (EmptyFileException e) {
+ // expected here
+ }
+ }
+
+ @Override
+ void runOneFile(File pFile) throws Exception {
+ PPDrawingTextListing.main(new String[]{pFile.getAbsolutePath()});
+ }
+}
--- /dev/null
+/* ====================================================================
+ 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.hslf.dev;
+
+import org.apache.poi.EmptyFileException;
+import org.apache.poi.hslf.HSLFTestDataSamples;
+import org.junit.Test;
+
+import java.io.File;
+
+import static org.junit.Assert.fail;
+
+public class TestPPTXMLDump extends BasePPTIteratingTest {
+ @Test
+ public void testMain() throws Exception {
+ PPTXMLDump.main(new String[0]);
+
+ PPTXMLDump.main(new String[]{
+ HSLFTestDataSamples.getSampleFile("slide_master.ppt").getAbsolutePath(),
+ HSLFTestDataSamples.getSampleFile("pictures.ppt").getAbsolutePath()
+ });
+
+ try {
+ PPTXMLDump.main(new String[]{"invalidfile"});
+ fail("Should catch exception here");
+ } catch (EmptyFileException e) {
+ // expected here
+ }
+ }
+
+ @Override
+ void runOneFile(File pFile) throws Exception {
+ PPTXMLDump.main(new String[]{pFile.getAbsolutePath()});
+ }
+}
--- /dev/null
+/* ====================================================================
+ 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.hslf.dev;
+
+import org.apache.poi.EmptyFileException;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+
+import static org.junit.Assert.fail;
+
+public class TestSLWTListing extends BasePPTIteratingTest {
+ @Test
+ public void testMain() throws IOException {
+ // calls System.exit(): SLWTListing.main(new String[0]);
+ try {
+ SLWTListing.main(new String[]{"invalidfile"});
+ fail("Should catch exception here");
+ } catch (EmptyFileException e) {
+ // expected here
+ }
+ }
+
+ @Override
+ void runOneFile(File pFile) throws Exception {
+ SLWTListing.main(new String[]{pFile.getAbsolutePath()});
+ }
+}
\ No newline at end of file
--- /dev/null
+/* ====================================================================
+ 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.hslf.dev;
+
+import org.apache.poi.EmptyFileException;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+
+import static org.junit.Assert.fail;
+
+public class TestSLWTTextListing extends BasePPTIteratingTest {
+ @Test
+ public void testMain() throws IOException {
+ // calls System.exit(): SLWTTextListing.main(new String[0]);
+
+ try {
+ SLWTTextListing.main(new String[]{"invalidfile"});
+ fail("Should catch exception here");
+ } catch (EmptyFileException e) {
+ // expected here
+ }
+ }
+
+ @Override
+ void runOneFile(File pFile) throws Exception {
+ SLWTTextListing.main(new String[]{pFile.getAbsolutePath()});
+ }
+}
\ No newline at end of file
--- /dev/null
+/* ====================================================================
+ 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.hslf.dev;
+
+import org.apache.poi.EmptyFileException;
+import org.apache.poi.hslf.HSLFTestDataSamples;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+
+import static org.junit.Assert.fail;
+
+public class TestSlideAndNotesAtomListing extends BasePPTIteratingTest {
+ @Test
+ public void testMain() throws IOException {
+ // calls System.exit(): SlideAndNotesAtomListing.main(new String[0]);
+ SlideAndNotesAtomListing.main(new String[] {
+ HSLFTestDataSamples.getSampleFile("slide_master.ppt").getAbsolutePath()
+ });
+
+ try {
+ SlideAndNotesAtomListing.main(new String[]{"invalidfile"});
+ fail("Should catch exception here");
+ } catch (EmptyFileException e) {
+ // expected here
+ }
+ }
+
+ @Override
+ void runOneFile(File pFile) throws Exception {
+ SlideAndNotesAtomListing.main(new String[]{pFile.getAbsolutePath()});
+ }
+}
\ No newline at end of file
--- /dev/null
+/* ====================================================================
+ 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.hslf.dev;
+
+import org.apache.poi.EmptyFileException;
+import org.apache.poi.hslf.HSLFTestDataSamples;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+
+import static org.junit.Assert.fail;
+
+public class TestSlideIdListing extends BasePPTIteratingTest {
+ @Test
+ public void testMain() throws IOException {
+ // calls System.exit(): SlideIdListing.main(new String[0]);
+ SlideIdListing.main(new String[] {
+ HSLFTestDataSamples.getSampleFile("slide_master.ppt").getAbsolutePath()
+ });
+
+ try {
+ SlideIdListing.main(new String[]{"invalidfile"});
+ fail("Should catch exception here");
+ } catch (EmptyFileException e) {
+ // expected here
+ }
+ }
+
+ @Override
+ void runOneFile(File pFile) throws Exception {
+ SlideIdListing.main(new String[]{pFile.getAbsolutePath()});
+ }
+}
\ No newline at end of file
--- /dev/null
+/* ====================================================================
+ 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.hslf.dev;
+
+import org.apache.poi.EmptyFileException;
+import org.apache.poi.hslf.HSLFTestDataSamples;
+import org.apache.poi.util.IOUtils;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Set;
+
+import static org.junit.Assert.fail;
+
+public class TestSlideShowDumper extends BasePPTIteratingTest {
+ private static final Set<String> FAILING = new HashSet<>();
+ static {
+ FAILING.add("cryptoapi-proc2356.ppt");
+ FAILING.add("41384.ppt");
+ FAILING.add("bug56240.ppt");
+ }
+
+ @Test
+ public void testMain() throws IOException {
+ SlideShowDumper.main(new String[0]);
+
+ // SlideShowDumper calls IOUtils.toByteArray(is), which would fail if a different size is defined
+ IOUtils.setByteArrayMaxOverride(-1);
+
+ SlideShowDumper.main(new String[] {
+ HSLFTestDataSamples.getSampleFile("slide_master.ppt").getAbsolutePath(),
+ HSLFTestDataSamples.getSampleFile("pictures.ppt").getAbsolutePath()
+ });
+
+ try {
+ SlideShowDumper.main(new String[]{"invalidfile"});
+ fail("Should catch exception here");
+ } catch (EmptyFileException e) {
+ // expected here
+ }
+ }
+
+ @Override
+ void runOneFile(File pFile) throws Exception {
+ try {
+ // SlideShowDumper calls IOUtils.toByteArray(is), which would fail if a different size is defined
+ IOUtils.setByteArrayMaxOverride(-1);
+
+ SlideShowDumper.main(new String[]{pFile.getAbsolutePath()});
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // some corrupted documents currently can cause this excpetion
+ if (!FAILING.contains(pFile.getName()) && !ENCRYPTED_FILES.contains(pFile.getName())) {
+ throw e;
+ }
+ } catch (FileNotFoundException e) {
+ // some old files are not detected correctly
+ if(!OLD_FILES.contains(pFile.getName())) {
+ throw e;
+ }
+ }
+ }
+}
\ No newline at end of file
--- /dev/null
+/* ====================================================================
+ 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.hslf.dev;
+
+import org.apache.poi.EmptyFileException;
+import org.apache.poi.hslf.HSLFTestDataSamples;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+
+import static org.junit.Assert.fail;
+
+public class TestSlideShowRecordDumper extends BasePPTIteratingTest {
+ @Test
+ public void testMain() throws IOException {
+ SlideShowRecordDumper.main(new String[0]);
+
+ SlideShowRecordDumper.main(new String[] {
+ HSLFTestDataSamples.getSampleFile("slide_master.ppt").getAbsolutePath(),
+ });
+
+ SlideShowRecordDumper.main(new String[] {
+ "-escher",
+ HSLFTestDataSamples.getSampleFile("slide_master.ppt").getAbsolutePath(),
+ });
+
+ SlideShowRecordDumper.main(new String[] {
+ "-verbose",
+ HSLFTestDataSamples.getSampleFile("pictures.ppt").getAbsolutePath()
+ });
+
+ try {
+ SlideShowRecordDumper.main(new String[]{"invalidfile"});
+ fail("Should catch exception here");
+ } catch (EmptyFileException e) {
+ // expected here
+ }
+ }
+
+ @Override
+ void runOneFile(File pFile) throws Exception {
+ SlideShowRecordDumper.main(new String[]{pFile.getAbsolutePath()});
+ }
+}
\ No newline at end of file
--- /dev/null
+/* ====================================================================
+ 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.hslf.dev;
+
+import org.apache.poi.EmptyFileException;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Set;
+
+import static org.junit.Assert.fail;
+
+public class TestTextStyleListing extends BasePPTIteratingTest {
+ private static Set<String> FAILING = new HashSet<>();
+ static {
+ FAILING.add("empty_textbox.ppt");
+ }
+
+ @Test
+ public void testMain() throws IOException {
+ // calls System.exit(): TextStyleListing.main(new String[0]);
+ try {
+ TextStyleListing.main(new String[]{"invalidfile"});
+ fail("Should catch exception here");
+ } catch (EmptyFileException e) {
+ // expected here
+ }
+ }
+
+ @Override
+ void runOneFile(File pFile) throws Exception {
+ try {
+ TextStyleListing.main(new String[]{pFile.getAbsolutePath()});
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // some corrupted documents currently can cause this excpetion
+ if (!FAILING.contains(pFile.getName())) {
+ throw e;
+ }
+ }
+ }
+}
\ No newline at end of file
--- /dev/null
+/* ====================================================================
+ 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.hslf.dev;
+
+import org.apache.poi.EmptyFileException;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+
+import static org.junit.Assert.fail;
+
+public class TestUserEditAndPersistListing extends BasePPTIteratingTest {
+ @Test
+ public void testMain() throws IOException {
+ // calls System.exit(): UserEditAndPersistListing.main(new String[0]);
+
+ try {
+ UserEditAndPersistListing.main(new String[]{"invalidfile"});
+ fail("Should catch exception here");
+ } catch (EmptyFileException e) {
+ // expected here
+ }
+ }
+
+ @Override
+ void runOneFile(File pFile) throws Exception {
+ UserEditAndPersistListing.main(new String[]{pFile.getAbsolutePath()});
+ }
+}
\ No newline at end of file
@Test
public void testWithData() throws Exception {
- new EscherDump().dumpOld(8, new ByteArrayInputStream(new byte[] { 00, 00, 00, 00, 00, 00, 00, 00 }), nullPS);
+ new EscherDump().dumpOld(8, new ByteArrayInputStream(new byte[] {0, 0, 0, 0, 0, 0, 0, 0}), nullPS);
}
@Test
//new EscherDump().dumpOld(data.length, new ByteArrayInputStream(data), System.out);
data = new byte[2586114];
- InputStream stream = HSSFTestDataSamples.openSampleFileStream("44593.xls");
- try {
+ try (InputStream stream = HSSFTestDataSamples.openSampleFileStream("44593.xls")) {
int bytes = IOUtils.readFully(stream, data);
assertTrue(bytes != -1);
//new EscherDump().dump(bytes, data, System.out);
//new EscherDump().dumpOld(bytes, new ByteArrayInputStream(data), System.out);
- } finally {
- stream.close();
}
}
import java.io.File;
import java.io.FileInputStream;
-import java.io.FilenameFilter;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.apache.poi.POIDataSamples;
-import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.util.NullOutputStream;
import org.junit.Rule;
}
private static void findFile(List<Object[]> list, String dir) {
- String[] files = new File(dir).list(new FilenameFilter() {
- @Override
- public boolean accept(File arg0, String arg1) {
- return arg1.toLowerCase(Locale.ROOT).endsWith(".xls");
- }
- });
+ String[] files = new File(dir).list((arg0, arg1) -> arg1.toLowerCase(Locale.ROOT).endsWith(".xls"));
assertNotNull("Did not find any xls files in directory " + dir, files);
runOneFile(file);
} catch (Exception e) {
// try to read it in HSSFWorkbook to quickly fail if we cannot read the file there at all and thus probably should use EXCLUDED instead
- FileInputStream stream = new FileInputStream(file);
- HSSFWorkbook wb = null;
- try {
- wb = new HSSFWorkbook(stream);
- assertNotNull(wb);
- } finally {
- if (wb != null) {
- wb.close();
- }
- stream.close();
- }
+ try (FileInputStream stream = new FileInputStream(file); HSSFWorkbook wb = new HSSFWorkbook(stream)) {
+ assertNotNull(wb);
+ }
throw e;
}
EXCLUDED.put("testEXCEL_5.xls", OldExcelFormatException.class); // Biff 5 / Excel 5
EXCLUDED.put("60284.xls", OldExcelFormatException.class); // Biff 5 / Excel 5
EXCLUDED.put("testEXCEL_95.xls", OldExcelFormatException.class); // Biff 5 / Excel 95
- EXCLUDED.put("60284.xls", OldExcelFormatException.class); // Biff 5 / Excel 95
EXCLUDED.put("43493.xls", RecordInputStream.LeftoverDataException.class); // HSSFWorkbook cannot open it as well
EXCLUDED.put("44958_1.xls", RecordInputStream.LeftoverDataException.class);
EXCLUDED.put("61300.xls", RecordFormatException.class);
try {
//System.setOut(new PrintStream(TestBiffViewer.NULL_OUTPUT_STREAM));
// use a NullOutputStream to not write the bytes anywhere for best runtime
- InputStream wb = new FileInputStream(pFile);
- try {
- BiffDrawingToXml.writeToFile(NULL_OUTPUT_STREAM, wb, false, new String[] {});
- } finally {
- wb.close();
- }
+ try (InputStream wb = new FileInputStream(pFile)) {
+ BiffDrawingToXml.writeToFile(NULL_OUTPUT_STREAM, wb, false, new String[]{});
+ }
} finally {
System.setOut(save);
}
EXCLUDED.put("testEXCEL_5.xls", OldExcelFormatException.class); // Biff 5 / Excel 5
EXCLUDED.put("60284.xls", OldExcelFormatException.class); // Biff 5 / Excel 5
EXCLUDED.put("testEXCEL_95.xls", OldExcelFormatException.class); // Biff 5 / Excel 95
- EXCLUDED.put("60284.xls", OldExcelFormatException.class); // Biff 5 / Excel 95
EXCLUDED.put("43493.xls", RecordInputStream.LeftoverDataException.class); // HSSFWorkbook cannot open it as well
EXCLUDED.put("44958_1.xls", RecordInputStream.LeftoverDataException.class);
EXCLUDED.put("XRefCalc.xls", RuntimeException.class); // "Buffer overrun"
EXCLUDED.put("testEXCEL_5.xls", OldExcelFormatException.class); // Biff 5 / Excel 5
EXCLUDED.put("60284.xls", OldExcelFormatException.class); // Biff 5 / Excel 5
EXCLUDED.put("testEXCEL_95.xls", OldExcelFormatException.class); // Biff 5 / Excel 95
- EXCLUDED.put("60284.xls", OldExcelFormatException.class); // Biff 5 / Excel 95
EXCLUDED.put("43493.xls", RecordInputStream.LeftoverDataException.class); // HSSFWorkbook cannot open it as well
EXCLUDED.put("44958_1.xls", RecordInputStream.LeftoverDataException.class);
EXCLUDED.put("61300.xls", RecordFormatException.class);
EXCLUDED.put("testEXCEL_5.xls", OldExcelFormatException.class); // Biff 5 / Excel 5
EXCLUDED.put("60284.xls", OldExcelFormatException.class); // Biff 5 / Excel 5
EXCLUDED.put("testEXCEL_95.xls", OldExcelFormatException.class); // Biff 5 / Excel 95
- EXCLUDED.put("60284.xls", OldExcelFormatException.class); // Biff 5 / Excel 95
EXCLUDED.put("43493.xls", RecordInputStream.LeftoverDataException.class); // HSSFWorkbook cannot open it as well
EXCLUDED.put("44958_1.xls", RecordInputStream.LeftoverDataException.class);
EXCLUDED.put("XRefCalc.xls", RuntimeException.class); // "Buffer overrun"
// clean up the re-saved file
assertTrue(!reSavedFile.exists() || reSavedFile.delete());
}
-
} finally {
System.setOut(save);
}
EXCLUDED.put("testEXCEL_5.xls", OldExcelFormatException.class); // Biff 5 / Excel 5
EXCLUDED.put("60284.xls", OldExcelFormatException.class); // Biff 5 / Excel 5
EXCLUDED.put("testEXCEL_95.xls", OldExcelFormatException.class); // Biff 5 / Excel 95
- EXCLUDED.put("60284.xls", OldExcelFormatException.class); // Biff 5 / Excel 95
EXCLUDED.put("61300.xls", RecordFormatException.class);
}