org.openide.util
Class MapFormat

java.lang.Object
  extended by java.text.Format
      extended by org.openide.util.MapFormat
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public class MapFormat
extends java.text.Format

A text format similar to 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");
 

Author:
Slavek Psenicka
See Also:
MessageFormat, Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.text.Format
java.text.Format.Field
 
Field Summary
(package private) static long serialVersionUID
          Array with to-be-skipped blocks
 
Constructor Summary
MapFormat(java.util.Map arguments)
          Constructor.
 
Method Summary
 java.lang.StringBuffer format(java.lang.Object pat, java.lang.StringBuffer result, java.text.FieldPosition fpos)
          Formats the parsed string by inserting table's values.
static java.lang.String format(java.lang.String pattern, java.util.Map arguments)
          Designated method.
 java.lang.String getLeftBrace()
          Returns string used as left brace
 java.util.Map getMap()
          Returns argument map
 java.lang.String getRightBrace()
          Returns string used as right brace
 boolean isExactMatch()
          Test whether both brackets are required in the expression.
 java.lang.String parse(java.lang.String source)
          Parses the string.
 java.lang.Object parseObject(java.lang.String text, java.text.ParsePosition status)
          Parses the string.
protected  java.lang.Object processKey(java.lang.String key)
          Returns the value for given key.
 java.lang.String processPattern(java.lang.String newPattern)
          Scans the pattern and prepares internal variables.
 void setExactMatch(boolean flag)
          Specify whether both brackets are required in the expression.
 void setLeftBrace(java.lang.String delimiter)
          Sets string used as left brace
 void setMap(java.util.Map map)
          Sets argument map This map should contain key-value pairs with key values used in formatted string expression.
 void setRightBrace(java.lang.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.
 
Methods inherited from class java.text.Format
clone, format, formatToCharacterIterator, parseObject
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

serialVersionUID

static final long serialVersionUID
Array with to-be-skipped blocks

See Also:
Constant Field Values
Constructor Detail

MapFormat

public MapFormat(java.util.Map arguments)
Constructor. For common work use format(pattern, arguments) .

Parameters:
arguments - keys and values to use in the format
Method Detail

format

public static java.lang.String format(java.lang.String pattern,
                                      java.util.Map arguments)
Designated method. It gets the string, initializes HashFormat object and returns converted string. It scans pattern for {} brackets, then parses enclosed string and replaces it with argument's get() value.

Parameters:
pattern - String to be parsed.
arguments - Map with key-value pairs to replace.
Returns:
Formatted string

processKey

protected java.lang.Object processKey(java.lang.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 return string.

Parameters:
key - Key.
Returns:
Value for this key.

processPattern

public java.lang.String processPattern(java.lang.String newPattern)
                                throws java.lang.IllegalArgumentException
Scans the pattern and prepares internal variables.

Parameters:
newPattern - String to be parsed.
Throws:
java.lang.IllegalArgumentException - if number of arguments exceeds BUFSIZE or parser found unmatched brackets (this exception should be switched off using setExactMatch(false)).

format

public java.lang.StringBuffer format(java.lang.Object pat,
                                     java.lang.StringBuffer result,
                                     java.text.FieldPosition fpos)
Formats the parsed string by inserting table's values.

Specified by:
format in class java.text.Format
Parameters:
pat - a string pattern
result - Buffer to be used for result.
fpos - position
Returns:
Formatted string

parseObject

public java.lang.Object parseObject(java.lang.String text,
                                    java.text.ParsePosition status)
Parses the string. Does not yet handle recursion (where the substituted strings contain %n references.)

Specified by:
parseObject in class java.text.Format

parse

public java.lang.String parse(java.lang.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 java.lang.String getLeftBrace()
Returns string used as left brace


setLeftBrace

public void setLeftBrace(java.lang.String delimiter)
Sets string used as left brace

Parameters:
delimiter - Left brace.

getRightBrace

public java.lang.String getRightBrace()
Returns string used as right brace


setRightBrace

public void setRightBrace(java.lang.String delimiter)
Sets string used as right brace

Parameters:
delimiter - Right brace.

getMap

public java.util.Map getMap()
Returns argument map


setMap

public void setMap(java.util.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