Browse Source

Bugzilla 53568: Fixed null returned by XSSFPicture.getPictureData()

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1396539 13f79535-47bb-0310-9956-ffa450edef68
tags/3.10-beta1
Yegor Kozlov 11 years ago
parent
commit
95366195ff

+ 1
- 0
src/documentation/content/xdocs/status.xml View File



<changes> <changes>
<release version="3.9-beta1" date="2012-??-??"> <release version="3.9-beta1" date="2012-??-??">
<action dev="poi-developers" type="fix">53568 - Fixed null returned by XSSFPicture.getPictureData()</action>
<action dev="poi-developers" type="fix">53950 - fixed setForceFormulaRecalculation to reset workbook-level "manual" flag</action> <action dev="poi-developers" type="fix">53950 - fixed setForceFormulaRecalculation to reset workbook-level "manual" flag</action>
<action dev="poi-developers" type="fix">52211 - avoid unnessary re-coverting content types to US-ASCII, it can cause exceptions on ibm mainframes</action> <action dev="poi-developers" type="fix">52211 - avoid unnessary re-coverting content types to US-ASCII, it can cause exceptions on ibm mainframes</action>
<action dev="poi-developers" type="fix">53568 - Set shapes anchors in XSSF when reading from existing drawings</action> <action dev="poi-developers" type="fix">53568 - Set shapes anchors in XSSF when reading from existing drawings</action>

+ 1
- 7
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPicture.java View File

*/ */
public XSSFPictureData getPictureData() { public XSSFPictureData getPictureData() {
String blipId = ctPicture.getBlipFill().getBlip().getEmbed(); String blipId = ctPicture.getBlipFill().getBlip().getEmbed();
for (POIXMLDocumentPart part : getDrawing().getRelations()) {
if(part.getPackageRelationship().getId().equals(blipId)){
return (XSSFPictureData)part;
}
}
logger.log(POILogger.WARN, "Picture data was not found for blipId=" + blipId);
return null;
return (XSSFPictureData)getDrawing().getRelationById(blipId);
} }


protected CTShapeProperties getShapeProperties(){ protected CTShapeProperties getShapeProperties(){

+ 9
- 23
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java View File

import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.regex.Pattern; import java.util.regex.Pattern;


import javax.xml.namespace.QName; import javax.xml.namespace.QName;
import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.CellReference;
import org.apache.poi.ss.util.WorkbookUtil; import org.apache.poi.ss.util.WorkbookUtil;
import org.apache.poi.util.*; import org.apache.poi.util.*;
import org.apache.poi.xslf.usermodel.XSLFPictureData;
import org.apache.poi.xssf.model.*; import org.apache.poi.xssf.model.*;
import org.apache.poi.xssf.usermodel.helpers.XSSFFormulaUtils; import org.apache.poi.xssf.usermodel.helpers.XSSFFormulaUtils;
import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlException;
* @see #addPicture(byte[], int) * @see #addPicture(byte[], int)
*/ */
public List<XSSFPictureData> getAllPictures() { public List<XSSFPictureData> getAllPictures() {
if(pictures == null) {
//In OOXML pictures are referred to in sheets,
//dive into sheet's relations, select drawings and their images
pictures = new ArrayList<XSSFPictureData>();
for(XSSFSheet sh : sheets){
for(POIXMLDocumentPart dr : sh.getRelations()){
if(dr instanceof XSSFDrawing){
for(POIXMLDocumentPart img : dr.getRelations()){
if(img instanceof XSSFPictureData){
pictures.add((XSSFPictureData)img);
}
}
}
}
if(pictures == null){
List<PackagePart> mediaParts = getPackage().getPartsByName(Pattern.compile("/xl/media/.*?"));
pictures = new ArrayList<XSSFPictureData>(mediaParts.size());
for(PackagePart part : mediaParts){
pictures.add(new XSSFPictureData(part, null));
} }
} }
return pictures;
return pictures; //YK: should return Collections.unmodifiableList(pictures);
} }


/** /**
* gGet the cell style object at the given index
* Get the cell style object at the given index
* *
* @param idx index within the set of styles * @param idx index within the set of styles
* @return XSSFCellStyle object at the index * @return XSSFCellStyle object at the index

+ 49
- 0
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPicture.java View File

import org.apache.poi.ss.usermodel.ClientAnchor; import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.BaseTestPicture; import org.apache.poi.ss.usermodel.BaseTestPicture;
import org.apache.poi.xssf.XSSFITestDataProvider; import org.apache.poi.xssf.XSSFITestDataProvider;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTTwoCellAnchor; import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTTwoCellAnchor;
import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.STEditAs; import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.STEditAs;


XSSFPicture shape2 = drawing.createPicture(anchor, jpegIdx); XSSFPicture shape2 = drawing.createPicture(anchor, jpegIdx);
assertEquals(2, shape2.getCTPicture().getNvPicPr().getCNvPr().getId()); assertEquals(2, shape2.getCTPicture().getNvPicPr().getCNvPr().getId());
} }

/**
* same image refrerred by mulitple sheets
*/
public void testMultiRelationShips(){
XSSFWorkbook wb = new XSSFWorkbook();

byte[] pic1Data = "test jpeg data".getBytes();
byte[] pic2Data = "test png data".getBytes();

List<XSSFPictureData> pictures = wb.getAllPictures();
assertEquals(0, pictures.size());

int pic1 = wb.addPicture(pic1Data, XSSFWorkbook.PICTURE_TYPE_JPEG);
int pic2 = wb.addPicture(pic2Data, XSSFWorkbook.PICTURE_TYPE_PNG);

XSSFSheet sheet1 = wb.createSheet();
XSSFDrawing drawing1 = sheet1.createDrawingPatriarch();
XSSFPicture shape1 = drawing1.createPicture(new XSSFClientAnchor(), pic1);
XSSFPicture shape2 = drawing1.createPicture(new XSSFClientAnchor(), pic2);

XSSFSheet sheet2 = wb.createSheet();
XSSFDrawing drawing2 = sheet2.createDrawingPatriarch();
XSSFPicture shape3 = drawing2.createPicture(new XSSFClientAnchor(), pic2);
XSSFPicture shape4 = drawing2.createPicture(new XSSFClientAnchor(), pic1);

assertEquals(2, pictures.size());

wb = XSSFTestDataSamples.writeOutAndReadBack(wb);
pictures = wb.getAllPictures();
assertEquals(2, pictures.size());

sheet1 = wb.getSheetAt(0);
drawing1 = sheet1.createDrawingPatriarch();
XSSFPicture shape11 = (XSSFPicture)drawing1.getShapes().get(0);
assertTrue(Arrays.equals(shape1.getPictureData().getData(), shape11.getPictureData().getData()));
XSSFPicture shape22 = (XSSFPicture)drawing1.getShapes().get(1);
assertTrue(Arrays.equals(shape2.getPictureData().getData(), shape22.getPictureData().getData()));

sheet2 = wb.getSheetAt(1);
drawing2 = sheet2.createDrawingPatriarch();
XSSFPicture shape33 = (XSSFPicture)drawing2.getShapes().get(0);
assertTrue(Arrays.equals(shape3.getPictureData().getData(), shape33.getPictureData().getData()));
XSSFPicture shape44 = (XSSFPicture)drawing2.getShapes().get(1);
assertTrue(Arrays.equals(shape4.getPictureData().getData(), shape44.getPictureData().getData()));

}

} }

+ 24
- 0
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPictureData.java View File

assertTrue(Arrays.equals(pngData, pictures2.get(pngIdx).getData())); assertTrue(Arrays.equals(pngData, pictures2.get(pngIdx).getData()));


} }

/**
* Bug 53568: XSSFPicture.getPictureData() can return null.
*/
public void test53568(){
XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("53568.xlsx");
List<XSSFPictureData> pictures = wb.getAllPictures();

XSSFSheet sheet1 = wb.getSheetAt(0);
List<XSSFShape> shapes1 = sheet1.createDrawingPatriarch().getShapes();

for(int i = 0; i < wb.getNumberOfSheets(); i++){
XSSFSheet sheet = wb.getSheetAt(i);
XSSFDrawing drawing = sheet.createDrawingPatriarch();
for(XSSFShape shape : drawing.getShapes()){
if(shape instanceof XSSFPicture){
XSSFPicture pic = (XSSFPicture)shape;
XSSFPictureData picData = pic.getPictureData();
assertNotNull(picData);
}
}
}

}
} }

+ 1
- 1
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFWorkbook.java View File

public void testBug47668() throws Exception { public void testBug47668() throws Exception {
XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("47668.xlsx"); XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("47668.xlsx");
List<XSSFPictureData> allPictures = workbook.getAllPictures(); List<XSSFPictureData> allPictures = workbook.getAllPictures();
assertEquals(2, allPictures.size());
assertEquals(1, allPictures.size());


PackagePartName imagePartName = PackagingURIHelper PackagePartName imagePartName = PackagingURIHelper
.createPartName("/xl/media/image1.jpeg"); .createPartName("/xl/media/image1.jpeg");

BIN
test-data/spreadsheet/53568.xlsx View File


Loading…
Cancel
Save