public class TemplateFormat extends Object
java.text.MessageFormat
.
TemplateFormat
formats a template string, replacing variable
placeholders with the corresponding values. There is no limit on the
number of variables or placeholders, and the relationship between variables
and placeholders does not need to be one-to-one, though each placeholder
must have a corresponding variable definition.
Here is an example template string:
My name is {name}, and I am {age} years old. Are you also {age} years old?A placeholder is a variable name surrounded by braces. Since the parser is lenient, a variable name can consist of a sequence of any characters except '{' or '}'. A null variable placeholder ({}) is not allowed.
Literal braces can be inserted into the template by doubling them. So for example, {{ will be interpreted as a literal single open brace.
Variables may appear in more than one place in a template; every occurrence
of a given variable placeholder is replaced with the value of that variable.
A ParsingException
is thrown if a variable placeholder is
encountered for which there is no binding.
The bind()
method, in its various forms, may be used to bind
a variable name to its value. Though values of various types are supported,
all values are converted to strings during substitution; the string form of
the value is substituted in place of the variable's placeholder.
MessageFormat
,
ParsingException
Modifier and Type | Field and Description |
---|---|
private StreamTokenizer |
st |
private String |
text |
private Hashtable |
varmap |
Constructor and Description |
---|
TemplateFormat(String text)
Construct a new
TemplateFormat for the given template
string. |
Modifier and Type | Method and Description |
---|---|
private void |
_format(StringReader source,
StringBuffer dest,
Vector vars) |
void |
bind(String var,
boolean value)
Bind a variable to a boolean value.
|
void |
bind(String var,
double value)
Bind a variable to a double value.
|
void |
bind(String var,
float value)
Bind a variable to a float value.
|
void |
bind(String var,
int value)
Bind a variable to an integer value.
|
void |
bind(String var,
long value)
Bind a variable to a long value.
|
void |
bind(String var,
String value)
Bind a variable to a string value.
|
void |
clear()
Clear all variable bindings.
|
private void |
flagMismatch(int c) |
void |
format(StringBuffer dest)
Format the template, substituting the variable values into their
placeholders.
|
String[] |
getVariables()
Compile a list of all variables referenced in the template.
|
private String |
lookup(String var) |
public TemplateFormat(String text)
TemplateFormat
for the given template
string. The string is not actually parsed until the format()
method is called. Once a TemplateFormat
is created, it can
be used over and over, with different variable bindings each time, if
so desired.text
- The template string to use.public void bind(String var, String value)
var
- The variable name.value
- The value.public void bind(String var, boolean value)
var
- The variable name.value
- The value.public void bind(String var, int value)
var
- The variable name.value
- The value.public void bind(String var, long value)
var
- The variable name.value
- The value.public void bind(String var, float value)
var
- The variable name.value
- The value.public void bind(String var, double value)
var
- The variable name.value
- The value.public void clear()
public void format(StringBuffer dest) throws ParsingException
dest
- The StringBuffer
to write the formatted
template to.ParsingException
- If an error occurred during parsing,
such as a mismatched brace, unterminated placeholder, or undefined
variable.public String[] getVariables() throws ParsingException
ParsingException
- If an error occurred during parsing,
such as a mismatched brace, unterminated placeholder, or undefined
variable.private void _format(StringReader source, StringBuffer dest, Vector vars) throws ParsingException, IOException
ParsingException
IOException
private void flagMismatch(int c) throws ParsingException
ParsingException
private String lookup(String var) throws ParsingException
ParsingException
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses