Tue, 09 Apr 2013 14:51:13 +0100
8010393: Update JAX-WS RI to 2.2.9-b12941
Reviewed-by: alanb, erikj
Contributed-by: miroslav.kos@oracle.com, martin.grebac@oracle.com
ohair@286 | 1 | /* |
alanb@368 | 2 | * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
ohair@286 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ohair@286 | 4 | * |
ohair@286 | 5 | * This code is free software; you can redistribute it and/or modify it |
ohair@286 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@286 | 7 | * published by the Free Software Foundation. Oracle designates this |
ohair@286 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@286 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
ohair@286 | 10 | * |
ohair@286 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ohair@286 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ohair@286 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ohair@286 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
ohair@286 | 15 | * accompanied this code). |
ohair@286 | 16 | * |
ohair@286 | 17 | * You should have received a copy of the GNU General Public License version |
ohair@286 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
ohair@286 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ohair@286 | 20 | * |
ohair@286 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@286 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@286 | 23 | * questions. |
ohair@286 | 24 | */ |
ohair@286 | 25 | |
ohair@286 | 26 | /* |
ohair@286 | 27 | * @(#)ParameterList.java 1.10 03/02/12 |
ohair@286 | 28 | */ |
ohair@286 | 29 | |
ohair@286 | 30 | |
ohair@286 | 31 | |
ohair@286 | 32 | package com.sun.xml.internal.messaging.saaj.packaging.mime.internet; |
ohair@286 | 33 | |
ohair@286 | 34 | import java.util.HashMap; |
ohair@286 | 35 | import java.util.Iterator; |
ohair@286 | 36 | import java.util.Map; |
ohair@286 | 37 | |
ohair@286 | 38 | /** |
ohair@286 | 39 | * This class holds MIME parameters (attribute-value pairs). |
ohair@286 | 40 | * |
ohair@286 | 41 | * @version 1.10, 03/02/12 |
ohair@286 | 42 | * @author John Mani |
ohair@286 | 43 | */ |
ohair@286 | 44 | |
ohair@286 | 45 | public final class ParameterList { |
ohair@286 | 46 | |
ohair@286 | 47 | private final HashMap list; |
ohair@286 | 48 | |
ohair@286 | 49 | /** |
ohair@286 | 50 | * No-arg Constructor. |
ohair@286 | 51 | */ |
ohair@286 | 52 | public ParameterList() { |
ohair@286 | 53 | this.list = new HashMap(); |
ohair@286 | 54 | } |
ohair@286 | 55 | |
ohair@286 | 56 | private ParameterList(HashMap m) { |
ohair@286 | 57 | this.list = m; |
ohair@286 | 58 | } |
ohair@286 | 59 | |
ohair@286 | 60 | /** |
ohair@286 | 61 | * Constructor that takes a parameter-list string. The String |
ohair@286 | 62 | * is parsed and the parameters are collected and stored internally. |
ohair@286 | 63 | * A ParseException is thrown if the parse fails. |
ohair@286 | 64 | * Note that an empty parameter-list string is valid and will be |
ohair@286 | 65 | * parsed into an empty ParameterList. |
ohair@286 | 66 | * |
ohair@286 | 67 | * @param s the parameter-list string. |
ohair@286 | 68 | * @exception ParseException if the parse fails. |
ohair@286 | 69 | */ |
ohair@286 | 70 | public ParameterList(String s) throws ParseException { |
ohair@286 | 71 | HeaderTokenizer h = new HeaderTokenizer(s, HeaderTokenizer.MIME); |
ohair@286 | 72 | HeaderTokenizer.Token tk; |
ohair@286 | 73 | int type; |
ohair@286 | 74 | String name; |
ohair@286 | 75 | |
ohair@286 | 76 | list = new HashMap(); |
ohair@286 | 77 | while (true) { |
ohair@286 | 78 | tk = h.next(); |
ohair@286 | 79 | type = tk.getType(); |
ohair@286 | 80 | |
ohair@286 | 81 | if (type == HeaderTokenizer.Token.EOF) // done |
ohair@286 | 82 | return; |
ohair@286 | 83 | |
ohair@286 | 84 | if ((char)type == ';') { |
ohair@286 | 85 | // expect parameter name |
ohair@286 | 86 | tk = h.next(); |
ohair@286 | 87 | // tolerate trailing semicolon, even though it violates the spec |
ohair@286 | 88 | if (tk.getType() == HeaderTokenizer.Token.EOF) |
ohair@286 | 89 | return; |
ohair@286 | 90 | // parameter name must be a MIME Atom |
ohair@286 | 91 | if (tk.getType() != HeaderTokenizer.Token.ATOM) |
ohair@286 | 92 | throw new ParseException(); |
ohair@286 | 93 | name = tk.getValue().toLowerCase(); |
ohair@286 | 94 | |
ohair@286 | 95 | // expect '=' |
ohair@286 | 96 | tk = h.next(); |
ohair@286 | 97 | if ((char)tk.getType() != '=') |
ohair@286 | 98 | throw new ParseException(); |
ohair@286 | 99 | |
ohair@286 | 100 | // expect parameter value |
ohair@286 | 101 | tk = h.next(); |
ohair@286 | 102 | type = tk.getType(); |
ohair@286 | 103 | // parameter value must be a MIME Atom or Quoted String |
ohair@286 | 104 | if (type != HeaderTokenizer.Token.ATOM && |
ohair@286 | 105 | type != HeaderTokenizer.Token.QUOTEDSTRING) |
ohair@286 | 106 | throw new ParseException(); |
ohair@286 | 107 | |
ohair@286 | 108 | list.put(name, tk.getValue()); |
ohair@286 | 109 | } else |
ohair@286 | 110 | throw new ParseException(); |
ohair@286 | 111 | } |
ohair@286 | 112 | } |
ohair@286 | 113 | |
ohair@286 | 114 | /** |
ohair@286 | 115 | * Return the number of parameters in this list. |
ohair@286 | 116 | * |
ohair@286 | 117 | * @return number of parameters. |
ohair@286 | 118 | */ |
ohair@286 | 119 | public int size() { |
ohair@286 | 120 | return list.size(); |
ohair@286 | 121 | } |
ohair@286 | 122 | |
ohair@286 | 123 | /** |
ohair@286 | 124 | * Returns the value of the specified parameter. Note that |
ohair@286 | 125 | * parameter names are case-insensitive. |
ohair@286 | 126 | * |
ohair@286 | 127 | * @param name parameter name. |
ohair@286 | 128 | * @return Value of the parameter. Returns |
ohair@286 | 129 | * <code>null</code> if the parameter is not |
ohair@286 | 130 | * present. |
ohair@286 | 131 | */ |
ohair@286 | 132 | public String get(String name) { |
ohair@286 | 133 | return (String)list.get(name.trim().toLowerCase()); |
ohair@286 | 134 | } |
ohair@286 | 135 | |
ohair@286 | 136 | /** |
ohair@286 | 137 | * Set a parameter. If this parameter already exists, it is |
ohair@286 | 138 | * replaced by this new value. |
ohair@286 | 139 | * |
ohair@286 | 140 | * @param name name of the parameter. |
ohair@286 | 141 | * @param value value of the parameter. |
ohair@286 | 142 | */ |
ohair@286 | 143 | public void set(String name, String value) { |
ohair@286 | 144 | list.put(name.trim().toLowerCase(), value); |
ohair@286 | 145 | } |
ohair@286 | 146 | |
ohair@286 | 147 | /** |
ohair@286 | 148 | * Removes the specified parameter from this ParameterList. |
ohair@286 | 149 | * This method does nothing if the parameter is not present. |
ohair@286 | 150 | * |
ohair@286 | 151 | * @param name name of the parameter. |
ohair@286 | 152 | */ |
ohair@286 | 153 | public void remove(String name) { |
ohair@286 | 154 | list.remove(name.trim().toLowerCase()); |
ohair@286 | 155 | } |
ohair@286 | 156 | |
ohair@286 | 157 | /** |
ohair@286 | 158 | * Return an enumeration of the names of all parameters in this |
ohair@286 | 159 | * list. |
ohair@286 | 160 | * |
ohair@286 | 161 | * @return Enumeration of all parameter names in this list. |
ohair@286 | 162 | */ |
ohair@286 | 163 | public Iterator getNames() { |
ohair@286 | 164 | return list.keySet().iterator(); |
ohair@286 | 165 | } |
ohair@286 | 166 | |
ohair@286 | 167 | |
ohair@286 | 168 | /** |
ohair@286 | 169 | * Convert this ParameterList into a MIME String. If this is |
ohair@286 | 170 | * an empty list, an empty string is returned. |
ohair@286 | 171 | * |
ohair@286 | 172 | * @return String |
ohair@286 | 173 | */ |
ohair@286 | 174 | public String toString() { |
ohair@286 | 175 | return toString(0); |
ohair@286 | 176 | } |
ohair@286 | 177 | |
ohair@286 | 178 | /** |
ohair@286 | 179 | * Convert this ParameterList into a MIME String. If this is |
ohair@286 | 180 | * an empty list, an empty string is returned. |
ohair@286 | 181 | * |
ohair@286 | 182 | * The 'used' parameter specifies the number of character positions |
ohair@286 | 183 | * already taken up in the field into which the resulting parameter |
ohair@286 | 184 | * list is to be inserted. It's used to determine where to fold the |
ohair@286 | 185 | * resulting parameter list. |
ohair@286 | 186 | * |
ohair@286 | 187 | * @param used number of character positions already used, in |
ohair@286 | 188 | * the field into which the parameter list is to |
ohair@286 | 189 | * be inserted. |
ohair@286 | 190 | * @return String |
ohair@286 | 191 | */ |
ohair@286 | 192 | public String toString(int used) { |
ohair@286 | 193 | StringBuffer sb = new StringBuffer(); |
ohair@286 | 194 | Iterator itr = list.entrySet().iterator(); |
ohair@286 | 195 | |
ohair@286 | 196 | while (itr.hasNext()) { |
ohair@286 | 197 | Map.Entry e = (Map.Entry)itr.next(); |
ohair@286 | 198 | String name = (String)e.getKey(); |
ohair@286 | 199 | String value = quote((String)e.getValue()); |
ohair@286 | 200 | sb.append("; "); |
ohair@286 | 201 | used += 2; |
ohair@286 | 202 | int len = name.length() + value.length() + 1; |
ohair@286 | 203 | if (used + len > 76) { // overflows ... |
ohair@286 | 204 | sb.append("\r\n\t"); // .. start new continuation line |
ohair@286 | 205 | used = 8; // account for the starting <tab> char |
ohair@286 | 206 | } |
ohair@286 | 207 | sb.append(name).append('='); |
ohair@286 | 208 | used += name.length() + 1; |
ohair@286 | 209 | if (used + value.length() > 76) { // still overflows ... |
ohair@286 | 210 | // have to fold value |
ohair@286 | 211 | String s = MimeUtility.fold(used, value); |
ohair@286 | 212 | sb.append(s); |
ohair@286 | 213 | int lastlf = s.lastIndexOf('\n'); |
ohair@286 | 214 | if (lastlf >= 0) // always true |
ohair@286 | 215 | used += s.length() - lastlf - 1; |
ohair@286 | 216 | else |
ohair@286 | 217 | used += s.length(); |
ohair@286 | 218 | } else { |
ohair@286 | 219 | sb.append(value); |
ohair@286 | 220 | used += value.length(); |
ohair@286 | 221 | } |
ohair@286 | 222 | } |
ohair@286 | 223 | |
ohair@286 | 224 | return sb.toString(); |
ohair@286 | 225 | } |
ohair@286 | 226 | |
ohair@286 | 227 | // Quote a parameter value token if required. |
ohair@286 | 228 | private String quote(String value) { |
alanb@368 | 229 | if ("".equals(value)) |
alanb@368 | 230 | return "\"\""; |
ohair@286 | 231 | return MimeUtility.quote(value, HeaderTokenizer.MIME); |
ohair@286 | 232 | } |
ohair@286 | 233 | |
ohair@286 | 234 | public ParameterList copy() { |
ohair@286 | 235 | return new ParameterList((HashMap)list.clone()); |
ohair@286 | 236 | } |
ohair@286 | 237 | } |