public class Formatter extends Object
A formatter is created for a given template string which contains named parameters like ${param1}
.
Using one of the set methods, values for the parameters can be supplied. Calling #format
creates the output string.
Non string objects which are passed in as parameters, will be converted using NLS.toUserString(Object)
A formatter is neither thread safe nor intended for reuse. Instead a formatter is created, supplied with the relevant parameters by chaining calls to set and then discarded after getting the result string via format.
An example call might look like this:
System.out.println(
Formatter.create("Hello ${programmer}")
.set("programmer, "Obi Wan")
.format());
NLS
uses this class by supplied translated patterns when calling NLS.fmtr(String)
.
NLS.fmtr(String)
Modifier | Constructor and Description |
---|---|
protected |
Formatter()
Use the static factory methods create to obtain a new instance.
|
Modifier and Type | Method and Description |
---|---|
static Formatter |
create(String pattern)
Creates a new formatter with the given pattern.
|
static Formatter |
create(String pattern,
String lang)
Creates a new formatter with the given pattern and language.
|
static Formatter |
createURLFormatter(String pattern)
Creates a new formatter with auto url encoding turned on.
|
String |
format()
Generates the formatted string.
|
Formatter |
set(Map<String,Object> ctx)
Sets the whole context as parameters in this formatter.
|
Formatter |
set(String property,
Object value)
Adds the replacement value to use for the given property.
|
Formatter |
setDirect(String property,
String value,
boolean urlEncode)
Directly sets the given string value for the given property.
|
Formatter |
setUnencoded(String property,
Object value)
Adds the replacement value to use for the given property, without url encoding the value.
|
String |
smartFormat()
Generates the formatted string using smart output formatting.
|
String |
toString() |
protected Formatter()
public static Formatter create(String pattern, String lang)
The given language will be used when converting non-string parameters.
pattern
- specifies the pattern to be used for creating the outputlang
- specifies the language used when converting non-string parameters.public static Formatter create(String pattern)
Uses the currently active language when converting non-string parameters.
pattern
- specifies the pattern to be used for creating the outputpublic static Formatter createURLFormatter(String pattern)
Any parameters passed to this formatter will be automatically url encoded.
pattern
- specifies the pattern to be used for creating the outputpublic Formatter set(String property, Object value)
property
- the parameter in the template string which should be replacedvalue
- the value which should be used as replacementpublic Formatter setUnencoded(String property, Object value)
Formatters created by #createURLFormatter perform automatic url conversion for all parameters. Using this method however, disables url encoding for the given parameter and value.
property
- the parameter in the template string which should be replacedvalue
- the value which should be used as replacementpublic Formatter set(Map<String,Object> ctx)
Calls #set for each entry in the given map.
ctx
- a Map which provides a set of entries to replace.public Formatter setDirect(String property, String value, boolean urlEncode)
Sets the given string as replacement value for the named parameter. The value will not be sent through
NLS.toUserString(Object)
and therefore not trimmed etc.
property
- the parameter in the template string which should be replacedvalue
- the value which should be used as replacementurlEncode
- determines if url encoding should be applied. If the parameter is set to false,
this method won't perform any url encoding, even if the formatter was created
using #createURLFormatterpublic String format()
Applies all supplied replacement values on detected parameters formatted like ${param}
.
IllegalArgumentException
- if the pattern is malformedpublic String smartFormat()
Applies all supplied replacement values on detected parameters formatted like ${param}
.
Block can be formed using '[' and ']' a whole block is only output, if at least one replacement
was not empty.
Consider the pattern [${salutation} ][${firstname}] ${lastname}
. This will create
Mr. Foo Bar if all three parameters are filled, but Mr. Bar if the first name is missing
or Foo Bar if the salutation is missing.
IllegalArgumentException
- if the pattern is malformedCopyright © 2018. All rights reserved.