1 17 18 package org.objectweb.jac.util; 19 20 import java.io.BufferedReader ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.InputStreamReader ; 24 import java.io.Reader ; 25 import java.util.Collection ; 26 import java.util.Hashtable ; 27 import java.util.Vector ; 28 import org.apache.log4j.Logger; 29 30 33 public class MimeTypes 34 { 35 static final Logger logger = Logger.getLogger("mime"); 36 37 public MimeTypes() { 38 } 39 40 43 public void readDefaults() { 44 String path = "org/objectweb/jac/util/mime.types"; 45 try { 46 InputStream input = 47 this.getClass().getClassLoader().getResourceAsStream(path); 48 if (input!=null) 49 read(new InputStreamReader (input)); 50 else 51 logger.warn("Resource not found: '"+path+"'"); 52 } catch (Exception e) { 53 logger.error("Failed to read default mime.types from '"+path+"'",e); 54 } 55 } 56 57 Hashtable extensions = new Hashtable (); 59 Hashtable types = new Hashtable (); 61 62 69 public void read(Reader in) throws IOException { 70 BufferedReader reader = new BufferedReader (in); 71 String line; 72 while ((line=reader.readLine())!=null) { 73 line = line.trim(); 74 int index = line.indexOf(' '); 75 if (index!=-1) { 76 String mimeType = line.substring(0,index).trim(); 77 Vector ext = new Vector (); 78 line = line.substring(index+1); 79 while((index=line.indexOf(' '))!=-1) { 80 String extension = line.substring(0,index); 81 ext.add(extension); 82 extensions.put(extension,mimeType); 83 line = line.substring(index+1).trim(); 84 } 85 ext.add(line); 86 extensions.put(line,mimeType); 87 String [] array = ExtArrays.emptyStringArray; 88 types.put(mimeType,ext.toArray(array)); 89 } 90 } 91 } 92 93 98 public String getMimeType(String filename) { 99 String mimeType = null; 100 int index = filename.lastIndexOf('.'); 101 if (index!=-1) { 102 mimeType = (String )extensions.get(filename.substring(index+1)); 103 } 104 return mimeType; 105 } 106 107 110 public Collection getMimeTypes() { 111 return types.keySet(); 112 } 113 } 114 | Popular Tags |