de.marcelsauer.tokenreplacer
Class FiniteStateMachineTokenReplacer

java.lang.Object
  extended by de.marcelsauer.tokenreplacer.FiniteStateMachineTokenReplacer
All Implemented Interfaces:
TokenReplacer

public class FiniteStateMachineTokenReplacer
extends java.lang.Object
implements TokenReplacer

parser implementation based on Finite State machine design

NOT PART OF THE PUBLIC API! STILL HERE AND PUBLIC IN CASE YOU NEED TO 'EMERGENCY' SUBCLASS.

Author:
msauer

Nested Class Summary
protected static class FiniteStateMachineTokenReplacer.State
           
 
Field Summary
protected  char argsEnd
           
protected  char argsSep
           
protected  char argsStart
           
protected  boolean generatorCachingEnabled
           
protected  boolean ignoreMissingValues
           
protected  char tokenEnd
           
protected  java.util.Map<java.lang.String,Token> tokens
           
protected  char tokenStart
           
 
Constructor Summary
FiniteStateMachineTokenReplacer()
           
 
Method Summary
protected  void checkArgumentsAreValid(java.lang.String tokenName, java.lang.StringBuilder args)
          stuff like {dynamic(1,)} or {dynamic(1,2,)} seems to be invalid TODO do proper parsing instead of regex
 TokenReplacer disableGeneratorCaching()
          turns generator caching OFF.
 TokenReplacer doNotIgnoreMissingValues()
          tells the TokenReplacer to report any tokens that can not be replaced.
 TokenReplacer enableGeneratorCaching()
          turns generator caching ON.
protected  void ensureOneChar(java.lang.String character)
           
protected  java.lang.String evalToken(java.lang.StringBuilder token, java.lang.StringBuilder args, java.util.Map<java.lang.String,java.lang.String> generatorCache)
           
protected  java.lang.String[] extractArgs(java.lang.String tokenName, java.lang.StringBuilder args)
           
 TokenReplacer ignoreMissingValues()
          tells the TokenReplacer to IGNORE any tokens that can not be replaced.
protected  boolean isEndOfArguments(char character)
           
protected  boolean isEndOfToken(char character)
           
protected  boolean isStartOfArguments(char character)
           
protected  boolean isStartOfToken(char character)
           
 TokenReplacer register(java.lang.String[] replacements)
          registers an array of replacements for a string based in indexed tokens.
 TokenReplacer register(java.lang.String token, Generator generator)
          registers a Token that will be replaced by the given Generator.
 TokenReplacer register(java.lang.String token, java.lang.String value)
          registers a static value for a given token.
 TokenReplacer register(Token token)
          registers a Token that needs to be replaced.
 java.lang.String substitute(java.lang.String toSubstitute)
          replaces all Token with one of the following: the provided static values set via TokenReplacer.register(String, String) the token registered via TokenReplacer.register(Token) the generator registered via TokenReplacer.register(String, Generator)
 TokenReplacer withArgumentDelimiter(java.lang.String argsSep)
           
 TokenReplacer withArgumentEnd(java.lang.String argsEnd)
           
 TokenReplacer withArgumentStart(java.lang.String argsStart)
           
 TokenReplacer withTokenEnd(java.lang.String tokenEnd)
           
 TokenReplacer withTokenStart(java.lang.String tokenStart)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

tokenStart

protected char tokenStart

tokenEnd

protected char tokenEnd

argsStart

protected char argsStart

argsEnd

protected char argsEnd

argsSep

protected char argsSep

ignoreMissingValues

protected boolean ignoreMissingValues

generatorCachingEnabled

protected boolean generatorCachingEnabled

tokens

protected final java.util.Map<java.lang.String,Token> tokens
Constructor Detail

FiniteStateMachineTokenReplacer

public FiniteStateMachineTokenReplacer()
Method Detail

substitute

public java.lang.String substitute(java.lang.String toSubstitute)
Description copied from interface: TokenReplacer
replaces all Token with one of the following:

isStartOfToken

protected boolean isStartOfToken(char character)

isStartOfArguments

protected boolean isStartOfArguments(char character)

isEndOfArguments

protected boolean isEndOfArguments(char character)

isEndOfToken

protected boolean isEndOfToken(char character)

extractArgs

protected java.lang.String[] extractArgs(java.lang.String tokenName,
                                         java.lang.StringBuilder args)

checkArgumentsAreValid

protected void checkArgumentsAreValid(java.lang.String tokenName,
                                      java.lang.StringBuilder args)
stuff like {dynamic(1,)} or {dynamic(1,2,)} seems to be invalid TODO do proper parsing instead of regex


evalToken

protected java.lang.String evalToken(java.lang.StringBuilder token,
                                     java.lang.StringBuilder args,
                                     java.util.Map<java.lang.String,java.lang.String> generatorCache)

register

public TokenReplacer register(java.lang.String token,
                              java.lang.String value)
Description copied from interface: TokenReplacer
registers a static value for a given token. if you need dynamic behaviour then use #register(Generator). same as registering a token via TokenReplacer.register(Token) and supplying a replacement value via Token.replacedBy(String).

Specified by:
register in interface TokenReplacer
Parameters:
token - the name of the token to be replaced e.g. for ${date} -> "date" would be the token, must not be null or empty
value - the static value that will be used when replacing the token, must not be null or empty
Returns:
the TokenReplacer to allow method chaining

register

public TokenReplacer register(Token token)
Description copied from interface: TokenReplacer
registers a Token that needs to be replaced.

Specified by:
register in interface TokenReplacer
Parameters:
token - the Token, must not be null, the token must have a valid value or generator associated with it which was set via Token.replacedBy(String) or Token.replacedBy(String)
Returns:
the #TokenReplacer to allow method chaining

register

public TokenReplacer register(java.lang.String token,
                              Generator generator)
Description copied from interface: TokenReplacer
registers a Token that will be replaced by the given Generator. same as registering a token via TokenReplacer.register(Token) and supplying a generator via Token.replacedBy(Generator)

Specified by:
register in interface TokenReplacer
Parameters:
token - the name of the token to be replaced e.g. for ${date} -> "date" would be the token, must not be null or empty
Returns:
the #TokenReplacer to allow method chaining

withTokenStart

public TokenReplacer withTokenStart(java.lang.String tokenStart)
Specified by:
withTokenStart in interface TokenReplacer
Parameters:
tokenStart - sets the token start identifier to the given value e.g. [dynamic] -> '[' would be the start identifier, e.g. '[', must not be null or empty
Returns:
the #TokenReplacer to allow method chaining

withTokenEnd

public TokenReplacer withTokenEnd(java.lang.String tokenEnd)
Specified by:
withTokenEnd in interface TokenReplacer
Parameters:
tokenEnd - sets the token end identifier to the given value e.g. [dynamic] -> ']' would be the end identifier, e.g. '[', must not be null or empty
Returns:
the #TokenReplacer to allow method chaining

ensureOneChar

protected void ensureOneChar(java.lang.String character)

withArgumentDelimiter

public TokenReplacer withArgumentDelimiter(java.lang.String argsSep)
Specified by:
withArgumentDelimiter in interface TokenReplacer
Parameters:
argsSep - changes the delimiter of the arguments to the given value e.g. {dynamic(1;2;3)} -> ';' would be the delimiter, must not be null or empty
Returns:
the #TokenReplacer to allow method chaining

withArgumentStart

public TokenReplacer withArgumentStart(java.lang.String argsStart)
Specified by:
withArgumentStart in interface TokenReplacer
Parameters:
argsStart - sets the argument start identifier to the given value e.g. {dynamic[1;2;3]} -> '[' would be the delimiter e.g. '[', must not be null or empty
Returns:
the #TokenReplacer to allow method chaining

withArgumentEnd

public TokenReplacer withArgumentEnd(java.lang.String argsEnd)
Specified by:
withArgumentEnd in interface TokenReplacer
Parameters:
argsEnd - sets the argument end identifier to the given value e.g. {dynamic[1;2;3]} -> ']' would be the delimiter e.g. ']', must not be null or empty
Returns:
the #TokenReplacer to allow method chaining

doNotIgnoreMissingValues

public TokenReplacer doNotIgnoreMissingValues()
Description copied from interface: TokenReplacer
tells the TokenReplacer to report any tokens that can not be replaced. if turned on an IllegalStateException will be thrown during token replacement. reporting errors is turned ON by DEFAULT.

Specified by:
doNotIgnoreMissingValues in interface TokenReplacer
Returns:
the #TokenReplacer to allow method chaining

ignoreMissingValues

public TokenReplacer ignoreMissingValues()
Description copied from interface: TokenReplacer
tells the TokenReplacer to IGNORE any tokens that can not be replaced. if turned OFF no Exceptions will be thrown during token replacement. reporting errors is turned ON by DEFAULT.

Specified by:
ignoreMissingValues in interface TokenReplacer
Returns:
the #TokenReplacer to allow method chaining

enableGeneratorCaching

public TokenReplacer enableGeneratorCaching()
Description copied from interface: TokenReplacer
turns generator caching ON. once a value is determined through a Generator all remaining values with the same token name will be replaced by the cached version. use TokenReplacer.disableGeneratorCaching() to turn caching off.

Specified by:
enableGeneratorCaching in interface TokenReplacer
Returns:
the #TokenReplacer to allow method chaining

disableGeneratorCaching

public TokenReplacer disableGeneratorCaching()
Description copied from interface: TokenReplacer
turns generator caching OFF. use TokenReplacer.enableGeneratorCaching() to turn caching on.

Specified by:
disableGeneratorCaching in interface TokenReplacer
Returns:
the #TokenReplacer to allow method chaining

register

public TokenReplacer register(java.lang.String[] replacements)
Description copied from interface: TokenReplacer
registers an array of replacements for a string based in indexed tokens. the tokens will be replaced in the order they were added to the array. e.g.
 toky.register(new String[] { "one", "two", "three" });
 toky.substitute("{0} {1} {2}")); // will result in "one two three"
 

Specified by:
register in interface TokenReplacer
Parameters:
replacements - the array of replacements that will be used when replacing an indexed strings, must not be null but can be empty
Returns:
the #TokenReplacer to allow method chaining


Copyright © 2011. All Rights Reserved.