KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > util > MimeTypes


1 /*
2   Copyright (C) 2002-2004 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.util;
19
20 import java.io.BufferedReader JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24 import java.io.Reader JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.Vector JavaDoc;
28 import org.apache.log4j.Logger;
29
30 /**
31  * Maps mime types to their extensions
32  */

33 public class MimeTypes
34 {
35     static final Logger logger = Logger.getLogger("mime");
36
37     public MimeTypes() {
38     }
39
40     /**
41      * Initialize with defaults builtin values
42      */

43     public void readDefaults() {
44         String JavaDoc path = "org/objectweb/jac/util/mime.types";
45         try {
46             InputStream JavaDoc input =
47                 this.getClass().getClassLoader().getResourceAsStream(path);
48             if (input!=null)
49                 read(new InputStreamReader JavaDoc(input));
50             else
51                 logger.warn("Resource not found: '"+path+"'");
52         } catch (Exception JavaDoc e) {
53             logger.error("Failed to read default mime.types from '"+path+"'",e);
54         }
55     }
56
57     // extension -> mimetype
58
Hashtable JavaDoc extensions = new Hashtable JavaDoc();
59     // mimetype -> extension[]
60
Hashtable JavaDoc types = new Hashtable JavaDoc();
61
62     /**
63      * Read mime types definitions from a stream.
64      *
65      * <p>The format of the stream must be:</p>
66      * <pre>mime-type [extension ...]</pre>
67      * <p>Tabulations are not supported as separators!!!</p>
68      */

69     public void read(Reader JavaDoc in) throws IOException JavaDoc {
70         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(in);
71         String JavaDoc line;
72         while ((line=reader.readLine())!=null) {
73             line = line.trim();
74             int index = line.indexOf(' ');
75             if (index!=-1) {
76                 String JavaDoc mimeType = line.substring(0,index).trim();
77                 Vector JavaDoc ext = new Vector JavaDoc();
78                 line = line.substring(index+1);
79                 while((index=line.indexOf(' '))!=-1) {
80                     String JavaDoc 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 JavaDoc[] array = ExtArrays.emptyStringArray;
88                 types.put(mimeType,ext.toArray(array));
89             }
90         }
91     }
92
93     /**
94      * Returns the mime type associated with the extension of a filename
95      *
96      * @return the mime type of null.
97      */

98     public String JavaDoc getMimeType(String JavaDoc filename) {
99         String JavaDoc mimeType = null;
100         int index = filename.lastIndexOf('.');
101         if (index!=-1) {
102             mimeType = (String JavaDoc)extensions.get(filename.substring(index+1));
103         }
104         return mimeType;
105     }
106
107     /**
108      * Returns all known mime types
109      */

110     public Collection JavaDoc getMimeTypes() {
111         return types.keySet();
112     }
113 }
114
Popular Tags