Add getMimeType() method to HWPF Picture, and fix some generics warnings

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@995464 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2010-09-09 15:30:55 +00:00
parent dac67e887e
commit 2b9a63d38d
3 changed files with 71 additions and 41 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="add">Add getMimeType() method to HWPF Picture, alongside existing file extension</action>
<action dev="poi-developers" type="add">Add code for reading Ole10Native data</action> <action dev="poi-developers" type="add">Add code for reading Ole10Native data</action>
<action dev="poi-developers" type="add">Add getMimeType() method to HSSF/XSSF PictureData, alongside existing file extension</action> <action dev="poi-developers" type="add">Add getMimeType() method to HSSF/XSSF PictureData, alongside existing file extension</action>
<action dev="poi-developers" type="fix">49887 - allow sheet names longer than 31 chars in XSSF, enforce name uniqueness on the first 31 chars</action> <action dev="poi-developers" type="fix">49887 - allow sheet names longer than 31 chars in XSSF, enforce name uniqueness on the first 31 chars</action>

View File

@ -17,16 +17,16 @@
package org.apache.poi.hwpf.usermodel; package org.apache.poi.hwpf.usermodel;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.POILogger;
import org.apache.poi.util.POILogFactory;
import java.io.OutputStream;
import java.io.IOException;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.InflaterInputStream; import java.util.zip.InflaterInputStream;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
/** /**
* Represents embedded picture extracted from Word Document * Represents embedded picture extracted from Word Document
* @author Dmitry Romanov * @author Dmitry Romanov
@ -217,6 +217,35 @@ public final class Picture
return extension; return extension;
} }
/**
* Returns the mime type for the image
*/
public String getMimeType() {
String extension = suggestFileExtension();
if("jpg".equals(extension)) {
return "image/jpeg";
}
if("png".equals(extension)) {
return "image/png";
}
if("gif".equals(extension)) {
return "image/gif";
}
if("bmp".equals(extension)) {
return "image/bmp";
}
if("tiff".equals(extension)) {
return "image/tiff";
}
if("wmf".equals(extension)) {
return "application/x-wmf";
}
if("emf".equals(extension)) {
return "application/x-emf";
}
return "image/unknown";
}
private String suggestFileExtension(byte[] _dataStream, int pictureBytesStartOffset) private String suggestFileExtension(byte[] _dataStream, int pictureBytesStartOffset)
{ {

View File

@ -29,7 +29,7 @@ import org.apache.poi.POIDataSamples;
/** /**
* Test the picture handling * Test the picture handling
* *
* @author Nick Burch (nick at torchbox dot com) * @author Nick Burch
*/ */
public final class TestPictures extends TestCase { public final class TestPictures extends TestCase {
@ -38,21 +38,18 @@ public final class TestPictures extends TestCase {
*/ */
public void testTwoImages() { public void testTwoImages() {
HWPFDocument doc = HWPFTestDataSamples.openSampleFile("two_images.doc"); HWPFDocument doc = HWPFTestDataSamples.openSampleFile("two_images.doc");
List pics = doc.getPicturesTable().getAllPictures(); List<Picture> pics = doc.getPicturesTable().getAllPictures();
assertNotNull(pics); assertNotNull(pics);
assertEquals(pics.size(), 2); assertEquals(pics.size(), 2);
for(int i=0; i<pics.size(); i++) { for(int i=0; i<pics.size(); i++) {
Object p = pics.get(i); Picture pic = (Picture)pics.get(i);
assertTrue(p instanceof Picture);
Picture pic = (Picture)p;
assertNotNull(pic.suggestFileExtension()); assertNotNull(pic.suggestFileExtension());
assertNotNull(pic.suggestFullFileName()); assertNotNull(pic.suggestFullFileName());
} }
Picture picA = (Picture)pics.get(0); Picture picA = pics.get(0);
Picture picB = (Picture)pics.get(1); Picture picB = pics.get(1);
assertEquals("jpg", picA.suggestFileExtension()); assertEquals("jpg", picA.suggestFileExtension());
assertEquals("jpg", picA.suggestFileExtension()); assertEquals("jpg", picA.suggestFileExtension());
} }
@ -62,25 +59,28 @@ public final class TestPictures extends TestCase {
*/ */
public void testDifferentImages() { public void testDifferentImages() {
HWPFDocument doc = HWPFTestDataSamples.openSampleFile("testPictures.doc"); HWPFDocument doc = HWPFTestDataSamples.openSampleFile("testPictures.doc");
List pics = doc.getPicturesTable().getAllPictures(); List<Picture> pics = doc.getPicturesTable().getAllPictures();
assertNotNull(pics); assertNotNull(pics);
assertEquals(7, pics.size()); assertEquals(7, pics.size());
for(int i=0; i<pics.size(); i++) { for(int i=0; i<pics.size(); i++) {
Object p = pics.get(i); Picture pic = (Picture)pics.get(i);
assertTrue(p instanceof Picture);
Picture pic = (Picture)p;
assertNotNull(pic.suggestFileExtension()); assertNotNull(pic.suggestFileExtension());
assertNotNull(pic.suggestFullFileName()); assertNotNull(pic.suggestFullFileName());
} }
assertEquals("jpg", ((Picture)pics.get(0)).suggestFileExtension()); assertEquals("jpg", pics.get(0).suggestFileExtension());
assertEquals("jpg", ((Picture)pics.get(1)).suggestFileExtension()); assertEquals("image/jpeg", pics.get(0).getMimeType());
assertEquals("png", ((Picture)pics.get(3)).suggestFileExtension()); assertEquals("jpg", pics.get(1).suggestFileExtension());
assertEquals("png", ((Picture)pics.get(4)).suggestFileExtension()); assertEquals("image/jpeg", pics.get(1).getMimeType());
assertEquals("wmf", ((Picture)pics.get(5)).suggestFileExtension()); assertEquals("png", pics.get(3).suggestFileExtension());
assertEquals("jpg", ((Picture)pics.get(6)).suggestFileExtension()); assertEquals("image/png", pics.get(3).getMimeType());
assertEquals("png", pics.get(4).suggestFileExtension());
assertEquals("image/png", pics.get(4).getMimeType());
assertEquals("wmf", pics.get(5).suggestFileExtension());
assertEquals("application/x-wmf", pics.get(5).getMimeType());
assertEquals("jpg", pics.get(6).suggestFileExtension());
assertEquals("image/jpeg", pics.get(6).getMimeType());
} }
/** /**
@ -88,12 +88,12 @@ public final class TestPictures extends TestCase {
*/ */
public void testEmfImage() { public void testEmfImage() {
HWPFDocument doc = HWPFTestDataSamples.openSampleFile("vector_image.doc"); HWPFDocument doc = HWPFTestDataSamples.openSampleFile("vector_image.doc");
List pics = doc.getPicturesTable().getAllPictures(); List<Picture> pics = doc.getPicturesTable().getAllPictures();
assertNotNull(pics); assertNotNull(pics);
assertEquals(1, pics.size()); assertEquals(1, pics.size());
Picture pic = (Picture)pics.get(0); Picture pic = pics.get(0);
assertNotNull(pic.suggestFileExtension()); assertNotNull(pic.suggestFileExtension());
assertNotNull(pic.suggestFullFileName()); assertNotNull(pic.suggestFullFileName());
assertTrue(pic.getSize() > 128); assertTrue(pic.getSize() > 128);
@ -117,12 +117,12 @@ public final class TestPictures extends TestCase {
// revisiting the implementation of office drawing objects. // revisiting the implementation of office drawing objects.
HWPFDocument doc = HWPFTestDataSamples.openSampleFile("emf_2003_image.doc"); HWPFDocument doc = HWPFTestDataSamples.openSampleFile("emf_2003_image.doc");
List pics = doc.getPicturesTable().getAllPictures(); List<Picture> pics = doc.getPicturesTable().getAllPictures();
assertNotNull(pics); assertNotNull(pics);
assertEquals(1, pics.size()); assertEquals(1, pics.size());
Picture pic = (Picture)pics.get(0); Picture pic = pics.get(0);
assertNotNull(pic.suggestFileExtension()); assertNotNull(pic.suggestFileExtension());
assertNotNull(pic.suggestFullFileName()); assertNotNull(pic.suggestFullFileName());
@ -140,14 +140,14 @@ public final class TestPictures extends TestCase {
public void testPicturesWithTable() { public void testPicturesWithTable() {
HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug44603.doc"); HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug44603.doc");
List pics = doc.getPicturesTable().getAllPictures(); List<Picture> pics = doc.getPicturesTable().getAllPictures();
assertEquals(2, pics.size()); assertEquals(2, pics.size());
} }
public void testPicturesInHeader() { public void testPicturesInHeader() {
HWPFDocument doc = HWPFTestDataSamples.openSampleFile("header_image.doc"); HWPFDocument doc = HWPFTestDataSamples.openSampleFile("header_image.doc");
List pics = doc.getPicturesTable().getAllPictures(); List<Picture> pics = doc.getPicturesTable().getAllPictures();
assertEquals(2, pics.size()); assertEquals(2, pics.size());
} }