Patch from Trejkaz from GitHub Pull 2 - HSSFWorkbook.getAllEmbeddedObjects() needs to recurse into container Shapes (with new unit test)

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1493001 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2013-06-14 08:53:29 +00:00
parent 042aab4030
commit bd1214db1e
2 changed files with 38 additions and 6 deletions

View File

@ -1723,13 +1723,24 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
if (null == patriarch){
return;
}
for (HSSFShape shape: patriarch.getChildren()){
if (shape instanceof HSSFObjectData){
getAllEmbeddedObjects(patriarch, objects);
}
/**
* Recursively iterates a shape container to get all embedded objects.
*
* @param parent the parent.
* @param objects the list of embedded objects to populate.
*/
private void getAllEmbeddedObjects(HSSFShapeContainer parent, List<HSSFObjectData> objects)
{
for (HSSFShape shape : parent.getChildren()) {
if (shape instanceof HSSFObjectData) {
objects.add((HSSFObjectData) shape);
} else if (shape instanceof HSSFShapeContainer) {
getAllEmbeddedObjects((HSSFShapeContainer) shape, objects);
}
}
}
public HSSFCreationHelper getCreationHelper() {
return new HSSFCreationHelper(this);
}

View File

@ -24,11 +24,9 @@ import java.io.IOException;
import java.util.List;
/**
* @author Evgeniy Berlog
* @date 13.07.12
* Tests for the embedded object fetching support in HSSF
*/
public class TestEmbeddedObjects extends TestCase{
public void testReadExistingObject() throws IOException {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls");
List<HSSFObjectData> list = wb.getAllEmbeddedObjects();
@ -38,4 +36,27 @@ public class TestEmbeddedObjects extends TestCase{
assertNotNull(obj.getDirectory());
assertNotNull(obj.getOLE2ClassName());
}
/**
* Need to recurse into the shapes to find this one
* See https://github.com/apache/poi/pull/2
*/
public void testReadNestedObject() throws IOException {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("WithCheckBoxes.xls");
List<HSSFObjectData> list = wb.getAllEmbeddedObjects();
assertEquals(list.size(), 1);
HSSFObjectData obj = list.get(0);
assertNotNull(obj.getObjectData());
assertNotNull(obj.getOLE2ClassName());
}
/**
* One with large numbers of recursivly embedded resources
* See https://github.com/apache/poi/pull/2
*/
public void testReadManyNestedObjects() throws IOException {
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("45538_form_Header.xls");
List<HSSFObjectData> list = wb.getAllEmbeddedObjects();
assertEquals(list.size(), 40);
}
}