1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.any23.mime; |
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
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 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
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 | |
} |