summaryrefslogtreecommitdiffstats
path: root/libjava/classpath/javax/activation/MimeTypeParameterList.java
blob: 3d36ede948b834e68bd9da2db3dd8093937d421a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/* MimeTypeParameterList.java -- Handle a list of MIME type parameters.
   Copyright (C) 2004 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

package javax.activation;

import gnu.java.lang.CPStringBuilder;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * A list of MIME type parameters, as specified in RFCs 2045 and 2046.
 *
 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
 * @version 1.1
 */
public class MimeTypeParameterList
{

  private final List<String> parameterNames;
  private final Map<String,String> parameterValues;

  /**
   * Constructor for an empty parameter list.
   */
  public MimeTypeParameterList()
  {
    parameterNames = new ArrayList<String>();
    parameterValues = new HashMap<String,String>();
  }

  /**
   * Constructor that parses the specified MIME parameter data.
   * @param parameterList a MIME parameter list string representation
   */
  public MimeTypeParameterList(String parameterList)
    throws MimeTypeParseException
  {
    this();
    parse(parameterList);
  }

  /**
   * Parses the specified MIME parameter data, storing the results in this
   * object.
   * @param parameterList a MIME parameter list string representation
   */
  protected void parse(String parameterList)
    throws MimeTypeParseException
  {
    if (parameterList == null)
      {
        return;
      }
    // Tokenize list into parameters
    char[] chars = parameterList.toCharArray();
    int len = chars.length;
    boolean inQuotedString = false;
    CPStringBuilder buffer = new CPStringBuilder();
    List<String> params = new ArrayList<String>();
    for (int i = 0; i < len; i++)
      {
        char c = chars[i];
        if (c == ';' && !inQuotedString)
          {
            String param = buffer.toString().trim();
            if (param.length() > 0)
              {
                params.add(param);
              }
            buffer.setLength(0);
          }
        else
          {
            if (c == '"')
              {
                inQuotedString = !inQuotedString;
              }
            buffer.append(c);
          }
      }
    String param = buffer.toString().trim();
    if (param.length() > 0)
      {
        params.add(param);
      }

    // Tokenize each parameter into name + value
    for (Iterator<String> i = params.iterator(); i.hasNext();)
      {
        param = i.next();
        int ei = param.indexOf('=');
        if (ei == -1)
          {
            throw new MimeTypeParseException("Couldn't find the '=' that " +
                                             "separates a parameter name " +
                                             "from its value.");
          }
        String name = param.substring(0, ei).trim();
        MimeType.checkValidity(name, "Parameter name is invalid");
        String value = param.substring(ei + 1).trim();
        len = value.length();
        if (len > 1 && value.charAt(0) == '"' &&
            value.charAt(len - 1) == '"')
          {
            value = unquote(value);
          }
        else
          {
            MimeType.checkValidity(name, "Parameter value is invalid");
          }

        parameterNames.add(name);
        parameterValues.put(name.toLowerCase(), value);
      }
  }

  /**
   * Returns the number of parameters.
   */
  public synchronized int size()
  {
    return parameterNames.size();
  }

  /**
   * Indicates if there are no parameters.
   */
  public synchronized boolean isEmpty()
  {
    return parameterNames.isEmpty();
  }

  /**
   * Returns the value for the specified parameter name.
   * @param name the parameter name
   */
  public synchronized String get(String name)
  {
    name = name.trim();
    return parameterValues.get(name.toLowerCase());
  }

  /**
   * Sets the value for the specified parameter name.
   * @param name the parameter name
   * @param value the parameter value
   */
  public synchronized void set(String name, String value)
  {
    name = name.trim();
    boolean exists = false;
    for (String pname : parameterNames)
      {
        if (name.equalsIgnoreCase(pname))
          {
            exists = true;
          }
      }
    if (!exists)
      {
        parameterNames.add(name);
      }
    parameterValues.put(name.toLowerCase(), value);
  }

  /**
   * Removes the parameter identified by the specified name.
   * @param name the parameter name
   */
  public synchronized void remove(String name)
  {
    name = name.trim();
    for (Iterator<String> i = parameterNames.iterator();i.hasNext();)
      {
        String pname = i.next();
        if (name.equalsIgnoreCase(pname))
          {
            i.remove();
          }
      }
    parameterValues.remove(name.toLowerCase());
  }

  /**
   * Returns an enumeration of all the parameter names.
   */
  // Raw type is forced by public spec.
  @SuppressWarnings("unchecked")
  public synchronized Enumeration getNames()
  {
    return new IteratorEnumeration(parameterNames.iterator());
  }

  /**
   * Returns an RFC 2045-compliant string representation of this parameter
   * list.
   */
  public synchronized String toString()
  {
    CPStringBuilder buffer = new CPStringBuilder();
    for (String name : parameterNames)
      {
        String value = parameterValues.get(name.toLowerCase());

        buffer.append(';');
        buffer.append(' ');
        buffer.append(name);
        buffer.append('=');
        buffer.append(quote(value));
      }
    return buffer.toString();
  }

  private static String quote(String value)
  {
    boolean needsQuoting = false;
    int len = value.length();
    for (int i = 0; i < len; i++)
      {
        if (!MimeType.isValidChar(value.charAt(i)))
          {
            needsQuoting = true;
            break;
          }
      }

    if (needsQuoting)
      {
        CPStringBuilder buffer = new CPStringBuilder();
        buffer.append('"');
        for (int i = 0; i < len; i++)
          {
            char c = value.charAt(i);
            if (c == '\\' || c == '"')
              {
                buffer.append('\\');
              }
            buffer.append(c);
          }
        buffer.append('"');
        return buffer.toString();
      }
    return value;
  }

  private static String unquote(String value)
  {
    int len = value.length();
    CPStringBuilder buffer = new CPStringBuilder();
    for (int i = 1; i < len - 1; i++)
      {
        char c = value.charAt(i);
        if (c == '\\')
          {
            i++;
            if (i < len - 1)
              {
                c = value.charAt(i);
                if (c != '\\' && c != '"')
                  {
                    buffer.append('\\');
                  }
              }
          }
        buffer.append(c);
      }
    return buffer.toString();
  }

  /**
   * Enumeration proxy for an Iterator.
   */
  static class IteratorEnumeration
    implements Enumeration<String>
  {

    final Iterator<String> iterator;

    IteratorEnumeration(Iterator<String> iterator)
    {
      this.iterator = iterator;
    }

    public boolean hasMoreElements()
    {
      return iterator.hasNext();
    }

    public String nextElement()
    {
      return iterator.next();
    }

  }

}
OpenPOWER on IntegriCloud