KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jpublish > util > mime > MimeTypeMap


1 /*--
2
3  Copyright (C) 2001-2003 Aetrion LLC.
4  All rights reserved.
5  
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions
8  are met:
9  
10  1. Redistributions of source code must retain the above copyright
11     notice, this list of conditions, and the following disclaimer.
12  
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions, and the disclaimer that follows
15     these conditions in the documentation and/or other materials
16     provided with the distribution.
17
18  3. The name "JPublish" must not be used to endorse or promote products
19     derived from this software without prior written permission. For
20     written permission, please contact info@aetrion.com.
21  
22  4. Products derived from this software may not be called "JPublish", nor
23     may "JPublish" appear in their name, without prior written permission
24     from Aetrion LLC (info@aetrion.com).
25  
26  In addition, the authors of this software request (but do not require)
27  that you include in the end-user documentation provided with the
28  redistribution and/or in the software itself an acknowledgement equivalent
29  to the following:
30      "This product includes software developed by
31       Aetrion LLC (http://www.aetrion.com/)."
32
33  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
37  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
39  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
41  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
42  IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
43  POSSIBILITY OF SUCH DAMAGE.
44
45  For more information on JPublish, please see <http://www.jpublish.org/>.
46  
47  */

48
49 package org.jpublish.util.mime;
50
51 import java.util.HashMap JavaDoc;
52 import java.util.Iterator JavaDoc;
53
54 /**
55  * Data structure representing a mime type map.
56  *
57  * @author Anthony Eden
58  */

59 public class MimeTypeMap extends HashMap JavaDoc {
60
61     /**
62      * The default mime type if none is set.
63      */

64     public static final String JavaDoc DEFAULT_MIME_TYPE = "application/octet-stream";
65
66     private String JavaDoc defaultMimeType = DEFAULT_MIME_TYPE;
67
68     /**
69      * Construct a new MimeTypeMap.
70      */

71     public MimeTypeMap() {
72         init();
73     }
74
75     /**
76      * Get the mime type for the given file extension.
77      *
78      * @return The mime type
79      */

80     public String JavaDoc getMimeType(String JavaDoc extension) {
81         String JavaDoc mimeType = (String JavaDoc) get(extension);
82         if (mimeType == null) {
83             return defaultMimeType;
84         } else {
85             return mimeType;
86         }
87     }
88
89     /**
90      * Get the extension for the given mime type. If the mime type is not mapped to an extension then return null.
91      *
92      * @param mimeType The mime type
93      * @return The extension
94      */

95     public String JavaDoc getExtension(String JavaDoc mimeType) {
96         Iterator JavaDoc iter = keySet().iterator();
97         while (iter.hasNext()) {
98             String JavaDoc extension = (String JavaDoc) iter.next();
99             if (get(extension).equals(mimeType)) {
100                 return extension;
101             }
102         }
103         return null;
104      }
105
106     /**
107      * Get the default mime type.
108      *
109      * @return The default mime type
110      */

111     public String JavaDoc getDefaultMimeType() {
112         return defaultMimeType;
113     }
114
115     /**
116      * Set the default mime type.
117      *
118      * @param defaultMimeType The new default mime type
119      */

120     public void setDefaultMimeType(String JavaDoc defaultMimeType) {
121         if (defaultMimeType != null) {
122             this.defaultMimeType = defaultMimeType;
123         } else {
124             this.defaultMimeType = DEFAULT_MIME_TYPE;
125         }
126     }
127
128     /**
129      * Initialize the mime type map. This will create default mime-type mappings for common mime types.
130      *
131      * <p>Defined types:</p>
132      *
133      * <table>
134      * <tr><td>html</td><td>text/html</td></tr>
135      * <tr><td>txt</td><td>text/plain</td></tr>
136      * <tr><td>xml</td><td>text/xml</td></tr>
137      * <tr><td>css</td><td>text/css</td></tr>
138      * <tr><td>gif</td><td>image/gif</td></tr>
139      * <tr><td>jpg</td><td>image/jpeg</td></tr>
140      * <tr><td>png</td><td>image/png</td></tr>
141      * </table>
142      */

143     protected void init() {
144         put("html", "text/html");
145         put("txt", "text/plain");
146         put("xml", "text/xml");
147         put("css", "text/css");
148         put("gif", "image/gif");
149         put("jpg", "image/jpeg");
150         put("png", "image/png");
151     }
152
153 }
154
Popular Tags