%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.jetspeed.util.MimeType |
|
|
1 | /* |
|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
|
3 | * contributor license agreements. See the NOTICE file distributed with |
|
4 | * this work for additional information regarding copyright ownership. |
|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
6 | * (the "License"); you may not use this file except in compliance with |
|
7 | * the License. You may obtain a copy of the License at |
|
8 | * |
|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
10 | * |
|
11 | * Unless required by applicable law or agreed to in writing, software |
|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 | * See the License for the specific language governing permissions and |
|
15 | * limitations under the License. |
|
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 | 0 | public static final MimeType HTML = new MimeType("text/html", "UTF-8"); //FIXME: test |
35 | 0 | public static final MimeType XHTML = new MimeType("text/xhtml"); |
36 | 0 | public static final MimeType WML = new MimeType("text/vnd.wap.wml"); |
37 | 0 | public static final MimeType XML = new MimeType("text/xml"); |
38 | 0 | public static final MimeType VXML = new MimeType("text/vxml"); |
39 | ||
40 | /** |
|
41 | * Standard ContentType String, with no encoding appended. |
|
42 | */ |
|
43 | 0 | private String mimeType = ""; |
44 | /** |
|
45 | * null value means default encoding. |
|
46 | * Otherwise, charset to be used. |
|
47 | */ |
|
48 | 0 | private String charSet = null; |
49 | ||
50 | public MimeType(String mimeType) |
|
51 | 0 | { |
52 | 0 | if (mimeType == null) |
53 | { |
|
54 | 0 | throw new NullPointerException(); |
55 | } |
|
56 | 0 | this.mimeType = mimeType; |
57 | 0 | } |
58 | ||
59 | /** |
|
60 | * |
|
61 | */ |
|
62 | public MimeType(String mimeType, String charSet) |
|
63 | 0 | { |
64 | 0 | if (mimeType == null) |
65 | { |
|
66 | 0 | throw new NullPointerException(); |
67 | } |
|
68 | 0 | this.mimeType = mimeType; |
69 | 0 | this.charSet = charSet; |
70 | 0 | } |
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 | 0 | String type = this.mimeType; |
80 | // get everything after "/" |
|
81 | 0 | type = type.substring(type.indexOf("/") + 1); |
82 | // remove any dot in the name |
|
83 | 0 | int idx = type.lastIndexOf("."); |
84 | 0 | if (idx >= 0) |
85 | { |
|
86 | 0 | type = type.substring(idx + 1); |
87 | } |
|
88 | //remove anything before a "-" |
|
89 | 0 | idx = type.lastIndexOf("-"); |
90 | 0 | if (idx >= 0) |
91 | { |
|
92 | 0 | type = type.substring(idx + 1); |
93 | } |
|
94 | ||
95 | 0 | return type.toLowerCase(); |
96 | } |
|
97 | ||
98 | /** |
|
99 | * Return the media type associated |
|
100 | */ |
|
101 | public String getContentType() |
|
102 | { |
|
103 | 0 | return this.mimeType; |
104 | } |
|
105 | ||
106 | /** |
|
107 | * Return the character encoding associated, if any |
|
108 | */ |
|
109 | public String getCharSet() |
|
110 | { |
|
111 | 0 | return this.charSet; |
112 | } |
|
113 | ||
114 | /** |
|
115 | * Convert this MimeType to its external String representation |
|
116 | */ |
|
117 | public String toString() |
|
118 | { |
|
119 | 0 | if (null == this.charSet) |
120 | { |
|
121 | 0 | return this.mimeType; |
122 | } |
|
123 | 0 | return this.mimeType + "; charset=" + this.charSet; |
124 | } |
|
125 | ||
126 | /** |
|
127 | * Compare one MimeType to another |
|
128 | */ |
|
129 | public boolean equals(Object obj) |
|
130 | { |
|
131 | 0 | if (this == obj) |
132 | { |
|
133 | 0 | return true; |
134 | } |
|
135 | ||
136 | 0 | if (obj instanceof MimeType) |
137 | { |
|
138 | 0 | MimeType comp = (MimeType) obj; |
139 | 0 | return this.toString().equals(comp.toString()); |
140 | } |
|
141 | else |
|
142 | { |
|
143 | 0 | return false; |
144 | } |
|
145 | } |
|
146 | ||
147 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |