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

changeset 0
373ffda63c9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaf_classes/javax/activation/MimeType.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,344 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package javax.activation;
    1.30 +
    1.31 +import java.io.*;
    1.32 +import java.util.Locale;
    1.33 +
    1.34 +/**
    1.35 + * A Multipurpose Internet Mail Extension (MIME) type, as defined
    1.36 + * in RFC 2045 and 2046.
    1.37 + *
    1.38 + * @since 1.6
    1.39 + */
    1.40 +public class MimeType implements Externalizable {
    1.41 +
    1.42 +    private String    primaryType;
    1.43 +    private String    subType;
    1.44 +    private MimeTypeParameterList parameters;
    1.45 +
    1.46 +    /**
    1.47 +     * A string that holds all the special chars.
    1.48 +     */
    1.49 +    private static final String TSPECIALS = "()<>@,;:/[]?=\\\"";
    1.50 +
    1.51 +    /**
    1.52 +     * Default constructor.
    1.53 +     */
    1.54 +    public MimeType() {
    1.55 +        primaryType = "application";
    1.56 +        subType = "*";
    1.57 +        parameters = new MimeTypeParameterList();
    1.58 +    }
    1.59 +
    1.60 +    /**
    1.61 +     * Constructor that builds a MimeType from a String.
    1.62 +     *
    1.63 +     * @param rawdata   the MIME type string
    1.64 +     */
    1.65 +    public MimeType(String rawdata) throws MimeTypeParseException {
    1.66 +        parse(rawdata);
    1.67 +    }
    1.68 +
    1.69 +    /**
    1.70 +     * Constructor that builds a MimeType with the given primary and sub type
    1.71 +     * but has an empty parameter list.
    1.72 +     *
    1.73 +     * @param primary   the primary MIME type
    1.74 +     * @param sub       the MIME sub-type
    1.75 +     * @exception       MimeTypeParseException  if the primary type or subtype
    1.76 +     *                                          is not a valid token
    1.77 +     */
    1.78 +    public MimeType(String primary, String sub) throws MimeTypeParseException {
    1.79 +        //    check to see if primary is valid
    1.80 +        if (isValidToken(primary)) {
    1.81 +            primaryType = primary.toLowerCase(Locale.ENGLISH);
    1.82 +        } else {
    1.83 +            throw new MimeTypeParseException("Primary type is invalid.");
    1.84 +        }
    1.85 +
    1.86 +        //    check to see if sub is valid
    1.87 +        if (isValidToken(sub)) {
    1.88 +            subType = sub.toLowerCase(Locale.ENGLISH);
    1.89 +        } else {
    1.90 +            throw new MimeTypeParseException("Sub type is invalid.");
    1.91 +        }
    1.92 +
    1.93 +        parameters = new MimeTypeParameterList();
    1.94 +    }
    1.95 +
    1.96 +    /**
    1.97 +     * A routine for parsing the MIME type out of a String.
    1.98 +     */
    1.99 +    private void parse(String rawdata) throws MimeTypeParseException {
   1.100 +        int slashIndex = rawdata.indexOf('/');
   1.101 +        int semIndex = rawdata.indexOf(';');
   1.102 +        if ((slashIndex < 0) && (semIndex < 0)) {
   1.103 +            //    neither character is present, so treat it
   1.104 +            //    as an error
   1.105 +            throw new MimeTypeParseException("Unable to find a sub type.");
   1.106 +        } else if ((slashIndex < 0) && (semIndex >= 0)) {
   1.107 +            //    we have a ';' (and therefore a parameter list),
   1.108 +            //    but no '/' indicating a sub type is present
   1.109 +            throw new MimeTypeParseException("Unable to find a sub type.");
   1.110 +        } else if ((slashIndex >= 0) && (semIndex < 0)) {
   1.111 +            //    we have a primary and sub type but no parameter list
   1.112 +            primaryType = rawdata.substring(0, slashIndex).trim().
   1.113 +                                                toLowerCase(Locale.ENGLISH);
   1.114 +            subType = rawdata.substring(slashIndex + 1).trim().
   1.115 +                                                toLowerCase(Locale.ENGLISH);
   1.116 +            parameters = new MimeTypeParameterList();
   1.117 +        } else if (slashIndex < semIndex) {
   1.118 +            //    we have all three items in the proper sequence
   1.119 +            primaryType = rawdata.substring(0, slashIndex).trim().
   1.120 +                                                toLowerCase(Locale.ENGLISH);
   1.121 +            subType = rawdata.substring(slashIndex + 1, semIndex).trim().
   1.122 +                                                toLowerCase(Locale.ENGLISH);
   1.123 +            parameters = new MimeTypeParameterList(rawdata.substring(semIndex));
   1.124 +        } else {
   1.125 +            // we have a ';' lexically before a '/' which means we
   1.126 +            // have a primary type and a parameter list but no sub type
   1.127 +            throw new MimeTypeParseException("Unable to find a sub type.");
   1.128 +        }
   1.129 +
   1.130 +        //    now validate the primary and sub types
   1.131 +
   1.132 +        //    check to see if primary is valid
   1.133 +        if (!isValidToken(primaryType))
   1.134 +            throw new MimeTypeParseException("Primary type is invalid.");
   1.135 +
   1.136 +        //    check to see if sub is valid
   1.137 +        if (!isValidToken(subType))
   1.138 +            throw new MimeTypeParseException("Sub type is invalid.");
   1.139 +    }
   1.140 +
   1.141 +    /**
   1.142 +     * Retrieve the primary type of this object.
   1.143 +     *
   1.144 +     * @return  the primary MIME type
   1.145 +     */
   1.146 +    public String getPrimaryType() {
   1.147 +        return primaryType;
   1.148 +    }
   1.149 +
   1.150 +    /**
   1.151 +     * Set the primary type for this object to the given String.
   1.152 +     *
   1.153 +     * @param primary   the primary MIME type
   1.154 +     * @exception       MimeTypeParseException  if the primary type
   1.155 +     *                                          is not a valid token
   1.156 +     */
   1.157 +    public void setPrimaryType(String primary) throws MimeTypeParseException {
   1.158 +        //    check to see if primary is valid
   1.159 +        if (!isValidToken(primaryType))
   1.160 +            throw new MimeTypeParseException("Primary type is invalid.");
   1.161 +        primaryType = primary.toLowerCase(Locale.ENGLISH);
   1.162 +    }
   1.163 +
   1.164 +    /**
   1.165 +     * Retrieve the subtype of this object.
   1.166 +     *
   1.167 +     * @return  the MIME subtype
   1.168 +     */
   1.169 +    public String getSubType() {
   1.170 +        return subType;
   1.171 +    }
   1.172 +
   1.173 +    /**
   1.174 +     * Set the subtype for this object to the given String.
   1.175 +     *
   1.176 +     * @param sub       the MIME subtype
   1.177 +     * @exception       MimeTypeParseException  if the subtype
   1.178 +     *                                          is not a valid token
   1.179 +     */
   1.180 +    public void setSubType(String sub) throws MimeTypeParseException {
   1.181 +        //    check to see if sub is valid
   1.182 +        if (!isValidToken(subType))
   1.183 +            throw new MimeTypeParseException("Sub type is invalid.");
   1.184 +        subType = sub.toLowerCase(Locale.ENGLISH);
   1.185 +    }
   1.186 +
   1.187 +    /**
   1.188 +     * Retrieve this object's parameter list.
   1.189 +     *
   1.190 +     * @return  a MimeTypeParameterList object representing the parameters
   1.191 +     */
   1.192 +    public MimeTypeParameterList getParameters() {
   1.193 +        return parameters;
   1.194 +    }
   1.195 +
   1.196 +    /**
   1.197 +     * Retrieve the value associated with the given name, or null if there
   1.198 +     * is no current association.
   1.199 +     *
   1.200 +     * @param name      the parameter name
   1.201 +     * @return          the paramter's value
   1.202 +     */
   1.203 +    public String getParameter(String name) {
   1.204 +        return parameters.get(name);
   1.205 +    }
   1.206 +
   1.207 +    /**
   1.208 +     * Set the value to be associated with the given name, replacing
   1.209 +     * any previous association.
   1.210 +     *
   1.211 +     * @param name      the parameter name
   1.212 +     * @param value     the paramter's value
   1.213 +     */
   1.214 +    public void setParameter(String name, String value) {
   1.215 +        parameters.set(name, value);
   1.216 +    }
   1.217 +
   1.218 +    /**
   1.219 +     * Remove any value associated with the given name.
   1.220 +     *
   1.221 +     * @param name      the parameter name
   1.222 +     */
   1.223 +    public void removeParameter(String name) {
   1.224 +        parameters.remove(name);
   1.225 +    }
   1.226 +
   1.227 +    /**
   1.228 +     * Return the String representation of this object.
   1.229 +     */
   1.230 +    public String toString() {
   1.231 +        return getBaseType() + parameters.toString();
   1.232 +    }
   1.233 +
   1.234 +    /**
   1.235 +     * Return a String representation of this object
   1.236 +     * without the parameter list.
   1.237 +     *
   1.238 +     * @return  the MIME type and sub-type
   1.239 +     */
   1.240 +    public String getBaseType() {
   1.241 +        return primaryType + "/" + subType;
   1.242 +    }
   1.243 +
   1.244 +    /**
   1.245 +     * Determine if the primary and sub type of this object is
   1.246 +     * the same as what is in the given type.
   1.247 +     *
   1.248 +     * @param type      the MimeType object to compare with
   1.249 +     * @return          true if they match
   1.250 +     */
   1.251 +    public boolean match(MimeType type) {
   1.252 +        return primaryType.equals(type.getPrimaryType())
   1.253 +                    && (subType.equals("*")
   1.254 +                            || type.getSubType().equals("*")
   1.255 +                            || (subType.equals(type.getSubType())));
   1.256 +    }
   1.257 +
   1.258 +    /**
   1.259 +     * Determine if the primary and sub type of this object is
   1.260 +     * the same as the content type described in rawdata.
   1.261 +     *
   1.262 +     * @param rawdata   the MIME type string to compare with
   1.263 +     * @return          true if they match
   1.264 +     */
   1.265 +    public boolean match(String rawdata) throws MimeTypeParseException {
   1.266 +        return match(new MimeType(rawdata));
   1.267 +    }
   1.268 +
   1.269 +    /**
   1.270 +     * The object implements the writeExternal method to save its contents
   1.271 +     * by calling the methods of DataOutput for its primitive values or
   1.272 +     * calling the writeObject method of ObjectOutput for objects, strings
   1.273 +     * and arrays.
   1.274 +     *
   1.275 +     * @param out       the ObjectOutput object to write to
   1.276 +     * @exception IOException Includes any I/O exceptions that may occur
   1.277 +     */
   1.278 +    public void writeExternal(ObjectOutput out) throws IOException {
   1.279 +        out.writeUTF(toString());
   1.280 +        out.flush();
   1.281 +    }
   1.282 +
   1.283 +    /**
   1.284 +     * The object implements the readExternal method to restore its
   1.285 +     * contents by calling the methods of DataInput for primitive
   1.286 +     * types and readObject for objects, strings and arrays.  The
   1.287 +     * readExternal method must read the values in the same sequence
   1.288 +     * and with the same types as were written by writeExternal.
   1.289 +     *
   1.290 +     * @param in        the ObjectInput object to read from
   1.291 +     * @exception ClassNotFoundException If the class for an object being
   1.292 +     *              restored cannot be found.
   1.293 +     */
   1.294 +    public void readExternal(ObjectInput in)
   1.295 +                                throws IOException, ClassNotFoundException {
   1.296 +        try {
   1.297 +            parse(in.readUTF());
   1.298 +        } catch (MimeTypeParseException e) {
   1.299 +            throw new IOException(e.toString());
   1.300 +        }
   1.301 +    }
   1.302 +
   1.303 +    //    below here be scary parsing related things
   1.304 +
   1.305 +    /**
   1.306 +     * Determine whether or not a given character belongs to a legal token.
   1.307 +     */
   1.308 +    private static boolean isTokenChar(char c) {
   1.309 +        return ((c > 040) && (c < 0177)) && (TSPECIALS.indexOf(c) < 0);
   1.310 +    }
   1.311 +
   1.312 +    /**
   1.313 +     * Determine whether or not a given string is a legal token.
   1.314 +     */
   1.315 +    private boolean isValidToken(String s) {
   1.316 +        int len = s.length();
   1.317 +        if (len > 0) {
   1.318 +            for (int i = 0; i < len; ++i) {
   1.319 +                char c = s.charAt(i);
   1.320 +                if (!isTokenChar(c)) {
   1.321 +                    return false;
   1.322 +                }
   1.323 +            }
   1.324 +            return true;
   1.325 +        } else {
   1.326 +            return false;
   1.327 +        }
   1.328 +    }
   1.329 +
   1.330 +    /**
   1.331 +     * A simple parser test,
   1.332 +     * for debugging...
   1.333 +     *
   1.334 +    public static void main(String[] args)
   1.335 +                                throws MimeTypeParseException, IOException {
   1.336 +        for (int i = 0; i < args.length; ++i) {
   1.337 +            System.out.println("Original: " + args[i]);
   1.338 +
   1.339 +            MimeType type = new MimeType(args[i]);
   1.340 +
   1.341 +            System.out.println("Short:    " + type.getBaseType());
   1.342 +            System.out.println("Parsed:   " + type.toString());
   1.343 +            System.out.println();
   1.344 +        }
   1.345 +    }
   1.346 +    */
   1.347 +}

mercurial