public final class TokenParser
extends java.lang.Object
This class is a singleton and is a central point for converting a token from a byte stream and back.
When the instances is created, a mapping from Token to its TokenHandlers is loaded from ptserver.data.TokenHandlers.properties ResourceBundle. This class fill parse a token from an input stream by figuring out its token identifier (based on the position of the TokenHandler in the TokenHandlers.properties) and then selecting appropriate TokenHandler to do the parsing
Similarly, the token would be converted to a byte stream by selecting a TokenHandler mapped to its class and using it to do the conversion.
Red (ahuseyno) |
Red (ahuseyno) |
Modifier and Type | Class and Description |
---|---|
static class |
TokenParser.HandlerData<T extends Token>
Data structure that stores handler, token type, and position tuple.
|
Modifier and Type | Method and Description |
---|---|
<T extends Token> |
convertToBytes(T token,
java.io.DataOutputStream outputStream)
Convert the token to a byte stream by first finding its TokenHandler and
writing its position to the stream (first 2 bytes) followed by the conversion
produced TokenHandler.
|
<T extends Token> |
convertToBytes(T token,
java.io.OutputStream outputStream)
Convert the token to a byte stream by first finding its TokenHandler and
writing its position to the stream (first 2 bytes) followed by the conversion
produced TokenHandler.
|
<T extends Token> |
convertToToken(java.io.DataInputStream inputStream)
Read and parse the inputStream in order to recreate the Token.
|
<T extends Token> |
convertToToken(java.io.InputStream inputStream)
Read and parse the inputStream in order to recreate the Token.
|
java.util.ArrayList<TokenParser.HandlerData<?>> |
getHandlerList()
Get the list of token handlers.
|
static TokenParser |
getInstance()
Return singleton instance of the TokenParser.
|
void |
setTokenHandlers(java.util.LinkedHashMap<java.lang.String,java.lang.String> tokenHandlerMap)
Set the list of token handlers.
|
public <T extends Token> void convertToBytes(T token, java.io.DataOutputStream outputStream) throws java.io.IOException, IllegalActionException
T
- Type of the target tokentoken
- Token to be convertedoutputStream
- outputStream used for the resulting byte streamjava.io.IOException
- if there is a problem with the outputStreamIllegalActionException
- if the state becomes inconsistentpublic <T extends Token> void convertToBytes(T token, java.io.OutputStream outputStream) throws java.io.IOException, IllegalActionException
T
- Type of the target tokentoken
- Token to be convertedoutputStream
- outputStream used for the resulting byte streamjava.io.IOException
- if there is a problem with the outputStreamIllegalActionException
- if the state becomes inconsistentpublic <T extends Token> T convertToToken(java.io.DataInputStream inputStream) throws java.io.IOException, IllegalActionException
T
- Type of TokeninputStream
- InputStream containing byteStream of the token data
where first 2 bytes indicated the token type (defined as TokenHandlers position)java.io.IOException
- is thrown in case of a problem with the outputStreamIllegalActionException
- is thrown if the state becomes inconsistentpublic <T extends Token> T convertToToken(java.io.InputStream inputStream) throws java.io.IOException, IllegalActionException
T
- Type of TokeninputStream
- InputStream containing byteStream of the token data
where first 2 bytes indicated the token type (defined as TokenHandlers position)java.io.IOException
- is thrown in case of a problem with the outputStreamIllegalActionException
- is thrown if the state becomes inconsistentpublic java.util.ArrayList<TokenParser.HandlerData<?>> getHandlerList()
public static TokenParser getInstance() throws IllegalActionException
IllegalActionException
- if there is a problem loading the mapping from TokenHandlers.properties file.public void setTokenHandlers(java.util.LinkedHashMap<java.lang.String,java.lang.String> tokenHandlerMap) throws IllegalActionException
tokenHandlerMap
- The populated map of token handlers.IllegalActionException
- If the token handler does not map to an
existing class or reflection instantiation of the handler fails.