Package com.sun.identity.tools.objects
Class MapFormat
- java.lang.Object
-
- java.text.Format
-
- com.sun.identity.tools.objects.MapFormat
-
- All Implemented Interfaces:
Serializable,Cloneable
public class MapFormat extends Format
A text format similar toMessageFormatbut 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");- Author:
- Slavek Psenicka
- See Also:
MessageFormat, Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.text.Format
Format.Field
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description StringBufferformat(Object pat, StringBuffer result, FieldPosition fpos)Formats the parsed string by inserting table's values.static Stringformat(String pattern, Map arguments)Designated method.StringgetLeftBrace()Returns string used as left braceMapgetMap()Returns argument mapStringgetRightBrace()Returns string used as right bracebooleanisExactMatch()Test whether both brackets are required in the expression.Stringparse(String source)Parses the string.ObjectparseObject(String text, ParsePosition status)Parses the string.protected ObjectprocessKey(String key)Returns the value for given key.StringprocessPattern(String newPattern)Scans the pattern and prepares internal variables.voidsetExactMatch(boolean flag)Specify whether both brackets are required in the expression.voidsetLeftBrace(String delimiter)Sets string used as left bracevoidsetMap(Map map)Sets argument map This map should contain key-value pairs with key values used in formatted string expression.voidsetRightBrace(String delimiter)Sets string used as right bracevoidsetThrowExceptionIfKeyWasNotFound(boolean flag)Specify whether formatter will throw exception if object for key was not found.booleanwillThrowExceptionIfKeyWasNotFound()Test whether formatter will throw exception if object for key was not found.-
Methods inherited from class java.text.Format
clone, format, formatToCharacterIterator, parseObject
-
-
-
-
Constructor Detail
-
MapFormat
public MapFormat(Map arguments)
Constructor. For common work useformat(pattern, arguments).- Parameters:
arguments- keys and values to use in the format
-
-
Method Detail
-
format
public static String format(String pattern, Map arguments)
Designated method. It gets the string, initializes HashFormat object and returns converted string. It scanspatternfor {} brackets, then parses enclosed string and replaces it with argument'sget()value.- Parameters:
pattern- String to be parsed.arguments- Map with key-value pairs to replace.- Returns:
- Formatted string
-
processKey
protected Object processKey(String key)
Returns the value for given key. Subclass may define its own beahvior of this method. For example, if key is not defined, subclass can returnstring. - Parameters:
key- Key.- Returns:
- Value for this key.
-
processPattern
public String processPattern(String newPattern) throws IllegalArgumentException
Scans the pattern and prepares internal variables.- Parameters:
newPattern- String to be parsed.- Throws:
IllegalArgumentException- if number of arguments exceeds BUFSIZE or parser found unmatched brackets (this exception should be switched off using setExactMatch(false)).
-
format
public StringBuffer format(Object pat, StringBuffer result, FieldPosition fpos)
Formats the parsed string by inserting table's values.
-
parseObject
public Object parseObject(String text, ParsePosition status)
Parses the string. Does not yet handle recursion (where the substituted strings contain %n references.)- Specified by:
parseObjectin classFormat
-
parse
public String parse(String source)
Parses the string. Does not yet handle recursion (where the substituted strings contain {n} references.)- Returns:
- New format.
-
willThrowExceptionIfKeyWasNotFound
public boolean willThrowExceptionIfKeyWasNotFound()
Test whether formatter will throw exception if object for key was not found. If given map does not contain object for key specified, it could throw an exception. Returns true if throws. If not, key is left unchanged.
-
setThrowExceptionIfKeyWasNotFound
public void setThrowExceptionIfKeyWasNotFound(boolean flag)
Specify whether formatter will throw exception if object for key was not found. If given map does not contain object for key specified, it could throw an exception. If does not throw, key is left unchanged.- Parameters:
flag- If true, formatter throws IllegalArgumentException.
-
isExactMatch
public boolean isExactMatch()
Test whether both brackets are required in the expression. If not, use setExactMatch(false) and formatter will ignore missing right bracket. Advanced feature.
-
setExactMatch
public void setExactMatch(boolean flag)
Specify whether both brackets are required in the expression. If not, use setExactMatch(false) and formatter will ignore missing right bracket. Advanced feature.- Parameters:
flag- If true, formatter will ignore missing right bracket (default = false)
-
getLeftBrace
public String getLeftBrace()
Returns string used as left brace
-
setLeftBrace
public void setLeftBrace(String delimiter)
Sets string used as left brace- Parameters:
delimiter- Left brace.
-
getRightBrace
public String getRightBrace()
Returns string used as right brace
-
setRightBrace
public void setRightBrace(String delimiter)
Sets string used as right brace- Parameters:
delimiter- Right brace.
-
getMap
public Map getMap()
Returns argument map
-
setMap
public void setMap(Map map)
Sets argument map This map should contain key-value pairs with key values used in formatted string expression. If value for key was not found, formatter leave key unchanged (except if you've set setThrowExceptionIfKeyWasNotFound(true), then it fires IllegalArgumentException.- Parameters:
map- the argument map
-
-