Fix issue found via large corpus tests: HSSFPicture in a HSSFShapeGroup might not have a patriarch set, so let's walk up the parents to try to find one.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1790472 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2017-04-06 21:49:53 +00:00
parent fe53bc134e
commit 2254ad2ac3
3 changed files with 30 additions and 1 deletions

View File

@ -204,7 +204,17 @@ public class HSSFPicture extends HSSFSimpleShape implements Picture {
* @return picture data for this shape * @return picture data for this shape
*/ */
public HSSFPictureData getPictureData(){ public HSSFPictureData getPictureData(){
InternalWorkbook iwb = getPatriarch().getSheet().getWorkbook().getWorkbook(); HSSFPatriarch patriarch = getPatriarch();
HSSFShape parent = getParent();
while(patriarch == null && parent != null) {
patriarch = parent.getPatriarch();
parent = parent.getParent();
}
if(patriarch == null) {
throw new IllegalStateException("Could not find a patriarch for a HSSPicture");
}
InternalWorkbook iwb = patriarch.getSheet().getWorkbook().getWorkbook();
EscherBSERecord bse = iwb.getBSERecord(getPictureIndex()); EscherBSERecord bse = iwb.getBSERecord(getPictureIndex());
EscherBlipRecord blipRecord = bse.getBlipRecord(); EscherBlipRecord blipRecord = bse.getBlipRecord();
return new HSSFPictureData(blipRecord); return new HSSFPictureData(blipRecord);

View File

@ -18,6 +18,7 @@
package org.apache.poi.ss.extractor; package org.apache.poi.ss.extractor;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -30,6 +31,8 @@ import javax.xml.bind.DatatypeConverter;
import org.apache.poi.EncryptedDocumentException; import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.POIDataSamples; import org.apache.poi.POIDataSamples;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
@ -112,4 +115,20 @@ public class TestEmbeddedExtractor {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
@Test
public void testNPE() throws IOException {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("angelo.edu_content_files_19555-nsse-2011-multiyear-benchmark.xls");
EmbeddedExtractor ee = new EmbeddedExtractor();
for (Sheet s : wb) {
for (EmbeddedData ed : ee.extractAll(s)) {
assertNotNull(ed.getFilename());
assertNotNull(ed.getEmbeddedData());
assertNotNull(ed.getShape());
}
}
}
} }