protected void parse(String parameterList) throws MimeTypeParseException {
if (parameterList == null)
return;
int length = parameterList.length();
if (length < = 0)
return;
int i;
char c;
for (i = skipWhiteSpace(parameterList, 0);
i < length && (c = parameterList.charAt(i)) == ';';
i = skipWhiteSpace(parameterList, i)) {
int lastIndex;
String name;
String value;
// eat the ';'
i++;
// now parse the parameter name
// skip whitespace
i = skipWhiteSpace(parameterList, i);
// tolerate trailing semicolon, even though it violates the spec
if (i >= length)
return;
// find the end of the token char run
lastIndex = i;
while ((i < length) && isTokenChar(parameterList.charAt(i)))
i++;
name = parameterList.substring(lastIndex, i).
toLowerCase(Locale.ENGLISH);
// now parse the '=' that separates the name from the value
i = skipWhiteSpace(parameterList, i);
if (i >= length || parameterList.charAt(i) != '=')
throw new MimeTypeParseException(
"Couldn't find the '=' that separates a " +
"parameter name from its value.");
// eat it and parse the parameter value
i++;
i = skipWhiteSpace(parameterList, i);
if (i >= length)
throw new MimeTypeParseException(
"Couldn't find a value for parameter named " + name);
// now find out whether or not we have a quoted value
c = parameterList.charAt(i);
if (c == '"') {
// yup it's quoted so eat it and capture the quoted string
i++;
if (i >= length)
throw new MimeTypeParseException(
"Encountered unterminated quoted parameter value.");
lastIndex = i;
// find the next unescaped quote
while (i < length) {
c = parameterList.charAt(i);
if (c == '"')
break;
if (c == '\\') {
// found an escape sequence
// so skip this and the
// next character
i++;
}
i++;
}
if (c != '"')
throw new MimeTypeParseException(
"Encountered unterminated quoted parameter value.");
value = unquote(parameterList.substring(lastIndex, i));
// eat the quote
i++;
} else if (isTokenChar(c)) {
// nope it's an ordinary token so it
// ends with a non-token char
lastIndex = i;
while (i < length && isTokenChar(parameterList.charAt(i)))
i++;
value = parameterList.substring(lastIndex, i);
} else {
// it ain't a value
throw new MimeTypeParseException(
"Unexpected character encountered at index " + i);
}
// now put the data into the hashtable
parameters.put(name, value);
}
if (i < length) {
throw new MimeTypeParseException(
"More characters encountered in input than expected.");
}
}
A routine for parsing the parameter list out of a String. |