public class MapFormat extends Format
MessageFormat
but using string rather than numeric keys.
You might use use this formatter like this:
MapFormat.format("Hello {name}", map);Or to have more control over it:
Map m = new HashMap (); m.put ("KEY", "value"); MapFormat f = new MapFormat (m); f.setLeftBrace ("__"); f.setRightBrace ("__"); String result = f.format ("the __KEY__ here");
MessageFormat
,
Serialized FormFormat.Field
Constructor and Description |
---|
MapFormat(Map<String,?> arguments)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
StringBuffer |
format(Object pat,
StringBuffer result,
FieldPosition fpos)
Formats the parsed string by inserting table's values.
|
static String |
format(String pattern,
Map arguments)
Designated method.
|
String |
getLeftBrace()
Returns string used as left brace.
|
Map |
getMap()
Returns argument map.
|
String |
getRightBrace()
Returns string used as right brace.
|
boolean |
isExactMatch()
Test whether both brackets are required in the expression.
|
String |
parse(String source)
Parses the string.
|
Object |
parseObject(String text,
ParsePosition status)
Parses the string.
|
protected Object |
processKey(String key)
Returns the value for given key.
|
String |
processPattern(String newPattern)
Scans the pattern and prepares internal variables.
|
void |
setExactMatch(boolean flag)
Specify whether both brackets are required in the expression.
|
void |
setLeftBrace(String delimiter)
Sets string used as left brace
|
void |
setMap(Map<String,?> map)
Sets argument map
This map should contain key-value pairs with key values used in
formatted string expression.
|
void |
setRightBrace(String delimiter)
Sets string used as right brace
|
void |
setThrowExceptionIfKeyWasNotFound(boolean flag)
Specify whether formatter will throw exception if object for key was not found.
|
boolean |
willThrowExceptionIfKeyWasNotFound()
Test whether formatter will throw exception if object for key was not found.
|
clone, format, formatToCharacterIterator, parseObject
public static String format(String pattern, Map arguments)
pattern
for {} brackets, then parses enclosed string and replaces it
with argument's get()
value.pattern
- String to be parsed.arguments
- Map with key-value pairs to replace.protected Object processKey(String key)
key
- Key.public String processPattern(String newPattern) throws IllegalArgumentException
newPattern
- String to be parsed.IllegalArgumentException
- if number of arguments exceeds BUFSIZE or
parser found unmatched brackets (this exception should be switched off
using setExactMatch(false)).public StringBuffer format(Object pat, StringBuffer result, FieldPosition fpos)
public Object parseObject(String text, ParsePosition status)
parseObject
in class Format
public String parse(String source)
source
- string to parsepublic boolean willThrowExceptionIfKeyWasNotFound()
public void setThrowExceptionIfKeyWasNotFound(boolean flag)
flag
- If true, formatter throws IllegalArgumentException.public boolean isExactMatch()
public void setExactMatch(boolean flag)
flag
- If true, formatter will ignore missing right bracket (default = false)public String getLeftBrace()
public void setLeftBrace(String delimiter)
delimiter
- Left brace.public String getRightBrace()
public void setRightBrace(String delimiter)
delimiter
- Right brace.public Map getMap()
public void setMap(Map<String,?> map)
map
- the argument map