1 /*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
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
33
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.Map;
37
38 /**
39 * This class holds MIME parameters (attribute-value pairs).
40 *
41 * @version 1.10, 03/02/12
42 * @author John Mani
43 */
44
45 public final class ParameterList {
46
47 private final HashMap<String, String> list;
48
49 /**
50 * No-arg Constructor.
51 */
52 public ParameterList() {
53 this.list = new HashMap<String, String>();
54 }
55
56 private ParameterList(HashMap<String, String> m) {
57 this.list = m;
58 }
59
60 /**
61 * Constructor that takes a parameter-list string. The String
62 * is parsed and the parameters are collected and stored internally.
63 * A ParseException is thrown if the parse fails.
64 * Note that an empty parameter-list string is valid and will be
65 * parsed into an empty ParameterList.
66 *
67 * @param s the parameter-list string.
68 * @exception ParseException if the parse fails.
69 */
70 public ParameterList(String s) throws ParseException {
71 HeaderTokenizer h = new HeaderTokenizer(s, HeaderTokenizer.MIME);
72 HeaderTokenizer.Token tk;
73 int type;
74 String name;
75
76 list = new HashMap<String, String>();
77 while (true) {
78 tk = h.next();
79 type = tk.getType();
80
81 if (type == HeaderTokenizer.Token.EOF) // done
82 return;
83
84 if ((char)type == ';') {
85 // expect parameter name
86 tk = h.next();
87 // tolerate trailing semicolon, even though it violates the spec
88 if (tk.getType() == HeaderTokenizer.Token.EOF)
89 return;
90 // parameter name must be a MIME Atom
91 if (tk.getType() != HeaderTokenizer.Token.ATOM)
92 throw new ParseException();
93 name = tk.getValue().toLowerCase();
94
95 // expect '='
96 tk = h.next();
154 list.remove(name.trim().toLowerCase());
155 }
156
157 /**
158 * Return an enumeration of the names of all parameters in this
159 * list.
160 *
161 * @return Enumeration of all parameter names in this list.
162 */
163 public Iterator<String> getNames() {
164 return list.keySet().iterator();
165 }
166
167
168 /**
169 * Convert this ParameterList into a MIME String. If this is
170 * an empty list, an empty string is returned.
171 *
172 * @return String
173 */
174 public String toString() {
175 return toString(0);
176 }
177
178 /**
179 * Convert this ParameterList into a MIME String. If this is
180 * an empty list, an empty string is returned.
181 *
182 * The 'used' parameter specifies the number of character positions
183 * already taken up in the field into which the resulting parameter
184 * list is to be inserted. It's used to determine where to fold the
185 * resulting parameter list.
186 *
187 * @param used number of character positions already used, in
188 * the field into which the parameter list is to
189 * be inserted.
190 * @return String
191 */
192 public String toString(int used) {
193 StringBuilder sb = new StringBuilder();
215 used += s.length() - lastlf - 1;
216 else
217 used += s.length();
218 } else {
219 sb.append(value);
220 used += value.length();
221 }
222 }
223
224 return sb.toString();
225 }
226
227 // Quote a parameter value token if required.
228 private String quote(String value) {
229 if ("".equals(value))
230 return "\"\"";
231 return MimeUtility.quote(value, HeaderTokenizer.MIME);
232 }
233
234 public ParameterList copy() {
235 return new ParameterList((HashMap)list.clone());
236 }
237 }
|
1 /*
2 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
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
33
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.Map;
37
38 /**
39 * This class holds MIME parameters (attribute-value pairs).
40 *
41 * @version 1.10, 03/02/12
42 * @author John Mani
43 */
44
45 public final class ParameterList {
46
47 private final HashMap<String, String> list;
48
49 /**
50 * No-arg Constructor.
51 */
52 public ParameterList() {
53 this.list = new HashMap<>();
54 }
55
56 private ParameterList(HashMap<String, String> m) {
57 this.list = m;
58 }
59
60 /**
61 * Constructor that takes a parameter-list string. The String
62 * is parsed and the parameters are collected and stored internally.
63 * A ParseException is thrown if the parse fails.
64 * Note that an empty parameter-list string is valid and will be
65 * parsed into an empty ParameterList.
66 *
67 * @param s the parameter-list string.
68 * @exception ParseException if the parse fails.
69 */
70 public ParameterList(String s) throws ParseException {
71 HeaderTokenizer h = new HeaderTokenizer(s, HeaderTokenizer.MIME);
72 HeaderTokenizer.Token tk;
73 int type;
74 String name;
75
76 list = new HashMap<>();
77 while (true) {
78 tk = h.next();
79 type = tk.getType();
80
81 if (type == HeaderTokenizer.Token.EOF) // done
82 return;
83
84 if ((char)type == ';') {
85 // expect parameter name
86 tk = h.next();
87 // tolerate trailing semicolon, even though it violates the spec
88 if (tk.getType() == HeaderTokenizer.Token.EOF)
89 return;
90 // parameter name must be a MIME Atom
91 if (tk.getType() != HeaderTokenizer.Token.ATOM)
92 throw new ParseException();
93 name = tk.getValue().toLowerCase();
94
95 // expect '='
96 tk = h.next();
154 list.remove(name.trim().toLowerCase());
155 }
156
157 /**
158 * Return an enumeration of the names of all parameters in this
159 * list.
160 *
161 * @return Enumeration of all parameter names in this list.
162 */
163 public Iterator<String> getNames() {
164 return list.keySet().iterator();
165 }
166
167
168 /**
169 * Convert this ParameterList into a MIME String. If this is
170 * an empty list, an empty string is returned.
171 *
172 * @return String
173 */
174 @Override
175 public String toString() {
176 return toString(0);
177 }
178
179 /**
180 * Convert this ParameterList into a MIME String. If this is
181 * an empty list, an empty string is returned.
182 *
183 * The 'used' parameter specifies the number of character positions
184 * already taken up in the field into which the resulting parameter
185 * list is to be inserted. It's used to determine where to fold the
186 * resulting parameter list.
187 *
188 * @param used number of character positions already used, in
189 * the field into which the parameter list is to
190 * be inserted.
191 * @return String
192 */
193 public String toString(int used) {
194 StringBuilder sb = new StringBuilder();
216 used += s.length() - lastlf - 1;
217 else
218 used += s.length();
219 } else {
220 sb.append(value);
221 used += value.length();
222 }
223 }
224
225 return sb.toString();
226 }
227
228 // Quote a parameter value token if required.
229 private String quote(String value) {
230 if ("".equals(value))
231 return "\"\"";
232 return MimeUtility.quote(value, HeaderTokenizer.MIME);
233 }
234
235 public ParameterList copy() {
236 return new ParameterList((HashMap<String, String>)list.clone());
237 }
238 }
|