1:
37:
38: package ;
39:
40: import ;
41:
42: import ;
43: import ;
44: import ;
45: import ;
46:
47:
53: public class MimeType
54: implements Externalizable
55: {
56:
57: static final String TSPECIALS = "()<>@,;:/[]?=\\\"";
58:
59: private String primaryType;
60: private String subType;
61: private MimeTypeParameterList parameters;
62:
63:
66: public MimeType()
67: {
68: primaryType = "application";
69: subType = "*";
70: parameters = new MimeTypeParameterList();
71: }
72:
73:
77: public MimeType(String rawdata)
78: throws MimeTypeParseException
79: {
80: parse(rawdata);
81: }
82:
83:
89: public MimeType(String primary, String sub)
90: throws MimeTypeParseException
91: {
92: checkValidity(primary, "Primary type is invalid");
93: checkValidity(sub, "Sub type is invalid");
94: primaryType = primary.toLowerCase();
95: subType = sub.toLowerCase();
96: parameters = new MimeTypeParameterList();
97: }
98:
99:
102: public String getPrimaryType()
103: {
104: return primaryType;
105: }
106:
107:
111: public void setPrimaryType(String primary)
112: throws MimeTypeParseException
113: {
114: checkValidity(primary, "Primary type is invalid");
115: primaryType = primary.toLowerCase();
116: }
117:
118:
121: public String getSubType()
122: {
123: return subType;
124: }
125:
126:
130: public void setSubType(String sub)
131: throws MimeTypeParseException
132: {
133: checkValidity(sub, "Sub type is invalid");
134: subType = sub.toLowerCase();
135: }
136:
137:
140: public MimeTypeParameterList getParameters()
141: {
142: return parameters;
143: }
144:
145:
149: public String getParameter(String name)
150: {
151: return parameters.get(name);
152: }
153:
154:
159: public void setParameter(String name, String value)
160: {
161: parameters.set(name, value);
162: }
163:
164:
168: public void removeParameter(String name)
169: {
170: parameters.remove(name);
171: }
172:
173:
176: public String toString()
177: {
178: return new CPStringBuilder(primaryType)
179: .append('/')
180: .append(subType)
181: .append(parameters.toString())
182: .toString();
183: }
184:
185:
189: public String getBaseType()
190: {
191: return new CPStringBuilder(primaryType)
192: .append('/')
193: .append(subType)
194: .toString();
195: }
196:
197:
201: public boolean match(MimeType type)
202: {
203: String primary2 = type.getPrimaryType();
204: String sub2 = type.getSubType();
205: return primaryType.equals(primary2) && (subType.equals(sub2) ||
206: "*".equals(subType) ||
207: "*".equals(sub2));
208: }
209:
210:
214: public boolean match(String rawdata)
215: throws MimeTypeParseException
216: {
217: return match(new MimeType(rawdata));
218: }
219:
220: public void writeExternal(ObjectOutput out)
221: throws IOException
222: {
223: out.writeUTF(toString());
224: out.flush();
225: }
226:
227: public void readExternal(ObjectInput in)
228: throws IOException, ClassNotFoundException
229: {
230: try
231: {
232: parse(in.readUTF());
233: }
234: catch (MimeTypeParseException e)
235: {
236: throw new IOException(e.getMessage());
237: }
238: }
239:
240: private void parse(String rawdata)
241: throws MimeTypeParseException
242: {
243: int si = rawdata.indexOf('/');
244: int pi = rawdata.indexOf(';');
245: if (si == -1)
246: {
247: throw new MimeTypeParseException("Unable to find a sub type.");
248: }
249: if (pi == -1)
250: {
251: primaryType = rawdata.substring(0, si).toLowerCase().trim();
252: subType = rawdata.substring(si + 1).toLowerCase().trim();
253: parameters = new MimeTypeParameterList();
254: }
255: else if (si < pi)
256: {
257: primaryType = rawdata.substring(0, si).toLowerCase().trim();
258: subType = rawdata.substring(si + 1, pi).toLowerCase().trim();
259: parameters = new MimeTypeParameterList(rawdata.substring(pi));
260: }
261: else
262: {
263: throw new MimeTypeParseException("Unable to find a sub type.");
264: }
265: checkValidity(primaryType, "Primary type is invalid");
266: checkValidity(subType, "Sub type is invalid");
267: }
268:
269: static void checkValidity(String token, String message)
270: throws MimeTypeParseException
271: {
272: int len = token.length();
273: if (len == 0)
274: {
275: throw new MimeTypeParseException(message, token);
276: }
277: for (int i = 0; i < len; i++)
278: {
279: char c = token.charAt(i);
280: if (!isValidChar(c))
281: {
282: throw new MimeTypeParseException(message, token);
283: }
284: }
285: }
286:
287: static boolean isValidChar(char c)
288: {
289: return c > ' ' && c <= '~' && TSPECIALS.indexOf(c) == -1;
290: }
291:
292: }