1 5 6 package org.joseki.util; 7 8 import org.apache.commons.logging.*; 10 11 17 18 public class HttpContentType 19 { 20 static Log logger = LogFactory.getLog(HttpContentType.class); 21 22 String mediaType = null; 23 String params[] = null; 24 String charset = null; 25 26 29 public HttpContentType(String s) 30 { 31 parse(s); 32 } 33 34 public HttpContentType(String s, String defaultMediaType, String defaultCharset) 35 { 36 this(s); 37 if (mediaType == null) 38 mediaType = defaultMediaType; 39 if (charset == null) 40 charset = defaultCharset; 41 } 42 43 public String getMediaType() 44 { 45 return mediaType; 46 } 47 48 public String getCharset() 49 { 50 return charset; 51 } 52 53 public String toString() 55 { 56 StringBuffer sbuff = new StringBuffer () ; 57 if ( mediaType != null ) 58 sbuff.append(mediaType) ; 59 if ( charset != null ) 60 { 61 sbuff.append("; charset=") ; 62 sbuff.append(charset) ; 63 } 64 return sbuff.toString() ; 65 } 66 67 private void parse(String s) 68 { 69 if (s == null) 70 return; 71 72 int j = s.indexOf(';'); 73 if (j == -1) 74 { 75 mediaType = s.trim() ; 76 return ; 77 } 78 79 mediaType = s.substring(0, j).trim(); 80 String sParam = s.substring(j + 1) ; 81 82 params = s.split(";") ; 83 for ( int i = 0 ; i < params.length ; i++ ) 84 { 85 params[i] = params[i].trim(); 86 87 if ( params[i].matches("charset\\s*=.*") ) 88 { 89 int k = params[i].indexOf('=') ; 90 charset = params[i].substring(k+1).trim() ; 91 } 92 } 93 } 94 } 95 96 121 | Popular Tags |