Home » apache-tomcat-6.0.26-src » org.apache » tomcat » util » http » [javadoc | source]

    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       /**
   33        * Parse the character encoding from the specified content type header.
   34        * If the content type is null, or there is no explicit character encoding,
   35        * <code>null</code> is returned.
   36        *
   37        * @param contentType a content type header
   38        */
   39       public static String getCharsetFromContentType(String contentType) {
   40   
   41           if (contentType == null)
   42               return (null);
   43           int start = contentType.indexOf("charset=");
   44           if (start < 0)
   45               return (null);
   46           String encoding = contentType.substring(start + 8);
   47           int end = encoding.indexOf(';');
   48           if (end >= 0)
   49               encoding = encoding.substring(0, end);
   50           encoding = encoding.trim();
   51           if ((encoding.length() > 2) && (encoding.startsWith("\""))
   52               && (encoding.endsWith("\"")))
   53               encoding = encoding.substring(1, encoding.length() - 1);
   54           return (encoding.trim());
   55   
   56       }
   57   
   58   
   59       /**
   60        * Returns true if the given content type contains a charset component,
   61        * false otherwise.
   62        *
   63        * @param type Content type
   64        * @return true if the given content type contains a charset component,
   65        * false otherwise
   66        */
   67       public static boolean hasCharset(String type) {
   68   
   69           boolean hasCharset = false;
   70   
   71           int len = type.length();
   72           int index = type.indexOf(';');
   73           while (index != -1) {
   74               index++;
   75               while (index < len && Character.isSpace(type.charAt(index))) {
   76                   index++;
   77               }
   78               if (index+8 < len
   79                       && type.charAt(index) == 'c'
   80                       && type.charAt(index+1) == 'h'
   81                       && type.charAt(index+2) == 'a'
   82                       && type.charAt(index+3) == 'r'
   83                       && type.charAt(index+4) == 's'
   84                       && type.charAt(index+5) == 'e'
   85                       && type.charAt(index+6) == 't'
   86                       && type.charAt(index+7) == '=') {
   87                   hasCharset = true;
   88                   break;
   89               }
   90               index = type.indexOf(';', index);
   91           }
   92   
   93           return hasCharset;
   94       }
   95   
   96   }

Home » apache-tomcat-6.0.26-src » org.apache » tomcat » util » http » [javadoc | source]