src/share/jaxws_classes/com/sun/xml/internal/messaging/saaj/packaging/mime/internet/ParameterList.java

Wed, 27 Apr 2016 01:27:09 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:27:09 +0800
changeset 0
373ffda63c9a
child 637
9c07ef4934dd
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17

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

mercurial