1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.util;
18
19
20 /***
21
22 <p>Utility class for declaring MIME types to use for various requests and provide
23 utility manipulation methods.</p>
24 <p>Added Content-Encoding capability, with defaults
25
26 @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 */
30 public class MimeType {
31
32 public static final MimeType HTML = new MimeType( "text/html", "UTF-8" );
33 public static final MimeType XHTML = new MimeType( "text/xhtml" );
34 public static final MimeType WML = new MimeType( "text/vnd.wap.wml" );
35 public static final MimeType XML = new MimeType( "text/xml" );
36 public static final MimeType VXML = new MimeType( "text/vxml" );
37
38 /***
39 * Standard ContentType String, with no encoding appended.
40 */
41 private String mimeType = "";
42 /***
43 * null value means default encoding.
44 * Otherwise, charset to be used.
45 */
46 private String charSet = null;
47
48 public MimeType( String mimeType ) {
49 if(mimeType == null) {
50 throw new NullPointerException();
51 }
52 this.mimeType = mimeType;
53 }
54
55 /***
56 *
57 */
58 public MimeType( String mimeType, String charSet ) {
59 if(mimeType == null) {
60 throw new NullPointerException();
61 }
62 this.mimeType = mimeType;
63 this.charSet = charSet;
64 }
65
66 /*** 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 simplify
70 * @return the simplified type
71 */
72 public String getCode() {
73 String type = this.mimeType;
74
75 type = type.substring(type.indexOf("/")+1);
76
77 int idx = type.lastIndexOf(".");
78 if (idx >= 0 ) {
79 type = type.substring(idx+1);
80 }
81
82 idx = type.lastIndexOf("-");
83 if (idx >= 0 ) {
84 type = type.substring(idx+1);
85 }
86
87 return type.toLowerCase();
88 }
89
90 /***
91 Return the media type associated
92 */
93 public String getContentType()
94 {
95 return this.mimeType;
96 }
97
98 /***
99 Return the character encoding associated, if any
100 */
101 public String getCharSet()
102 {
103 return this.charSet;
104 }
105
106 /***
107 Convert this MimeType to its external String representation
108 */
109 public String toString()
110 {
111 if( null == this.charSet )
112 {
113 return this.mimeType;
114 }
115 return this.mimeType +
116 "; charset=" +
117 this.charSet;
118 }
119
120 /***
121 Compare one MimeType to another
122 */
123 public boolean equals( Object obj ) {
124 if ( this == obj) {
125 return true;
126 }
127
128 if (obj instanceof MimeType) {
129 MimeType comp = (MimeType)obj;
130 return this.toString().equals( comp.toString() );
131 } else {
132 return false;
133 }
134 }
135
136 }