KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > syndication > fetcher > impl > ResponseHandler


1 /*
2  * Copyright 2004 Sun Microsystems, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 com.sun.syndication.fetcher.impl;
18
19 import java.net.URLConnection JavaDoc;
20 import java.util.regex.Matcher JavaDoc;
21 import java.util.regex.Pattern JavaDoc;
22
23 /**
24  * Utility class to help deal with HTTP responses
25  *
26  */

27 public class ResponseHandler {
28     public static final String JavaDoc defaultCharacterEncoding = "ISO-8859-1";
29     
30     private static Pattern JavaDoc characterEncodingPattern = Pattern.compile("charset=([.[^; ]]*)");
31     
32     public static String JavaDoc getCharacterEncoding(URLConnection JavaDoc connection) {
33         return getCharacterEncoding(connection.getContentType());
34     }
35     
36     /**
37      *
38      * <p>Gets the character encoding of a response. (Note that this is different to
39      * the content-encoding)</p>
40      *
41      * @param contentTypeHeader the value of the content-type HTTP header eg: text/html; charset=ISO-8859-4
42      * @return the character encoding, eg: ISO-8859-4
43      */

44     public static String JavaDoc getCharacterEncoding(String JavaDoc contentTypeHeader) {
45         if (contentTypeHeader == null) {
46             return defaultCharacterEncoding;
47         }
48         
49         Matcher JavaDoc m = characterEncodingPattern.matcher(contentTypeHeader);
50         //if (!m.matches()) {
51
if (!m.find()) {
52             return defaultCharacterEncoding;
53         } else {
54             return m.group(1);
55         }
56     }
57 }
58
Popular Tags