src/share/jaf_classes/javax/activation/MimeType.java

Wed, 27 Apr 2016 01:27:09 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:27:09 +0800
changeset 0
373ffda63c9a
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package javax.activation;
aoqi@0 27
aoqi@0 28 import java.io.*;
aoqi@0 29 import java.util.Locale;
aoqi@0 30
aoqi@0 31 /**
aoqi@0 32 * A Multipurpose Internet Mail Extension (MIME) type, as defined
aoqi@0 33 * in RFC 2045 and 2046.
aoqi@0 34 *
aoqi@0 35 * @since 1.6
aoqi@0 36 */
aoqi@0 37 public class MimeType implements Externalizable {
aoqi@0 38
aoqi@0 39 private String primaryType;
aoqi@0 40 private String subType;
aoqi@0 41 private MimeTypeParameterList parameters;
aoqi@0 42
aoqi@0 43 /**
aoqi@0 44 * A string that holds all the special chars.
aoqi@0 45 */
aoqi@0 46 private static final String TSPECIALS = "()<>@,;:/[]?=\\\"";
aoqi@0 47
aoqi@0 48 /**
aoqi@0 49 * Default constructor.
aoqi@0 50 */
aoqi@0 51 public MimeType() {
aoqi@0 52 primaryType = "application";
aoqi@0 53 subType = "*";
aoqi@0 54 parameters = new MimeTypeParameterList();
aoqi@0 55 }
aoqi@0 56
aoqi@0 57 /**
aoqi@0 58 * Constructor that builds a MimeType from a String.
aoqi@0 59 *
aoqi@0 60 * @param rawdata the MIME type string
aoqi@0 61 */
aoqi@0 62 public MimeType(String rawdata) throws MimeTypeParseException {
aoqi@0 63 parse(rawdata);
aoqi@0 64 }
aoqi@0 65
aoqi@0 66 /**
aoqi@0 67 * Constructor that builds a MimeType with the given primary and sub type
aoqi@0 68 * but has an empty parameter list.
aoqi@0 69 *
aoqi@0 70 * @param primary the primary MIME type
aoqi@0 71 * @param sub the MIME sub-type
aoqi@0 72 * @exception MimeTypeParseException if the primary type or subtype
aoqi@0 73 * is not a valid token
aoqi@0 74 */
aoqi@0 75 public MimeType(String primary, String sub) throws MimeTypeParseException {
aoqi@0 76 // check to see if primary is valid
aoqi@0 77 if (isValidToken(primary)) {
aoqi@0 78 primaryType = primary.toLowerCase(Locale.ENGLISH);
aoqi@0 79 } else {
aoqi@0 80 throw new MimeTypeParseException("Primary type is invalid.");
aoqi@0 81 }
aoqi@0 82
aoqi@0 83 // check to see if sub is valid
aoqi@0 84 if (isValidToken(sub)) {
aoqi@0 85 subType = sub.toLowerCase(Locale.ENGLISH);
aoqi@0 86 } else {
aoqi@0 87 throw new MimeTypeParseException("Sub type is invalid.");
aoqi@0 88 }
aoqi@0 89
aoqi@0 90 parameters = new MimeTypeParameterList();
aoqi@0 91 }
aoqi@0 92
aoqi@0 93 /**
aoqi@0 94 * A routine for parsing the MIME type out of a String.
aoqi@0 95 */
aoqi@0 96 private void parse(String rawdata) throws MimeTypeParseException {
aoqi@0 97 int slashIndex = rawdata.indexOf('/');
aoqi@0 98 int semIndex = rawdata.indexOf(';');
aoqi@0 99 if ((slashIndex < 0) && (semIndex < 0)) {
aoqi@0 100 // neither character is present, so treat it
aoqi@0 101 // as an error
aoqi@0 102 throw new MimeTypeParseException("Unable to find a sub type.");
aoqi@0 103 } else if ((slashIndex < 0) && (semIndex >= 0)) {
aoqi@0 104 // we have a ';' (and therefore a parameter list),
aoqi@0 105 // but no '/' indicating a sub type is present
aoqi@0 106 throw new MimeTypeParseException("Unable to find a sub type.");
aoqi@0 107 } else if ((slashIndex >= 0) && (semIndex < 0)) {
aoqi@0 108 // we have a primary and sub type but no parameter list
aoqi@0 109 primaryType = rawdata.substring(0, slashIndex).trim().
aoqi@0 110 toLowerCase(Locale.ENGLISH);
aoqi@0 111 subType = rawdata.substring(slashIndex + 1).trim().
aoqi@0 112 toLowerCase(Locale.ENGLISH);
aoqi@0 113 parameters = new MimeTypeParameterList();
aoqi@0 114 } else if (slashIndex < semIndex) {
aoqi@0 115 // we have all three items in the proper sequence
aoqi@0 116 primaryType = rawdata.substring(0, slashIndex).trim().
aoqi@0 117 toLowerCase(Locale.ENGLISH);
aoqi@0 118 subType = rawdata.substring(slashIndex + 1, semIndex).trim().
aoqi@0 119 toLowerCase(Locale.ENGLISH);
aoqi@0 120 parameters = new MimeTypeParameterList(rawdata.substring(semIndex));
aoqi@0 121 } else {
aoqi@0 122 // we have a ';' lexically before a '/' which means we
aoqi@0 123 // have a primary type and a parameter list but no sub type
aoqi@0 124 throw new MimeTypeParseException("Unable to find a sub type.");
aoqi@0 125 }
aoqi@0 126
aoqi@0 127 // now validate the primary and sub types
aoqi@0 128
aoqi@0 129 // check to see if primary is valid
aoqi@0 130 if (!isValidToken(primaryType))
aoqi@0 131 throw new MimeTypeParseException("Primary type is invalid.");
aoqi@0 132
aoqi@0 133 // check to see if sub is valid
aoqi@0 134 if (!isValidToken(subType))
aoqi@0 135 throw new MimeTypeParseException("Sub type is invalid.");
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 /**
aoqi@0 139 * Retrieve the primary type of this object.
aoqi@0 140 *
aoqi@0 141 * @return the primary MIME type
aoqi@0 142 */
aoqi@0 143 public String getPrimaryType() {
aoqi@0 144 return primaryType;
aoqi@0 145 }
aoqi@0 146
aoqi@0 147 /**
aoqi@0 148 * Set the primary type for this object to the given String.
aoqi@0 149 *
aoqi@0 150 * @param primary the primary MIME type
aoqi@0 151 * @exception MimeTypeParseException if the primary type
aoqi@0 152 * is not a valid token
aoqi@0 153 */
aoqi@0 154 public void setPrimaryType(String primary) throws MimeTypeParseException {
aoqi@0 155 // check to see if primary is valid
aoqi@0 156 if (!isValidToken(primaryType))
aoqi@0 157 throw new MimeTypeParseException("Primary type is invalid.");
aoqi@0 158 primaryType = primary.toLowerCase(Locale.ENGLISH);
aoqi@0 159 }
aoqi@0 160
aoqi@0 161 /**
aoqi@0 162 * Retrieve the subtype of this object.
aoqi@0 163 *
aoqi@0 164 * @return the MIME subtype
aoqi@0 165 */
aoqi@0 166 public String getSubType() {
aoqi@0 167 return subType;
aoqi@0 168 }
aoqi@0 169
aoqi@0 170 /**
aoqi@0 171 * Set the subtype for this object to the given String.
aoqi@0 172 *
aoqi@0 173 * @param sub the MIME subtype
aoqi@0 174 * @exception MimeTypeParseException if the subtype
aoqi@0 175 * is not a valid token
aoqi@0 176 */
aoqi@0 177 public void setSubType(String sub) throws MimeTypeParseException {
aoqi@0 178 // check to see if sub is valid
aoqi@0 179 if (!isValidToken(subType))
aoqi@0 180 throw new MimeTypeParseException("Sub type is invalid.");
aoqi@0 181 subType = sub.toLowerCase(Locale.ENGLISH);
aoqi@0 182 }
aoqi@0 183
aoqi@0 184 /**
aoqi@0 185 * Retrieve this object's parameter list.
aoqi@0 186 *
aoqi@0 187 * @return a MimeTypeParameterList object representing the parameters
aoqi@0 188 */
aoqi@0 189 public MimeTypeParameterList getParameters() {
aoqi@0 190 return parameters;
aoqi@0 191 }
aoqi@0 192
aoqi@0 193 /**
aoqi@0 194 * Retrieve the value associated with the given name, or null if there
aoqi@0 195 * is no current association.
aoqi@0 196 *
aoqi@0 197 * @param name the parameter name
aoqi@0 198 * @return the paramter's value
aoqi@0 199 */
aoqi@0 200 public String getParameter(String name) {
aoqi@0 201 return parameters.get(name);
aoqi@0 202 }
aoqi@0 203
aoqi@0 204 /**
aoqi@0 205 * Set the value to be associated with the given name, replacing
aoqi@0 206 * any previous association.
aoqi@0 207 *
aoqi@0 208 * @param name the parameter name
aoqi@0 209 * @param value the paramter's value
aoqi@0 210 */
aoqi@0 211 public void setParameter(String name, String value) {
aoqi@0 212 parameters.set(name, value);
aoqi@0 213 }
aoqi@0 214
aoqi@0 215 /**
aoqi@0 216 * Remove any value associated with the given name.
aoqi@0 217 *
aoqi@0 218 * @param name the parameter name
aoqi@0 219 */
aoqi@0 220 public void removeParameter(String name) {
aoqi@0 221 parameters.remove(name);
aoqi@0 222 }
aoqi@0 223
aoqi@0 224 /**
aoqi@0 225 * Return the String representation of this object.
aoqi@0 226 */
aoqi@0 227 public String toString() {
aoqi@0 228 return getBaseType() + parameters.toString();
aoqi@0 229 }
aoqi@0 230
aoqi@0 231 /**
aoqi@0 232 * Return a String representation of this object
aoqi@0 233 * without the parameter list.
aoqi@0 234 *
aoqi@0 235 * @return the MIME type and sub-type
aoqi@0 236 */
aoqi@0 237 public String getBaseType() {
aoqi@0 238 return primaryType + "/" + subType;
aoqi@0 239 }
aoqi@0 240
aoqi@0 241 /**
aoqi@0 242 * Determine if the primary and sub type of this object is
aoqi@0 243 * the same as what is in the given type.
aoqi@0 244 *
aoqi@0 245 * @param type the MimeType object to compare with
aoqi@0 246 * @return true if they match
aoqi@0 247 */
aoqi@0 248 public boolean match(MimeType type) {
aoqi@0 249 return primaryType.equals(type.getPrimaryType())
aoqi@0 250 && (subType.equals("*")
aoqi@0 251 || type.getSubType().equals("*")
aoqi@0 252 || (subType.equals(type.getSubType())));
aoqi@0 253 }
aoqi@0 254
aoqi@0 255 /**
aoqi@0 256 * Determine if the primary and sub type of this object is
aoqi@0 257 * the same as the content type described in rawdata.
aoqi@0 258 *
aoqi@0 259 * @param rawdata the MIME type string to compare with
aoqi@0 260 * @return true if they match
aoqi@0 261 */
aoqi@0 262 public boolean match(String rawdata) throws MimeTypeParseException {
aoqi@0 263 return match(new MimeType(rawdata));
aoqi@0 264 }
aoqi@0 265
aoqi@0 266 /**
aoqi@0 267 * The object implements the writeExternal method to save its contents
aoqi@0 268 * by calling the methods of DataOutput for its primitive values or
aoqi@0 269 * calling the writeObject method of ObjectOutput for objects, strings
aoqi@0 270 * and arrays.
aoqi@0 271 *
aoqi@0 272 * @param out the ObjectOutput object to write to
aoqi@0 273 * @exception IOException Includes any I/O exceptions that may occur
aoqi@0 274 */
aoqi@0 275 public void writeExternal(ObjectOutput out) throws IOException {
aoqi@0 276 out.writeUTF(toString());
aoqi@0 277 out.flush();
aoqi@0 278 }
aoqi@0 279
aoqi@0 280 /**
aoqi@0 281 * The object implements the readExternal method to restore its
aoqi@0 282 * contents by calling the methods of DataInput for primitive
aoqi@0 283 * types and readObject for objects, strings and arrays. The
aoqi@0 284 * readExternal method must read the values in the same sequence
aoqi@0 285 * and with the same types as were written by writeExternal.
aoqi@0 286 *
aoqi@0 287 * @param in the ObjectInput object to read from
aoqi@0 288 * @exception ClassNotFoundException If the class for an object being
aoqi@0 289 * restored cannot be found.
aoqi@0 290 */
aoqi@0 291 public void readExternal(ObjectInput in)
aoqi@0 292 throws IOException, ClassNotFoundException {
aoqi@0 293 try {
aoqi@0 294 parse(in.readUTF());
aoqi@0 295 } catch (MimeTypeParseException e) {
aoqi@0 296 throw new IOException(e.toString());
aoqi@0 297 }
aoqi@0 298 }
aoqi@0 299
aoqi@0 300 // below here be scary parsing related things
aoqi@0 301
aoqi@0 302 /**
aoqi@0 303 * Determine whether or not a given character belongs to a legal token.
aoqi@0 304 */
aoqi@0 305 private static boolean isTokenChar(char c) {
aoqi@0 306 return ((c > 040) && (c < 0177)) && (TSPECIALS.indexOf(c) < 0);
aoqi@0 307 }
aoqi@0 308
aoqi@0 309 /**
aoqi@0 310 * Determine whether or not a given string is a legal token.
aoqi@0 311 */
aoqi@0 312 private boolean isValidToken(String s) {
aoqi@0 313 int len = s.length();
aoqi@0 314 if (len > 0) {
aoqi@0 315 for (int i = 0; i < len; ++i) {
aoqi@0 316 char c = s.charAt(i);
aoqi@0 317 if (!isTokenChar(c)) {
aoqi@0 318 return false;
aoqi@0 319 }
aoqi@0 320 }
aoqi@0 321 return true;
aoqi@0 322 } else {
aoqi@0 323 return false;
aoqi@0 324 }
aoqi@0 325 }
aoqi@0 326
aoqi@0 327 /**
aoqi@0 328 * A simple parser test,
aoqi@0 329 * for debugging...
aoqi@0 330 *
aoqi@0 331 public static void main(String[] args)
aoqi@0 332 throws MimeTypeParseException, IOException {
aoqi@0 333 for (int i = 0; i < args.length; ++i) {
aoqi@0 334 System.out.println("Original: " + args[i]);
aoqi@0 335
aoqi@0 336 MimeType type = new MimeType(args[i]);
aoqi@0 337
aoqi@0 338 System.out.println("Short: " + type.getBaseType());
aoqi@0 339 System.out.println("Parsed: " + type.toString());
aoqi@0 340 System.out.println();
aoqi@0 341 }
aoqi@0 342 }
aoqi@0 343 */
aoqi@0 344 }

mercurial