001 /* 002 Copyright (C) 2002-2004 Laurent Martelli <laurent@aopsys.com> 003 004 This program is free software; you can redistribute it and/or modify 005 it under the terms of the GNU Lesser General Public License as 006 published by the Free Software Foundation; either version 2 of the 007 License, or (at your option) any later version. 008 009 This program is distributed in the hope that it will be useful, 010 but WITHOUT ANY WARRANTY; without even the implied warranty of 011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 GNU Lesser General Public License for more details. 013 014 You should have received a copy of the GNU Lesser General Public License 015 along with this program; if not, write to the Free Software 016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 017 018 package org.objectweb.jac.util; 019 020 import java.io.BufferedReader; 021 import java.io.IOException; 022 import java.io.InputStream; 023 import java.io.InputStreamReader; 024 import java.io.Reader; 025 import java.util.Collection; 026 import java.util.Hashtable; 027 import java.util.Vector; 028 import org.apache.log4j.Logger; 029 030 /** 031 * Maps mime types to their extensions 032 */ 033 public class MimeTypes 034 { 035 static final Logger logger = Logger.getLogger("mime"); 036 037 public MimeTypes() { 038 } 039 040 /** 041 * Initialize with defaults builtin values 042 */ 043 public void readDefaults() { 044 String path = "org/objectweb/jac/util/mime.types"; 045 try { 046 InputStream input = 047 this.getClass().getClassLoader().getResourceAsStream(path); 048 if (input!=null) 049 read(new InputStreamReader(input)); 050 else 051 logger.warn("Resource not found: '"+path+"'"); 052 } catch (Exception e) { 053 logger.error("Failed to read default mime.types from '"+path+"'",e); 054 } 055 } 056 057 // extension -> mimetype 058 Hashtable extensions = new Hashtable(); 059 // mimetype -> extension[] 060 Hashtable types = new Hashtable(); 061 062 /** 063 * Read mime types definitions from a stream. 064 * 065 * <p>The format of the stream must be:</p> 066 * <pre>mime-type [extension ...]</pre> 067 * <p>Tabulations are not supported as separators!!!</p> 068 */ 069 public void read(Reader in) throws IOException { 070 BufferedReader reader = new BufferedReader(in); 071 String line; 072 while ((line=reader.readLine())!=null) { 073 line = line.trim(); 074 int index = line.indexOf(' '); 075 if (index!=-1) { 076 String mimeType = line.substring(0,index).trim(); 077 Vector ext = new Vector(); 078 line = line.substring(index+1); 079 while((index=line.indexOf(' '))!=-1) { 080 String extension = line.substring(0,index); 081 ext.add(extension); 082 extensions.put(extension,mimeType); 083 line = line.substring(index+1).trim(); 084 } 085 ext.add(line); 086 extensions.put(line,mimeType); 087 String[] array = ExtArrays.emptyStringArray; 088 types.put(mimeType,ext.toArray(array)); 089 } 090 } 091 } 092 093 /** 094 * Returns the mime type associated with the extension of a filename 095 * 096 * @return the mime type of null. 097 */ 098 public String getMimeType(String filename) { 099 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 /** 108 * Returns all known mime types 109 */ 110 public Collection getMimeTypes() { 111 return types.keySet(); 112 } 113 }