##### Coverage Report - org.apache.any23.mime.MIMEType

Classes in this File | Line Coverage | Branch Coverage | Complexity
MIMEType |
0% | 0/47
0% | 0/32
| 3.1;3.1

1 |  |
     /*
2 |  |
      * Licensed to the Apache Software Foundation (ASF) under one or more
3 |  |
      * contributor license agreements.  See the NOTICE file distributed with
4 |  |
      * this work for additional information regarding copyright ownership.
5 |  |
      * The ASF licenses this file to You under the Apache License, Version 2.0
6 |  |
      * (the "License"); you may not use this file except in compliance with
7 |  |
      * the License.  You may obtain a copy of the License at
8 |  |
      *
9 |  |
      *  http://www.apache.org/licenses/LICENSE-2.0
10 |  |
      *
11 |  |
      * Unless required by applicable law or agreed to in writing, software
12 |  |
      * distributed under the License is distributed on an "AS IS" BASIS,
13 |  |
      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 |  |
      * See the License for the specific language governing permissions and
15 |  |
      * limitations under the License.
16 |  |
      */
17 |  |
18 |  |
     package org.apache.any23.mime;
19 |  |
20 |  |
     /**
21 |  |
      * A MIME type with an optional q (quality) value.
22 |  |
      *
23 |  |
      * @author Richard Cyganiak (richard@cyganiak.de)
24 |  |
      */
25 | 0 |
     public class MIMEType implements Comparable<MIMEType> {
26 |  |
27 |  |
         private final static String MSG = "Cannot parse MIME type (expected type/subtype[;q=x.y] format): ";
28 |  |
29 |  |
         private final String type;
30 |  |
31 |  |
         private final String subtype;
32 |  |
33 |  |
         private final double q;
34 |  |
35 |  |
         /**
36 |  |
          * Parses the given MIME type string returning an instance of
37 |  |
          * {@link MIMEType}.
38 |  |
          * The expected format for <code>mimeType</code> is
39 |  |
          * <code>type/subtype[;q=x.y]</code> .
40 |  |
          * An example of valid mime type is: <code>application/rdf+xml;q=0.9</code>
41 |  |
          *
42 |  |
          * @param mimeType
43 |  |
          * @return the mime type instance.
44 |  |
          * @throws IllegalArgumentException if the <code>mimeType</code> is not well formatted.
45 |  |
          */
46 |  |
         public static MIMEType parse(String mimeType) {
47 | 0 |
             if (mimeType == null) return null;
48 | 0 |
             int i = mimeType.indexOf(';');
49 | 0 |
             double q = 1.0;
50 | 0 |
             if (i > -1) {
51 | 0 |
                 String[] params = mimeType.substring(i + 1).split(";");
52 | 0 |
                 for (String param : params) {
53 | 0 |
                     int i2 = param.indexOf('=');
54 | 0 |
                     if (i2 == -1) continue;
55 | 0 |
                     if (!"q".equals(param.substring(0, i2).trim().toLowerCase())) continue;
56 | 0 |
                     String value = param.substring(i2 + 1);
57 |  |
                     try {
58 | 0 |
                         q = Double.parseDouble(value);
59 | 0 |
                     } catch (NumberFormatException ex) {
60 | 0 |
                         continue;
61 | 0 |
                     }
62 | 0 |
                     if (q <= 0.0 || q >= 1.0) {
63 | 0 |
                         q = 1.0;
64 |  |
                     }
65 |  |
                 }
66 | 0 |
             } else {
67 | 0 |
                 i = mimeType.length();
68 |  |
             }
69 | 0 |
             String type = mimeType.substring(0, i);
70 | 0 |
             int i2 = type.indexOf('/');
71 | 0 |
             if (i2 == -1) {
72 | 0 |
                 throw new IllegalArgumentException(MSG + mimeType);
73 |  |
             }
74 | 0 |
             String p1 = type.substring(0, i2).trim().toLowerCase();
75 | 0 |
             String p2 = type.substring(i2 + 1).trim().toLowerCase();
76 | 0 |
             if ("*".equals(p1)) {
77 | 0 |
                 if (!"*".equals(p2)) {
78 | 0 |
                     throw new IllegalArgumentException(MSG + mimeType);
79 |  |
                 }
80 | 0 |
                 return new MIMEType(null, null, q);
81 |  |
             }
82 | 0 |
             if ("*".equals(p2)) {
83 | 0 |
                 return new MIMEType(p1, null, q);
84 |  |
             }
85 | 0 |
             return new MIMEType(p1, p2, q);
86 |  |
         }
87 |  |
88 | 0 |
         private MIMEType(String type, String subtype, double q) {
89 | 0 |
             this.type = type;
90 | 0 |
             this.subtype = subtype;
91 | 0 |
             this.q = q;
92 | 0 |
         }
93 |  |
94 |  |
         public String getMajorType() {
95 | 0 |
             return (type == null ? "*" : type);
96 |  |
         }
97 |  |
98 |  |
         public String getSubtype() {
99 | 0 |
             return (subtype == null ? "*" : subtype);
100 |  |
         }
101 |  |
102 |  |
         public String getFullType() {
103 | 0 |
             return getMajorType() + "/" + getSubtype();
104 |  |
         }
105 |  |
106 |  |
         public double getQuality() {
107 | 0 |
             return q;
108 |  |
         }
109 |  |
110 |  |
         public boolean isAnyMajorType() {
111 | 0 |
             return type == null;
112 |  |
         }
113 |  |
114 |  |
         public boolean isAnySubtype() {
115 | 0 |
             return subtype == null;
116 |  |
         }
117 |  |
118 |  |
         public String toString() {
119 | 0 |
             if (q == 1.0) {
120 | 0 |
                 return getFullType();
121 |  |
             }
122 | 0 |
             return getFullType() + ";q=" + q;
123 |  |
         }
124 |  |
125 |  |
         public int compareTo(MIMEType other) {
126 | 0 |
             return getFullType().compareTo(other.getFullType());
127 |  |
         }
128 |  |
129 |  |
     }