Coverage report

  %line %branch
org.apache.turbine.services.mimetype.util.MimeType
0% 
0% 

 1  
 package org.apache.turbine.services.mimetype.util;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2005 The Apache Software Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License")
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import java.util.ArrayList;
 20  
 
 21  
 /**
 22  
  * This class is used to represent parsed MIME types.
 23  
  * The representation is parsed from a string based
 24  
  * representation of the MIME type, as defined in the RFC1345.
 25  
  *
 26  
  * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
 27  
  * @version $Id: MimeType.java 264148 2005-08-29 14:21:04Z henning $
 28  
  */
 29  
 public class MimeType
 30  
         implements Cloneable
 31  
 {
 32  
     /**
 33  
      * A list of well known MIME types.
 34  
      */
 35  
     public static MimeType TEXT_HTML;
 36  
     public static MimeType TEXT_WML;
 37  
     public static MimeType TEXT_HDML;
 38  
     public static MimeType TEXT_CHTML;
 39  
     public static MimeType TEXT_PLAIN;
 40  
     public static MimeType MULTIPART;
 41  
     public static MimeType MULTIPART_FORM_DATA;
 42  
     public static MimeType APPLICATION_POSTSCRIPT;
 43  
     public static MimeType APPLICATION_OCTET_STREAM;
 44  
     public static MimeType APPLICATION_X_JAVA_AGENT;
 45  
     public static MimeType APPLICATION_X_WWW_FORM_URLENCODED;
 46  
     public static MimeType MESSAGE_HTTP;
 47  
     public static MimeType TEXT_CSS;
 48  
     public static MimeType TEXT;
 49  
     public static MimeType IMAGE_GIF;
 50  
     public static MimeType IMAGE_JPEG;
 51  
     public static MimeType IMAGE_WBMP;
 52  
 
 53  
     static
 54  
     {
 55  0
         TEXT_HTML =
 56  
                 new MimeType("text/html");
 57  0
         TEXT_WML =
 58  
                 new MimeType("text/vnd.wap.wml");
 59  0
         TEXT_HDML =
 60  
                 new MimeType("text/x-hdml");
 61  0
         TEXT_CHTML =
 62  
                 new MimeType("text/x-chtml");
 63  0
         TEXT_PLAIN =
 64  
                 new MimeType("text/plain");
 65  0
         MULTIPART =
 66  
                 new MimeType("multipart/*");
 67  0
         MULTIPART_FORM_DATA =
 68  
                 new MimeType("multipart/form-data");
 69  0
         APPLICATION_POSTSCRIPT =
 70  
                 new MimeType("application/postscript");
 71  0
         APPLICATION_OCTET_STREAM =
 72  
                 new MimeType("application/octet-stream");
 73  0
         APPLICATION_X_JAVA_AGENT =
 74  
                 new MimeType("application/x-java-agent");
 75  0
         APPLICATION_X_WWW_FORM_URLENCODED =
 76  
                 new MimeType("application/x-www-form-urlencoded");
 77  0
         MESSAGE_HTTP =
 78  
                 new MimeType("message/http");
 79  0
         TEXT_CSS =
 80  
                 new MimeType("text/css");
 81  0
         TEXT =
 82  
                 new MimeType("text/*");
 83  0
         IMAGE_GIF =
 84  
                 new MimeType("image/gif");
 85  0
         IMAGE_JPEG =
 86  
                 new MimeType("image/jpeg");
 87  0
         IMAGE_WBMP =
 88  
                 new MimeType("image/vnd.wap.wbmp");
 89  0
     }
 90  
 
 91  
     /**
 92  
      * MIME type matching constants.
 93  
      */
 94  
     public static final int NO_MATCH = 0;
 95  
     public static final int MATCH_TYPE = 1;
 96  
     public static final int MATCH_SUBTYPE = 2;
 97  
     public static final int MATCH_SPECIFIC_SUBTYPE = 3;
 98  
 
 99  
     /**
 100  
      * A string representation of the main type.
 101  
      */
 102  
     private String mimeType;
 103  
 
 104  
     /**
 105  
      * A string representation of the subtype.
 106  
      */
 107  
     private String mimeSubtype;
 108  
 
 109  
     /**
 110  
      * Parameter names.
 111  
      */
 112  
     private String parameterNames[];
 113  
 
 114  
     /**
 115  
      * Parameter values.
 116  
      */
 117  
     private String parameterValues[];
 118  
 
 119  
     /**
 120  
      * A string representation of the MIME type.
 121  
      */
 122  
     private String mimeTypeString;
 123  
 
 124  
     /**
 125  
      * Constructs a new MIME type by parsing a specification string.
 126  
      *
 127  
      * @param spec a string representing a MIME type.
 128  
      */
 129  
     public MimeType(String spec)
 130  
     {
 131  0
         this(spec, true);
 132  0
     }
 133  
 
 134  
     /**
 135  
      * Constructs a new MIME type by parsing a specification string.
 136  
      *
 137  
      * @param spec a string representing a MIME type.
 138  
      * @param parsep a flag for parsing parameters also.
 139  
      * @throws IllegalArgumentException for parsing errors.
 140  
      */
 141  
     public MimeType(String spec,
 142  
                     boolean parsep)
 143  0
     {
 144  0
         int start = 0;
 145  0
         char look = '\0';
 146  0
         int length = spec.length();
 147  
 
 148  
         // Skip leading/trailing blanks.
 149  0
         while ((start < length) &&
 150  
                 Character.isWhitespace(spec.charAt(start)))
 151  
         {
 152  0
             start++;
 153  
         }
 154  0
         while ((length > start) &&
 155  
                 Character.isWhitespace(spec.charAt(length - 1)))
 156  
         {
 157  0
             length--;
 158  
         }
 159  
 
 160  
         // Get the type.
 161  0
         StringBuffer sb = new StringBuffer();
 162  0
         while ((start < length) &&
 163  
                 ((look = spec.charAt(start)) != '/'))
 164  
         {
 165  0
             sb.append((char) look);
 166  0
             start++;
 167  
         }
 168  0
         if (look != '/')
 169  
         {
 170  0
             throw new IllegalArgumentException(
 171  
                     "Syntax error in MIME type " + spec);
 172  
         }
 173  0
         mimeType = sb.toString();
 174  
 
 175  
         // Get the subtype.
 176  0
         start++;
 177  0
         sb.setLength(0);
 178  
         while ((start < length) &&
 179  0
                 ((look = spec.charAt(start)) != ';') &&
 180  
                 !Character.isWhitespace(look))
 181  
         {
 182  0
             sb.append((char) look);
 183  0
             start++;
 184  
         }
 185  0
         mimeSubtype = sb.toString();
 186  
 
 187  0
         if (parsep)
 188  
         {
 189  
             // Get parameters, if any.
 190  0
             while ((start < length) &&
 191  
                     Character.isWhitespace(spec.charAt(start)))
 192  
             {
 193  0
                 start++;
 194  
             }
 195  0
             if (start < length)
 196  
             {
 197  0
                 if (spec.charAt(start) != ';')
 198  
                 {
 199  0
                     throw new IllegalArgumentException(
 200  
                             "Syntax error in MIME type parameters " + spec);
 201  
                 }
 202  0
                 start++;
 203  0
                 ArrayList na = new ArrayList(4);
 204  0
                 ArrayList va = new ArrayList(4);
 205  0
                 while (start < length)
 206  
                 {
 207  
                     // Get the name.
 208  0
                     while ((start < length) &&
 209  
                             Character.isWhitespace(spec.charAt(start)))
 210  
                     {
 211  0
                         start++;
 212  
                     }
 213  0
                     sb.setLength(0);
 214  
                     while ((start < length) &&
 215  0
                             ((look = spec.charAt(start)) != '=') &&
 216  
                             !Character.isWhitespace(look))
 217  
                     {
 218  0
                         sb.append(Character.toLowerCase((char) look));
 219  0
                         start++;
 220  
                     }
 221  0
                     String name = sb.toString();
 222  
 
 223  
                     // Get the value.
 224  0
                     while ((start < length) &&
 225  
                             Character.isWhitespace(spec.charAt(start)))
 226  
                     {
 227  0
                         start++;
 228  
                     }
 229  0
                     if (spec.charAt(start) != '=')
 230  
                     {
 231  0
                         throw new IllegalArgumentException(
 232  
                                 "Syntax error in MIME type parameters " + spec);
 233  
                     }
 234  0
                     start++;
 235  0
                     while ((start < length) &&
 236  
                             Character.isWhitespace(spec.charAt(start)))
 237  
                     {
 238  0
                         start++;
 239  
                     }
 240  0
                     sb.setLength(0);
 241  0
                     char delim = ';';
 242  0
                     if (spec.charAt(start) == '"')
 243  
                     {
 244  0
                         start++;
 245  0
                         delim = '"';
 246  
                     }
 247  
                     while ((start < length) &&
 248  0
                             ((look = spec.charAt(start)) != delim) &&
 249  
                             ((delim == '"') ||
 250  
                             !Character.isWhitespace(look)))
 251  
                     {
 252  0
                         sb.append((char) look);
 253  0
                         start++;
 254  
                     }
 255  0
                     while ((start < length) &&
 256  
                             (spec.charAt(start) != ';'))
 257  
                     {
 258  0
                         start++;
 259  
                     }
 260  0
                     start++;
 261  0
                     String value = sb.toString();
 262  
 
 263  0
                     na.add(name);
 264  0
                     va.add(value);
 265  
                 }
 266  0
                 parameterNames = (String[]) na.toArray(new String[na.size()]);
 267  0
                 parameterValues = (String[]) va.toArray(new String[va.size()]);
 268  
             }
 269  
         }
 270  0
     }
 271  
 
 272  
     /**
 273  
      * Contructs a new MIME type from specified types.
 274  
      *
 275  
      * @param type a type.
 276  
      * @param subtype a subtype.
 277  
      * @throws NullPointerException if type or subtype are nulls.
 278  
      */
 279  
     public MimeType(String type,
 280  
                     String subtype)
 281  
     {
 282  0
         this(type, subtype, null, class="keyword">null);
 283  0
     }
 284  
 
 285  
     /**
 286  
      * Contructs a new MIME type from specified parameters.
 287  
      *
 288  
      * @param type a type.
 289  
      * @param subtype a subtype.
 290  
      * @param names parameters names.
 291  
      * @param values parameter values.
 292  
      * @throws NullPointerException if type or subtype are nulls.
 293  
      */
 294  
     public MimeType(String type,
 295  
                     String subtype,
 296  
                     String names[],
 297  
                     String values[])
 298  0
     {
 299  0
         if ((type == null) ||
 300  
                 (subtype == null))
 301  
         {
 302  0
             throw new NullPointerException("MIME type or subtype missing");
 303  
         }
 304  0
         mimeType = type.trim();
 305  0
         mimeSubtype = subtype.trim();
 306  0
         parameterNames = names;
 307  0
         parameterValues = values;
 308  0
     }
 309  
 
 310  
     /**
 311  
      * Compares the specified MIME type to this one
 312  
      * and returns a matching level:
 313  
      * NO_MATCH=types do not match,
 314  
      * MATCH_TYPE=types match,
 315  
      * MATCH_SPECIFIC_TYPE=types match exactly,
 316  
      * MATCH_SUBTYPE=types match, subtypes match too,
 317  
      * MATCH_SPECIFIC_SUBTYPE=types match, subtypes match exactly.
 318  
      *
 319  
      * @param other the MimeType to compare.
 320  
      * @return the matching level.
 321  
      */
 322  
     public int match(MimeType other)
 323  
     {
 324  0
         if (mimeType.equals("*") ||
 325  
                 other.mimeType.equals("*"))
 326  
         {
 327  0
             return MATCH_TYPE;
 328  
         }
 329  0
         else if (!mimeType.equalsIgnoreCase(other.mimeType))
 330  
         {
 331  0
             return NO_MATCH;
 332  
         }
 333  0
         else if (mimeSubtype.equals("*") ||
 334  
                 other.mimeSubtype.equals("*"))
 335  
         {
 336  0
             return MATCH_SUBTYPE;
 337  
         }
 338  0
         else if (!mimeSubtype.equalsIgnoreCase(other.mimeSubtype))
 339  
         {
 340  0
             return NO_MATCH;
 341  
         }
 342  
         else
 343  
         {
 344  0
             return MATCH_SPECIFIC_SUBTYPE;
 345  
         }
 346  
     }
 347  
 
 348  
     /**
 349  
      * Gets the main type of the MIME type.
 350  
      *
 351  
      * @return the main type as a string.
 352  
      */
 353  
     public String getType()
 354  
     {
 355  0
         return mimeType;
 356  
     }
 357  
 
 358  
     /**
 359  
      * Gets the subtype of the MIME type.
 360  
      *
 361  
      * @return the subtype as a string.
 362  
      */
 363  
     public String getSubtype()
 364  
     {
 365  0
         return mimeSubtype;
 366  
     }
 367  
 
 368  
     /**
 369  
      * Gets the type and the subtype of the MIME type.
 370  
      *
 371  
      * @return the types as a string.
 372  
      */
 373  
     public String getTypes()
 374  
     {
 375  0
         return mimeType + '/' + mimeSubtype;
 376  
     }
 377  
 
 378  
     /**
 379  
      * Checks whether the MIME type contains the specified parameter.
 380  
      *
 381  
      * @param param the name opf the parameter.
 382  
      * @return true if the parameter found, otherwise false.
 383  
      */
 384  
     public boolean hasParameter(String param)
 385  
     {
 386  0
         String[] na = parameterNames;
 387  0
         if (na != null)
 388  
         {
 389  0
             for (int i = 0; i < na.length; i++)
 390  
             {
 391  0
                 if (na[i].equalsIgnoreCase(param))
 392  
                 {
 393  0
                     return true;
 394  
                 }
 395  
             }
 396  
         }
 397  0
         return false;
 398  
     }
 399  
 
 400  
     /**
 401  
      * Gets the value of a MIME type parameter.
 402  
      * The first parameter with the specifed name will be returned.
 403  
      *
 404  
      * @param param the name of the parameter.
 405  
      * @return the value of the parameter, or null.
 406  
      */
 407  
     public String getParameter(String param)
 408  
     {
 409  0
         String[] na = parameterNames;
 410  0
         if (na != null)
 411  
         {
 412  0
             String[] va = parameterValues;
 413  0
             for (int i = 0; i < na.length; i++)
 414  
             {
 415  0
                 if (na[i].equalsIgnoreCase(param))
 416  
                 {
 417  0
                     return va[i];
 418  
                 }
 419  
             }
 420  
         }
 421  0
         return null;
 422  
     }
 423  
 
 424  
     /**
 425  
      * Sets the value of a MIME type parameter replacing the old one.
 426  
      *
 427  
      * @param param the name of the parameter.
 428  
      * @param value the value of the parameter.
 429  
      */
 430  
     public synchronized void setParameter(String param,
 431  
                                           String value)
 432  
     {
 433  0
         if (parameterNames != null)
 434  
         {
 435  0
             for (int i = 0; i < parameterNames.length; i++)
 436  
             {
 437  0
                 if (parameterNames[i].equalsIgnoreCase(param))
 438  
                 {
 439  0
                     parameterValues[i] = value;
 440  0
                     mimeTypeString = null;
 441  0
                     return;
 442  
                 }
 443  
             }
 444  
         }
 445  0
         addParameter(param, value);
 446  0
     }
 447  
 
 448  
     /**
 449  
      * Adds a parameter to the MIME type.
 450  
      *
 451  
      * @param param the name of the parameter.
 452  
      * @param value the value of the parameter.
 453  
      */
 454  
     public void addParameter(String param,
 455  
                              String value)
 456  
     {
 457  0
         addParameters(new String[]{param}, class="keyword">new String[]{value});
 458  0
     }
 459  
 
 460  
     /**
 461  
      * Adds parameters to the MIME type.
 462  
      *
 463  
      * @param params an array of parameter names.
 464  
      * @param values an array of parameter values.
 465  
      * @throw IllegalArgumentException for incorrect parameters.
 466  
      */
 467  
     public synchronized void addParameters(String[] params,
 468  
                                            String[] values)
 469  
     {
 470  0
         if ((params == null) ||
 471  
                 (values == null) ||
 472  
                 (params.length != values.length))
 473  0
             throw new IllegalArgumentException("Incorrect MIME type parameters");
 474  
 
 475  0
         if (parameterNames != null)
 476  
         {
 477  0
             String[] na = new String[parameterNames.length + params.length];
 478  0
             String[] va = new String[parameterValues.length + values.length];
 479  0
             System.arraycopy(parameterNames, 0, na, 0, parameterNames.length);
 480  0
             System.arraycopy(params, 0, na, parameterNames.length, params.length);
 481  0
             System.arraycopy(parameterValues, 0, va, 0, parameterValues.length);
 482  0
             System.arraycopy(values, 0, va, parameterValues.length, values.length);
 483  0
             parameterNames = na;
 484  0
             parameterValues = va;
 485  
         }
 486  
         else
 487  
         {
 488  0
             parameterNames = params;
 489  0
             parameterValues = values;
 490  
         }
 491  0
         mimeTypeString = null;
 492  0
     }
 493  
 
 494  
     /**
 495  
      * Converts the MIME type into a string.
 496  
      *
 497  
      * @return the string representation of the MIME type.
 498  
      */
 499  
     public String toString()
 500  
     {
 501  0
         if (mimeTypeString == null)
 502  
         {
 503  0
             StringBuffer sb = new StringBuffer(mimeType);
 504  0
             sb.append('/');
 505  0
             sb.append(mimeSubtype);
 506  0
             String[] na = parameterNames;
 507  0
             if (na != null)
 508  
             {
 509  0
                 String[] va = parameterValues;
 510  0
                 for (int i = 0; i < va.length; i++)
 511  
                 {
 512  0
                     sb.append(';');
 513  0
                     sb.append(na[i]);
 514  0
                     if (va[i] != null)
 515  
                     {
 516  0
                         sb.append('=');
 517  0
                         sb.append(va[i]);
 518  
                     }
 519  
                 }
 520  
             }
 521  0
             mimeTypeString = sb.toString();
 522  
         }
 523  0
         return mimeTypeString;
 524  
     }
 525  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.