KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > http > ContentType


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.tomcat.util.http;
19
20
21 /**
22  * Usefull methods for Content-Type processing
23  *
24  * @author James Duncan Davidson [duncan@eng.sun.com]
25  * @author James Todd [gonzo@eng.sun.com]
26  * @author Jason Hunter [jch@eng.sun.com]
27  * @author Harish Prabandham
28  * @author costin@eng.sun.com
29  */

30 public class ContentType {
31
32     // Basically return everything after ";charset="
33
// If no charset specified, use the HTTP default (ASCII) character set.
34
public static String JavaDoc getCharsetFromContentType(String JavaDoc type) {
35         if (type == null) {
36             return null;
37         }
38         int semi = type.indexOf(";");
39         if (semi == -1) {
40             return null;
41         }
42         int charsetLocation = type.indexOf("charset=", semi);
43         if (charsetLocation == -1) {
44             return null;
45         }
46     String JavaDoc afterCharset = type.substring(charsetLocation + 8);
47         // The charset value in a Content-Type header is allowed to be quoted
48
// and charset values can't contain quotes. Just convert any quote
49
// chars into spaces and let trim clean things up.
50
afterCharset = afterCharset.replace('"', ' ');
51         String JavaDoc encoding = afterCharset.trim();
52         return encoding;
53     }
54
55
56     /**
57      * Returns true if the given content type contains a charset component,
58      * false otherwise.
59      *
60      * @param type Content type
61      * @return true if the given content type contains a charset component,
62      * false otherwise
63      */

64     public static boolean hasCharset(String JavaDoc type) {
65
66         boolean hasCharset = false;
67
68         int len = type.length();
69         int index = type.indexOf(';');
70         while (index != -1) {
71             index++;
72             while (index < len && Character.isSpace(type.charAt(index))) {
73                 index++;
74             }
75             if (index+8 < len
76                     && type.charAt(index) == 'c'
77                     && type.charAt(index+1) == 'h'
78                     && type.charAt(index+2) == 'a'
79                     && type.charAt(index+3) == 'r'
80                     && type.charAt(index+4) == 's'
81                     && type.charAt(index+5) == 'e'
82                     && type.charAt(index+6) == 't'
83                     && type.charAt(index+7) == '=') {
84                 hasCharset = true;
85                 break;
86             }
87             index = type.indexOf(';', index);
88         }
89
90         return hasCharset;
91     }
92
93 }
94
Popular Tags