author | mkos |
Sun, 30 Dec 2012 00:00:00 +0100 | |
changeset 22678 | ac1ea46be942 |
parent 12009 | 4abb694f273a |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
22678
ac1ea46be942
8029237: Update copyright year to match last edit in jaxws repository for 2012
mkos
parents:
12009
diff
changeset
|
2 |
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
12009 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.tools.internal.xjc.util; |
|
27 |
||
28 |
import java.text.ParseException; |
|
29 |
import java.util.ArrayList; |
|
30 |
import java.util.Collection; |
|
31 |
import java.util.HashMap; |
|
32 |
import java.util.List; |
|
33 |
import java.util.Map; |
|
34 |
||
35 |
import javax.activation.MimeType; |
|
36 |
import javax.activation.MimeTypeParseException; |
|
37 |
||
38 |
/** |
|
39 |
* @author Kohsuke Kawaguchi |
|
40 |
*/ |
|
41 |
public class MimeTypeRange { |
|
42 |
public final String majorType; |
|
43 |
public final String subType; |
|
44 |
||
45 |
public final Map<String,String> parameters = new HashMap<String, String>(); |
|
46 |
||
47 |
/** |
|
48 |
* Each media-range MAY be followed by one or more accept-params, |
|
49 |
* beginning with the "q" parameter for indicating a relative quality |
|
50 |
* factor. The first "q" parameter (if any) separates the media-range |
|
51 |
* parameter(s) from the accept-params. Quality factors allow the user |
|
52 |
* or user agent to indicate the relative degree of preference for that |
|
53 |
* media-range, using the qvalue scale from 0 to 1 (section 3.9). The |
|
54 |
* default value is q=1. |
|
55 |
*/ |
|
56 |
public final float q; |
|
57 |
||
58 |
// accept-extension is not implemented |
|
59 |
||
60 |
public static List<MimeTypeRange> parseRanges(String s) throws ParseException { |
|
61 |
StringCutter cutter = new StringCutter(s,true); |
|
62 |
List<MimeTypeRange> r = new ArrayList<MimeTypeRange>(); |
|
63 |
while(cutter.length()>0) { |
|
64 |
r.add(new MimeTypeRange(cutter)); |
|
65 |
} |
|
66 |
return r; |
|
67 |
} |
|
68 |
||
69 |
public MimeTypeRange(String s) throws ParseException { |
|
70 |
this(new StringCutter(s,true)); |
|
71 |
} |
|
72 |
||
73 |
/** |
|
74 |
* Used only to produce the static constants within this class. |
|
75 |
*/ |
|
76 |
private static MimeTypeRange create(String s) { |
|
77 |
try { |
|
78 |
return new MimeTypeRange(s); |
|
79 |
} catch (ParseException e) { |
|
80 |
// we only use this method for known inputs |
|
81 |
throw new Error(e); |
|
82 |
} |
|
83 |
} |
|
84 |
||
85 |
/** |
|
86 |
* @param cutter |
|
87 |
* A string like "text/html; charset=utf-8; |
|
88 |
*/ |
|
89 |
private MimeTypeRange(StringCutter cutter) throws ParseException { |
|
90 |
majorType = cutter.until("/"); |
|
91 |
cutter.next("/"); |
|
92 |
subType = cutter.until("[;,]"); |
|
93 |
||
94 |
float q = 1.0f; |
|
95 |
||
96 |
while(cutter.length()>0) { |
|
97 |
String sep = cutter.next("[;,]"); |
|
98 |
if(sep.equals(",")) |
|
99 |
break; |
|
100 |
||
101 |
String key = cutter.until("="); |
|
102 |
cutter.next("="); |
|
103 |
String value; |
|
104 |
char ch = cutter.peek(); |
|
105 |
if(ch=='"') { |
|
106 |
// quoted |
|
107 |
cutter.next("\""); |
|
108 |
value = cutter.until("\""); |
|
109 |
cutter.next("\""); |
|
110 |
} else { |
|
111 |
value = cutter.until("[;,]"); |
|
112 |
} |
|
113 |
||
114 |
if(key.equals("q")) { |
|
115 |
q = Float.parseFloat(value); |
|
116 |
} else { |
|
117 |
parameters.put(key,value); |
|
118 |
} |
|
119 |
} |
|
120 |
||
121 |
this.q = q; |
|
122 |
} |
|
123 |
||
124 |
public MimeType toMimeType() throws MimeTypeParseException { |
|
125 |
// due to the additional error check done in the MimeType class, |
|
126 |
// an error at this point is possible |
|
127 |
return new MimeType(toString()); |
|
128 |
} |
|
129 |
||
130 |
public String toString() { |
|
131 |
StringBuilder sb = new StringBuilder(majorType+'/'+subType); |
|
132 |
if(q!=1) |
|
133 |
sb.append("; q=").append(q); |
|
134 |
||
135 |
for( Map.Entry<String,String> p : parameters.entrySet() ) { |
|
136 |
// I'm too lazy to quote the value |
|
137 |
sb.append("; ").append(p.getKey()).append('=').append(p.getValue()); |
|
138 |
} |
|
139 |
return sb.toString(); |
|
140 |
} |
|
141 |
||
142 |
public static final MimeTypeRange ALL = create("*/*"); |
|
143 |
||
144 |
/** |
|
145 |
* Creates a range by merging all the given types. |
|
146 |
*/ |
|
147 |
public static MimeTypeRange merge( Collection<MimeTypeRange> types ) { |
|
148 |
if(types.size()==0) throw new IllegalArgumentException(); |
|
149 |
if(types.size()==1) return types.iterator().next(); |
|
150 |
||
151 |
String majorType=null; |
|
152 |
for (MimeTypeRange mt : types) { |
|
153 |
if(majorType==null) majorType = mt.majorType; |
|
154 |
if(!majorType.equals(mt.majorType)) |
|
155 |
return ALL; |
|
156 |
} |
|
157 |
||
158 |
return create(majorType+"/*"); |
|
159 |
} |
|
160 |
||
161 |
public static void main(String[] args) throws ParseException { |
|
162 |
for( MimeTypeRange m : parseRanges(args[0])) |
|
163 |
System.out.println(m.toString()); |
|
164 |
} |
|
165 |
} |