org.extex.scanner.type.token
Class TokenFactoryImpl

java.lang.Object
  extended by org.extex.scanner.type.token.TokenFactoryImpl
All Implemented Interfaces:
CatcodeVisitor<Token,java.lang.String,UnicodeChar,java.lang.String>, TokenFactory

public class TokenFactoryImpl
extends java.lang.Object
implements TokenFactory, CatcodeVisitor<Token,java.lang.String,UnicodeChar,java.lang.String>

This is a implementation of a token factory. This means that the factory pattern is applied here. This pattern opens the possibility to cache the instances for Tokens to reduce the number of objects present in the system.

The Visitor Pattern

In addition the visitor pattern is used to select the appropriate instantiation method. The visit methods are not meant to be used externally. They are purely internal. Despite their general definition the visit methods are in fact used in the following way:

  Token visit*(String value, UnicodeChar character) throws CatcodeException
 

This means that they are expected to return the new token. The first argument is the value, which is mainly meaningful for control sequence tokens. The third argument contains the Unicode character for single letter tokens.

Version:
$Revision: 4738 $
Author:
Gerd Neugebauer

Constructor Summary
TokenFactoryImpl()
          Creates a new object.
 
Method Summary
 Token createToken(Catcode code, int c, java.lang.String namespace)
          Create a new Token of the appropriate kind.
 Token createToken(Catcode code, UnicodeChar c, java.lang.String namespace)
          Get an instance of a token with a given Catcode and Unicode character value.
 Token createToken(Catcode code, UnicodeChar esc, java.lang.String value, java.lang.String namespace)
          Get an instance of a token with a given Catcode and value.
 Tokens toTokens(java.lang.CharSequence s)
          Convert a character sequence to a list of tokens.
 Tokens toTokens(long l)
          Convert a long value into a list of tokens.
 Token visitActive(java.lang.String value, UnicodeChar uchar, java.lang.String namespace)
          Active characters are cached.
 Token visitComment(java.lang.String value, UnicodeChar uchar, java.lang.String namespace)
          Comments are ignored thus null is returned in any case.
 Token visitCr(java.lang.String value, UnicodeChar uchar, java.lang.String namespace)
          There is only one CrToken.
 Token visitEscape(java.lang.String value, UnicodeChar uchar, java.lang.String namespace)
          This visit method is invoked on an escape token.
 Token visitIgnore(java.lang.String value, UnicodeChar uchar, java.lang.String namespace)
          Ignored characters are simply ignored;-)
 Token visitInvalid(java.lang.String value, UnicodeChar uchar, java.lang.String namespace)
          Invalid characters are ignored; even without any error message.
 Token visitLeftBrace(java.lang.String value, UnicodeChar uchar, java.lang.String namespace)
          A left brace token is expected to take a single character only.
 Token visitLetter(java.lang.String value, UnicodeChar uchar, java.lang.String namespace)
          A letter token is expected to take a single character only.
 Token visitMacroParam(java.lang.String value, UnicodeChar uchar, java.lang.String namespace)
          This visit method is invoked on a macro parameter token.
 Token visitMathShift(java.lang.String value, UnicodeChar uchar, java.lang.String namespace)
          This visit method is invoked on a math shift token.
 Token visitOther(java.lang.String value, UnicodeChar uchar, java.lang.String namespace)
          This visit method is invoked on an other token.
 Token visitRightBrace(java.lang.String value, UnicodeChar uchar, java.lang.String namespace)
          This visit method is invoked on a right brace token.
 Token visitSpace(java.lang.String value, UnicodeChar uchar, java.lang.String namespace)
          There is only one space token.
 Token visitSubMark(java.lang.String value, UnicodeChar uchar, java.lang.String namespace)
          This visit method is invoked on a sub mark token.
 Token visitSupMark(java.lang.String value, UnicodeChar uchar, java.lang.String namespace)
          This visit method is invoked on a sup mark token.
 Token visitTabMark(java.lang.String value, UnicodeChar uchar, java.lang.String namespace)
          This visit method is invoked on a tab mark token.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TokenFactoryImpl

public TokenFactoryImpl()
Creates a new object.

Method Detail

createToken

public Token createToken(Catcode code,
                         int c,
                         java.lang.String namespace)
                  throws CatcodeException
Create a new Token of the appropriate kind. Tokens are immutable (no setters) thus the factory pattern can be applied.

Specified by:
createToken in interface TokenFactory
Parameters:
code - the category code
c - the character value
namespace - the name space to use
Returns:
the new token
Throws:
CatcodeException - in case of an error
See Also:
TokenFactory.createToken( org.extex.scanner.type.Catcode, int, java.lang.String)

createToken

public Token createToken(Catcode code,
                         UnicodeChar c,
                         java.lang.String namespace)
                  throws CatcodeException
Get an instance of a token with a given Catcode and Unicode character value.

Specified by:
createToken in interface TokenFactory
Parameters:
code - the catcode
c - the Unicode character value
namespace - the name space for the token. This is relevant for ACTIVE and ESCAPE category codes only.
Returns:
the appropriate token
Throws:
CatcodeException - in case of an error
See Also:
TokenFactory.createToken( org.extex.scanner.type.Catcode, org.extex.core.UnicodeChar, java.lang.String)

createToken

public Token createToken(Catcode code,
                         UnicodeChar esc,
                         java.lang.String value,
                         java.lang.String namespace)
                  throws CatcodeException
Get an instance of a token with a given Catcode and value.

Specified by:
createToken in interface TokenFactory
Parameters:
code - the catcode
esc - the Unicode character value of the escape character
value - the value
namespace - the name space for the token. This is relevant for ACTIVE and ESCAPE category codes only.
Returns:
the appropriate token
Throws:
CatcodeException - in case of an error
See Also:
TokenFactory.createToken( org.extex.scanner.type.Catcode, org.extex.core.UnicodeChar, java.lang.String, java.lang.String)

toTokens

public Tokens toTokens(java.lang.CharSequence s)
                throws CatcodeException
Convert a character sequence to a list of tokens.

Each character of the string is converted into a OtherToken and added to the internal list. An exception is made for spaces which are converted into a SpaceToken.

Specified by:
toTokens in interface TokenFactory
Parameters:
s - the character sequence to translate to tokens
Returns:
the token list
Throws:
CatcodeException - in case of an error
See Also:
TokenFactory.toTokens(java.lang.CharSequence)

toTokens

public Tokens toTokens(long l)
                throws CatcodeException
Convert a long value into a list of tokens.

Each character is converted into a OtherToken and added to the internal list.

Specified by:
toTokens in interface TokenFactory
Parameters:
l - the value to convert
Returns:
the token list
Throws:
CatcodeException - in case of an error
See Also:
TokenFactory.toTokens(long)

visitActive

public Token visitActive(java.lang.String value,
                         UnicodeChar uchar,
                         java.lang.String namespace)
                  throws CatcodeException
Active characters are cached. Thus a look-up in the cache precedes the creation of a new token.

Specified by:
visitActive in interface CatcodeVisitor<Token,java.lang.String,UnicodeChar,java.lang.String>
Parameters:
value - the string value of the token
uchar - the character value of the token
namespace - the name space of the token
Returns:
the active token
Throws:
CatcodeException - in case of an error
See Also:
CatcodeVisitor.visitActive( java.lang.Object, java.lang.Object, java.lang.Object)

visitComment

public Token visitComment(java.lang.String value,
                          UnicodeChar uchar,
                          java.lang.String namespace)
Comments are ignored thus null is returned in any case.

Specified by:
visitComment in interface CatcodeVisitor<Token,java.lang.String,UnicodeChar,java.lang.String>
Parameters:
value - the string value of the token
uchar - the character value of the token
namespace - the name space of the token
Returns:
null
See Also:
CatcodeVisitor.visitComment( java.lang.Object, java.lang.Object, java.lang.Object)

visitCr

public Token visitCr(java.lang.String value,
                     UnicodeChar uchar,
                     java.lang.String namespace)
There is only one CrToken.

Specified by:
visitCr in interface CatcodeVisitor<Token,java.lang.String,UnicodeChar,java.lang.String>
Parameters:
value - the string value of the token
uchar - the character value of the token
namespace - the name space of the token
Returns:
the CR token
See Also:
CatcodeVisitor.visitCr(java.lang.Object, java.lang.Object, java.lang.Object)

visitEscape

public Token visitEscape(java.lang.String value,
                         UnicodeChar uchar,
                         java.lang.String namespace)
                  throws CatcodeException
This visit method is invoked on an escape token. In TeX this normally means a control sequence.

Specified by:
visitEscape in interface CatcodeVisitor<Token,java.lang.String,UnicodeChar,java.lang.String>
Parameters:
value - the string value of the token
uchar - the character value of the token
namespace - the name space of the token
Returns:
the token requested
Throws:
CatcodeException - in case of an error
See Also:
CatcodeVisitor.visitEscape( java.lang.Object, java.lang.Object, java.lang.Object)

visitIgnore

public Token visitIgnore(java.lang.String value,
                         UnicodeChar uchar,
                         java.lang.String namespace)
Ignored characters are simply ignored;-)

Specified by:
visitIgnore in interface CatcodeVisitor<Token,java.lang.String,UnicodeChar,java.lang.String>
Parameters:
value - the string value token or null
uchar - the requested character code
namespace - the third argument is ignored
Returns:
null
See Also:
CatcodeVisitor.visitIgnore( java.lang.Object, java.lang.Object, java.lang.Object)

visitInvalid

public Token visitInvalid(java.lang.String value,
                          UnicodeChar uchar,
                          java.lang.String namespace)
Invalid characters are ignored; even without any error message.

Specified by:
visitInvalid in interface CatcodeVisitor<Token,java.lang.String,UnicodeChar,java.lang.String>
Parameters:
value - the string value token or null
uchar - the requested character code
namespace - the third argument is ignored
Returns:
null
See Also:
CatcodeVisitor.visitInvalid( java.lang.Object, java.lang.Object, java.lang.Object)

visitLeftBrace

public Token visitLeftBrace(java.lang.String value,
                            UnicodeChar uchar,
                            java.lang.String namespace)
                     throws CatcodeException
A left brace token is expected to take a single character only.

Specified by:
visitLeftBrace in interface CatcodeVisitor<Token,java.lang.String,UnicodeChar,java.lang.String>
Parameters:
value - the string value of the token
uchar - the character value of the token
namespace - the name space of the token
Returns:
the token requested
Throws:
CatcodeException - in case of an error
See Also:
CatcodeVisitor.visitLeftBrace( java.lang.Object, java.lang.Object, java.lang.Object)

visitLetter

public Token visitLetter(java.lang.String value,
                         UnicodeChar uchar,
                         java.lang.String namespace)
                  throws CatcodeException
A letter token is expected to take a single character only.

Specified by:
visitLetter in interface CatcodeVisitor<Token,java.lang.String,UnicodeChar,java.lang.String>
Parameters:
value - the string value of the token
uchar - the character value of the token
namespace - the name space of the token
Returns:
the token requested
Throws:
CatcodeException - in case of an error
See Also:
CatcodeVisitor.visitLetter( java.lang.Object, java.lang.Object, java.lang.Object)

visitMacroParam

public Token visitMacroParam(java.lang.String value,
                             UnicodeChar uchar,
                             java.lang.String namespace)
                      throws CatcodeException
Description copied from interface: CatcodeVisitor
This visit method is invoked on a macro parameter token. In TeX this normally is a #.

Specified by:
visitMacroParam in interface CatcodeVisitor<Token,java.lang.String,UnicodeChar,java.lang.String>
Parameters:
value - the string value of the token
uchar - the character value of the token
namespace - the name space of the token
Returns:
the token requested
Throws:
CatcodeException - in case of an error
See Also:
CatcodeVisitor.visitMacroParam( java.lang.Object, java.lang.Object, java.lang.Object)

visitMathShift

public Token visitMathShift(java.lang.String value,
                            UnicodeChar uchar,
                            java.lang.String namespace)
                     throws CatcodeException
Description copied from interface: CatcodeVisitor
This visit method is invoked on a math shift token. In TeX this normally is a $.

Specified by:
visitMathShift in interface CatcodeVisitor<Token,java.lang.String,UnicodeChar,java.lang.String>
Parameters:
value - the string value of the token
uchar - the character value of the token
namespace - the name space of the token
Returns:
the token requested
Throws:
CatcodeException - in case of an error
See Also:
CatcodeVisitor.visitMathShift( java.lang.Object, java.lang.Object, java.lang.Object)

visitOther

public Token visitOther(java.lang.String value,
                        UnicodeChar uchar,
                        java.lang.String namespace)
                 throws CatcodeException
Description copied from interface: CatcodeVisitor
This visit method is invoked on an other token.

Specified by:
visitOther in interface CatcodeVisitor<Token,java.lang.String,UnicodeChar,java.lang.String>
Parameters:
value - the string value of the token
uchar - the character value of the token
namespace - the name space of the token
Returns:
the token requested
Throws:
CatcodeException - in case of an error
See Also:
CatcodeVisitor.visitOther( java.lang.Object, java.lang.Object, java.lang.Object)

visitRightBrace

public Token visitRightBrace(java.lang.String value,
                             UnicodeChar uchar,
                             java.lang.String namespace)
                      throws CatcodeException
Description copied from interface: CatcodeVisitor
This visit method is invoked on a right brace token.

Specified by:
visitRightBrace in interface CatcodeVisitor<Token,java.lang.String,UnicodeChar,java.lang.String>
Parameters:
value - the string value of the token
uchar - the character value of the token
namespace - the name space of the token
Returns:
the token requested
Throws:
CatcodeException - in case of an error
See Also:
CatcodeVisitor.visitRightBrace( java.lang.Object, java.lang.Object, java.lang.Object)

visitSpace

public Token visitSpace(java.lang.String value,
                        UnicodeChar uchar,
                        java.lang.String namespace)
There is only one space token. It has the character code 32.

Specified by:
visitSpace in interface CatcodeVisitor<Token,java.lang.String,UnicodeChar,java.lang.String>
Parameters:
value - the string value token or null
uchar - the requested character code
namespace - the third argument is ignored
Returns:
the space token.
See Also:
CatcodeVisitor.visitSpace( java.lang.Object, java.lang.Object, java.lang.Object), "The TeXbook [Chapter 8, p. 47]"

visitSubMark

public Token visitSubMark(java.lang.String value,
                          UnicodeChar uchar,
                          java.lang.String namespace)
                   throws CatcodeException
Description copied from interface: CatcodeVisitor
This visit method is invoked on a sub mark token. In TeX this normally is a _.

Specified by:
visitSubMark in interface CatcodeVisitor<Token,java.lang.String,UnicodeChar,java.lang.String>
Parameters:
value - the string value of the token
uchar - the character value of the token
namespace - the name space of the token
Returns:
the token requested
Throws:
CatcodeException - in case of an error
See Also:
CatcodeVisitor.visitSubMark( java.lang.Object, java.lang.Object, java.lang.Object)

visitSupMark

public Token visitSupMark(java.lang.String value,
                          UnicodeChar uchar,
                          java.lang.String namespace)
                   throws CatcodeException
Description copied from interface: CatcodeVisitor
This visit method is invoked on a sup mark token. In TeX this normally is a ^.

Specified by:
visitSupMark in interface CatcodeVisitor<Token,java.lang.String,UnicodeChar,java.lang.String>
Parameters:
value - the string value of the token
uchar - the character value of the token
namespace - the name space of the token
Returns:
the token requested
Throws:
CatcodeException - in case of an error
See Also:
CatcodeVisitor.visitSupMark( java.lang.Object, java.lang.Object, java.lang.Object)

visitTabMark

public Token visitTabMark(java.lang.String value,
                          UnicodeChar uchar,
                          java.lang.String namespace)
                   throws CatcodeException
Description copied from interface: CatcodeVisitor
This visit method is invoked on a tab mark token. In TeX this normally is a &.

Specified by:
visitTabMark in interface CatcodeVisitor<Token,java.lang.String,UnicodeChar,java.lang.String>
Parameters:
value - the string value of the token
uchar - the character value of the token
namespace - the name space of the token
Returns:
the token requested
Throws:
CatcodeException - in case of an error
See Also:
CatcodeVisitor.visitTabMark( java.lang.Object, java.lang.Object, java.lang.Object)