KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joseki > util > HttpContentType


1 /*
2  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6 package org.joseki.util;
7
8 //import com.hp.hpl.jena.rdf.model.* ;
9
import org.apache.commons.logging.*;
10
11 /**
12  * Handle HTTP content type
13  *
14  * @author Andy Seaborne
15  * @version $Id: HttpContentType.java,v 1.3 2004/04/30 14:13:13 andy_seaborne Exp $
16  */

17
18 public class HttpContentType
19 {
20     static Log logger = LogFactory.getLog(HttpContentType.class);
21
22     String JavaDoc mediaType = null;
23     String JavaDoc params[] = null;
24     String JavaDoc charset = null;
25
26     // See Accept/Accept-Charset: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
27
// See http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html (3.4 and 3.7)
28

29     public HttpContentType(String JavaDoc s)
30     {
31         parse(s);
32     }
33
34     public HttpContentType(String JavaDoc s, String JavaDoc defaultMediaType, String JavaDoc defaultCharset)
35     {
36         this(s);
37         if (mediaType == null)
38             mediaType = defaultMediaType;
39         if (charset == null)
40             charset = defaultCharset;
41     }
42
43     public String JavaDoc getMediaType()
44     {
45         return mediaType;
46     }
47
48     public String JavaDoc getCharset()
49     {
50         return charset;
51     }
52     
53     // Ignore misc params.
54
public String JavaDoc toString()
55     {
56         StringBuffer JavaDoc sbuff = new StringBuffer JavaDoc() ;
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 JavaDoc 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 JavaDoc 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 /*
97  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP All rights
98  * reserved.
99  *
100  * Redistribution and use in source and binary forms, with or without
101  * modification, are permitted provided that the following conditions are met: 1.
102  * Redistributions of source code must retain the above copyright notice, this
103  * list of conditions and the following disclaimer. 2. Redistributions in
104  * binary form must reproduce the above copyright notice, this list of
105  * conditions and the following disclaimer in the documentation and/or other
106  * materials provided with the distribution. 3. The name of the author may not
107  * be used to endorse or promote products derived from this software without
108  * specific prior written permission.
109  *
110  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
111  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
112  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
113  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
114  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
115  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
116  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
117  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
118  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
119  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
120  */

121
Popular Tags