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