Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ContentType |
|
| 2.0;2 |
1 | // Copyright 2004, 2005 The Apache Software Foundation | |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | // you may not use this file except in compliance with the License. | |
5 | // You may obtain a copy of the License at | |
6 | // | |
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
8 | // | |
9 | // Unless required by applicable law or agreed to in writing, software | |
10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | // See the License for the specific language governing permissions and | |
13 | // limitations under the License. | |
14 | ||
15 | package org.apache.tapestry.util; | |
16 | ||
17 | import java.util.HashMap; | |
18 | import java.util.Map; | |
19 | import java.util.Set; | |
20 | import java.util.StringTokenizer; | |
21 | ||
22 | import org.apache.hivemind.util.Defense; | |
23 | ||
24 | /** | |
25 | * Represents an HTTP content type. Allows to set various elements like the mime type, the character | |
26 | * set, and other parameters. This is similar to a number of other implementations of the same | |
27 | * concept in JAF, etc. We have created this simple implementation to avoid including the whole | |
28 | * libraries. | |
29 | * | |
30 | * @author mindbridge | |
31 | * @since 3.0 | |
32 | */ | |
33 | public class ContentType | |
34 | { | |
35 | 0 | private String _baseType = ""; |
36 | ||
37 | 0 | private String _subType = ""; |
38 | ||
39 | 0 | private final Map _parameters = new HashMap(); |
40 | ||
41 | /** | |
42 | * Creates a new empty content type. | |
43 | */ | |
44 | public ContentType() | |
45 | 0 | { |
46 | 0 | } |
47 | ||
48 | /** | |
49 | * Creates a new content type from the argument. The format of the argument has to be | |
50 | * basetype/subtype(;key=value)* | |
51 | * | |
52 | * @param contentType | |
53 | * the content type that needs to be represented | |
54 | */ | |
55 | public ContentType(String contentType) | |
56 | { | |
57 | 0 | this(); |
58 | 0 | parse(contentType); |
59 | 0 | } |
60 | ||
61 | /** | |
62 | * Returns true only if the other object is another instance of ContentType, and has the ssame | |
63 | * baseType, subType and set of parameters. | |
64 | */ | |
65 | public boolean equals(Object o) | |
66 | { | |
67 | 0 | if (o == null) |
68 | 0 | return false; |
69 | ||
70 | 0 | if (o.getClass() != this.getClass()) |
71 | 0 | return false; |
72 | ||
73 | 0 | ContentType ct = (ContentType) o; |
74 | ||
75 | 0 | return _baseType.equals(ct._baseType) && _subType.equals(ct._subType) |
76 | && _parameters.equals(ct._parameters); | |
77 | } | |
78 | ||
79 | /** | |
80 | * @return the base type of the content type | |
81 | */ | |
82 | public String getBaseType() | |
83 | { | |
84 | 0 | return _baseType; |
85 | } | |
86 | ||
87 | /** | |
88 | * @param baseType | |
89 | */ | |
90 | public void setBaseType(String baseType) | |
91 | { | |
92 | 0 | Defense.notNull(baseType, "baseType"); |
93 | ||
94 | 0 | _baseType = baseType; |
95 | 0 | } |
96 | ||
97 | /** | |
98 | * @return the sub-type of the content type | |
99 | */ | |
100 | public String getSubType() | |
101 | { | |
102 | 0 | return _subType; |
103 | } | |
104 | ||
105 | /** | |
106 | * @param subType | |
107 | */ | |
108 | public void setSubType(String subType) | |
109 | { | |
110 | 0 | Defense.notNull(subType, "subType"); |
111 | ||
112 | 0 | _subType = subType; |
113 | 0 | } |
114 | ||
115 | /** | |
116 | * @return the MIME type of the content type | |
117 | */ | |
118 | public String getMimeType() | |
119 | { | |
120 | 0 | return _baseType + "/" + _subType; |
121 | } | |
122 | ||
123 | /** | |
124 | * @return the list of names of parameters in this content type | |
125 | */ | |
126 | public String[] getParameterNames() | |
127 | { | |
128 | 0 | Set parameterNames = _parameters.keySet(); |
129 | 0 | return (String[]) parameterNames.toArray(new String[parameterNames.size()]); |
130 | } | |
131 | ||
132 | /** | |
133 | * @param key | |
134 | * the name of the content type parameter | |
135 | * @return the value of the content type parameter | |
136 | */ | |
137 | public String getParameter(String key) | |
138 | { | |
139 | 0 | Defense.notNull(key, "key"); |
140 | ||
141 | 0 | return (String) _parameters.get(key); |
142 | } | |
143 | ||
144 | /** | |
145 | * @param key | |
146 | * the name of the content type parameter | |
147 | * @param value | |
148 | * the value of the content type parameter | |
149 | */ | |
150 | public void setParameter(String key, String value) | |
151 | { | |
152 | 0 | Defense.notNull(key, "key"); |
153 | 0 | Defense.notNull(value, "value"); |
154 | ||
155 | 0 | _parameters.put(key.toLowerCase(), value); |
156 | 0 | } |
157 | ||
158 | /** | |
159 | * Parses the argument and configures the content type accordingly. The format of the argument | |
160 | * has to be type/subtype(;key=value)* | |
161 | * | |
162 | * @param contentType | |
163 | * the content type that needs to be represented | |
164 | */ | |
165 | public void parse(String contentType) | |
166 | { | |
167 | 0 | _baseType = ""; |
168 | 0 | _subType = ""; |
169 | 0 | _parameters.clear(); |
170 | ||
171 | 0 | StringTokenizer tokens = new StringTokenizer(contentType, ";"); |
172 | 0 | if (!tokens.hasMoreTokens()) |
173 | 0 | return; |
174 | ||
175 | 0 | String mimeType = tokens.nextToken(); |
176 | 0 | StringTokenizer mimeTokens = new StringTokenizer(mimeType, "/"); |
177 | 0 | setBaseType(mimeTokens.hasMoreTokens() ? mimeTokens.nextToken() : ""); |
178 | 0 | setSubType(mimeTokens.hasMoreTokens() ? mimeTokens.nextToken() : ""); |
179 | ||
180 | 0 | while (tokens.hasMoreTokens()) |
181 | { | |
182 | 0 | String parameter = tokens.nextToken(); |
183 | ||
184 | 0 | StringTokenizer parameterTokens = new StringTokenizer(parameter, "="); |
185 | 0 | String key = parameterTokens.hasMoreTokens() ? parameterTokens.nextToken() : ""; |
186 | 0 | String value = parameterTokens.hasMoreTokens() ? parameterTokens.nextToken() : ""; |
187 | 0 | setParameter(key, value); |
188 | 0 | } |
189 | 0 | } |
190 | ||
191 | /** | |
192 | * @return the string representation of this content type | |
193 | */ | |
194 | public String unparse() | |
195 | { | |
196 | 0 | StringBuffer buf = new StringBuffer(getMimeType()); |
197 | ||
198 | 0 | String[] parameterNames = getParameterNames(); |
199 | 0 | for (int i = 0; i < parameterNames.length; i++) |
200 | { | |
201 | 0 | String key = parameterNames[i]; |
202 | 0 | String value = getParameter(key); |
203 | 0 | buf.append(";").append(key).append("=").append(value); |
204 | } | |
205 | ||
206 | 0 | return buf.toString(); |
207 | } | |
208 | ||
209 | /** | |
210 | * @return the string representation of this content type. Same as unparse(). | |
211 | */ | |
212 | public String toString() | |
213 | { | |
214 | 0 | return unparse(); |
215 | } | |
216 | ||
217 | } |