Top Description Inners Fields Constructors Methods
javax.mail.internet

public Class HeaderTokenizer

extends Object
Class Inheritance
Imports
java.util.*

This class tokenizes RFC822 and MIME headers into the basic symbols specified by RFC822 and MIME.

This class handles folded headers (ie headers with embedded CRLF SPACE sequences). The folds are removed in the returned tokens.

Authors
John Mani, Bill Shannon

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
HeaderTokenizer.Token

The Token class represents tokens returned by the HeaderTokenizer.

Field Summary

Modifier and TypeField and Description
private int
private String
private static final HeaderTokenizer.Token
private int
public static final String
MIME

MIME specials

private int
private int
public static final String
RFC822

RFC822 specials

private boolean
private String

Constructor Summary

AccessConstructor and Description
public
HeaderTokenizer(String
The rfc822 header to be tokenized
header
,
String
Set of delimiter characters to be used to delimit ATOMS. These are usually RFC822 or MIME
delimiters
,
boolean
If true, comments are skipped and not returned as tokens
skipComments
)

Constructor that takes a rfc822 style header.

public
HeaderTokenizer(String
The header that is tokenized
header
,
String
The delimiters to be used
delimiters
)

Constructor.

public
HeaderTokenizer(String
the header string
header
)

Constructor.

Method Summary

Modifier and TypeMethod and Description
private HeaderTokenizer.Token
collectString(char eos, boolean keepEscapes)

private static String
filterToken(String s, int start, int end, boolean keepEscapes)

private HeaderTokenizer.Token
getNext(char endOfAtom, boolean keepEscapes)

public String

Returns:

String rest of header. null is returned if we are already at end of header
getRemainder
()

Return the rest of the Header.

public HeaderTokenizer.Token

Returns:

the next Token
next
()

Parses the next token from this String.

public HeaderTokenizer.Token

Returns:

the next Token
next
(char
if not NUL, character marking end of token
endOfAtom
)

Parses the next token from this String.

public HeaderTokenizer.Token

Returns:

the next Token
next
(char
if not NUL, character marking end of token
endOfAtom
,
boolean
keep all backslashes in returned string?
keepEscapes
)

Parses the next token from this String.

public HeaderTokenizer.Token

Returns:

the next Token
peek
()

Peek at the next token, without actually removing the token from the parse stream.

private int
private static String
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

currentPosback to summary
private int currentPos
delimitersback to summary
private String delimiters
EOFTokenback to summary
private static final HeaderTokenizer.Token EOFToken
maxPosback to summary
private int maxPos
MIMEback to summary
public static final String MIME

MIME specials

nextPosback to summary
private int nextPos
peekPosback to summary
private int peekPos
RFC822back to summary
public static final String RFC822

RFC822 specials

skipCommentsback to summary
private boolean skipComments
stringback to summary
private String string

Constructor Detail

HeaderTokenizerback to summary
public HeaderTokenizer(String header, String delimiters, boolean skipComments)

Constructor that takes a rfc822 style header.

Parameters
header:String

The rfc822 header to be tokenized

delimiters:String

Set of delimiter characters to be used to delimit ATOMS. These are usually RFC822 or MIME

skipComments:boolean

If true, comments are skipped and not returned as tokens

HeaderTokenizerback to summary
public HeaderTokenizer(String header, String delimiters)

Constructor. Comments are ignored and not returned as tokens

Parameters
header:String

The header that is tokenized

delimiters:String

The delimiters to be used

HeaderTokenizerback to summary
public HeaderTokenizer(String header)

Constructor. The RFC822 defined delimiters - RFC822 - are used to delimit ATOMS. Also comments are skipped and not returned as tokens

Parameters
header:String

the header string

Method Detail

collectStringback to summary
private HeaderTokenizer.Token collectString(char eos, boolean keepEscapes) throws ParseException
filterTokenback to summary
private static String filterToken(String s, int start, int end, boolean keepEscapes)
getNextback to summary
private HeaderTokenizer.Token getNext(char endOfAtom, boolean keepEscapes) throws ParseException
getRemainderback to summary
public String getRemainder()

Return the rest of the Header.

Returns:String

String rest of header. null is returned if we are already at end of header

nextback to summary
public HeaderTokenizer.Token next() throws ParseException

Parses the next token from this String.

Clients sit in a loop calling next() to parse successive tokens until an EOF Token is returned.

Returns:HeaderTokenizer.Token

the next Token

Exceptions
ParseException:
if the parse fails
nextback to summary
public HeaderTokenizer.Token next(char endOfAtom) throws ParseException

Parses the next token from this String. If endOfAtom is not NUL, the token extends until the endOfAtom character is seen, or to the end of the header. This method is useful when parsing headers that don't obey the MIME specification, e.g., by failing to quote parameter values that contain spaces.

Parameters
endOfAtom:char

if not NUL, character marking end of token

Returns:HeaderTokenizer.Token

the next Token

Exceptions
ParseException:
if the parse fails
Since
JavaMail 1.5
nextback to summary
public HeaderTokenizer.Token next(char endOfAtom, boolean keepEscapes) throws ParseException

Parses the next token from this String. endOfAtom is handled as above. If keepEscapes is true, any backslash escapes are preserved in the returned string. This method is useful when parsing headers that don't obey the MIME specification, e.g., by failing to escape backslashes in the filename parameter.

Parameters
endOfAtom:char

if not NUL, character marking end of token

keepEscapes:boolean

keep all backslashes in returned string?

Returns:HeaderTokenizer.Token

the next Token

Exceptions
ParseException:
if the parse fails
Since
JavaMail 1.5
peekback to summary
public HeaderTokenizer.Token peek() throws ParseException

Peek at the next token, without actually removing the token from the parse stream. Invoking this method multiple times will return successive tokens, until next() is called.

Returns:HeaderTokenizer.Token

the next Token

Exceptions
ParseException:
if the parse fails
skipWhiteSpaceback to summary
private int skipWhiteSpace()
trimWhiteSpaceback to summary
private static String trimWhiteSpace(String s)
javax.mail.internet back to summary

public Class HeaderTokenizer.Token

extends Object
Class Inheritance

The Token class represents tokens returned by the HeaderTokenizer.

Field Summary

Modifier and TypeField and Description
public static final int
ATOM

Token type indicating an ATOM.

public static final int
COMMENT

Token type indicating a comment.

public static final int
EOF

Token type indicating end of input.

public static final int
QUOTEDSTRING

Token type indicating a quoted string.

private int
private String

Constructor Summary

AccessConstructor and Description
public
Token(int
Token type
type
,
String
Token value
value
)

Constructor.

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

the token type
getType
()

Return the type of the token.

public String

Returns:

token value
getValue
()

Returns the value of the token just read.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ATOMback to summary
public static final int ATOM

Token type indicating an ATOM.

COMMENTback to summary
public static final int COMMENT

Token type indicating a comment. The value field contains the comment string without the comment start and end symbols.

EOFback to summary
public static final int EOF

Token type indicating end of input.

QUOTEDSTRINGback to summary
public static final int QUOTEDSTRING

Token type indicating a quoted string. The value field contains the string without the quotes.

typeback to summary
private int type
valueback to summary
private String value

Constructor Detail

Tokenback to summary
public Token(int type, String value)

Constructor.

Parameters
type:int

Token type

value:String

Token value

Method Detail

getTypeback to summary
public int getType()

Return the type of the token. If the token represents a delimiter or a control character, the type is that character itself, converted to an integer. Otherwise, it's value is one of the following:

  • ATOM A sequence of ASCII characters delimited by either SPACE, CTL, "(", <"> or the specified SPECIALS
  • QUOTEDSTRING A sequence of ASCII characters within quotes
  • COMMENT A sequence of ASCII characters within "(" and ")".
  • EOF End of header
Returns:int

the token type

getValueback to summary
public String getValue()

Returns the value of the token just read. When the current token is a quoted string, this field contains the body of the string, without the quotes. When the current token is a comment, this field contains the body of the comment.

Returns:String

token value