01: /*
02: * Copyright 1999-2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.apache.tomcat.util.http;
18:
19: /**
20: * Usefull methods for Content-Type processing
21: *
22: * @author James Duncan Davidson [duncan@eng.sun.com]
23: * @author James Todd [gonzo@eng.sun.com]
24: * @author Jason Hunter [jch@eng.sun.com]
25: * @author Harish Prabandham
26: * @author costin@eng.sun.com
27: */
28: public class ContentType {
29:
30: // Basically return everything after ";charset="
31: // If no charset specified, use the HTTP default (ASCII) character set.
32: public static String getCharsetFromContentType(String type) {
33: if (type == null) {
34: return null;
35: }
36: int semi = type.indexOf(";");
37: if (semi == -1) {
38: return null;
39: }
40: int charsetLocation = type.indexOf("charset=", semi);
41: if (charsetLocation == -1) {
42: return null;
43: }
44: String afterCharset = type.substring(charsetLocation + 8);
45: // The charset value in a Content-Type header is allowed to be quoted
46: // and charset values can't contain quotes. Just convert any quote
47: // chars into spaces and let trim clean things up.
48: afterCharset = afterCharset.replace('"', ' ');
49: String encoding = afterCharset.trim();
50: return encoding;
51: }
52:
53: // Bad method: the user may set the charset explicitely
54:
55: // /** Utility method for parsing the mime type and setting
56: // * the encoding to locale. Also, convert from java Locale to mime
57: // * encodings
58: // */
59: // public static String constructLocalizedContentType(String type,
60: // Locale loc) {
61: // // Cut off everything after the semicolon
62: // int semi = type.indexOf(";");
63: // if (semi != -1) {
64: // type = type.substring(0, semi);
65: // }
66:
67: // // Append the appropriate charset, based on the locale
68: // String charset = LocaleToCharsetMap.getCharset(loc);
69: // if (charset != null) {
70: // type = type + "; charset=" + charset;
71: // }
72:
73: // return type;
74: // }
75:
76: }
|