Package org.moeaframework.util.io
Class Tokenizer
java.lang.Object
org.moeaframework.util.io.Tokenizer
Tokenizer for encoding and decoding content on a line, escaping any special characters.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionDecodes or parses the string into individual tokens, converting any escaped characters back to their original.String[]
decodeToArray
(String line) Decodes or parses the string into individual tokens.Encodes the tokens into a string.Encodes the tokens into a string.Encodes the tokens into a string.Escapes the characters in a string.void
escapeChar
(CharSequence original, CharSequence replacement) Registers an escaped character by specifying the original character and its escaped representation.Returns the delimiter used by this tokenizer.void
reset()
Resets this tokenizer back to its default state, removing any custom settings.void
setDelimiter
(char delimiter) Sets the delimiter, adding it as an escape character if not already configured.Unescape the string without splitting into tokens.
-
Constructor Details
-
Tokenizer
public Tokenizer()Constructs a new tokenizer with default settings.
-
-
Method Details
-
reset
public void reset()Resets this tokenizer back to its default state, removing any custom settings. -
escapeChar
Registers an escaped character by specifying the original character and its escaped representation. Note that whitespace, control characters, unicode, and'\'
are escaped by default.- Parameters:
original
- the original characterreplacement
- the replacement string, which must start with'\'
-
setDelimiter
public void setDelimiter(char delimiter) Sets the delimiter, adding it as an escape character if not already configured.- Parameters:
delimiter
- the delimiter character
-
getDelimiter
Returns the delimiter used by this tokenizer.- Returns:
- the delimiter character
-
unescape
Unescape the string without splitting into tokens.- Parameters:
str
- the string- Returns:
- the unescaped string
-
decodeToArray
Decodes or parses the string into individual tokens. Seedecode(String)
for details.- Parameters:
line
- the line to decode- Returns:
- the tokens
-
decode
Decodes or parses the string into individual tokens, converting any escaped characters back to their original.Leading and trailing whitespace are trimmed from the tokens. Any such whitespace that is part of the token needs to be escaped. For example,
" foo "
becomes["foo"]
, but the whitespace can be escaped with"\ \ foo\ \ "
.If the delimiter is a whitespace character, multiple adjacent whitespace are treated as one delimiter. However, if the delimiter is a non-whitespace character, each delimiter denotes a new token. This leads to slightly different behavior when dealing with delimiters. For instance,
"foo bar"
becomes["foo", "bar"]
, but"foo,,bar"
becomes["foo", "", "bar"]
.- Parameters:
line
- the line to decode- Returns:
- the tokens
-
escape
Escapes the characters in a string.- Parameters:
str
- the string- Returns:
- the escaped string
-
encode
Encodes the tokens into a string. Seeencode(Iterable)
for details.- Parameters:
tokens
- the tokens to encode- Returns:
- the encoded string
-
encode
Encodes the tokens into a string. Seeencode(Iterable)
for details.- Parameters:
tokens
- the tokens to encode- Returns:
- the encoded string
-
encode
Encodes the tokens into a string. Each token will be escaped following the rules of this tokenizer and joined into a string separated by the delimiter.- Parameters:
tokens
- the tokens to encode- Returns:
- the encoded string
-