org.apache.geronimo.javamail.store.imap.connection
Class IMAPResponseTokenizer

java.lang.Object
  extended by org.apache.geronimo.javamail.store.imap.connection.IMAPResponseTokenizer

public class IMAPResponseTokenizer
extends Object

Version:
$Rev: 670052 $ $Date: 2008-06-20 16:15:52 -0400 (Fri, 20 Jun 2008) $

Nested Class Summary
static class IMAPResponseTokenizer.Token
           
 
Field Summary
protected static MailDateFormat dateParser
           
protected static byte[] decodingTable
           
static IMAPResponseTokenizer.Token EOF
           
static IMAPResponseTokenizer.Token NIL
           
 
Constructor Summary
IMAPResponseTokenizer(byte[] response)
           
 
Method Summary
 void checkLeftParen()
          Ensure that the next token in the parsed response is a '(' character.
 boolean checkListEnd()
          Check to see if we're at the end of a parenthized list without advancing the parsing pointer.
 void checkRightParen()
          Ensure that the next token in the parsed response is a ')' character.
 void checkToken(int type)
          Get the next token and verify that it's of the expected type for the context.
 String decode(String original)
          Decode a Base 64 encoded string value.
static int decode(String original, int index, StringBuffer result)
          Decode a section of an encoded string value.
static int getEncoding(byte[] value)
          Determine what type of token encoding needs to be used for a string value.
 String getRemainder()
          Get the remainder of the response as a string.
 boolean hasMore()
          Test if we have more data in the response buffer.
 int indexOf(String needle)
          Search for a given string starting from the current position cursor.
 int indexOf(String needle, int position)
          Search for a string in the response buffer starting from the indicated position.
protected static void initializeDecodingTable()
           
 boolean match(int position, String needle)
          Test if the bytes in the response buffer match a given string value.
 IMAPResponseTokenizer.Token next()
           
 IMAPResponseTokenizer.Token next(boolean nilAllowed)
           
 IMAPResponseTokenizer.Token next(boolean nilAllowed, boolean expandedDelimiters)
           
 boolean notListEnd()
          Tests if we've reached the end of a parenthetical list in our parsing stream.
 IMAPResponseTokenizer.Token peek()
           
 IMAPResponseTokenizer.Token peek(boolean nilAllowed)
           
 IMAPResponseTokenizer.Token peek(boolean nilAllowed, boolean expandedDelimiters)
           
 InternetAddress readAddress()
          Read an internet address from a Fetch response.
 InternetAddress[] readAddressList()
          Parse out a list of addresses.
 String readAtom()
          Read a string-valued token from the response, verifying this is an ATOM token.
 String readAtom(boolean expandedDelimiters)
          Read a string-valued token from the response, verifying this is an ATOM token.
 byte[] readByteArray()
          Read the next token as binary data.
 Date readDate()
          Read a date from a response string.
 Date readDateOrNil()
          Read a date from a response string.
 String readEncodedString()
          Read an encoded string-valued token from the response.
 Flags readFlagList()
          Read a list of Flag values from an IMAP response, returning a Flags instance containing the appropriate pieces.
 int readInteger()
          Read a number-valued token from the response.
protected  IMAPResponseTokenizer.Token readLiteral()
          Parse out a literal string from the response, using the length encoded before the listeral.
protected  byte[] readLiteralData()
          Parse out a literal string from the response, using the length encoded before the listeral.
 int readLong()
          Read a number-valued token from the response.
 ParameterList readParameterList()
          Read a ContentType or ContentDisposition parameter list from an IMAP command response.
protected  String readQuotedStringOrNil()
          Read a quoted string-valued token from the response.
 String readString()
          Read a string-valued token from the response.
 List readStringList()
          Reads a string item which can be encoded either as a single string-valued token or a parenthized list of string tokens.
 String readStringOrNil()
          Read a string-valued token from the response.
 List readStrings()
          Reads all remaining tokens and returns them as a list of strings.
 List readSystemNameList()
          Read a list of Flag values from an IMAP response, returning a Flags instance containing the appropriate pieces.
 void skipExtensionItem()
          Skip over an extension item.
 void skipNestedValue()
          Skip over a parenthized value that we're not interested in.
protected  byte[] subarray(int start, int end)
          Extract a subarray from the response buffer.
protected  String substring(int start, int end)
          Extract a substring from the response buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

decodingTable

protected static final byte[] decodingTable

dateParser

protected static MailDateFormat dateParser

EOF

public static final IMAPResponseTokenizer.Token EOF

NIL

public static final IMAPResponseTokenizer.Token NIL
Constructor Detail

IMAPResponseTokenizer

public IMAPResponseTokenizer(byte[] response)
Method Detail

initializeDecodingTable

protected static void initializeDecodingTable()

getRemainder

public String getRemainder()
Get the remainder of the response as a string.

Returns:
A string representing the remainder of the response.

next

public IMAPResponseTokenizer.Token next()
                                 throws MessagingException
Throws:
MessagingException

next

public IMAPResponseTokenizer.Token next(boolean nilAllowed)
                                 throws MessagingException
Throws:
MessagingException

next

public IMAPResponseTokenizer.Token next(boolean nilAllowed,
                                        boolean expandedDelimiters)
                                 throws MessagingException
Throws:
MessagingException

peek

public IMAPResponseTokenizer.Token peek()
                                 throws MessagingException
Throws:
MessagingException

peek

public IMAPResponseTokenizer.Token peek(boolean nilAllowed)
                                 throws MessagingException
Throws:
MessagingException

peek

public IMAPResponseTokenizer.Token peek(boolean nilAllowed,
                                        boolean expandedDelimiters)
                                 throws MessagingException
Throws:
MessagingException

readLiteral

protected IMAPResponseTokenizer.Token readLiteral()
                                           throws MessagingException
Parse out a literal string from the response, using the length encoded before the listeral.

Returns:
The LITERAL token with the value.
Throws:
ResponseFormatException
MessagingException

readLiteralData

protected byte[] readLiteralData()
                          throws MessagingException
Parse out a literal string from the response, using the length encoded before the listeral.

Returns:
The byte[] array with the value.
Throws:
ResponseFormatException
MessagingException

substring

protected String substring(int start,
                           int end)
Extract a substring from the response buffer.

Parameters:
start - The starting offset.
end - The end offset (+ 1).
Returns:
A String extracted from the buffer.

subarray

protected byte[] subarray(int start,
                          int end)
Extract a subarray from the response buffer.

Parameters:
start - The starting offset.
end - The end offset (+ 1).
Returns:
A byte array string extracted rom the buffer.

match

public boolean match(int position,
                     String needle)
Test if the bytes in the response buffer match a given string value.

Parameters:
position - The compare position.
needle - The needle string we're testing for.
Returns:
True if the bytes match the needle value, false for any mismatch.

indexOf

public int indexOf(String needle)
Search for a given string starting from the current position cursor.

Parameters:
needle - The search string.
Returns:
The index of a match (in absolute byte position in the response buffer).

indexOf

public int indexOf(String needle,
                   int position)
Search for a string in the response buffer starting from the indicated position.

Parameters:
needle - The search string.
position - The starting buffer position.
Returns:
The index of the match position. Returns -1 for no match.

checkLeftParen

public void checkLeftParen()
                    throws MessagingException
Ensure that the next token in the parsed response is a '(' character.

Throws:
ResponseFormatException
MessagingException

checkRightParen

public void checkRightParen()
                     throws MessagingException
Ensure that the next token in the parsed response is a ')' character.

Throws:
ResponseFormatException
MessagingException

readString

public String readString()
                  throws MessagingException
Read a string-valued token from the response. A string valued token can be either a quoted string, a literal value, or an atom. Any other token type is an error.

Returns:
The string value of the source token.
Throws:
ResponseFormatException
MessagingException

readEncodedString

public String readEncodedString()
                         throws MessagingException
Read an encoded string-valued token from the response. A string valued token can be either a quoted string, a literal value, or an atom. Any other token type is an error.

Returns:
The string value of the source token.
Throws:
ResponseFormatException
MessagingException

decode

public String decode(String original)
              throws MessagingException
Decode a Base 64 encoded string value.

Parameters:
original - The original encoded string.
Returns:
The decoded string.
Throws:
MessagingException

decode

public static int decode(String original,
                         int index,
                         StringBuffer result)
                  throws MessagingException
Decode a section of an encoded string value.

Parameters:
original - The original source string.
index - The current working index.
result - The StringBuffer used for the decoded result.
Returns:
The new index for the decoding operation.
Throws:
MessagingException

readAtom

public String readAtom()
                throws MessagingException
Read a string-valued token from the response, verifying this is an ATOM token.

Returns:
The string value of the source token.
Throws:
ResponseFormatException
MessagingException

readAtom

public String readAtom(boolean expandedDelimiters)
                throws MessagingException
Read a string-valued token from the response, verifying this is an ATOM token.

Returns:
The string value of the source token.
Throws:
ResponseFormatException
MessagingException

readInteger

public int readInteger()
                throws MessagingException
Read a number-valued token from the response. This must be an ATOM token.

Returns:
The integer value of the source token.
Throws:
ResponseFormatException
MessagingException

readLong

public int readLong()
             throws MessagingException
Read a number-valued token from the response. This must be an ATOM token.

Returns:
The long value of the source token.
Throws:
ResponseFormatException
MessagingException

readStringOrNil

public String readStringOrNil()
                       throws MessagingException
Read a string-valued token from the response. A string valued token can be either a quoted string, a literal value, or an atom. Any other token type is an error.

Returns:
The string value of the source token.
Throws:
ResponseFormatException
MessagingException

readQuotedStringOrNil

protected String readQuotedStringOrNil()
                                throws MessagingException
Read a quoted string-valued token from the response. Any other token type other than NIL is an error.

Returns:
The string value of the source token.
Throws:
ResponseFormatException
MessagingException

readDate

public Date readDate()
              throws MessagingException
Read a date from a response string. This is expected to be in Internet Date format, but there's a lot of variation implemented out there. If we're unable to format this correctly, we'll just return null.

Returns:
A Date object created from the source date.
Throws:
MessagingException

readDateOrNil

public Date readDateOrNil()
                   throws MessagingException
Read a date from a response string. This is expected to be in Internet Date format, but there's a lot of variation implemented out there. If we're unable to format this correctly, we'll just return null.

Returns:
A Date object created from the source date.
Throws:
MessagingException

readAddress

public InternetAddress readAddress()
                            throws MessagingException
Read an internet address from a Fetch response. The addresses are returned as a set of string tokens in the order "personal list mailbox host". Any of these tokens can be NIL. The address may also be the start of a group list, which is indicated by the host being NIL. If we have found the start of a group, then we need to parse multiple elements until we find the group end marker (indicated by both the mailbox and the host being NIL), and create a group InternetAddress instance from this.

Returns:
An InternetAddress instance parsed from the element.
Throws:
ResponseFormatException
MessagingException

readAddressList

public InternetAddress[] readAddressList()
                                  throws MessagingException
Parse out a list of addresses. This list of addresses is surrounded by parentheses, and each address is also parenthized (SP?).

Returns:
An array of the parsed addresses.
Throws:
ResponseFormatException
MessagingException

checkListEnd

public boolean checkListEnd()
                     throws MessagingException
Check to see if we're at the end of a parenthized list without advancing the parsing pointer. If we are at the end, then this will step over the closing paren.

Returns:
True if the next token is a closing list paren, false otherwise.
Throws:
ResponseFormatException
MessagingException

readStringList

public List readStringList()
                    throws MessagingException
Reads a string item which can be encoded either as a single string-valued token or a parenthized list of string tokens.

Returns:
A List containing all of the strings.
Throws:
ResponseFormatException
MessagingException

readStrings

public List readStrings()
                 throws MessagingException
Reads all remaining tokens and returns them as a list of strings. NIL values are not supported.

Returns:
A List containing all of the strings.
Throws:
ResponseFormatException
MessagingException

skipExtensionItem

public void skipExtensionItem()
                       throws MessagingException
Skip over an extension item. This may be either a string token or a parenthized item (with potential nesting). At the point where this is called, we're looking for a closing ')', but we know it is not that. An EOF is an error, however,

Throws:
MessagingException

skipNestedValue

public void skipNestedValue()
                     throws MessagingException
Skip over a parenthized value that we're not interested in. These lists may contain nested sublists, so we need to handle the nesting properly.

Throws:
MessagingException

checkToken

public void checkToken(int type)
                throws MessagingException
Get the next token and verify that it's of the expected type for the context.

Parameters:
type - The type of token we're expecting.
Throws:
MessagingException

readByteArray

public byte[] readByteArray()
                     throws MessagingException
Read the next token as binary data. The next token can be a literal, a quoted string, or the token NIL (which returns a null result). Any other token throws a ResponseFormatException.

Returns:
A byte array representing the rest of the response data.
Throws:
MessagingException

getEncoding

public static int getEncoding(byte[] value)
Determine what type of token encoding needs to be used for a string value.

Parameters:
value - The string to test.
Returns:
Either Token.ATOM, Token.QUOTEDSTRING, or Token.LITERAL, depending on the characters contained in the value.

readParameterList

public ParameterList readParameterList()
                                throws MessagingException
Read a ContentType or ContentDisposition parameter list from an IMAP command response.

Returns:
A ParameterList instance containing the parameters.
Throws:
MessagingException

hasMore

public boolean hasMore()
                throws MessagingException
Test if we have more data in the response buffer.

Returns:
true if there are more tokens to process. false if we've reached the end of the stream.
Throws:
MessagingException

notListEnd

public boolean notListEnd()
                   throws MessagingException
Tests if we've reached the end of a parenthetical list in our parsing stream.

Returns:
true if the next token will be a ')'. false if the next token is anything else.
Throws:
MessagingException

readFlagList

public Flags readFlagList()
                   throws MessagingException
Read a list of Flag values from an IMAP response, returning a Flags instance containing the appropriate pieces.

Returns:
A Flags instance with the flag values.
Throws:
MessagingException

readSystemNameList

public List readSystemNameList()
                        throws MessagingException
Read a list of Flag values from an IMAP response, returning a Flags instance containing the appropriate pieces.

Returns:
A Flags instance with the flag values.
Throws:
MessagingException


Copyright © 2003-2009 The Apache Software Foundation. All Rights Reserved.