1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaf_classes/javax/activation/MimeTypeParameterList.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,339 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package javax.activation; 1.30 + 1.31 +import java.util.Hashtable; 1.32 +import java.util.Enumeration; 1.33 +import java.util.Locale; 1.34 + 1.35 +/** 1.36 + * A parameter list of a MimeType 1.37 + * as defined in RFC 2045 and 2046. The Primary type of the 1.38 + * object must already be stripped off. 1.39 + * 1.40 + * @see javax.activation.MimeType 1.41 + * 1.42 + * @since 1.6 1.43 + */ 1.44 +public class MimeTypeParameterList { 1.45 + private Hashtable parameters; 1.46 + 1.47 + /** 1.48 + * A string that holds all the special chars. 1.49 + */ 1.50 + private static final String TSPECIALS = "()<>@,;:/[]?=\\\""; 1.51 + 1.52 + 1.53 + /** 1.54 + * Default constructor. 1.55 + */ 1.56 + public MimeTypeParameterList() { 1.57 + parameters = new Hashtable(); 1.58 + } 1.59 + 1.60 + /** 1.61 + * Constructs a new MimeTypeParameterList with the passed in data. 1.62 + * 1.63 + * @param parameterList an RFC 2045, 2046 compliant parameter list. 1.64 + */ 1.65 + public MimeTypeParameterList(String parameterList) 1.66 + throws MimeTypeParseException { 1.67 + parameters = new Hashtable(); 1.68 + 1.69 + // now parse rawdata 1.70 + parse(parameterList); 1.71 + } 1.72 + 1.73 + /** 1.74 + * A routine for parsing the parameter list out of a String. 1.75 + * 1.76 + * @param parameterList an RFC 2045, 2046 compliant parameter list. 1.77 + */ 1.78 + protected void parse(String parameterList) throws MimeTypeParseException { 1.79 + if (parameterList == null) 1.80 + return; 1.81 + 1.82 + int length = parameterList.length(); 1.83 + if (length <= 0) 1.84 + return; 1.85 + 1.86 + int i; 1.87 + char c; 1.88 + for (i = skipWhiteSpace(parameterList, 0); 1.89 + i < length && (c = parameterList.charAt(i)) == ';'; 1.90 + i = skipWhiteSpace(parameterList, i)) { 1.91 + int lastIndex; 1.92 + String name; 1.93 + String value; 1.94 + 1.95 + // eat the ';' 1.96 + i++; 1.97 + 1.98 + // now parse the parameter name 1.99 + 1.100 + // skip whitespace 1.101 + i = skipWhiteSpace(parameterList, i); 1.102 + 1.103 + // tolerate trailing semicolon, even though it violates the spec 1.104 + if (i >= length) 1.105 + return; 1.106 + 1.107 + // find the end of the token char run 1.108 + lastIndex = i; 1.109 + while ((i < length) && isTokenChar(parameterList.charAt(i))) 1.110 + i++; 1.111 + 1.112 + name = parameterList.substring(lastIndex, i). 1.113 + toLowerCase(Locale.ENGLISH); 1.114 + 1.115 + // now parse the '=' that separates the name from the value 1.116 + i = skipWhiteSpace(parameterList, i); 1.117 + 1.118 + if (i >= length || parameterList.charAt(i) != '=') 1.119 + throw new MimeTypeParseException( 1.120 + "Couldn't find the '=' that separates a " + 1.121 + "parameter name from its value."); 1.122 + 1.123 + // eat it and parse the parameter value 1.124 + i++; 1.125 + i = skipWhiteSpace(parameterList, i); 1.126 + 1.127 + if (i >= length) 1.128 + throw new MimeTypeParseException( 1.129 + "Couldn't find a value for parameter named " + name); 1.130 + 1.131 + // now find out whether or not we have a quoted value 1.132 + c = parameterList.charAt(i); 1.133 + if (c == '"') { 1.134 + // yup it's quoted so eat it and capture the quoted string 1.135 + i++; 1.136 + if (i >= length) 1.137 + throw new MimeTypeParseException( 1.138 + "Encountered unterminated quoted parameter value."); 1.139 + 1.140 + lastIndex = i; 1.141 + 1.142 + // find the next unescaped quote 1.143 + while (i < length) { 1.144 + c = parameterList.charAt(i); 1.145 + if (c == '"') 1.146 + break; 1.147 + if (c == '\\') { 1.148 + // found an escape sequence 1.149 + // so skip this and the 1.150 + // next character 1.151 + i++; 1.152 + } 1.153 + i++; 1.154 + } 1.155 + if (c != '"') 1.156 + throw new MimeTypeParseException( 1.157 + "Encountered unterminated quoted parameter value."); 1.158 + 1.159 + value = unquote(parameterList.substring(lastIndex, i)); 1.160 + // eat the quote 1.161 + i++; 1.162 + } else if (isTokenChar(c)) { 1.163 + // nope it's an ordinary token so it 1.164 + // ends with a non-token char 1.165 + lastIndex = i; 1.166 + while (i < length && isTokenChar(parameterList.charAt(i))) 1.167 + i++; 1.168 + value = parameterList.substring(lastIndex, i); 1.169 + } else { 1.170 + // it ain't a value 1.171 + throw new MimeTypeParseException( 1.172 + "Unexpected character encountered at index " + i); 1.173 + } 1.174 + 1.175 + // now put the data into the hashtable 1.176 + parameters.put(name, value); 1.177 + } 1.178 + if (i < length) { 1.179 + throw new MimeTypeParseException( 1.180 + "More characters encountered in input than expected."); 1.181 + } 1.182 + } 1.183 + 1.184 + /** 1.185 + * Return the number of name-value pairs in this list. 1.186 + * 1.187 + * @return the number of parameters 1.188 + */ 1.189 + public int size() { 1.190 + return parameters.size(); 1.191 + } 1.192 + 1.193 + /** 1.194 + * Determine whether or not this list is empty. 1.195 + * 1.196 + * @return true if there are no parameters 1.197 + */ 1.198 + public boolean isEmpty() { 1.199 + return parameters.isEmpty(); 1.200 + } 1.201 + 1.202 + /** 1.203 + * Retrieve the value associated with the given name, or null if there 1.204 + * is no current association. 1.205 + * 1.206 + * @param name the parameter name 1.207 + * @return the parameter's value 1.208 + */ 1.209 + public String get(String name) { 1.210 + return (String)parameters.get(name.trim().toLowerCase(Locale.ENGLISH)); 1.211 + } 1.212 + 1.213 + /** 1.214 + * Set the value to be associated with the given name, replacing 1.215 + * any previous association. 1.216 + * 1.217 + * @param name the parameter name 1.218 + * @param value the parameter's value 1.219 + */ 1.220 + public void set(String name, String value) { 1.221 + parameters.put(name.trim().toLowerCase(Locale.ENGLISH), value); 1.222 + } 1.223 + 1.224 + /** 1.225 + * Remove any value associated with the given name. 1.226 + * 1.227 + * @param name the parameter name 1.228 + */ 1.229 + public void remove(String name) { 1.230 + parameters.remove(name.trim().toLowerCase(Locale.ENGLISH)); 1.231 + } 1.232 + 1.233 + /** 1.234 + * Retrieve an enumeration of all the names in this list. 1.235 + * 1.236 + * @return an enumeration of all parameter names 1.237 + */ 1.238 + public Enumeration getNames() { 1.239 + return parameters.keys(); 1.240 + } 1.241 + 1.242 + /** 1.243 + * Return a string representation of this object. 1.244 + */ 1.245 + public String toString() { 1.246 + StringBuffer buffer = new StringBuffer(); 1.247 + buffer.ensureCapacity(parameters.size() * 16); 1.248 + // heuristic: 8 characters per field 1.249 + 1.250 + Enumeration keys = parameters.keys(); 1.251 + while (keys.hasMoreElements()) { 1.252 + String key = (String)keys.nextElement(); 1.253 + buffer.append("; "); 1.254 + buffer.append(key); 1.255 + buffer.append('='); 1.256 + buffer.append(quote((String)parameters.get(key))); 1.257 + } 1.258 + 1.259 + return buffer.toString(); 1.260 + } 1.261 + 1.262 + // below here be scary parsing related things 1.263 + 1.264 + /** 1.265 + * Determine whether or not a given character belongs to a legal token. 1.266 + */ 1.267 + private static boolean isTokenChar(char c) { 1.268 + return ((c > 040) && (c < 0177)) && (TSPECIALS.indexOf(c) < 0); 1.269 + } 1.270 + 1.271 + /** 1.272 + * return the index of the first non white space character in 1.273 + * rawdata at or after index i. 1.274 + */ 1.275 + private static int skipWhiteSpace(String rawdata, int i) { 1.276 + int length = rawdata.length(); 1.277 + while ((i < length) && Character.isWhitespace(rawdata.charAt(i))) 1.278 + i++; 1.279 + return i; 1.280 + } 1.281 + 1.282 + /** 1.283 + * A routine that knows how and when to quote and escape the given value. 1.284 + */ 1.285 + private static String quote(String value) { 1.286 + boolean needsQuotes = false; 1.287 + 1.288 + // check to see if we actually have to quote this thing 1.289 + int length = value.length(); 1.290 + for (int i = 0; (i < length) && !needsQuotes; i++) { 1.291 + needsQuotes = !isTokenChar(value.charAt(i)); 1.292 + } 1.293 + 1.294 + if (needsQuotes) { 1.295 + StringBuffer buffer = new StringBuffer(); 1.296 + buffer.ensureCapacity((int)(length * 1.5)); 1.297 + 1.298 + // add the initial quote 1.299 + buffer.append('"'); 1.300 + 1.301 + // add the properly escaped text 1.302 + for (int i = 0; i < length; ++i) { 1.303 + char c = value.charAt(i); 1.304 + if ((c == '\\') || (c == '"')) 1.305 + buffer.append('\\'); 1.306 + buffer.append(c); 1.307 + } 1.308 + 1.309 + // add the closing quote 1.310 + buffer.append('"'); 1.311 + 1.312 + return buffer.toString(); 1.313 + } else { 1.314 + return value; 1.315 + } 1.316 + } 1.317 + 1.318 + /** 1.319 + * A routine that knows how to strip the quotes and 1.320 + * escape sequences from the given value. 1.321 + */ 1.322 + private static String unquote(String value) { 1.323 + int valueLength = value.length(); 1.324 + StringBuffer buffer = new StringBuffer(); 1.325 + buffer.ensureCapacity(valueLength); 1.326 + 1.327 + boolean escaped = false; 1.328 + for (int i = 0; i < valueLength; ++i) { 1.329 + char currentChar = value.charAt(i); 1.330 + if (!escaped && (currentChar != '\\')) { 1.331 + buffer.append(currentChar); 1.332 + } else if (escaped) { 1.333 + buffer.append(currentChar); 1.334 + escaped = false; 1.335 + } else { 1.336 + escaped = true; 1.337 + } 1.338 + } 1.339 + 1.340 + return buffer.toString(); 1.341 + } 1.342 +}