View Javadoc
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  public class MIMEType implements Comparable<MIMEType> {
26  
27      private static final 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      private MIMEType(String type, String subtype, double q) {
36          this.type = type;
37          this.subtype = subtype;
38          this.q = q;
39      }
40  
41      /**
42       * Parses the given MIME type string returning an instance of
43       * {@link MIMEType}.
44       * The expected format for <code>mimeType</code> is
45       * <code>type/subtype[;q=x.y]</code> .
46       * An example of valid mime type is: <code>application/rdf+xml;q=0.9</code> 
47       *
48       * @param mimeType a provided mimetype string.
49       * @return the mime type instance.
50       * @throws IllegalArgumentException if the <code>mimeType</code> is not well formatted.
51       */
52      public static MIMEType parse(String mimeType) {
53          if (mimeType == null) {
54            return null;
55          }
56          int i = mimeType.indexOf(';');
57          double q = 1.0;
58          if (i > -1) {
59              String[] params = mimeType.substring(i + 1).split(";");
60              for (String param : params) {
61                  int i2 = param.indexOf('=');
62                  if (i2 == -1) {
63                    continue;
64                  }
65                  if (!"q".equals(param.substring(0, i2).trim().toLowerCase())){
66                    continue;
67                  }
68                  String value = param.substring(i2 + 1);
69                  try {
70                      q = Double.parseDouble(value);
71                  } catch (NumberFormatException ex) {
72                      continue;
73                  }
74                  if (q <= 0.0 || q >= 1.0) {
75                      q = 1.0;
76                  }
77              }
78          } else {
79              i = mimeType.length();
80          }
81          String type = mimeType.substring(0, i);
82          int i2 = type.indexOf('/');
83          if (i2 == -1) {
84              throw new IllegalArgumentException(MSG + mimeType);
85          }
86          String p1 = type.substring(0, i2).trim().toLowerCase();
87          String p2 = type.substring(i2 + 1).trim().toLowerCase();
88          if ("*".equals(p1)) {
89              if (!"*".equals(p2)) {
90                  throw new IllegalArgumentException(MSG + mimeType);
91              }
92              return new MIMEType(null, null, q);
93          }
94          if ("*".equals(p2)) {
95              return new MIMEType(p1, null, q);
96          }
97          return new MIMEType(p1, p2, q);
98      }
99  
100     public String getMajorType() {
101         return type == null ? "*" : type;
102     }
103 
104     public String getSubtype() {
105         return subtype == null ? "*" : subtype;
106     }
107 
108     public String getFullType() {
109         return getMajorType() + "/" + getSubtype();
110     }
111 
112     public double getQuality() {
113         return q;
114     }
115 
116     public boolean isAnyMajorType() {
117         return type == null;
118     }
119 
120     public boolean isAnySubtype() {
121         return subtype == null;
122     }
123 
124     @Override
125     public String toString() {
126         if (q == 1.0) {
127             return getFullType();
128         }
129         return getFullType() + ";q=" + q;
130     }
131 
132     @Override
133     public int compareTo(MIMEType other) {
134         return getFullType().compareTo(other.getFullType());
135     }
136 
137 }