Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / java / net / MimeTypeMapper.java
blob8153694b4833e118ff28921322d7d4612b64c954
1 /* MimeTypeMapper.java -- A class for mapping file names to MIME types
2 Copyright (C) 1998 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package java.net;
40 import gnu.classpath.SystemProperties;
42 import java.io.FileReader;
43 import java.io.IOException;
44 import java.io.LineNumberReader;
45 import java.util.Hashtable;
46 import java.util.Iterator;
47 import java.util.Map;
48 import java.util.NoSuchElementException;
49 import java.util.StringTokenizer;
50 import java.util.TreeMap;
53 /**
54 * This non-public class is used to implement the FileNameMap interface
55 * which defines a mechanism for mapping filenames to MIME types.
57 * @version 0.5
59 * @author Aaron M. Renn (arenn@urbanophile.com)
61 class MimeTypeMapper implements FileNameMap
63 /**
64 * This array of strings is used to identify a MIME type based on a file
65 * extension. This is list is based on the Apache mime.types file.
67 protected static final String[][] mime_strings =
69 { "ai", "application/postscript" }
70 , { "aif", "audio/x-aiff" }
71 , { "aifc", "audio/x-aiff" }
72 , { "aiff", "audio/x-aiff" }
73 , { "asc", "text/plain" }
74 , { "au", "audio/basic" }
75 , { "avi", "video/x-msvideo" }
76 , { "bcpio", "application/x-bcpio" }
77 , { "bin", "application/octet-stream" }
78 , { "bmp", "image/bmp" }
79 , { "bz2", "application/x-bzip2" }
80 , { "cdf", "application/x-netcdf" }
81 , { "chrt", "application/x-kchart" }
82 , { "class", "application/octet-stream" }
83 , { "cpio", "application/x-cpio" }
84 , { "cpt", "application/mac-compactpro" }
85 , { "csh", "application/x-csh" }
86 , { "css", "text/css" }
87 , { "dcr", "application/x-director" }
88 , { "dir", "application/x-director" }
89 , { "djv", "image/vnd.djvu" }
90 , { "djvu", "image/vnd.djvu" }
91 , { "dll", "application/octet-stream" }
92 , { "dms", "application/octet-stream" }
93 , { "doc", "application/msword" }
94 , { "dvi", "application/x-dvi" }
95 , { "dxr", "application/x-director" }
96 , { "eps", "application/postscript" }
97 , { "etx", "text/x-setext" }
98 , { "exe", "application/octet-stream" }
99 , { "ez", "application/andrew-inset" }
100 , { "gif", "image/gif" }
101 , { "gtar", "application/x-gtar" }
102 , { "gz", "application/x-gzip" }
103 , { "hdf", "application/x-hdf" }
104 , { "hqx", "application/mac-binhex40" }
105 , { "htm", "text/html" }
106 , { "html", "text/html" }
107 , { "ice", "x-conference/x-cooltalk" }
108 , { "ief", "image/ief" }
109 , { "iges", "model/iges" }
110 , { "igs", "model/iges" }
111 , { "img", "application/octet-stream" }
112 , { "iso", "application/octet-stream" }
113 , { "jpe", "image/jpeg" }
114 , { "jpeg", "image/jpeg" }
115 , { "jpg", "image/jpeg" }
116 , { "js", "application/x-javascript" }
117 , { "kar", "audio/midi" }
118 , { "kil", "application/x-killustrator" }
119 , { "kpr", "application/x-kpresenter" }
120 , { "kpt", "application/x-kpresenter" }
121 , { "ksp", "application/x-kspread" }
122 , { "kwd", "application/x-kword" }
123 , { "kwt", "application/x-kword" }
124 , { "latex", "application/x-latex" }
125 , { "lha", "application/octet-stream" }
126 , { "lzh", "application/octet-stream" }
127 , { "m3u", "audio/x-mpegurl" }
128 , { "man", "application/x-troff-man" }
129 , { "me", "application/x-troff-me" }
130 , { "mesh", "model/mesh" }
131 , { "mid", "audio/midi" }
132 , { "midi", "audio/midi" }
133 , { "mif", "application/vnd.mif" }
134 , { "mov", "video/quicktime" }
135 , { "movie", "video/x-sgi-movie" }
136 , { "mp2", "audio/mpeg" }
137 , { "mp3", "audio/mpeg" }
138 , { "mpe", "video/mpeg" }
139 , { "mpeg", "video/mpeg" }
140 , { "mpg", "video/mpeg" }
141 , { "mpga", "audio/mpeg" }
142 , { "ms", "application/x-troff-ms" }
143 , { "msh", "model/mesh" }
144 , { "mxu", "video/vnd.mpegurl" }
145 , { "nc", "application/x-netcdf" }
146 , { "ogg", "application/ogg" }
147 , { "pbm", "image/x-portable-bitmap" }
148 , { "pdb", "chemical/x-pdb" }
149 , { "pdf", "application/pdf" }
150 , { "pgm", "image/x-portable-graymap" }
151 , { "pgn", "application/x-chess-pgn" }
152 , { "png", "image/png" }
153 , { "pnm", "image/x-portable-anymap" }
154 , { "ppm", "image/x-portable-pixmap" }
155 , { "ppt", "application/vnd.ms-powerpoint" }
156 , { "ps", "application/postscript" }
157 , { "qt", "video/quicktime" }
158 , { "ra", "audio/x-realaudio" }
159 , { "ram", "audio/x-pn-realaudio" }
160 , { "ras", "image/x-cmu-raster" }
161 , { "rgb", "image/x-rgb" }
162 , { "rm", "audio/x-pn-realaudio" }
163 , { "roff", "application/x-troff" }
164 , { "rpm", "application/x-rpm" }
165 , { "rtf", "text/rtf" }
166 , { "rtx", "text/richtext" }
167 , { "sgm", "text/sgml" }
168 , { "sgml", "text/sgml" }
169 , { "sh", "application/x-sh" }
170 , { "shar", "application/x-shar" }
171 , { "silo", "model/mesh" }
172 , { "sit", "application/x-stuffit" }
173 , { "skd", "application/x-koan" }
174 , { "skm", "application/x-koan" }
175 , { "skp", "application/x-koan" }
176 , { "skt", "application/x-koan" }
177 , { "smi", "application/smil" }
178 , { "smil", "application/smil" }
179 , { "snd", "audio/basic" }
180 , { "so", "application/octet-stream" }
181 , { "spl", "application/x-futuresplash" }
182 , { "src", "application/x-wais-source" }
183 , { "stc", "application/vnd.sun.xml.calc.template" }
184 , { "std", "application/vnd.sun.xml.draw.template" }
185 , { "sti", "application/vnd.sun.xml.impress.template" }
186 , { "stw", "application/vnd.sun.xml.writer.template" }
187 , { "sv4cpio", "application/x-sv4cpio" }
188 , { "sv4crc", "application/x-sv4crc" }
189 , { "swf", "application/x-shockwave-flash" }
190 , { "sxc", "application/vnd.sun.xml.calc" }
191 , { "sxd", "application/vnd.sun.xml.draw" }
192 , { "sxg", "application/vnd.sun.xml.writer.global" }
193 , { "sxi", "application/vnd.sun.xml.impress" }
194 , { "sxm", "application/vnd.sun.xml.math" }
195 , { "sxw", "application/vnd.sun.xml.writer" }
196 , { "t", "application/x-troff" }
197 , { "tar", "application/x-tar" }
198 , { "tcl", "application/x-tcl" }
199 , { "tex", "application/x-tex" }
200 , { "texi", "application/x-texinfo" }
201 , { "texinfo", "application/x-texinfo" }
202 , { "tgz", "application/x-gzip" }
203 , { "tif", "image/tiff" }
204 , { "tiff", "image/tiff" }
205 , { "torrent", "application/x-bittorrent" }
206 , { "tr", "application/x-troff" }
207 , { "tsv", "text/tab-separated-values" }
208 , { "txt", "text/plain" }
209 , { "ustar", "application/x-ustar" }
210 , { "vcd", "application/x-cdlink" }
211 , { "vrml", "model/vrml" }
212 , { "wav", "audio/x-wav" }
213 , { "wbmp", "image/vnd.wap.wbmp" }
214 , { "wbxml", "application/vnd.wap.wbxml" }
215 , { "wml", "text/vnd.wap.wml" }
216 , { "wmlc", "application/vnd.wap.wmlc" }
217 , { "wmls", "text/vnd.wap.wmlscript" }
218 , { "wmlsc", "application/vnd.wap.wmlscriptc" }
219 , { "wrl", "model/vrml" }
220 , { "xbm", "image/x-xbitmap" }
221 , { "xht", "application/xhtml+xml" }
222 , { "xhtml", "application/xhtml+xml" }
223 , { "xls", "application/vnd.ms-excel" }
224 , { "xml", "text/xml" }
225 , { "xpm", "image/x-xpixmap" }
226 , { "xsl", "text/xml" }
227 , { "xwd", "image/x-xwindowdump" }
228 , { "xyz", "chemical/x-xyz" }
229 , { "zip", "application/zip" }
233 * The MIME types above are put into this Hashtable for faster lookup.
235 private Hashtable mime_types = new Hashtable(150);
238 * Create a new <code>MimeTypeMapper</code> object.
240 public MimeTypeMapper()
242 for (int i = 0; i < mime_strings.length; i++)
243 mime_types.put(mime_strings[i][0], mime_strings[i][1]);
245 // Now read from the system mime database, if it exists. Entries found
246 // here override our internal ones.
249 // On Linux this usually means /etc/mime.types.
250 String file
251 = SystemProperties.getProperty("gnu.classpath.mime.types.file");
252 if (file != null)
253 fillFromFile(mime_types, file);
255 catch (IOException ignore)
260 public static void fillFromFile (Map table, String fname)
261 throws IOException
263 LineNumberReader reader =
264 new LineNumberReader (new FileReader (fname));
266 while (reader.ready ())
268 StringTokenizer tokenizer =
269 new StringTokenizer (reader.readLine ());
273 String t = tokenizer.nextToken ();
275 if (! t.startsWith ("#"))
277 while (true)
279 // Read the next extension
280 String e = tokenizer.nextToken ();
281 if ((e != null) && (! e.startsWith ("#")))
282 table.put (e, t);
283 else
284 break;
288 catch (NoSuchElementException ex)
290 // Do nothing.
296 * The method returns the MIME type of the filename passed as an argument.
297 * The value returned is based on the extension of the filename. The
298 * default content type returned if this method cannot determine the
299 * actual content type is "application/octet-stream"
301 * @param filename The name of the file to return the MIME type for
303 * @return The MIME type
305 public String getContentTypeFor(String filename)
307 int index = filename.lastIndexOf(".");
308 if (index != -1)
310 if (index == filename.length())
311 return "application/octet-stream";
312 else
313 filename = filename.substring(index + 1);
316 String type = (String) mime_types.get(filename);
317 if (type == null)
318 return "application/octet-stream";
319 else
320 return type;
324 * Run this class as a program to create a new mime_strings table.
326 public static void main(String[] args) throws IOException
328 TreeMap map = new TreeMap();
329 // It is fine to hard-code the name here. This is only ever
330 // used by maintainers, who can hack it if they need to re-run
331 // it.
332 fillFromFile(map, "/etc/mime.types");
333 Iterator it = map.keySet().iterator();
334 boolean first = true;
335 while (it.hasNext())
337 String key = (String) it.next();
338 String value = (String) map.get(key);
339 // Put the "," first since it is easier to make correct syntax this way.
340 System.out.println(" " + (first ? " " : ", ")
341 + "{ \"" + key + "\", \"" + value + "\" }");
342 first = false;