View Javadoc

1   package org.apache.turbine.services.mimetype;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.Locale;
24  
25  import org.apache.turbine.services.TurbineServices;
26  import org.apache.turbine.services.mimetype.util.MimeType;
27  
28  /***
29   * This is a static accessor to MIME types and charsets.
30   *
31   * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
32   * @version $Id: TurbineMimeTypes.java 534527 2007-05-02 16:10:59Z tv $
33   */
34  public abstract class TurbineMimeTypes
35  {
36      /***
37       * Gets the MIME content type for a file as a string.
38       *
39       * @param file the file.
40       * @return the MIME type string.
41       */
42      public static String getContentType(File file)
43      {
44          return getService().getContentType(file);
45      }
46  
47      /***
48       * Gets the MIME content type for a named file as a string.
49       *
50       * @param name the name of the file.
51       * @return the MIME type string.
52       */
53      public static String getContentType(String name)
54      {
55          return getService().getContentType(name);
56      }
57  
58      /***
59       * Gets the MIME content type for a file name extension as a string.
60       *
61       * @param ext the file name extension.
62       * @param def the default type if none is found.
63       * @return the MIME type string.
64       */
65      public static String getContentType(String ext,
66                                          String def)
67      {
68          return getService().getContentType(ext, def);
69      }
70  
71      /***
72       * Gets the MIME content type for a file.
73       *
74       * @param file the file.
75       * @return the MIME type.
76       */
77      public static MimeType getMimeContentType(File file)
78      {
79          return getService().getMimeContentType(file);
80      }
81  
82      /***
83       * Gets the MIME content type for a named file.
84       *
85       * @param name the name of the file.
86       * @return the MIME type.
87       */
88      public static MimeType getMimeContentType(String name)
89      {
90          return getService().getMimeContentType(name);
91      }
92  
93      /***
94       * Gets the MIME content type for a file name extension.
95       *
96       * @param ext the file name extension.
97       * @param def the default type if none is found.
98       * @return the MIME type.
99       */
100     public static MimeType getMimeContentType(String ext,
101                                               String def)
102     {
103         return getService().getMimeContentType(ext, def);
104     }
105 
106     /***
107      * Gets the default file name extension for a MIME type.
108      * Note that the mappers are called in the reverse order.
109      *
110      * @param mime the MIME type.
111      * @return the file name extension or null.
112      */
113     public static String getDefaultExtension(MimeType mime)
114     {
115         return getService().getDefaultExtension(mime);
116     }
117 
118     /***
119      * Gets the charset for a locale. First a locale specific charset
120      * is searched for, then a country specific one and lastly a language
121      * specific one. If none is found, the default charset is returned.
122      *
123      * @param locale the locale.
124      * @return the charset.
125      */
126     public static String getCharSet(Locale locale)
127     {
128         return getService().getCharSet(locale);
129     }
130 
131     /***
132      * Gets the charset for a locale with a variant. The search
133      * is performed in the following order:
134      * "lang"_"country"_"variant"="charset",
135      * _"counry"_"variant"="charset",
136      * "lang"__"variant"="charset",
137      * __"variant"="charset",
138      * "lang"_"country"="charset",
139      * _"country"="charset",
140      * "lang"="charset".
141      * If nothing of the above is found, the default charset is returned.
142      *
143      * @param locale the locale.
144      * @param variant a variant field.
145      * @return the charset.
146      */
147     public static String getCharSet(Locale locale,
148                                     String variant)
149     {
150         return getService().getCharSet(locale, variant);
151     }
152 
153     /***
154      * Gets the charset for a specified key.
155      *
156      * @param key the key for the charset.
157      * @return the found charset or the default one.
158      */
159     public static String getCharSet(String key)
160     {
161         return getService().getCharSet(key);
162     }
163 
164     /***
165      * Gets the charset for a specified key.
166      *
167      * @param key the key for the charset.
168      * @param def the default charset if none is found.
169      * @return the found charset or the given default.
170      */
171     public static String getCharSet(String key,
172                                     String def)
173     {
174         return getService().getCharSet(key, def);
175     }
176 
177     /***
178      * Gets the MIME type service implementation.
179      *
180      * @return the MIME type service implementation.
181      */
182     protected static MimeTypeService getService()
183     {
184         return (MimeTypeService) TurbineServices.
185                 getInstance().getService(MimeTypeService.SERVICE_NAME);
186     }
187 }