From 2b9a63d38d54ab9b52884fd2cbdc4d0519fb8d1d Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Thu, 9 Sep 2010 15:30:55 +0000 Subject: [PATCH] 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 --- src/documentation/content/xdocs/status.xml | 1 + .../apache/poi/hwpf/usermodel/Picture.java | 41 +++++++++-- .../poi/hwpf/usermodel/TestPictures.java | 70 +++++++++---------- 3 files changed, 71 insertions(+), 41 deletions(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 0b7a2577c..193752ed3 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + Add getMimeType() method to HWPF Picture, alongside existing file extension Add code for reading Ole10Native data Add getMimeType() method to HSSF/XSSF PictureData, alongside existing file extension 49887 - allow sheet names longer than 31 chars in XSSF, enforce name uniqueness on the first 31 chars diff --git a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Picture.java b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Picture.java index e485bd010..57d59c091 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Picture.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/Picture.java @@ -17,16 +17,16 @@ 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.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; 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 * @author Dmitry Romanov @@ -216,6 +216,35 @@ public final class Picture } 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) diff --git a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestPictures.java b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestPictures.java index a5f7c3c7e..f33682bf0 100644 --- a/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestPictures.java +++ b/src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestPictures.java @@ -29,7 +29,7 @@ import org.apache.poi.POIDataSamples; /** * Test the picture handling * - * @author Nick Burch (nick at torchbox dot com) + * @author Nick Burch */ public final class TestPictures extends TestCase { @@ -38,21 +38,18 @@ public final class TestPictures extends TestCase { */ public void testTwoImages() { HWPFDocument doc = HWPFTestDataSamples.openSampleFile("two_images.doc"); - List pics = doc.getPicturesTable().getAllPictures(); + List pics = doc.getPicturesTable().getAllPictures(); assertNotNull(pics); assertEquals(pics.size(), 2); for(int i=0; i pics = doc.getPicturesTable().getAllPictures(); assertNotNull(pics); assertEquals(7, pics.size()); for(int i=0; i pics = doc.getPicturesTable().getAllPictures(); assertNotNull(pics); assertEquals(1, pics.size()); - Picture pic = (Picture)pics.get(0); + Picture pic = pics.get(0); assertNotNull(pic.suggestFileExtension()); assertNotNull(pic.suggestFullFileName()); assertTrue(pic.getSize() > 128); @@ -117,12 +117,12 @@ public final class TestPictures extends TestCase { // revisiting the implementation of office drawing objects. HWPFDocument doc = HWPFTestDataSamples.openSampleFile("emf_2003_image.doc"); - List pics = doc.getPicturesTable().getAllPictures(); + List pics = doc.getPicturesTable().getAllPictures(); assertNotNull(pics); assertEquals(1, pics.size()); - Picture pic = (Picture)pics.get(0); + Picture pic = pics.get(0); assertNotNull(pic.suggestFileExtension()); assertNotNull(pic.suggestFullFileName()); @@ -140,33 +140,33 @@ public final class TestPictures extends TestCase { public void testPicturesWithTable() { HWPFDocument doc = HWPFTestDataSamples.openSampleFile("Bug44603.doc"); - List pics = doc.getPicturesTable().getAllPictures(); + List pics = doc.getPicturesTable().getAllPictures(); assertEquals(2, pics.size()); } - public void testPicturesInHeader() { - HWPFDocument doc = HWPFTestDataSamples.openSampleFile("header_image.doc"); + public void testPicturesInHeader() { + HWPFDocument doc = HWPFTestDataSamples.openSampleFile("header_image.doc"); - List pics = doc.getPicturesTable().getAllPictures(); - assertEquals(2, pics.size()); - } + List pics = doc.getPicturesTable().getAllPictures(); + assertEquals(2, pics.size()); + } public void testFastSaved() { - HWPFDocument doc = HWPFTestDataSamples.openSampleFile("rasp.doc"); + HWPFDocument doc = HWPFTestDataSamples.openSampleFile("rasp.doc"); - doc.getPicturesTable().getAllPictures(); // just check that we do not throw Exception + doc.getPicturesTable().getAllPictures(); // just check that we do not throw Exception } public void testFastSaved2() { - HWPFDocument doc = HWPFTestDataSamples.openSampleFile("o_kurs.doc"); + HWPFDocument doc = HWPFTestDataSamples.openSampleFile("o_kurs.doc"); - doc.getPicturesTable().getAllPictures(); // just check that we do not throw Exception + doc.getPicturesTable().getAllPictures(); // just check that we do not throw Exception } public void testFastSaved3() { - HWPFDocument doc = HWPFTestDataSamples.openSampleFile("ob_is.doc"); + HWPFDocument doc = HWPFTestDataSamples.openSampleFile("ob_is.doc"); - doc.getPicturesTable().getAllPictures(); // just check that we do not throw Exception + doc.getPicturesTable().getAllPictures(); // just check that we do not throw Exception } }