}
result.append (c);
}
result.append ('\"');
return result.toString();
}
/**
* Parses the given string into canonical pieces and stores the pieces in
* {@link #myPieces <CODE>myPieces</CODE>}.
* <P>
* Special rules applied:
* <UL>
* <LI> If the media type is text, the value of a charset parameter is
* converted to lowercase.
* </UL>
*
* @param s MIME media type string.
*
* @exception NullPointerException
* (unchecked exception) Thrown if <CODE>s</CODE> is null.
* @exception IllegalArgumentException
* (unchecked exception) Thrown if <CODE>s</CODE> does not obey the
* syntax for a MIME media type string.
*/
private void parse(String s) {
// Initialize.
if (s == null) {
throw new NullPointerException();
}
LexicalAnalyzer theLexer = new LexicalAnalyzer (s);
int theLexemeType;
Vector thePieces = new Vector();
boolean mediaTypeIsText = false;
boolean parameterNameIsCharset = false;
// Parse media type.
if (theLexer.getLexemeType() == TOKEN_LEXEME) {
String mt = toUnicodeLowerCase (theLexer.getLexeme());
thePieces.add (mt);
theLexer.nextLexeme();
mediaTypeIsText = mt.equals ("text");
} else {
throw new IllegalArgumentException();
}
// Parse slash.
if (theLexer.getLexemeType() == TSPECIAL_LEXEME &&
theLexer.getLexemeFirstCharacter() == '/') {
theLexer.nextLexeme();
} else {
throw new IllegalArgumentException();
}
if (theLexer.getLexemeType() == TOKEN_LEXEME) {
thePieces.add (toUnicodeLowerCase (theLexer.getLexeme()));
theLexer.nextLexeme();
} else {
throw new IllegalArgumentException();
}
// Parse zero or more parameters.
while (theLexer.getLexemeType() == TSPECIAL_LEXEME &&
theLexer.getLexemeFirstCharacter() == ';') {
// Parse semicolon.
theLexer.nextLexeme();
// Parse parameter name.
if (theLexer.getLexemeType() == TOKEN_LEXEME) {
String pn = toUnicodeLowerCase (theLexer.getLexeme());
thePieces.add (pn);
theLexer.nextLexeme();
parameterNameIsCharset = pn.equals ("charset");
} else {
throw new IllegalArgumentException();
}
// Parse equals.
if (theLexer.getLexemeType() == TSPECIAL_LEXEME &&
theLexer.getLexemeFirstCharacter() == '=') {
theLexer.nextLexeme();
} else {
throw new IllegalArgumentException();
}
// Parse parameter value.
if (theLexer.getLexemeType() == TOKEN_LEXEME) {
String pv = theLexer.getLexeme();
thePieces.add(mediaTypeIsText && parameterNameIsCharset ?
toUnicodeLowerCase (pv) :
pv);
theLexer.nextLexeme();
} else if (theLexer.getLexemeType() == QUOTED_STRING_LEXEME) {
String pv = removeBackslashes (theLexer.getLexeme());
thePieces.add(mediaTypeIsText && parameterNameIsCharset ?
toUnicodeLowerCase (pv) :
pv);
theLexer.nextLexeme();
} else {
throw new IllegalArgumentException();
}
}
=6= |