325N/A/*
325N/A * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage javax.activation;
325N/A
325N/Aimport java.io.*;
325N/Aimport java.util.Locale;
325N/A
325N/A/**
325N/A * A Multipurpose Internet Mail Extension (MIME) type, as defined
325N/A * in RFC 2045 and 2046.
325N/A *
325N/A * @since 1.6
325N/A */
325N/Apublic class MimeType implements Externalizable {
325N/A
325N/A private String primaryType;
325N/A private String subType;
325N/A private MimeTypeParameterList parameters;
325N/A
325N/A /**
325N/A * A string that holds all the special chars.
325N/A */
325N/A private static final String TSPECIALS = "()<>@,;:/[]?=\\\"";
325N/A
325N/A /**
325N/A * Default constructor.
325N/A */
325N/A public MimeType() {
325N/A primaryType = "application";
325N/A subType = "*";
325N/A parameters = new MimeTypeParameterList();
325N/A }
325N/A
325N/A /**
325N/A * Constructor that builds a MimeType from a String.
325N/A *
325N/A * @param rawdata the MIME type string
325N/A */
325N/A public MimeType(String rawdata) throws MimeTypeParseException {
325N/A parse(rawdata);
325N/A }
325N/A
325N/A /**
325N/A * Constructor that builds a MimeType with the given primary and sub type
325N/A * but has an empty parameter list.
325N/A *
325N/A * @param primary the primary MIME type
325N/A * @param sub the MIME sub-type
325N/A * @exception MimeTypeParseException if the primary type or subtype
325N/A * is not a valid token
325N/A */
325N/A public MimeType(String primary, String sub) throws MimeTypeParseException {
325N/A // check to see if primary is valid
325N/A if (isValidToken(primary)) {
325N/A primaryType = primary.toLowerCase(Locale.ENGLISH);
325N/A } else {
325N/A throw new MimeTypeParseException("Primary type is invalid.");
325N/A }
325N/A
325N/A // check to see if sub is valid
325N/A if (isValidToken(sub)) {
325N/A subType = sub.toLowerCase(Locale.ENGLISH);
325N/A } else {
325N/A throw new MimeTypeParseException("Sub type is invalid.");
325N/A }
325N/A
325N/A parameters = new MimeTypeParameterList();
325N/A }
325N/A
325N/A /**
325N/A * A routine for parsing the MIME type out of a String.
325N/A */
325N/A private void parse(String rawdata) throws MimeTypeParseException {
325N/A int slashIndex = rawdata.indexOf('/');
325N/A int semIndex = rawdata.indexOf(';');
325N/A if ((slashIndex < 0) && (semIndex < 0)) {
325N/A // neither character is present, so treat it
325N/A // as an error
325N/A throw new MimeTypeParseException("Unable to find a sub type.");
325N/A } else if ((slashIndex < 0) && (semIndex >= 0)) {
325N/A // we have a ';' (and therefore a parameter list),
325N/A // but no '/' indicating a sub type is present
325N/A throw new MimeTypeParseException("Unable to find a sub type.");
325N/A } else if ((slashIndex >= 0) && (semIndex < 0)) {
325N/A // we have a primary and sub type but no parameter list
325N/A primaryType = rawdata.substring(0, slashIndex).trim().
325N/A toLowerCase(Locale.ENGLISH);
325N/A subType = rawdata.substring(slashIndex + 1).trim().
325N/A toLowerCase(Locale.ENGLISH);
325N/A parameters = new MimeTypeParameterList();
325N/A } else if (slashIndex < semIndex) {
325N/A // we have all three items in the proper sequence
325N/A primaryType = rawdata.substring(0, slashIndex).trim().
325N/A toLowerCase(Locale.ENGLISH);
325N/A subType = rawdata.substring(slashIndex + 1, semIndex).trim().
325N/A toLowerCase(Locale.ENGLISH);
325N/A parameters = new MimeTypeParameterList(rawdata.substring(semIndex));
325N/A } else {
325N/A // we have a ';' lexically before a '/' which means we
325N/A // have a primary type and a parameter list but no sub type
325N/A throw new MimeTypeParseException("Unable to find a sub type.");
325N/A }
325N/A
325N/A // now validate the primary and sub types
325N/A
325N/A // check to see if primary is valid
325N/A if (!isValidToken(primaryType))
325N/A throw new MimeTypeParseException("Primary type is invalid.");
325N/A
325N/A // check to see if sub is valid
325N/A if (!isValidToken(subType))
325N/A throw new MimeTypeParseException("Sub type is invalid.");
325N/A }
325N/A
325N/A /**
325N/A * Retrieve the primary type of this object.
325N/A *
325N/A * @return the primary MIME type
325N/A */
325N/A public String getPrimaryType() {
325N/A return primaryType;
325N/A }
325N/A
325N/A /**
325N/A * Set the primary type for this object to the given String.
325N/A *
325N/A * @param primary the primary MIME type
325N/A * @exception MimeTypeParseException if the primary type
325N/A * is not a valid token
325N/A */
325N/A public void setPrimaryType(String primary) throws MimeTypeParseException {
325N/A // check to see if primary is valid
325N/A if (!isValidToken(primaryType))
325N/A throw new MimeTypeParseException("Primary type is invalid.");
325N/A primaryType = primary.toLowerCase(Locale.ENGLISH);
325N/A }
325N/A
325N/A /**
325N/A * Retrieve the subtype of this object.
325N/A *
325N/A * @return the MIME subtype
325N/A */
325N/A public String getSubType() {
325N/A return subType;
325N/A }
325N/A
325N/A /**
325N/A * Set the subtype for this object to the given String.
325N/A *
325N/A * @param sub the MIME subtype
325N/A * @exception MimeTypeParseException if the subtype
325N/A * is not a valid token
325N/A */
325N/A public void setSubType(String sub) throws MimeTypeParseException {
325N/A // check to see if sub is valid
325N/A if (!isValidToken(subType))
325N/A throw new MimeTypeParseException("Sub type is invalid.");
325N/A subType = sub.toLowerCase(Locale.ENGLISH);
325N/A }
325N/A
325N/A /**
325N/A * Retrieve this object's parameter list.
325N/A *
325N/A * @return a MimeTypeParameterList object representing the parameters
325N/A */
325N/A public MimeTypeParameterList getParameters() {
325N/A return parameters;
325N/A }
325N/A
325N/A /**
325N/A * Retrieve the value associated with the given name, or null if there
325N/A * is no current association.
325N/A *
325N/A * @param name the parameter name
325N/A * @return the paramter's value
325N/A */
325N/A public String getParameter(String name) {
325N/A return parameters.get(name);
325N/A }
325N/A
325N/A /**
325N/A * Set the value to be associated with the given name, replacing
325N/A * any previous association.
325N/A *
325N/A * @param name the parameter name
325N/A * @param value the paramter's value
325N/A */
325N/A public void setParameter(String name, String value) {
325N/A parameters.set(name, value);
325N/A }
325N/A
325N/A /**
325N/A * Remove any value associated with the given name.
325N/A *
325N/A * @param name the parameter name
325N/A */
325N/A public void removeParameter(String name) {
325N/A parameters.remove(name);
325N/A }
325N/A
325N/A /**
325N/A * Return the String representation of this object.
325N/A */
325N/A public String toString() {
325N/A return getBaseType() + parameters.toString();
325N/A }
325N/A
325N/A /**
325N/A * Return a String representation of this object
325N/A * without the parameter list.
325N/A *
325N/A * @return the MIME type and sub-type
325N/A */
325N/A public String getBaseType() {
325N/A return primaryType + "/" + subType;
325N/A }
325N/A
325N/A /**
325N/A * Determine if the primary and sub type of this object is
325N/A * the same as what is in the given type.
325N/A *
325N/A * @param type the MimeType object to compare with
325N/A * @return true if they match
325N/A */
325N/A public boolean match(MimeType type) {
325N/A return primaryType.equals(type.getPrimaryType())
325N/A && (subType.equals("*")
325N/A || type.getSubType().equals("*")
325N/A || (subType.equals(type.getSubType())));
325N/A }
325N/A
325N/A /**
325N/A * Determine if the primary and sub type of this object is
325N/A * the same as the content type described in rawdata.
325N/A *
325N/A * @param rawdata the MIME type string to compare with
325N/A * @return true if they match
325N/A */
325N/A public boolean match(String rawdata) throws MimeTypeParseException {
325N/A return match(new MimeType(rawdata));
325N/A }
325N/A
325N/A /**
325N/A * The object implements the writeExternal method to save its contents
325N/A * by calling the methods of DataOutput for its primitive values or
325N/A * calling the writeObject method of ObjectOutput for objects, strings
325N/A * and arrays.
325N/A *
325N/A * @param out the ObjectOutput object to write to
325N/A * @exception IOException Includes any I/O exceptions that may occur
325N/A */
325N/A public void writeExternal(ObjectOutput out) throws IOException {
325N/A out.writeUTF(toString());
325N/A out.flush();
325N/A }
325N/A
325N/A /**
325N/A * The object implements the readExternal method to restore its
325N/A * contents by calling the methods of DataInput for primitive
325N/A * types and readObject for objects, strings and arrays. The
325N/A * readExternal method must read the values in the same sequence
325N/A * and with the same types as were written by writeExternal.
325N/A *
325N/A * @param in the ObjectInput object to read from
325N/A * @exception ClassNotFoundException If the class for an object being
325N/A * restored cannot be found.
325N/A */
325N/A public void readExternal(ObjectInput in)
325N/A throws IOException, ClassNotFoundException {
325N/A try {
325N/A parse(in.readUTF());
325N/A } catch (MimeTypeParseException e) {
325N/A throw new IOException(e.toString());
325N/A }
325N/A }
325N/A
325N/A // below here be scary parsing related things
325N/A
325N/A /**
325N/A * Determine whether or not a given character belongs to a legal token.
325N/A */
325N/A private static boolean isTokenChar(char c) {
325N/A return ((c > 040) && (c < 0177)) && (TSPECIALS.indexOf(c) < 0);
325N/A }
325N/A
325N/A /**
325N/A * Determine whether or not a given string is a legal token.
325N/A */
325N/A private boolean isValidToken(String s) {
325N/A int len = s.length();
325N/A if (len > 0) {
325N/A for (int i = 0; i < len; ++i) {
325N/A char c = s.charAt(i);
325N/A if (!isTokenChar(c)) {
325N/A return false;
325N/A }
325N/A }
325N/A return true;
325N/A } else {
325N/A return false;
325N/A }
325N/A }
325N/A
325N/A /**
325N/A * A simple parser test,
325N/A * for debugging...
325N/A *
325N/A public static void main(String[] args)
325N/A throws MimeTypeParseException, IOException {
325N/A for (int i = 0; i < args.length; ++i) {
325N/A System.out.println("Original: " + args[i]);
325N/A
325N/A MimeType type = new MimeType(args[i]);
325N/A
325N/A System.out.println("Short: " + type.getBaseType());
325N/A System.out.println("Parsed: " + type.toString());
325N/A System.out.println();
325N/A }
325N/A }
325N/A */
325N/A}