Invokes parser for extension header field.
IMPL_NOTE: optimize it (maybe use ParserCore.nameValue()?).
String name = lexer.getNextToken(':");
lexer.consume(1);
lexer.SPorHT();
String bodyWithParam = StringTokenizer.convertNewLines(
lexer.getRest().trim());
char colonDelimiter = ';";
int index = getDelimiterIndex(bodyWithParam, colonDelimiter);
if (index == -1) {
// no parameters were specified
// String body = lexer.getLine().trim();
ExtensionHeader retval = new ExtensionHeader(name,
bodyWithParam, bodyWithParam);
return retval;
} else {
int currPos = lexer.getPtr();
String body = StringTokenizer.convertNewLines(
lexer.getBuffer().substring(currPos, currPos + index).trim());
lexer.consume(index + 1);
lexer.SPorHT();
ExtensionHeader retval = new
ExtensionHeader(name, bodyWithParam, body);
String paramName, paramVal;
boolean headerEnd = false;
int eqIndex, semicolonIndex;
while (!headerEnd) {
paramName = lexer.peekLine().trim();
eqIndex = paramName.indexOf('=");
semicolonIndex = paramName.indexOf(colonDelimiter);
if ((eqIndex != -1) &&
(semicolonIndex > eqIndex || semicolonIndex == -1)) {
// parameter with a value
paramName = lexer.getString('=");
// take the rest of the line
paramVal = lexer.peekLine().trim();
// IMPL_NOTE: remove !!!
// System.out.println("*** Rest is '" + paramVal + "'");
if (paramVal.indexOf(colonDelimiter) != -1) {
paramVal = lexer.getString(colonDelimiter);
} else {
paramVal = lexer.getLine().trim();
headerEnd = true;
}
} else {
if (semicolonIndex != -1) {
// parameter without a value
paramName = lexer.getString(colonDelimiter);
} else {
// the last parameter without a value
paramName = lexer.getLine().trim();
headerEnd = true;
}
paramVal = "";
}
paramName = paramName.trim();
paramVal = paramVal.trim();
// IMPL_NOTE: remove !!!
// System.out.println("*** paramName = " + paramName);
// IMPL_NOTE: remove !!!
// System.out.println(">>> Adding '" +
// paramName + "' = '" + paramVal + "'");
// The following toLowerCase() call is required because
// some 'known' headers (like Accept-Language) haven't
// corresponding implementation classes, but
// ExtensionHeader class is used to represent them.
//
// So, here we have to care about 'known' parameters for
// the mentioned headers. For example, a parameter 'q'
// must be case-insensitive.
paramName = paramName.toLowerCase();
// Validate parameter's name and value
if (!Lexer.isValidName(paramName)) {
throw new ParseException("Invalid parameter's name.", 0);
}
if (!Lexer.isValidParameterValue(paramVal)) {
throw new ParseException("Invalid parameter's value.", 0);
}
if (retval.getParameter(paramName) != null) {
throw new ParseException(
"Duplicated parameter: " + paramName, 0);
}
retval.setParameter(paramName, paramVal);
} // end while()
return retval;
}