Author: scottbw
Date: Thu Nov 10 16:00:40 2011
New Revision: 1200396

URL: http://svn.apache.org/viewvc?rev=1200396&view=rev
Log:
Added call to content-type sniffing method for icons where there is no file 
extension provided (see WOOKIE-260)

Modified:
    
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/impl/IconEntity.java
    
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/ContentTypeUtils.java
    
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/WidgetPackageUtils.java

Modified: 
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/impl/IconEntity.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/impl/IconEntity.java?rev=1200396&r1=1200395&r2=1200396&view=diff
==============================================================================
--- 
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/impl/IconEntity.java
 (original)
+++ 
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/impl/IconEntity.java
 Thu Nov 10 16:00:40 2011
@@ -80,9 +80,13 @@ public class IconEntity extends Abstract
                        fSrc = null;
                }
                try {
-                       if (!ContentTypeUtils.isSupportedImageType(fSrc)) fSrc 
= null;
+                       if ( !ContentTypeUtils.isSupportedImageType(fSrc)){
+                         if 
(!ContentTypeUtils.isSupportedImageType(WidgetPackageUtils.getEntryStream 
(fSrc, zip)) ){
+                      fSrc = null;                         
+                         }
+                       }  
                } catch (Exception e1) {
-                       // TODO Auto-generated catch block
+                       fSrc = null;
                        e1.printStackTrace();
                }
 

Modified: 
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/ContentTypeUtils.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/ContentTypeUtils.java?rev=1200396&r1=1200395&r2=1200396&view=diff
==============================================================================
--- 
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/ContentTypeUtils.java
 (original)
+++ 
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/ContentTypeUtils.java
 Thu Nov 10 16:00:40 2011
@@ -16,6 +16,7 @@ package org.apache.wookie.w3c.util;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 
 import org.apache.commons.lang.StringUtils;
 
@@ -46,6 +47,17 @@ public class ContentTypeUtils {
                String type = getContentType(file);
                return isSupported(type, SUPPORTED_IMAGE_TYPES);
        }       
+       
+       /**
+        * Checks to see if an inputstream contains a supported image type
+        * @param inputStream
+        * @return true if the file is a supported image type 
+        * @throws IOException
+        */
+       public static boolean isSupportedImageType(InputStream inputStream) 
throws IOException{
+         String type = sniffContentType(inputStream);
+         return isSupported(type, SUPPORTED_IMAGE_TYPES);
+       }
 
        /**
         * Gets the content type of a file
@@ -73,21 +85,32 @@ public class ContentTypeUtils {
         */
        protected static String sniffContentType(File file) throws IOException{
                FileInputStream stream = new FileInputStream(file);
-               byte[] bytes = new byte[8];
-               stream.read(bytes);
-               String[] hex = new String[8];
-               String hexString = "";  
-               for (int i=0;i<8;i++){
-                       hex[i]= getHexValue(bytes[i]);
-                       hexString += hex[i]+" ";
-               }       
-               String prefix = new String(bytes);
-               if (prefix.startsWith("GIF87") || prefix.startsWith("GIF89")) 
return "image/gif";
-               if (hex[0].equals("ff") && hex[1].equals("d8")) return 
"image/jpeg";
-               if (hex[0].equals("42") && hex[1].equals("4d")) return 
"image/bmp";
-               if (hex[0].equals("00") && hex[1].equals("00") && 
hex[2].equals("01") && hex[3].equals("00")) return "image/vnd.microsoft.icon";
-               if (hexString.trim().equals("89 50 4e 47 0d 0a 1a 0a")) return 
"image/png";     
-               return null;
+               return sniffContentType(stream);
+       }
+       
+        /**
+   * Sniffs the content type for images and other common types
+   * @param inpuStream the inputStream to sniff
+   * @return the content type of the stream if it matches a known signature, 
otherwise Null
+   * @throws IOException 
+   */
+       protected static String sniffContentType(InputStream inputStream) 
throws IOException{
+         if (inputStream == null) return null;
+         byte[] bytes = new byte[8];
+    inputStream.read(bytes);
+    String[] hex = new String[8];
+    String hexString = "";  
+    for (int i=0;i<8;i++){
+      hex[i]= getHexValue(bytes[i]);
+      hexString += hex[i]+" ";
+    } 
+    String prefix = new String(bytes);
+    if (prefix.startsWith("GIF87") || prefix.startsWith("GIF89")) return 
"image/gif";
+    if (hex[0].equals("ff") && hex[1].equals("d8")) return "image/jpeg";
+    if (hex[0].equals("42") && hex[1].equals("4d")) return "image/bmp";
+    if (hex[0].equals("00") && hex[1].equals("00") && hex[2].equals("01") && 
hex[3].equals("00")) return "image/vnd.microsoft.icon";
+    if (hexString.trim().equals("89 50 4e 47 0d 0a 1a 0a")) return 
"image/png"; 
+    return null;
        }
        
        /**

Modified: 
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/WidgetPackageUtils.java
URL: 
http://svn.apache.org/viewvc/incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/WidgetPackageUtils.java?rev=1200396&r1=1200395&r2=1200396&view=diff
==============================================================================
--- 
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/WidgetPackageUtils.java
 (original)
+++ 
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/WidgetPackageUtils.java
 Thu Nov 10 16:00:40 2011
@@ -22,6 +22,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Enumeration;
+import java.util.zip.ZipException;
 
 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
 import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
@@ -202,6 +203,21 @@ public class WidgetPackageUtils {
        }
 
        /**
+        * Gets the input stream for an entry in the zip 
+        * @param entry the name of the entry
+        * @param zipFile
+        * @return the input stream
+        * @throws ZipException
+        * @throws IOException
+        */
+       public static InputStream getEntryStream(String entry, ZipFile zipFile) 
throws ZipException, IOException{
+         ZipArchiveEntry zipEntry;
+         zipEntry = zipFile.getEntry(entry);
+         if (zipEntry == null) return null;
+         return zipFile.getInputStream(zipEntry);
+       }
+       
+       /**
         * uses apache commons compress to unpack all the zip entries into a 
target folder
         * partly adapted from the examples on the apache commons compress 
site, and an
         * example of generic Zip unpacking. Note this iterates over the 
ZipArchiveEntry enumeration rather


Reply via email to