OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [java/] [awt/] [datatransfer/] [MimeType.java] - Blame information for rev 771

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 771 jeremybenn
/* MimeType.java -- A helper class for mime handling in DataFlavor
2
   Copyright (C) 2006 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
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.
10
 
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.
15
 
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.
20
 
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.
25
 
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. */
37
 
38
 
39
package java.awt.datatransfer;
40
 
41
import gnu.java.lang.CPStringBuilder;
42
 
43
import java.io.Externalizable;
44
import java.io.IOException;
45
import java.io.ObjectInput;
46
import java.io.ObjectOutput;
47
import java.util.HashMap;
48
import java.util.Iterator;
49
import java.util.Map;
50
import java.util.NoSuchElementException;
51
import java.util.Set;
52
import java.util.StringTokenizer;
53
 
54
/**
55
 * A helper class for mime handling in DataFlavor.
56
 *
57
 * A Mauve test for DataFlavor.writeExternal() shows that a non-public
58
 * class java.awt.datatransfer.MimeType gets serialized. This class
59
 * is mainly here for serialization compatibility. Of course,
60
 * now that we have it here, we can just as well implement some
61
 * mime handling facility here.
62
 */
63
class MimeType
64
  implements Externalizable
65
{
66
 
67
  /**
68
   * The primary type.
69
   */
70
  private String primaryType;
71
 
72
  /**
73
   * The subtype.
74
   */
75
  private String subType;
76
 
77
  /**
78
   * Additional parameters to be appended to the mime string.
79
   */
80
  private HashMap parameters;
81
 
82
  /**
83
   * This is only here for deserialization.
84
   */
85
  public MimeType()
86
  {
87
    parameters = new HashMap();
88
  }
89
 
90
  /**
91
   * Creates a new MimeType object.
92
   *
93
   * @param mime the mime type
94
   */
95
  MimeType(String mime)
96
    throws MimeTypeParseException
97
  {
98
    this();
99
    parse(mime);
100
  }
101
 
102
  /**
103
   * Adds a mime parameter.
104
   *
105
   * @param param the parameter key
106
   * @param value the parameter value
107
   */
108
  void addParameter(String param, String value)
109
  {
110
    parameters.put(param, value);
111
  }
112
 
113
  /**
114
   * Removes the parameter with the specified key.
115
   *
116
   * @param param the parameter to remove
117
   */
118
  void removeParameter(String param)
119
  {
120
    parameters.remove(param);
121
  }
122
 
123
  /**
124
   * Returns the parameter for the <code>key</code>.
125
   *
126
   * @param key the parameter key
127
   *
128
   * @return the parameter for the <code>key</code>
129
   */
130
  String getParameter(String key)
131
  {
132
    return (String) parameters.get(key);
133
  }
134
 
135
  /**
136
   * Returns the primary type.
137
   *
138
   * @return the primary type
139
   */
140
  String getPrimaryType()
141
  {
142
    return primaryType;
143
  }
144
 
145
  String getSubType()
146
  {
147
    return subType;
148
  }
149
 
150
  /**
151
   * Returns the base type of this mime type. This is the primary
152
   * type plus the subtype, separated by '/'.
153
   *
154
   * @return the base type of this mime type
155
   */
156
  String getBaseType()
157
  {
158
    return primaryType + '/' + subType;
159
  }
160
 
161
  /**
162
   * Returns <code>true</code> if this mime type and another mime type
163
   * match. This will be true when their primary types are equal, and their
164
   * subtypes are equal (or when either subtype is * ).
165
   *
166
   * @param other the other mime type
167
   *
168
   * @return <code>true</code> if the mime types match, <code>false</code>
169
   *         otherwise
170
   */
171
  boolean matches(MimeType other)
172
  {
173
    boolean match = false;
174
    if (other != null)
175
      {
176
        match = primaryType.equals(other.primaryType)
177
                && (subType.equals("*") || other.subType.equals("*")
178
                    || subType.equals(other.subType));
179
      }
180
    return match;
181
  }
182
 
183
  /**
184
   * Serializes the mime type.
185
   *
186
   * @param in the input stream to read from
187
   *
188
   * @throws ClassNotFoundException not thrown here
189
   * @throws IOException when something goes wrong on the input stream,
190
   *         or when the mime type can't be parsed
191
   */
192
  public void readExternal(ObjectInput in)
193
    throws ClassNotFoundException, IOException
194
  {
195
    String mime = in.readUTF();
196
    parameters.clear();
197
    try
198
      {
199
        parse(mime);
200
      }
201
    catch (MimeTypeParseException ex)
202
      {
203
        IOException ioEx = new IOException();
204
        ioEx.initCause(ex);
205
        throw ioEx;
206
      }
207
  }
208
 
209
  /**
210
   * Serializes this mime type.
211
   *
212
   * @param out the output stream
213
   *
214
   * @throws IOException when something goes wrong on the output stream
215
   */
216
  public void writeExternal(ObjectOutput out)
217
    throws IOException
218
  {
219
    out.writeUTF(toString());
220
  }
221
 
222
  /**
223
   * Creates a string representation of this mime type.
224
   *
225
   * @return a string representation of this mime type
226
   */
227
  public String toString()
228
  {
229
    CPStringBuilder s = new CPStringBuilder();
230
    s.append(primaryType);
231
    s.append('/');
232
    s.append(subType);
233
    if (parameters.size() > 0)
234
      {
235
        Set entries = parameters.entrySet();
236
        for (Iterator i = entries.iterator(); i.hasNext();)
237
          {
238
            s.append("; ");
239
            Map.Entry entry = (Map.Entry) i.next();
240
            s.append(entry.getKey());
241
            s.append('=');
242
            s.append(entry.getValue());
243
          }
244
      }
245
    return s.toString();
246
  }
247
 
248
  /**
249
   * Parses the specified mime type string and initializes the fields
250
   * of this object.
251
   *
252
   * @param mime the mime type string
253
   */
254
  private void parse(String mime)
255
    throws MimeTypeParseException
256
  {
257
    // FIXME: Maybe implement more sophisticated mime string parsing according
258
    // to RFC 2045 and 2046.
259
    StringTokenizer tokenizer = new StringTokenizer(mime);
260
    try
261
      {
262
        primaryType = tokenizer.nextToken("/");
263
        subType = tokenizer.nextToken("/;");
264
      }
265
    catch (NoSuchElementException ex)
266
      {
267
        throw new MimeTypeParseException("Expected / separator");
268
      }
269
 
270
    // Add any parameters.
271
    while (tokenizer.hasMoreTokens())
272
      {
273
        String keyValuePair = tokenizer.nextToken(";");
274
        int i = keyValuePair.indexOf('=');
275
        if (i == -1)
276
          throw new MimeTypeParseException("Expected = as parameter separator");
277
        String key = keyValuePair.substring(0, i).trim();
278
        String value = keyValuePair.substring(i + 1).trim();
279
        parameters.put(key, value);
280
      }
281
  }
282
 
283
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.