001: package org.apache.turbine.services.mimetype;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.File;
023: import java.util.Locale;
024:
025: import org.apache.turbine.services.TurbineServices;
026: import org.apache.turbine.services.mimetype.util.MimeType;
027:
028: /**
029: * This is a static accessor to MIME types and charsets.
030: *
031: * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
032: * @version $Id: TurbineMimeTypes.java 534527 2007-05-02 16:10:59Z tv $
033: */
034: public abstract class TurbineMimeTypes {
035: /**
036: * Gets the MIME content type for a file as a string.
037: *
038: * @param file the file.
039: * @return the MIME type string.
040: */
041: public static String getContentType(File file) {
042: return getService().getContentType(file);
043: }
044:
045: /**
046: * Gets the MIME content type for a named file as a string.
047: *
048: * @param name the name of the file.
049: * @return the MIME type string.
050: */
051: public static String getContentType(String name) {
052: return getService().getContentType(name);
053: }
054:
055: /**
056: * Gets the MIME content type for a file name extension as a string.
057: *
058: * @param ext the file name extension.
059: * @param def the default type if none is found.
060: * @return the MIME type string.
061: */
062: public static String getContentType(String ext, String def) {
063: return getService().getContentType(ext, def);
064: }
065:
066: /**
067: * Gets the MIME content type for a file.
068: *
069: * @param file the file.
070: * @return the MIME type.
071: */
072: public static MimeType getMimeContentType(File file) {
073: return getService().getMimeContentType(file);
074: }
075:
076: /**
077: * Gets the MIME content type for a named file.
078: *
079: * @param name the name of the file.
080: * @return the MIME type.
081: */
082: public static MimeType getMimeContentType(String name) {
083: return getService().getMimeContentType(name);
084: }
085:
086: /**
087: * Gets the MIME content type for a file name extension.
088: *
089: * @param ext the file name extension.
090: * @param def the default type if none is found.
091: * @return the MIME type.
092: */
093: public static MimeType getMimeContentType(String ext, String def) {
094: return getService().getMimeContentType(ext, def);
095: }
096:
097: /**
098: * Gets the default file name extension for a MIME type.
099: * Note that the mappers are called in the reverse order.
100: *
101: * @param mime the MIME type.
102: * @return the file name extension or null.
103: */
104: public static String getDefaultExtension(MimeType mime) {
105: return getService().getDefaultExtension(mime);
106: }
107:
108: /**
109: * Gets the charset for a locale. First a locale specific charset
110: * is searched for, then a country specific one and lastly a language
111: * specific one. If none is found, the default charset is returned.
112: *
113: * @param locale the locale.
114: * @return the charset.
115: */
116: public static String getCharSet(Locale locale) {
117: return getService().getCharSet(locale);
118: }
119:
120: /**
121: * Gets the charset for a locale with a variant. The search
122: * is performed in the following order:
123: * "lang"_"country"_"variant"="charset",
124: * _"counry"_"variant"="charset",
125: * "lang"__"variant"="charset",
126: * __"variant"="charset",
127: * "lang"_"country"="charset",
128: * _"country"="charset",
129: * "lang"="charset".
130: * If nothing of the above is found, the default charset is returned.
131: *
132: * @param locale the locale.
133: * @param variant a variant field.
134: * @return the charset.
135: */
136: public static String getCharSet(Locale locale, String variant) {
137: return getService().getCharSet(locale, variant);
138: }
139:
140: /**
141: * Gets the charset for a specified key.
142: *
143: * @param key the key for the charset.
144: * @return the found charset or the default one.
145: */
146: public static String getCharSet(String key) {
147: return getService().getCharSet(key);
148: }
149:
150: /**
151: * Gets the charset for a specified key.
152: *
153: * @param key the key for the charset.
154: * @param def the default charset if none is found.
155: * @return the found charset or the given default.
156: */
157: public static String getCharSet(String key, String def) {
158: return getService().getCharSet(key, def);
159: }
160:
161: /**
162: * Gets the MIME type service implementation.
163: *
164: * @return the MIME type service implementation.
165: */
166: protected static MimeTypeService getService() {
167: return (MimeTypeService) TurbineServices.getInstance()
168: .getService(MimeTypeService.SERVICE_NAME);
169: }
170: }
|