]> source.dussan.org Git - poi.git/commitdiff
Add getMimeType() method to HSSF/XSSF PictureData, alongside existing file extension
authorNick Burch <nick@apache.org>
Thu, 9 Sep 2010 09:28:04 +0000 (09:28 +0000)
committerNick Burch <nick@apache.org>
Thu, 9 Sep 2010 09:28:04 +0000 (09:28 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@995360 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/java/org/apache/poi/hssf/usermodel/HSSFPictureData.java
src/java/org/apache/poi/ss/usermodel/PictureData.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPictureData.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPictureData.java
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFPictureData.java

index 135bb70d55027cee53d59694621152f5984e2a69..f79a1c7bc917fd28c4ece03a04d3c6c9972176d0 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.7-beta3" date="2010-??-??">
+           <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">49878 - improved API for hiding sheets</action>
            <action dev="poi-developers" type="fix">49875 - fixed  XSSFWorkbook.createSheet to throw exception if sheet name begins or ends with a single quote (')</action>
index 8ee20103002dd484aae8fb26d450da7087e42bf7..158d98c36d8b925de7635e4d8838d2b589ec639a 100644 (file)
@@ -81,10 +81,8 @@ public class HSSFPictureData implements PictureData
     * @see #getFormat
     * @return 'wmf', 'jpeg' etc depending on the format. never <code>null</code>
     */
-    public String suggestFileExtension()
-    {
-        switch (blip.getRecordId())
-        {
+    public String suggestFileExtension() {
+        switch (blip.getRecordId()) {
             case EscherMetafileBlip.RECORD_ID_WMF:
                 return "wmf";
             case EscherMetafileBlip.RECORD_ID_EMF:
@@ -101,5 +99,26 @@ public class HSSFPictureData implements PictureData
                 return "";
         }
     }
+    
+    /**
+     * Returns the mime type for the image
+     */
+    public String getMimeType() {
+       switch (blip.getRecordId()) {
+           case EscherMetafileBlip.RECORD_ID_WMF:
+               return "application/x-wmf";
+           case EscherMetafileBlip.RECORD_ID_EMF:
+               return "application/x-emf";
+           case EscherMetafileBlip.RECORD_ID_PICT:
+               return "image/x-pict";
+           case EscherBitmapBlip.RECORD_ID_PNG:
+               return "image/png";
+           case EscherBitmapBlip.RECORD_ID_JPEG:
+               return "image/jpeg";
+           case EscherBitmapBlip.RECORD_ID_DIB:
+               return "image/bmp";
+           default:
+               return "image/unknown";
+       }
+    }
 }
-
index a799de88eb9597d681a8fee657df4ac97bad37a0..954337829df6b510acf5670bf209790c344452ac 100644 (file)
@@ -33,4 +33,8 @@ public interface PictureData {
      */
     String suggestFileExtension();
 
+    /**
+     * Returns the mime type for the image
+     */
+    String getMimeType();
 }
\ No newline at end of file
index 0454d90855bdff00874f6c6b87cfe92bbf2936e7..814ef73354bd49e656c690b21713b0ca46b9e87d 100644 (file)
@@ -120,4 +120,8 @@ public class XSSFPictureData extends POIXMLDocumentPart implements PictureData {
         }
         return 0;
     }
+
+    public String getMimeType() {
+        return getPackagePart().getContentType();
+    }
 }
index 06868d8376eb48f4ca41b651a5bafd63573bbe09..55677e565e3f05558033524ab79881165074bf80 100644 (file)
@@ -36,8 +36,10 @@ public final class TestXSSFPictureData extends TestCase {
 
         assertEquals(5, pictures.size());
         String[] ext = {"jpeg", "emf", "png", "emf", "wmf"};
+        String[] mimetype = {"image/jpeg", "image/x-emf", "image/png", "image/x-emf", "image/x-wmf"};
         for (int i = 0; i < pictures.size(); i++) {
             assertEquals(ext[i], pictures.get(i).suggestFileExtension());
+            assertEquals(mimetype[i], pictures.get(i).getMimeType());
         }
 
         int num = pictures.size();
index eb7d12ebf58b07e78ce46e3fc342e20f75a76b00..e351e266904d99a9e5db319fdbcc5f6a0cd00bcc 100644 (file)
@@ -57,6 +57,7 @@ public final class TestHSSFPictureData extends TestCase{
                 assertEquals(192, jpg.getWidth());
                 assertEquals(176, jpg.getHeight());
                 assertEquals(HSSFWorkbook.PICTURE_TYPE_JPEG, pict.getFormat());
+                assertEquals("image/jpeg", pict.getMimeType());
             } else if (ext.equals("png")){
                 //try to read image data using javax.imageio.* (JDK 1.4+)
                 BufferedImage png = ImageIO.read(new ByteArrayInputStream(data));
@@ -64,6 +65,7 @@ public final class TestHSSFPictureData extends TestCase{
                 assertEquals(300, png.getWidth());
                 assertEquals(300, png.getHeight());
                 assertEquals(HSSFWorkbook.PICTURE_TYPE_PNG, pict.getFormat());
+                assertEquals("image/png", pict.getMimeType());
             } else {
                 //TODO: test code for PICT, WMF and EMF
             }