Fix bug #49765 - Fix XWPFDocument.addPicture so that it correctly sets up relationships

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@998960 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2010-09-20 15:43:07 +00:00
parent cc419a3a85
commit cbe4cd8b21
4 changed files with 54 additions and 71 deletions

View File

@ -34,6 +34,7 @@
<changes> <changes>
<release version="3.7-beta3" date="2010-??-??"> <release version="3.7-beta3" date="2010-??-??">
<action dev="poi-developers" type="fix">49765 - Fix XWPFDocument.addPicture so that it correctly sets up relationships</action>
<action dev="poi-developers" type="fix">48018 - Improve HWPF handling of lists in documents read and then saved, by preserving order better</action> <action dev="poi-developers" type="fix">48018 - Improve HWPF handling of lists in documents read and then saved, by preserving order better</action>
<action dev="poi-developers" type="fix">49820 - Fix HWPF paragraph levels, so that outline levels can be properly fetched</action> <action dev="poi-developers" type="fix">49820 - Fix HWPF paragraph levels, so that outline levels can be properly fetched</action>
<action dev="poi-developers" type="fix">47271 - Avoid infinite loops on broken HWPF documents with a corrupt CHP style with a parent of itself</action> <action dev="poi-developers" type="fix">47271 - Avoid infinite loops on broken HWPF documents with a corrupt CHP style with a parent of itself</action>

View File

@ -230,7 +230,9 @@ public class POIXMLDocumentPart {
try { try {
PackagePartName ppName = PackagingURIHelper.createPartName(descriptor.getFileName(idx)); PackagePartName ppName = PackagingURIHelper.createPartName(descriptor.getFileName(idx));
PackageRelationship rel = null; PackageRelationship rel = null;
if(!noRelation) rel = packagePart.addRelationship(ppName, TargetMode.INTERNAL, descriptor.getRelation()); if(!noRelation) {
rel = packagePart.addRelationship(ppName, TargetMode.INTERNAL, descriptor.getRelation());
}
PackagePart part = packagePart.getPackage().createPart(ppName, descriptor.getContentType()); PackagePart part = packagePart.getPackage().createPart(ppName, descriptor.getContentType());
POIXMLDocumentPart doc = factory.newDocumentPart(descriptor); POIXMLDocumentPart doc = factory.newDocumentPart(descriptor);
doc.packageRel = rel; doc.packageRel = rel;

View File

@ -16,6 +16,7 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.xwpf.usermodel; package org.apache.poi.xwpf.usermodel;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -994,13 +995,12 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
*/ */
public int addPicture(InputStream is, int format) throws IOException, InvalidFormatException { public int addPicture(InputStream is, int format) throws IOException, InvalidFormatException {
int imageNumber = getNextPicNameNumber(format); int imageNumber = getNextPicNameNumber(format);
XWPFPictureData img = (XWPFPictureData)createRelationship(XWPFPictureData.RELATIONS[format], XWPFFactory.getInstance(), imageNumber, true); XWPFPictureData img = (XWPFPictureData)createRelationship(XWPFPictureData.RELATIONS[format], XWPFFactory.getInstance(), imageNumber, false);
OutputStream out = img.getPackagePart().getOutputStream(); OutputStream out = img.getPackagePart().getOutputStream();
IOUtils.copy(is, out); IOUtils.copy(is, out);
out.close(); out.close();
pictures.add(img); pictures.add(img);
return getAllPictures().size()-1; return getAllPictures().size()-1;
} }
/** /**
@ -1013,18 +1013,11 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
* @throws InvalidFormatException * @throws InvalidFormatException
*/ */
public int addPicture(byte[] pictureData, int format) throws InvalidFormatException { public int addPicture(byte[] pictureData, int format) throws InvalidFormatException {
int imageNumber = getNextPicNameNumber(format);
XWPFPictureData img = (XWPFPictureData)createRelationship(XWPFPictureData.RELATIONS[format], XWPFFactory.getInstance(), imageNumber, false);
try { try {
OutputStream out = img.getPackagePart().getOutputStream(); return addPicture(new ByteArrayInputStream(pictureData), format);
out.write(pictureData);
out.close();
} catch (IOException e){ } catch (IOException e){
throw new POIXMLException(e); throw new POIXMLException(e);
} }
pictures.add(img);
return getAllPictures().size()-1;
} }
/** /**
@ -1064,48 +1057,6 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
return null; return null;
} }
/**
* Add the picture to drawing relations
*
* @param img the PictureData of the Picture,
* @throws InvalidFormatException
*/
public PackageRelationship addPictureReference(byte[] pictureData, int format) throws InvalidFormatException{
int imageNumber = getNextPicNameNumber(format);
XWPFPictureData img = (XWPFPictureData)createRelationship(XWPFPictureData.RELATIONS[format], XWPFFactory.getInstance(), imageNumber, false);
PackageRelationship rel = null;
try {
OutputStream out = img.getPackagePart().getOutputStream();
out.write(pictureData);
out.close();
rel = img.getPackageRelationship();
pictures.add(img);
} catch (IOException e){
throw new POIXMLException(e);
}
return rel;
}
/**
* Add the picture to drawing relations
*
* @param img the PictureData of the Picture,
* @throws InvalidFormatException
* @throws IOException
*/
public PackageRelationship addPictureReference(InputStream is, int format) throws InvalidFormatException, IOException{
PackageRelationship rel = null;
int imageNumber = getNextPicNameNumber(format);
XWPFPictureData img = (XWPFPictureData)createRelationship(XWPFPictureData.RELATIONS[format], XWPFFactory.getInstance(), imageNumber, false);
OutputStream out = img.getPackagePart().getOutputStream();
IOUtils.copy(is, out);
out.close();
rel = img.getPackageRelationship();
pictures.add(img);
return rel;
}
/** /**
* getNumbering * getNumbering
* @return numbering * @return numbering

View File

@ -24,6 +24,7 @@ import junit.framework.TestCase;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.PackageRelationship; import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.xssf.usermodel.XSSFRelation;
import org.apache.poi.xwpf.XWPFTestDataSamples; import org.apache.poi.xwpf.XWPFTestDataSamples;
public class TestXWPFPictureData extends TestCase { public class TestXWPFPictureData extends TestCase {
@ -57,30 +58,58 @@ public class TestXWPFPictureData extends TestCase {
} }
} }
public void testNew(){ public void testNew() throws Exception {
XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("EmptyDocumentWithHeaderFooter.docx"); XWPFDocument doc = XWPFTestDataSamples.openSampleDocument("EmptyDocumentWithHeaderFooter.docx");
byte[] jpegData = "test jpeg data".getBytes(); byte[] jpegData = "test jpeg data".getBytes();
byte[] wmfData = "test wmf data".getBytes(); byte[] wmfData = "test wmf data".getBytes();
byte[] pngData = "test png data".getBytes(); byte[] pngData = "test png data".getBytes();
List<XWPFPictureData> pictures = doc.getAllPictures(); List<XWPFPictureData> pictures = doc.getAllPictures();
assertEquals(0, pictures.size()); assertEquals(0, pictures.size());
// Document shouldn't have any image relationships
assertEquals(13, doc.getPackagePart().getRelationships().size());
for(PackageRelationship rel : doc.getPackagePart().getRelationships()) {
if(rel.getRelationshipType().equals(XSSFRelation.IMAGE_JPEG.getRelation())) {
fail("Shouldn't have JPEG yet");
}
}
// Add the image
int jpegIdx; int jpegIdx;
try {
jpegIdx = doc.addPicture(jpegData, XWPFDocument.PICTURE_TYPE_JPEG); jpegIdx = doc.addPicture(jpegData, XWPFDocument.PICTURE_TYPE_JPEG);
assertEquals(1, pictures.size()); assertEquals(1, pictures.size());
assertEquals("jpeg", pictures.get(jpegIdx).suggestFileExtension()); assertEquals("jpeg", pictures.get(jpegIdx).suggestFileExtension());
assertTrue(Arrays.equals(jpegData, pictures.get(jpegIdx).getData())); assertTrue(Arrays.equals(jpegData, pictures.get(jpegIdx).getData()));
PackageRelationship addPictureReference = doc.addPictureReference(jpegData, Document.PICTURE_TYPE_JPEG ); // Ensure it now has one
XWPFPictureData pictureDataByID = doc.getPictureDataByID(addPictureReference.getId()); assertEquals(14, doc.getPackagePart().getRelationships().size());
PackageRelationship jpegRel = null;
for(PackageRelationship rel : doc.getPackagePart().getRelationships()) {
if(rel.getRelationshipType().equals(XWPFRelation.IMAGE_JPEG.getRelation())) {
if(jpegRel != null)
fail("Found 2 jpegs!");
jpegRel = rel;
}
}
assertNotNull("JPEG Relationship not found", jpegRel);
// Check the details
assertEquals(XWPFRelation.IMAGE_JPEG.getRelation(), jpegRel.getRelationshipType());
assertEquals("/word/document.xml", jpegRel.getSource().getPartName().toString());
assertEquals("/word/media/image1.jpeg", jpegRel.getTargetURI().getPath());
XWPFPictureData pictureDataByID = doc.getPictureDataByID(jpegRel.getId());
byte [] newJPEGData = pictureDataByID.getData(); byte [] newJPEGData = pictureDataByID.getData();
assertEquals(newJPEGData.length, jpegData.length); assertEquals(newJPEGData.length, jpegData.length);
for(int i = 0; i < newJPEGData.length; i++){ for(int i = 0; i < newJPEGData.length; i++){
assertEquals(newJPEGData[i], jpegData[i]); assertEquals(newJPEGData[i], jpegData[i]);
} }
} catch (InvalidFormatException e) {
// TODO Auto-generated catch block // Save an re-load, check it appears
e.printStackTrace(); doc = XWPFTestDataSamples.writeOutAndReadBack(doc);
} assertEquals(1, doc.getAllPictures().size());
assertEquals(1, doc.getAllPackagePictures().size());
} }
} }