]> source.dussan.org Git - poi.git/commitdiff
Add getMimeType() method to HWPF Picture, and fix some generics warnings
authorNick Burch <nick@apache.org>
Thu, 9 Sep 2010 15:30:55 +0000 (15:30 +0000)
committerNick Burch <nick@apache.org>
Thu, 9 Sep 2010 15:30:55 +0000 (15:30 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@995464 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/scratchpad/src/org/apache/poi/hwpf/usermodel/Picture.java
src/scratchpad/testcases/org/apache/poi/hwpf/usermodel/TestPictures.java

index 0b7a2577cc0716c161713d344bece9c8ab05431e..193752ed3ab948aa54ea785117e1e771ff6d3351 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <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 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>
index e485bd01021b5f740dd2dc17129d2e7758016def..57d59c0912b99fc6c44270b38ae9bbfb78508beb 100644 (file)
 
 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)
index a5f7c3c7e4df1d974e3e58a577227819dd64c5d5..f33682bf0325b2f7d97766aaad88f3eef222cea4 100644 (file)
@@ -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<Picture> pics = doc.getPicturesTable().getAllPictures();
 
                assertNotNull(pics);
                assertEquals(pics.size(), 2);
                for(int i=0; i<pics.size(); i++) {
-                       Object p = pics.get(i);
-                       assertTrue(p instanceof Picture);
-
-                       Picture pic = (Picture)p;
+                       Picture pic = (Picture)pics.get(i);
                        assertNotNull(pic.suggestFileExtension());
                        assertNotNull(pic.suggestFullFileName());
                }
 
-               Picture picA = (Picture)pics.get(0);
-               Picture picB = (Picture)pics.get(1);
+               Picture picA = pics.get(0);
+               Picture picB = pics.get(1);
                assertEquals("jpg", picA.suggestFileExtension());
                assertEquals("jpg", picA.suggestFileExtension());
        }
@@ -62,25 +59,28 @@ public final class TestPictures extends TestCase {
         */
        public void testDifferentImages() {
                HWPFDocument doc = HWPFTestDataSamples.openSampleFile("testPictures.doc");
-               List pics = doc.getPicturesTable().getAllPictures();
+               List<Picture> pics = doc.getPicturesTable().getAllPictures();
 
                assertNotNull(pics);
                assertEquals(7, pics.size());
                for(int i=0; i<pics.size(); i++) {
-                       Object p = pics.get(i);
-                       assertTrue(p instanceof Picture);
-
-                       Picture pic = (Picture)p;
+                       Picture pic = (Picture)pics.get(i);
                        assertNotNull(pic.suggestFileExtension());
                        assertNotNull(pic.suggestFullFileName());
                }
 
-               assertEquals("jpg", ((Picture)pics.get(0)).suggestFileExtension());
-               assertEquals("jpg", ((Picture)pics.get(1)).suggestFileExtension());
-               assertEquals("png", ((Picture)pics.get(3)).suggestFileExtension());
-               assertEquals("png", ((Picture)pics.get(4)).suggestFileExtension());
-               assertEquals("wmf", ((Picture)pics.get(5)).suggestFileExtension());
-               assertEquals("jpg", ((Picture)pics.get(6)).suggestFileExtension());
+               assertEquals("jpg", pics.get(0).suggestFileExtension());
+               assertEquals("image/jpeg", pics.get(0).getMimeType());
+               assertEquals("jpg", pics.get(1).suggestFileExtension());
+      assertEquals("image/jpeg", pics.get(1).getMimeType());
+               assertEquals("png", pics.get(3).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() {
                HWPFDocument doc = HWPFTestDataSamples.openSampleFile("vector_image.doc");
-               List pics = doc.getPicturesTable().getAllPictures();
+               List<Picture> 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<Picture> 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<Picture> 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<Picture> 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
     }
 
 }