KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > util > MIMEUtils


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.util;
17
18 import java.io.BufferedInputStream JavaDoc;
19 import java.io.BufferedReader JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.io.InputStreamReader JavaDoc;
26 import java.io.Reader JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30
31 /**
32  * A collection of <code>File</code>, <code>URL</code> and filename
33  * utility methods.
34  *
35  * @author <a HREF="mailto:stefano@apache.org">Stefano Mazzocchi</a>
36  * @author <a HREF="mailto:tk-cocoon@datas-world.de">Torsten Knodt</a>
37  * @author <a HREF="mailto:jefft@apache.org">Jeff Turner</a>
38  * @version CVS $Id: MIMEUtils.java 30932 2004-07-29 17:35:38Z vgritsenko $
39  */

40 public class MIMEUtils {
41
42     private static final String JavaDoc MIME_MAPPING_FILE = "org/apache/cocoon/util/mime.types";
43
44     /** Default extensions for MIME types. */
45     final private static Map JavaDoc extMap = new HashMap();
46     /** MIME types for extensions. */
47     final private static Map JavaDoc mimeMap = new HashMap();
48
49     /**
50      * Load the MIME type mapping
51      */

52     static {
53         try {
54             final InputStream JavaDoc is = MIMEUtils.class.getClassLoader().getResourceAsStream(MIME_MAPPING_FILE);
55             if (null == is) {
56                 throw new RuntimeException JavaDoc("Cocoon cannot load MIME type mappings from " + MIME_MAPPING_FILE);
57             }
58             loadMimeTypes(new InputStreamReader JavaDoc(is), extMap, mimeMap);
59         } catch (IOException JavaDoc ioe) {
60             throw new RuntimeException JavaDoc("Cocoon cannot load MIME type mappings from " + MIME_MAPPING_FILE);
61         }
62     }
63
64     /**
65      * Return the MIME type for a given file.
66      *
67      * @param file File.
68      * @return MIME type.
69      */

70     public static String JavaDoc getMIMEType(final File JavaDoc file)
71             throws FileNotFoundException JavaDoc, IOException JavaDoc {
72         BufferedInputStream JavaDoc in = null;
73
74         try {
75             in = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
76             byte[] buf = new byte[3];
77             int count = in.read(buf, 0, 3);
78
79             if (count < 3) {
80                 return (null);
81             }
82
83             if ((buf[0]) == (byte)'G' && (buf[1]) == (byte)'I' && (buf[2]) == (byte)'F') {
84                 return ("image/gif");
85             }
86
87             if ((buf[0]) == (byte)0xFF && (buf[1]) == (byte)0xD8) {
88                 return ("image/jpeg");
89             }
90
91         } finally {
92             if (in != null) {
93                 try {
94                     in.close();
95                 } catch (IOException JavaDoc e) {
96                 }
97             }
98         }
99         final String JavaDoc name = file.getName();
100         int index = name.lastIndexOf(".");
101         String JavaDoc fileExt = ".";
102         if (index != -1) {
103             fileExt = name.substring(index);
104         }
105         return getMIMEType(fileExt);
106     }
107
108     /**
109      * Return the MIME type for a given filename extension.
110      *
111      * @param ext Filename extension.
112      *
113      * @return MIME type.
114      */

115     public static String JavaDoc getMIMEType(final String JavaDoc ext) {
116         return (String JavaDoc)mimeMap.get(ext);
117     }
118
119     /**
120      * Return the default filename extension for a given MIME type.
121      *
122      * @param type MIME type.
123      *
124      * @return Filename extension.
125      */

126     public static String JavaDoc getDefaultExtension(final String JavaDoc type) {
127         return (String JavaDoc)extMap.get(type);
128     }
129
130     /**
131      * Parses a <code>mime.types</code> file, and generates mappings between
132      * MIME types and extensions.
133      * For example, if a line contains:
134      * <pre>text/html html htm</pre>
135      * Then 'html' will be the default extension for text/html, and both 'html'
136      * and 'htm' will have MIME type 'text/html'.
137      * Lines starting with '#' are treated as comments and ignored. If an
138      * extension is listed for two MIME types, the first will be chosen.
139      *
140      * @param in Reader of bytes from <code>mime.types</code> file content
141      * @param extMap Empty map of default extensions, keyed by MIME type. Will
142      * be filled in by this method.
143      * @param mimeMap Empty map of MIME types, keyed by extension. Will be
144      * filled in by this method.
145      */

146     public static void loadMimeTypes(Reader JavaDoc in, Map JavaDoc extMap, Map JavaDoc mimeMap) throws IOException JavaDoc {
147         BufferedReader JavaDoc br = new BufferedReader JavaDoc(in);
148         String JavaDoc line;
149         while ((line = br.readLine()) != null) {
150             if (line.startsWith("#")) {
151                 continue;
152             }
153             if (line.trim().equals("")) {
154                 continue;
155             }
156             StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(line, " \t");
157             String JavaDoc mimeType = tok.nextToken();
158             if (tok.hasMoreTokens()) {
159                 String JavaDoc defaultExt = tok.nextToken();
160                 if (!extMap.containsKey(mimeType)) {
161                     extMap.put(mimeType, "." + defaultExt);
162                 }
163                 if (!mimeMap.containsKey("." + defaultExt)) {
164                     mimeMap.put("." + defaultExt, mimeType);
165                 }
166                 while (tok.hasMoreTokens()) {
167                     String JavaDoc ext = tok.nextToken();
168                     if (!mimeMap.containsKey("." + ext)) {
169                         mimeMap.put("." + ext, mimeType);
170                     }
171                 }
172             }
173         }
174     }
175 }
176
Popular Tags