KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > mimetype > util > MimeTypeMapper


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

18
19 import java.io.BufferedReader JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileReader JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.io.StringReader JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29
30 /**
31  * This class defines mappings between MIME types and the corresponding
32  * file name extensions. The mappings are defined as lines formed
33  * by a MIME type name followed by a list of extensions separated
34  * by a whitespace.
35  *
36  * @author <a HREF="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
37  * @version $Id: MimeTypeMapper.java,v 1.3.2.2 2004/05/20 03:06:48 seade Exp $
38  */

39 public class MimeTypeMapper
40 {
41     /**
42      * Mappings between MIME types and file name extensions.
43      */

44     private HashMap JavaDoc mimeTypeExtensions = new HashMap JavaDoc();
45     protected HashMap JavaDoc extensionMimeTypes = new HashMap JavaDoc();
46
47     /**
48      * Constructs an empty MIME type mapper.
49      */

50     public MimeTypeMapper()
51     {
52     }
53
54     /**
55      * Constructs a mapper reading from a stream.
56      *
57      * @param input an input stream.
58      * @throws IOException for an incorrect stream.
59      */

60     public MimeTypeMapper(InputStream JavaDoc input)
61             throws IOException JavaDoc
62     {
63         parse(new BufferedReader JavaDoc(
64                 new InputStreamReader JavaDoc(input, CharSetMap.DEFAULT_CHARSET)));
65     }
66
67     /**
68      * Constructs a mapper reading from a file.
69      *
70      * @param file an input file.
71      * @throws IOException for an incorrect file.
72      */

73     public MimeTypeMapper(File JavaDoc file)
74             throws IOException JavaDoc
75     {
76         FileReader JavaDoc freader = new FileReader JavaDoc(file);
77         try
78         {
79             parse(new BufferedReader JavaDoc(freader));
80         }
81         finally
82         {
83             try
84             {
85                 freader.close();
86             }
87             catch (IOException JavaDoc x)
88             {
89             }
90         }
91     }
92
93     /**
94      * Constructs a mapper reading from a file path.
95      *
96      * @param path an input file path.
97      * @throws IOException for an incorrect file.
98      */

99     public MimeTypeMapper(String JavaDoc path)
100             throws IOException JavaDoc
101     {
102         this(new File JavaDoc(path));
103     }
104
105     /**
106      * Sets a MIME content type mapping to extensions.
107      *
108      * @param spec a MIME type extension specification to parse.
109      */

110     public void setContentType(String JavaDoc spec)
111     {
112         try
113         {
114             parse(new BufferedReader JavaDoc(new StringReader JavaDoc(spec)));
115         }
116         catch (IOException JavaDoc x)
117         {
118         }
119     }
120
121     /**
122      * Gets a MIME content type corresponding to a specified file name extension.
123      *
124      * @param ext a file name extension.
125      * @return the corresponding MIME type as a string or null.
126      */

127     public String JavaDoc getContentType(String JavaDoc ext)
128     {
129         return (String JavaDoc) mimeTypeExtensions.get(ext);
130     }
131
132     /**
133      * Gets a file name extension corresponding to a specified MIME content type.
134      *
135      * @param mime a MIME type as a string.
136      * @return the corresponding file name extension or null.
137      */

138     public String JavaDoc getExtension(String JavaDoc type)
139     {
140         return (String JavaDoc) extensionMimeTypes.get(type);
141     }
142
143     /**
144      * Parses MIME type extensions.
145      *
146      * @param reader a reader to parse.
147      * @throws IOException for an incorrect reader.
148      */

149     protected synchronized void parse(BufferedReader JavaDoc reader)
150             throws IOException JavaDoc
151     {
152         int l,count = 0;
153         String JavaDoc next;
154         String JavaDoc str = null;
155         HashMap JavaDoc mimeTypes = (HashMap JavaDoc) extensionMimeTypes.clone();
156         HashMap JavaDoc extensions = (HashMap JavaDoc) mimeTypeExtensions.clone();
157         while ((next = reader.readLine()) != null)
158         {
159             str = str == null ? next : str + next;
160             if ((l = str.length()) == 0)
161             {
162                 str = null;
163                 continue;
164             }
165             // Check for continuation line.
166
if (str.charAt(l - 1) != '\\')
167             {
168                 count += parseMimeTypeExtension(str, mimeTypes, extensions);
169                 str = null;
170             }
171             else
172             {
173                 str = str.substring(0, l - 1);
174             }
175         }
176         if (str != null)
177         {
178             count += parseMimeTypeExtension(str, mimeTypes, extensions);
179         }
180         if (count > 0)
181         {
182             extensionMimeTypes = mimeTypes;
183             mimeTypeExtensions = extensions;
184         }
185     }
186
187     /**
188      * Parses a MIME type extension.
189      *
190      * @param spec an extension specification to parse.
191      * @param mimeTypes a map of MIME types.
192      * @param extensions a map of extensions.
193      * @return the number of file name extensions parsed.
194      */

195     protected int parseMimeTypeExtension(String JavaDoc spec,
196                                          Map JavaDoc mimeTypes,
197                                          Map JavaDoc extensions)
198     {
199         int count = 0;
200         spec = spec.trim();
201         if ((spec.length() > 0) &&
202                 (spec.charAt(0) != '#'))
203         {
204             StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(spec);
205             String JavaDoc type = tokens.nextToken();
206             String JavaDoc ext;
207             while (tokens.hasMoreTokens())
208             {
209                 ext = tokens.nextToken();
210                 if (ext.length() == 0)
211                 {
212                     continue;
213                 }
214                 extensions.put(ext, type);
215                 if (count++ == 0)
216                 {
217                     mimeTypes.put(type, ext);
218                 }
219             }
220         }
221         return count;
222     }
223 }
224
Popular Tags