libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / javax / activation / MimeTypeParameterList.java
blobe15469da88ca7fff136443032329ffe94f3ec8d9
1 /* MimeTypeParameterList.java -- Handle a list of MIME type parameters.
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.util.ArrayList;
43 import java.util.Enumeration;
44 import java.util.HashMap;
45 import java.util.Iterator;
46 import java.util.List;
47 import java.util.Map;
49 /**
50 * A list of MIME type parameters, as specified in RFCs 2045 and 2046.
52 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
53 * @version 1.1
55 public class MimeTypeParameterList
58 private final List<String> parameterNames;
59 private final Map<String,String> parameterValues;
61 /**
62 * Constructor for an empty parameter list.
64 public MimeTypeParameterList()
66 parameterNames = new ArrayList<String>();
67 parameterValues = new HashMap<String,String>();
70 /**
71 * Constructor that parses the specified MIME parameter data.
72 * @param parameterList a MIME parameter list string representation
74 public MimeTypeParameterList(String parameterList)
75 throws MimeTypeParseException
77 this();
78 parse(parameterList);
81 /**
82 * Parses the specified MIME parameter data, storing the results in this
83 * object.
84 * @param parameterList a MIME parameter list string representation
86 protected void parse(String parameterList)
87 throws MimeTypeParseException
89 if (parameterList == null)
91 return;
93 // Tokenize list into parameters
94 char[] chars = parameterList.toCharArray();
95 int len = chars.length;
96 boolean inQuotedString = false;
97 CPStringBuilder buffer = new CPStringBuilder();
98 List<String> params = new ArrayList<String>();
99 for (int i = 0; i < len; i++)
101 char c = chars[i];
102 if (c == ';' && !inQuotedString)
104 String param = buffer.toString().trim();
105 if (param.length() > 0)
107 params.add(param);
109 buffer.setLength(0);
111 else
113 if (c == '"')
115 inQuotedString = !inQuotedString;
117 buffer.append(c);
120 String param = buffer.toString().trim();
121 if (param.length() > 0)
123 params.add(param);
126 // Tokenize each parameter into name + value
127 for (Iterator<String> i = params.iterator(); i.hasNext();)
129 param = i.next();
130 int ei = param.indexOf('=');
131 if (ei == -1)
133 throw new MimeTypeParseException("Couldn't find the '=' that " +
134 "separates a parameter name " +
135 "from its value.");
137 String name = param.substring(0, ei).trim();
138 MimeType.checkValidity(name, "Parameter name is invalid");
139 String value = param.substring(ei + 1).trim();
140 len = value.length();
141 if (len > 1 && value.charAt(0) == '"' &&
142 value.charAt(len - 1) == '"')
144 value = unquote(value);
146 else
148 MimeType.checkValidity(name, "Parameter value is invalid");
151 parameterNames.add(name);
152 parameterValues.put(name.toLowerCase(), value);
157 * Returns the number of parameters.
159 public synchronized int size()
161 return parameterNames.size();
165 * Indicates if there are no parameters.
167 public synchronized boolean isEmpty()
169 return parameterNames.isEmpty();
173 * Returns the value for the specified parameter name.
174 * @param name the parameter name
176 public synchronized String get(String name)
178 name = name.trim();
179 return parameterValues.get(name.toLowerCase());
183 * Sets the value for the specified parameter name.
184 * @param name the parameter name
185 * @param value the parameter value
187 public synchronized void set(String name, String value)
189 name = name.trim();
190 boolean exists = false;
191 for (String pname : parameterNames)
193 if (name.equalsIgnoreCase(pname))
195 exists = true;
198 if (!exists)
200 parameterNames.add(name);
202 parameterValues.put(name.toLowerCase(), value);
206 * Removes the parameter identified by the specified name.
207 * @param name the parameter name
209 public synchronized void remove(String name)
211 name = name.trim();
212 for (Iterator<String> i = parameterNames.iterator();i.hasNext();)
214 String pname = i.next();
215 if (name.equalsIgnoreCase(pname))
217 i.remove();
220 parameterValues.remove(name.toLowerCase());
224 * Returns an enumeration of all the parameter names.
226 // Raw type is forced by public spec.
227 @SuppressWarnings("unchecked")
228 public synchronized Enumeration getNames()
230 return new IteratorEnumeration(parameterNames.iterator());
234 * Returns an RFC 2045-compliant string representation of this parameter
235 * list.
237 public synchronized String toString()
239 CPStringBuilder buffer = new CPStringBuilder();
240 for (String name : parameterNames)
242 String value = parameterValues.get(name.toLowerCase());
244 buffer.append(';');
245 buffer.append(' ');
246 buffer.append(name);
247 buffer.append('=');
248 buffer.append(quote(value));
250 return buffer.toString();
253 private static String quote(String value)
255 boolean needsQuoting = false;
256 int len = value.length();
257 for (int i = 0; i < len; i++)
259 if (!MimeType.isValidChar(value.charAt(i)))
261 needsQuoting = true;
262 break;
266 if (needsQuoting)
268 CPStringBuilder buffer = new CPStringBuilder();
269 buffer.append('"');
270 for (int i = 0; i < len; i++)
272 char c = value.charAt(i);
273 if (c == '\\' || c == '"')
275 buffer.append('\\');
277 buffer.append(c);
279 buffer.append('"');
280 return buffer.toString();
282 return value;
285 private static String unquote(String value)
287 int len = value.length();
288 CPStringBuilder buffer = new CPStringBuilder();
289 for (int i = 1; i < len - 1; i++)
291 char c = value.charAt(i);
292 if (c == '\\')
294 i++;
295 if (i < len - 1)
297 c = value.charAt(i);
298 if (c != '\\' && c != '"')
300 buffer.append('\\');
304 buffer.append(c);
306 return buffer.toString();
310 * Enumeration proxy for an Iterator.
312 static class IteratorEnumeration
313 implements Enumeration<String>
316 final Iterator<String> iterator;
318 IteratorEnumeration(Iterator<String> iterator)
320 this.iterator = iterator;
323 public boolean hasMoreElements()
325 return iterator.hasNext();
328 public String nextElement()
330 return iterator.next();