Class StringTranslator
- java.lang.Object
-
- org.daisy.braille.utils.impl.tools.table.StringTranslator
-
public class StringTranslator extends Object
Provides a string to string translator.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classStringTranslator.MatchModeDefines the mode to use when matching patterns in the StringTranslator.
-
Constructor Summary
Constructors Constructor Description StringTranslator()Creates a new Translator with match mode set to "greedy".StringTranslator(StringTranslator.MatchMode mode)Creates a new Translator with the match mode set to the supplied value.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddToken(String token, String translation)Adds a token to the list of translations.StringTranslator.MatchModegetMatchMode()Gets the current match mode.intgetTokenCount()Gets the number of tokens in the translator.voidsetMatchMode(StringTranslator.MatchMode mode)Sets the current match mode.Stringtranslate(String str)Translates the given string with the current match mode.Stringtranslate(String str, StringTranslator.MatchMode mode)Translates the given string with the specified match mode.
-
-
-
Constructor Detail
-
StringTranslator
public StringTranslator(StringTranslator.MatchMode mode)
Creates a new Translator with the match mode set to the supplied value.- Parameters:
mode- the mode to use
-
StringTranslator
public StringTranslator()
Creates a new Translator with match mode set to "greedy".
-
-
Method Detail
-
addToken
public void addToken(String token, String translation)
Adds a token to the list of translations.- Parameters:
token- a token in the string to translatetranslation- the translation- Throws:
IllegalArgumentException- if the token is already in the list
-
getMatchMode
public StringTranslator.MatchMode getMatchMode()
Gets the current match mode.- Returns:
- returns the current match mode
-
setMatchMode
public void setMatchMode(StringTranslator.MatchMode mode)
Sets the current match mode.- Parameters:
mode- the mode to use in subsequent calls to translate()
-
getTokenCount
public int getTokenCount()
Gets the number of tokens in the translator.- Returns:
- returns the number of tokens
-
translate
public String translate(String str, StringTranslator.MatchMode mode)
Translates the given string with the specified match mode.- Parameters:
str- the string to translatemode- the mode to use- Returns:
- returns the translated string
- Throws:
IllegalArgumentException- if the entire string could not be translated
-
translate
public String translate(String str)
Translates the given string with the current match mode. The whole string must be translatable, or the operation will fail.- Parameters:
str- the string to translate- Returns:
- returns the translated string
- Throws:
IllegalArgumentException- if the entire string could not be translated
-
-