libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / javax / activation / MimeType.java
blob70045d6686b3b2a0a6d99270a3dbc6e79866b6ef
1 /* MimeType.java -- A MIME type as defined in RFC2046 and RFC2047.
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax.activation;
40 import gnu.java.lang.CPStringBuilder;
42 import java.io.Externalizable;
43 import java.io.IOException;
44 import java.io.ObjectInput;
45 import java.io.ObjectOutput;
47 /**
48 * A MIME content type, as defined in RFCs 2045 and 2046.
50 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
51 * @version 1.1
53 public class MimeType
54 implements Externalizable
57 static final String TSPECIALS = "()<>@,;:/[]?=\\\"";
59 private String primaryType;
60 private String subType;
61 private MimeTypeParameterList parameters;
63 /**
64 * Constructor for an <code>application/*</code> content type.
66 public MimeType()
68 primaryType = "application";
69 subType = "*";
70 parameters = new MimeTypeParameterList();
73 /**
74 * Constructor that parses a raw String.
75 * @param rawdata the MIME type string
77 public MimeType(String rawdata)
78 throws MimeTypeParseException
80 parse(rawdata);
83 /**
84 * Constructor for a new MIME type with the given primary and sub types
85 * and an empty parameter list.
86 * @param primary the primary type
87 * @param sub the subtype
89 public MimeType(String primary, String sub)
90 throws MimeTypeParseException
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();
99 /**
100 * Returns the primary type.
102 public String getPrimaryType()
104 return primaryType;
108 * Sets the primary type.
109 * @param primary the new primary type
111 public void setPrimaryType(String primary)
112 throws MimeTypeParseException
114 checkValidity(primary, "Primary type is invalid");
115 primaryType = primary.toLowerCase();
119 * Returns the subtype.
121 public String getSubType()
123 return subType;
127 * Sets the subtype.
128 * @param sub the new subtype
130 public void setSubType(String sub)
131 throws MimeTypeParseException
133 checkValidity(sub, "Sub type is invalid");
134 subType = sub.toLowerCase();
138 * Returns the MIME parameters.
140 public MimeTypeParameterList getParameters()
142 return parameters;
146 * Returns the parameter value for the specified name.
147 * @param name the parameter name
149 public String getParameter(String name)
151 return parameters.get(name);
155 * Sets the parameter value for the specified name.
156 * @param name the parameter name
157 * @param value the new value
159 public void setParameter(String name, String value)
161 parameters.set(name, value);
165 * Removes the parameter value for the specified name.
166 * @param name the parameter name
168 public void removeParameter(String name)
170 parameters.remove(name);
174 * Returns the complete string representation of this MIME type.
176 public String toString()
178 return new CPStringBuilder(primaryType)
179 .append('/')
180 .append(subType)
181 .append(parameters.toString())
182 .toString();
186 * Returns the string representation of this MIME type without
187 * parameters.
189 public String getBaseType()
191 return new CPStringBuilder(primaryType)
192 .append('/')
193 .append(subType)
194 .toString();
198 * Returns true if the primary and subtype of this MIME type are the
199 * same as in the given MIME type.
201 public boolean match(MimeType type)
203 String primary2 = type.getPrimaryType();
204 String sub2 = type.getSubType();
205 return primaryType.equals(primary2) && (subType.equals(sub2) ||
206 "*".equals(subType) ||
207 "*".equals(sub2));
211 * Returns true if the primary and subtype of this MIME type are the
212 * same as in the given MIME type string.
214 public boolean match(String rawdata)
215 throws MimeTypeParseException
217 return match(new MimeType(rawdata));
220 public void writeExternal(ObjectOutput out)
221 throws IOException
223 out.writeUTF(toString());
224 out.flush();
227 public void readExternal(ObjectInput in)
228 throws IOException, ClassNotFoundException
232 parse(in.readUTF());
234 catch (MimeTypeParseException e)
236 throw new IOException(e.getMessage());
240 private void parse(String rawdata)
241 throws MimeTypeParseException
243 int si = rawdata.indexOf('/');
244 int pi = rawdata.indexOf(';');
245 if (si == -1)
247 throw new MimeTypeParseException("Unable to find a sub type.");
249 if (pi == -1)
251 primaryType = rawdata.substring(0, si).toLowerCase().trim();
252 subType = rawdata.substring(si + 1).toLowerCase().trim();
253 parameters = new MimeTypeParameterList();
255 else if (si < pi)
257 primaryType = rawdata.substring(0, si).toLowerCase().trim();
258 subType = rawdata.substring(si + 1, pi).toLowerCase().trim();
259 parameters = new MimeTypeParameterList(rawdata.substring(pi));
261 else
263 throw new MimeTypeParseException("Unable to find a sub type.");
265 checkValidity(primaryType, "Primary type is invalid");
266 checkValidity(subType, "Sub type is invalid");
269 static void checkValidity(String token, String message)
270 throws MimeTypeParseException
272 int len = token.length();
273 if (len == 0)
275 throw new MimeTypeParseException(message, token);
277 for (int i = 0; i < len; i++)
279 char c = token.charAt(i);
280 if (!isValidChar(c))
282 throw new MimeTypeParseException(message, token);
287 static boolean isValidChar(char c)
289 return c > ' ' && c <= '~' && TSPECIALS.indexOf(c) == -1;