aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package javax.activation; aoqi@0: aoqi@0: import java.util.Hashtable; aoqi@0: import java.util.Enumeration; aoqi@0: import java.util.Locale; aoqi@0: aoqi@0: /** aoqi@0: * A parameter list of a MimeType aoqi@0: * as defined in RFC 2045 and 2046. The Primary type of the aoqi@0: * object must already be stripped off. aoqi@0: * aoqi@0: * @see javax.activation.MimeType aoqi@0: * aoqi@0: * @since 1.6 aoqi@0: */ aoqi@0: public class MimeTypeParameterList { aoqi@0: private Hashtable parameters; aoqi@0: aoqi@0: /** aoqi@0: * A string that holds all the special chars. aoqi@0: */ aoqi@0: private static final String TSPECIALS = "()<>@,;:/[]?=\\\""; aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * Default constructor. aoqi@0: */ aoqi@0: public MimeTypeParameterList() { aoqi@0: parameters = new Hashtable(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Constructs a new MimeTypeParameterList with the passed in data. aoqi@0: * aoqi@0: * @param parameterList an RFC 2045, 2046 compliant parameter list. aoqi@0: */ aoqi@0: public MimeTypeParameterList(String parameterList) aoqi@0: throws MimeTypeParseException { aoqi@0: parameters = new Hashtable(); aoqi@0: aoqi@0: // now parse rawdata aoqi@0: parse(parameterList); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * A routine for parsing the parameter list out of a String. aoqi@0: * aoqi@0: * @param parameterList an RFC 2045, 2046 compliant parameter list. aoqi@0: */ aoqi@0: protected void parse(String parameterList) throws MimeTypeParseException { aoqi@0: if (parameterList == null) aoqi@0: return; aoqi@0: aoqi@0: int length = parameterList.length(); aoqi@0: if (length <= 0) aoqi@0: return; aoqi@0: aoqi@0: int i; aoqi@0: char c; aoqi@0: for (i = skipWhiteSpace(parameterList, 0); aoqi@0: i < length && (c = parameterList.charAt(i)) == ';'; aoqi@0: i = skipWhiteSpace(parameterList, i)) { aoqi@0: int lastIndex; aoqi@0: String name; aoqi@0: String value; aoqi@0: aoqi@0: // eat the ';' aoqi@0: i++; aoqi@0: aoqi@0: // now parse the parameter name aoqi@0: aoqi@0: // skip whitespace aoqi@0: i = skipWhiteSpace(parameterList, i); aoqi@0: aoqi@0: // tolerate trailing semicolon, even though it violates the spec aoqi@0: if (i >= length) aoqi@0: return; aoqi@0: aoqi@0: // find the end of the token char run aoqi@0: lastIndex = i; aoqi@0: while ((i < length) && isTokenChar(parameterList.charAt(i))) aoqi@0: i++; aoqi@0: aoqi@0: name = parameterList.substring(lastIndex, i). aoqi@0: toLowerCase(Locale.ENGLISH); aoqi@0: aoqi@0: // now parse the '=' that separates the name from the value aoqi@0: i = skipWhiteSpace(parameterList, i); aoqi@0: aoqi@0: if (i >= length || parameterList.charAt(i) != '=') aoqi@0: throw new MimeTypeParseException( aoqi@0: "Couldn't find the '=' that separates a " + aoqi@0: "parameter name from its value."); aoqi@0: aoqi@0: // eat it and parse the parameter value aoqi@0: i++; aoqi@0: i = skipWhiteSpace(parameterList, i); aoqi@0: aoqi@0: if (i >= length) aoqi@0: throw new MimeTypeParseException( aoqi@0: "Couldn't find a value for parameter named " + name); aoqi@0: aoqi@0: // now find out whether or not we have a quoted value aoqi@0: c = parameterList.charAt(i); aoqi@0: if (c == '"') { aoqi@0: // yup it's quoted so eat it and capture the quoted string aoqi@0: i++; aoqi@0: if (i >= length) aoqi@0: throw new MimeTypeParseException( aoqi@0: "Encountered unterminated quoted parameter value."); aoqi@0: aoqi@0: lastIndex = i; aoqi@0: aoqi@0: // find the next unescaped quote aoqi@0: while (i < length) { aoqi@0: c = parameterList.charAt(i); aoqi@0: if (c == '"') aoqi@0: break; aoqi@0: if (c == '\\') { aoqi@0: // found an escape sequence aoqi@0: // so skip this and the aoqi@0: // next character aoqi@0: i++; aoqi@0: } aoqi@0: i++; aoqi@0: } aoqi@0: if (c != '"') aoqi@0: throw new MimeTypeParseException( aoqi@0: "Encountered unterminated quoted parameter value."); aoqi@0: aoqi@0: value = unquote(parameterList.substring(lastIndex, i)); aoqi@0: // eat the quote aoqi@0: i++; aoqi@0: } else if (isTokenChar(c)) { aoqi@0: // nope it's an ordinary token so it aoqi@0: // ends with a non-token char aoqi@0: lastIndex = i; aoqi@0: while (i < length && isTokenChar(parameterList.charAt(i))) aoqi@0: i++; aoqi@0: value = parameterList.substring(lastIndex, i); aoqi@0: } else { aoqi@0: // it ain't a value aoqi@0: throw new MimeTypeParseException( aoqi@0: "Unexpected character encountered at index " + i); aoqi@0: } aoqi@0: aoqi@0: // now put the data into the hashtable aoqi@0: parameters.put(name, value); aoqi@0: } aoqi@0: if (i < length) { aoqi@0: throw new MimeTypeParseException( aoqi@0: "More characters encountered in input than expected."); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return the number of name-value pairs in this list. aoqi@0: * aoqi@0: * @return the number of parameters aoqi@0: */ aoqi@0: public int size() { aoqi@0: return parameters.size(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Determine whether or not this list is empty. aoqi@0: * aoqi@0: * @return true if there are no parameters aoqi@0: */ aoqi@0: public boolean isEmpty() { aoqi@0: return parameters.isEmpty(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Retrieve the value associated with the given name, or null if there aoqi@0: * is no current association. aoqi@0: * aoqi@0: * @param name the parameter name aoqi@0: * @return the parameter's value aoqi@0: */ aoqi@0: public String get(String name) { aoqi@0: return (String)parameters.get(name.trim().toLowerCase(Locale.ENGLISH)); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Set the value to be associated with the given name, replacing aoqi@0: * any previous association. aoqi@0: * aoqi@0: * @param name the parameter name aoqi@0: * @param value the parameter's value aoqi@0: */ aoqi@0: public void set(String name, String value) { aoqi@0: parameters.put(name.trim().toLowerCase(Locale.ENGLISH), value); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Remove any value associated with the given name. aoqi@0: * aoqi@0: * @param name the parameter name aoqi@0: */ aoqi@0: public void remove(String name) { aoqi@0: parameters.remove(name.trim().toLowerCase(Locale.ENGLISH)); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Retrieve an enumeration of all the names in this list. aoqi@0: * aoqi@0: * @return an enumeration of all parameter names aoqi@0: */ aoqi@0: public Enumeration getNames() { aoqi@0: return parameters.keys(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Return a string representation of this object. aoqi@0: */ aoqi@0: public String toString() { aoqi@0: StringBuffer buffer = new StringBuffer(); aoqi@0: buffer.ensureCapacity(parameters.size() * 16); aoqi@0: // heuristic: 8 characters per field aoqi@0: aoqi@0: Enumeration keys = parameters.keys(); aoqi@0: while (keys.hasMoreElements()) { aoqi@0: String key = (String)keys.nextElement(); aoqi@0: buffer.append("; "); aoqi@0: buffer.append(key); aoqi@0: buffer.append('='); aoqi@0: buffer.append(quote((String)parameters.get(key))); aoqi@0: } aoqi@0: aoqi@0: return buffer.toString(); aoqi@0: } aoqi@0: aoqi@0: // below here be scary parsing related things aoqi@0: aoqi@0: /** aoqi@0: * Determine whether or not a given character belongs to a legal token. aoqi@0: */ aoqi@0: private static boolean isTokenChar(char c) { aoqi@0: return ((c > 040) && (c < 0177)) && (TSPECIALS.indexOf(c) < 0); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * return the index of the first non white space character in aoqi@0: * rawdata at or after index i. aoqi@0: */ aoqi@0: private static int skipWhiteSpace(String rawdata, int i) { aoqi@0: int length = rawdata.length(); aoqi@0: while ((i < length) && Character.isWhitespace(rawdata.charAt(i))) aoqi@0: i++; aoqi@0: return i; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * A routine that knows how and when to quote and escape the given value. aoqi@0: */ aoqi@0: private static String quote(String value) { aoqi@0: boolean needsQuotes = false; aoqi@0: aoqi@0: // check to see if we actually have to quote this thing aoqi@0: int length = value.length(); aoqi@0: for (int i = 0; (i < length) && !needsQuotes; i++) { aoqi@0: needsQuotes = !isTokenChar(value.charAt(i)); aoqi@0: } aoqi@0: aoqi@0: if (needsQuotes) { aoqi@0: StringBuffer buffer = new StringBuffer(); aoqi@0: buffer.ensureCapacity((int)(length * 1.5)); aoqi@0: aoqi@0: // add the initial quote aoqi@0: buffer.append('"'); aoqi@0: aoqi@0: // add the properly escaped text aoqi@0: for (int i = 0; i < length; ++i) { aoqi@0: char c = value.charAt(i); aoqi@0: if ((c == '\\') || (c == '"')) aoqi@0: buffer.append('\\'); aoqi@0: buffer.append(c); aoqi@0: } aoqi@0: aoqi@0: // add the closing quote aoqi@0: buffer.append('"'); aoqi@0: aoqi@0: return buffer.toString(); aoqi@0: } else { aoqi@0: return value; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * A routine that knows how to strip the quotes and aoqi@0: * escape sequences from the given value. aoqi@0: */ aoqi@0: private static String unquote(String value) { aoqi@0: int valueLength = value.length(); aoqi@0: StringBuffer buffer = new StringBuffer(); aoqi@0: buffer.ensureCapacity(valueLength); aoqi@0: aoqi@0: boolean escaped = false; aoqi@0: for (int i = 0; i < valueLength; ++i) { aoqi@0: char currentChar = value.charAt(i); aoqi@0: if (!escaped && (currentChar != '\\')) { aoqi@0: buffer.append(currentChar); aoqi@0: } else if (escaped) { aoqi@0: buffer.append(currentChar); aoqi@0: escaped = false; aoqi@0: } else { aoqi@0: escaped = true; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: return buffer.toString(); aoqi@0: } aoqi@0: }