KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > mimetype > TurbineMimeTypeService


1 package org.apache.turbine.services.mimetype;
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.File JavaDoc;
20 import java.io.IOException JavaDoc;
21
22 import java.util.Locale JavaDoc;
23
24 import org.apache.commons.configuration.Configuration;
25
26 import org.apache.turbine.services.InitializationException;
27 import org.apache.turbine.services.TurbineBaseService;
28 import org.apache.turbine.services.mimetype.util.CharSetMap;
29 import org.apache.turbine.services.mimetype.util.MimeType;
30 import org.apache.turbine.services.mimetype.util.MimeTypeMap;
31 import org.apache.turbine.services.servlet.TurbineServlet;
32
33 /**
34  * The MimeType Service maintains mappings between MIME types and
35  * the corresponding file name extensions, and between locales and
36  * character encodings.
37  *
38  * <p>The MIME type mappings can be defined in MIME type files
39  * located in user's home directory, Java home directory or
40  * the current class jar. The default mapping file is defined
41  * with the mime.type.file property. In addition, the service maintains
42  * a set of most common mappings.
43  *
44  * <p>The charset mappings can be defined in property files
45  * located in user's home directory, Java home directory or
46  * the current class jar. The default mapping file is defined
47  * with the charset.file property. In addition, the service maintains
48  * a set of most common mappings.
49  *
50  * @author <a HREF="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
51  * @version $Id: TurbineMimeTypeService.java,v 1.6.2.2 2004/05/20 03:06:50 seade Exp $
52  */

53 public class TurbineMimeTypeService
54         extends TurbineBaseService
55         implements MimeTypeService
56 {
57     /**
58      * The MIME type file property.
59      */

60     public static final String JavaDoc MIME_TYPES = "mime.types";
61
62     /**
63      * The charset file property.
64      */

65     public static final String JavaDoc CHARSETS = "charsets";
66
67     /**
68      * The MIME type map used by the service.
69      */

70     private MimeTypeMap mimeTypeMap;
71
72     /**
73      * The charset map used by the service.
74      */

75     private CharSetMap charSetMap;
76
77     /**
78      * Constructs a new service.
79      */

80     public TurbineMimeTypeService()
81     {
82     }
83
84     /**
85      * Initializes the service.
86      *
87      * @throws InitializationException if initialization fails.
88      */

89     public void init()
90             throws InitializationException
91     {
92         String JavaDoc path = null;
93         Configuration conf = getConfiguration();
94         if (conf != null)
95         {
96             path = conf.getString(MIME_TYPES);
97             if (path != null)
98             {
99                 path = TurbineServlet.getRealPath(path);
100             }
101         }
102         if (path != null)
103         {
104             try
105             {
106                 mimeTypeMap = new MimeTypeMap(path);
107             }
108             catch (IOException JavaDoc x)
109             {
110                 throw new InitializationException(path, x);
111             }
112         }
113         else
114         {
115             mimeTypeMap = new MimeTypeMap();
116         }
117
118         if (conf != null)
119         {
120             path = conf.getString(CHARSETS);
121             if (path != null)
122             {
123                 path = TurbineServlet.getRealPath(path);
124             }
125         }
126         if (path != null)
127         {
128             try
129             {
130                 charSetMap = new CharSetMap(path);
131             }
132             catch (IOException JavaDoc x)
133             {
134                 throw new InitializationException(path, x);
135             }
136         }
137         else
138         {
139             charSetMap = new CharSetMap();
140         }
141         setInit(true);
142     }
143
144     /**
145      * Sets a MIME content type mapping to extensions to the map.
146      * The extension is specified by a MIME type name followed
147      * by a list of file name extensions separated by a whitespace.
148      *
149      * @param spec a MIME type extension specification to add.
150      */

151     public void setContentType(String JavaDoc spec)
152     {
153         mimeTypeMap.setContentType(spec);
154     }
155
156     /**
157      * Gets the MIME content type for a file as a string.
158      *
159      * @param file the file.
160      * @return the MIME type string.
161      */

162     public String JavaDoc getContentType(File JavaDoc file)
163     {
164         return mimeTypeMap.getContentType(file);
165     }
166
167     /**
168      * Gets the MIME content type for a named file as a string.
169      *
170      * @param name the name of the file.
171      * @return the MIME type string.
172      */

173     public String JavaDoc getContentType(String JavaDoc name)
174     {
175         return mimeTypeMap.getContentType(name);
176     }
177
178     /**
179      * Gets the MIME content type for a file name extension as a string.
180      *
181      * @param ext the file name extension.
182      * @param def the default type if none is found.
183      * @return the MIME type string.
184      */

185     public String JavaDoc getContentType(String JavaDoc ext,
186                                  String JavaDoc def)
187     {
188         return mimeTypeMap.getContentType(ext, def);
189     }
190
191     /**
192      * Gets the MIME content type for a file.
193      *
194      * @param file the file.
195      * @return the MIME type.
196      */

197     public MimeType getMimeContentType(File JavaDoc file)
198     {
199         return mimeTypeMap.getMimeContentType(file);
200     }
201
202     /**
203      * Gets the MIME content type for a named file.
204      *
205      * @param name the name of the file.
206      * @return the MIME type.
207      */

208     public MimeType getMimeContentType(String JavaDoc name)
209     {
210         return mimeTypeMap.getMimeContentType(name);
211     }
212
213     /**
214      * Gets the MIME content type for a file name extension.
215      *
216      * @param ext the file name extension.
217      * @param def the default type if none is found.
218      * @return the MIME type.
219      */

220     public MimeType getMimeContentType(String JavaDoc ext,
221                                        String JavaDoc def)
222     {
223         return mimeTypeMap.getMimeContentType(ext, def);
224     }
225
226     /**
227      * Gets the default file name extension for a MIME type.
228      * Note that the mappers are called in the reverse order.
229      *
230      * @param type the MIME type as a string.
231      * @return the file name extension or null.
232      */

233     public String JavaDoc getDefaultExtension(String JavaDoc type)
234     {
235         return mimeTypeMap.getDefaultExtension(type);
236     }
237
238     /**
239      * Gets the default file name extension for a MIME type.
240      * Note that the mappers are called in the reverse order.
241      *
242      * @param mime the MIME type.
243      * @return the file name extension or null.
244      */

245     public String JavaDoc getDefaultExtension(MimeType mime)
246     {
247         return mimeTypeMap.getDefaultExtension(mime);
248     }
249
250     /**
251      * Sets a locale-charset mapping.
252      *
253      * @param key the key for the charset.
254      * @param charset the corresponding charset.
255      */

256     public void setCharSet(String JavaDoc key,
257                            String JavaDoc charset)
258     {
259         charSetMap.setCharSet(key, charset);
260     }
261
262     /**
263      * Gets the charset for a locale. First a locale specific charset
264      * is searched for, then a country specific one and lastly a language
265      * specific one. If none is found, the default charset is returned.
266      *
267      * @param locale the locale.
268      * @return the charset.
269      */

270     public String JavaDoc getCharSet(Locale JavaDoc locale)
271     {
272         return charSetMap.getCharSet(locale);
273     }
274
275     /**
276      * Gets the charset for a locale with a variant. The search
277      * is performed in the following order:
278      * "lang"_"country"_"variant"="charset",
279      * _"counry"_"variant"="charset",
280      * "lang"__"variant"="charset",
281      * __"variant"="charset",
282      * "lang"_"country"="charset",
283      * _"country"="charset",
284      * "lang"="charset".
285      * If nothing of the above is found, the default charset is returned.
286      *
287      * @param locale the locale.
288      * @param variant a variant field.
289      * @return the charset.
290      */

291     public String JavaDoc getCharSet(Locale JavaDoc locale,
292                              String JavaDoc variant)
293     {
294         return charSetMap.getCharSet(locale, variant);
295     }
296
297     /**
298      * Gets the charset for a specified key.
299      *
300      * @param key the key for the charset.
301      * @return the found charset or the default one.
302      */

303     public String JavaDoc getCharSet(String JavaDoc key)
304     {
305         return charSetMap.getCharSet(key);
306     }
307
308     /**
309      * Gets the charset for a specified key.
310      *
311      * @param key the key for the charset.
312      * @param def the default charset if none is found.
313      * @return the found charset or the given default.
314      */

315     public String JavaDoc getCharSet(String JavaDoc key,
316                              String JavaDoc def)
317     {
318         return charSetMap.getCharSet(key, def);
319     }
320 }
321
Popular Tags