1 package org.columba.core.io; 17 18 import java.io.File ; 19 import java.io.IOException ; 20 import java.util.Hashtable ; 21 import java.util.Map ; 22 import java.util.regex.Matcher ; 23 import java.util.regex.Pattern ; 24 25 public class DefaultMimeTypeTable { 26 27 private static Map mimeTable = loadTable(); 28 29 private static Map <String , String > loadTable() { 30 Map <String , String > result = new Hashtable <String , String >(); 31 32 try { 33 String table = DiskIO 34 .readStringFromResource("org/columba/core/config/mime_table"); 35 Pattern listPattern = Pattern.compile("(\\w+) (\\w+/\\w+)"); 36 Matcher matcher = listPattern.matcher(table); 37 while (matcher.find()) { 38 result.put(matcher.group(1), matcher.group(2)); 39 } 40 } catch (IOException e) { 41 } 43 44 return result; 45 } 46 47 public static String lookup(File file) { 48 int dotPos = file.getName().lastIndexOf('.'); 49 String _return = lookup(file.getName().substring(dotPos + 1)); 50 if (dotPos == -1 || dotPos == file.getName().length() - 1) { 51 _return = "application/octet-stream"; 52 } 53 return _return; 54 55 } 56 57 public static String lookup(String string) { 58 String lookupResult = (String ) mimeTable.get(string); 59 60 if (lookupResult == null) { 61 lookupResult = "application/octet-stream"; 62 } 63 64 return lookupResult; 65 } 66 67 } 68 | Popular Tags |