1 /*
2 * Copyright (c) 1997, 2013, 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
27 * @(#)ParameterList.java 1.10 03/02/12
28 */
29
30
31
32 package com.sun.xml.internal.messaging.saaj.packaging.mime.internet;
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 list;
48
49 /**
50 * No-arg Constructor.
51 */
52 public ParameterList() {
53 this.list = new HashMap();
54 }
55
56 private ParameterList(HashMap 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();
113
114 /**
115 * Return the number of parameters in this list.
116 *
117 * @return number of parameters.
118 */
119 public int size() {
120 return list.size();
121 }
122
123 /**
124 * Returns the value of the specified parameter. Note that
125 * parameter names are case-insensitive.
126 *
127 * @param name parameter name.
128 * @return Value of the parameter. Returns
129 * <code>null</code> if the parameter is not
130 * present.
131 */
132 public String get(String name) {
133 return (String)list.get(name.trim().toLowerCase());
134 }
135
136 /**
137 * Set a parameter. If this parameter already exists, it is
138 * replaced by this new value.
139 *
140 * @param name name of the parameter.
141 * @param value value of the parameter.
142 */
143 public void set(String name, String value) {
144 list.put(name.trim().toLowerCase(), value);
145 }
146
147 /**
148 * Removes the specified parameter from this ParameterList.
149 * This method does nothing if the parameter is not present.
150 *
151 * @param name name of the parameter.
152 */
153 public void remove(String name) {
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 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 StringBuffer sb = new StringBuffer();
194 Iterator itr = list.entrySet().iterator();
195
196 while (itr.hasNext()) {
197 Map.Entry e = (Map.Entry)itr.next();
198 String name = (String)e.getKey();
199 String value = quote((String)e.getValue());
200 sb.append("; ");
201 used += 2;
202 int len = name.length() + value.length() + 1;
203 if (used + len > 76) { // overflows ...
204 sb.append("\r\n\t"); // .. start new continuation line
205 used = 8; // account for the starting <tab> char
206 }
207 sb.append(name).append('=');
208 used += name.length() + 1;
209 if (used + value.length() > 76) { // still overflows ...
210 // have to fold value
211 String s = MimeUtility.fold(used, value);
212 sb.append(s);
213 int lastlf = s.lastIndexOf('\n');
214 if (lastlf >= 0) // always true
215 used += s.length() - lastlf - 1;
216 else
217 used += s.length();
218 } else {
219 sb.append(value);
|
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
27 * @(#)ParameterList.java 1.10 03/02/12
28 */
29
30
31
32 package com.sun.xml.internal.messaging.saaj.packaging.mime.internet;
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();
113
114 /**
115 * Return the number of parameters in this list.
116 *
117 * @return number of parameters.
118 */
119 public int size() {
120 return list.size();
121 }
122
123 /**
124 * Returns the value of the specified parameter. Note that
125 * parameter names are case-insensitive.
126 *
127 * @param name parameter name.
128 * @return Value of the parameter. Returns
129 * <code>null</code> if the parameter is not
130 * present.
131 */
132 public String get(String name) {
133 return list.get(name.trim().toLowerCase());
134 }
135
136 /**
137 * Set a parameter. If this parameter already exists, it is
138 * replaced by this new value.
139 *
140 * @param name name of the parameter.
141 * @param value value of the parameter.
142 */
143 public void set(String name, String value) {
144 list.put(name.trim().toLowerCase(), value);
145 }
146
147 /**
148 * Removes the specified parameter from this ParameterList.
149 * This method does nothing if the parameter is not present.
150 *
151 * @param name name of the parameter.
152 */
153 public void remove(String name) {
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();
194 Iterator<Map.Entry<String, String>> itr = list.entrySet().iterator();
195
196 while (itr.hasNext()) {
197 Map.Entry<String, String> e = itr.next();
198 String name = e.getKey();
199 String value = quote(e.getValue());
200 sb.append("; ");
201 used += 2;
202 int len = name.length() + value.length() + 1;
203 if (used + len > 76) { // overflows ...
204 sb.append("\r\n\t"); // .. start new continuation line
205 used = 8; // account for the starting <tab> char
206 }
207 sb.append(name).append('=');
208 used += name.length() + 1;
209 if (used + value.length() > 76) { // still overflows ...
210 // have to fold value
211 String s = MimeUtility.fold(used, value);
212 sb.append(s);
213 int lastlf = s.lastIndexOf('\n');
214 if (lastlf >= 0) // always true
215 used += s.length() - lastlf - 1;
216 else
217 used += s.length();
218 } else {
219 sb.append(value);
|