View Javadoc

1   package org.apache.turbine.services.mimetype;
2   
3   /*
4    * Copyright 2001-2005 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;
20  import java.util.Locale;
21  
22  import org.apache.turbine.services.TurbineServices;
23  import org.apache.turbine.services.mimetype.util.MimeType;
24  
25  /***
26   * This is a static accessor to MIME types and charsets.
27   *
28   * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
29   * @version $Id: TurbineMimeTypes.java 264148 2005-08-29 14:21:04Z henning $
30   */
31  public abstract class TurbineMimeTypes
32  {
33      /***
34       * Gets the MIME content type for a file as a string.
35       *
36       * @param file the file.
37       * @return the MIME type string.
38       */
39      public static String getContentType(File file)
40      {
41          return getService().getContentType(file);
42      }
43  
44      /***
45       * Gets the MIME content type for a named file as a string.
46       *
47       * @param name the name of the file.
48       * @return the MIME type string.
49       */
50      public static String getContentType(String name)
51      {
52          return getService().getContentType(name);
53      }
54  
55      /***
56       * Gets the MIME content type for a file name extension as a string.
57       *
58       * @param ext the file name extension.
59       * @param def the default type if none is found.
60       * @return the MIME type string.
61       */
62      public static String getContentType(String ext,
63                                          String def)
64      {
65          return getService().getContentType(ext, def);
66      }
67  
68      /***
69       * Gets the MIME content type for a file.
70       *
71       * @param file the file.
72       * @return the MIME type.
73       */
74      public static MimeType getMimeContentType(File file)
75      {
76          return getService().getMimeContentType(file);
77      }
78  
79      /***
80       * Gets the MIME content type for a named file.
81       *
82       * @param name the name of the file.
83       * @return the MIME type.
84       */
85      public static MimeType getMimeContentType(String name)
86      {
87          return getService().getMimeContentType(name);
88      }
89  
90      /***
91       * Gets the MIME content type for a file name extension.
92       *
93       * @param ext the file name extension.
94       * @param def the default type if none is found.
95       * @return the MIME type.
96       */
97      public static MimeType getMimeContentType(String ext,
98                                                String def)
99      {
100         return getService().getMimeContentType(ext, def);
101     }
102 
103     /***
104      * Gets the default file name extension for a MIME type.
105      * Note that the mappers are called in the reverse order.
106      *
107      * @param mime the MIME type.
108      * @return the file name extension or null.
109      */
110     public static String getDefaultExtension(MimeType mime)
111     {
112         return getService().getDefaultExtension(mime);
113     }
114 
115     /***
116      * Gets the charset for a locale. First a locale specific charset
117      * is searched for, then a country specific one and lastly a language
118      * specific one. If none is found, the default charset is returned.
119      *
120      * @param locale the locale.
121      * @return the charset.
122      */
123     public static String getCharSet(Locale locale)
124     {
125         return getService().getCharSet(locale);
126     }
127 
128     /***
129      * Gets the charset for a locale with a variant. The search
130      * is performed in the following order:
131      * "lang"_"country"_"variant"="charset",
132      * _"counry"_"variant"="charset",
133      * "lang"__"variant"="charset",
134      * __"variant"="charset",
135      * "lang"_"country"="charset",
136      * _"country"="charset",
137      * "lang"="charset".
138      * If nothing of the above is found, the default charset is returned.
139      *
140      * @param locale the locale.
141      * @param variant a variant field.
142      * @return the charset.
143      */
144     public static String getCharSet(Locale locale,
145                                     String variant)
146     {
147         return getService().getCharSet(locale, variant);
148     }
149 
150     /***
151      * Gets the charset for a specified key.
152      *
153      * @param key the key for the charset.
154      * @return the found charset or the default one.
155      */
156     public static String getCharSet(String key)
157     {
158         return getService().getCharSet(key);
159     }
160 
161     /***
162      * Gets the charset for a specified key.
163      *
164      * @param key the key for the charset.
165      * @param def the default charset if none is found.
166      * @return the found charset or the given default.
167      */
168     public static String getCharSet(String key,
169                                     String def)
170     {
171         return getService().getCharSet(key, def);
172     }
173 
174     /***
175      * Gets the MIME type service implementation.
176      *
177      * @return the MIME type service implementation.
178      */
179     protected static MimeTypeService getService()
180     {
181         return (MimeTypeService) TurbineServices.
182                 getInstance().getService(MimeTypeService.SERVICE_NAME);
183     }
184 }