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

Save This Page
Home » glassfish-v2ur2-b04-src » javax » activation » [javadoc | source]