1/*2 * Copyright 2000-2001,2004 The Apache Software Foundation.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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.util;
181920/***2122<p>Utility class for declaring MIME types to use for various requests and provide23utility manipulation methods.</p>24<p>Added Content-Encoding capability, with defaults2526@author <a href="mailto:raphael@apache.org">Raphaël Luta</a>27@author <a href="mailto:sgala@apache.org">Santiago Gala</a>28@version $Id: MimeType.java,v 1.8 2004/02/23 03:23:42 jford Exp $29*/30publicclassMimeType {
3132publicstaticfinalMimeType HTML = new MimeType( "text/html", "UTF-8" ); //FIXME: test33publicstaticfinalMimeType XHTML = newMimeType( "text/xhtml" );
34publicstaticfinalMimeType WML = new MimeType( "text/vnd.wap.wml" );
35publicstaticfinalMimeType XML = new MimeType( "text/xml" );
36publicstaticfinalMimeType VXML = new MimeType( "text/vxml" );
3738/***39 * Standard ContentType String, with no encoding appended.40 */41private String mimeType = "";
42/***43 * null value means default encoding.44 * Otherwise, charset to be used.45 */46private String charSet = null;
4748publicMimeType( String mimeType ) {
49if(mimeType == null) {
50thrownew NullPointerException();
51 }
52this.mimeType = mimeType;
53 }
5455/***56 *57 */58publicMimeType( String mimeType, String charSet ) {
59if(mimeType == null) {
60thrownew NullPointerException();
61 }
62this.mimeType = mimeType;
63this.charSet = charSet;
64 }
6566/*** Extracts from this MimeType a user-friendly identifying code 67 * ie "html" for "text/html" or "wml" for "text/vnd.wap.wml"68 *69 * @param mime the type to simplify70 * @return the simplified type71 */72public String getCode() {
73 String type = this.mimeType;
74// get everything after "/"75 type = type.substring(type.indexOf("/")+1);
76// remove any dot in the name77int idx = type.lastIndexOf(".");
78if (idx >= 0 ) {
79 type = type.substring(idx+1);
80 }
81//remove anything before a "-"82 idx = type.lastIndexOf("-");
83if (idx >= 0 ) {
84 type = type.substring(idx+1);
85 }
8687return type.toLowerCase();
88 }
8990/***91 Return the media type associated92 */93public String getContentType()
94 {
95returnthis.mimeType;
96 }
9798/***99 Return the character encoding associated, if any100 */101public String getCharSet()
102 {
103returnthis.charSet;
104 }
105106/***107 Convert this MimeType to its external String representation108 */109public String toString()
110 {
111if( null == this.charSet )
112 {
113returnthis.mimeType;
114 }
115returnthis.mimeType +
116"; charset=" +
117this.charSet;
118 }
119120/***121 Compare one MimeType to another122 */123publicboolean equals( Object obj ) {
124if ( this == obj) {
125returntrue;
126 }
127128if (obj instanceof MimeType) {
129MimeType comp = (MimeType)obj;
130returnthis.toString().equals( comp.toString() );
131 } else {
132return false;
133 }
134 }
135136 }