org.openide.util
Class MapFormat

java.lang.Object
  |
  +--java.text.Format
        |
        +--org.openide.util.MapFormat
All Implemented Interfaces:
Cloneable, Serializable
Direct Known Subclasses:
ExternalCompilerGroup.Format, ProcessExecutor.Format

public class MapFormat
extends 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");
 

See Also:
MessageFormat, Serialized Form

Constructor Summary
MapFormat(Map arguments)
          Constructor.
 
Method Summary
 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()
          Do you require both brackets in expression? If not, use setExactMatch(false) and formatter will ignore missing right bracket.
 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)
          Do you require both brackets in expression? If not, use setExactMatch(false) and formatter will ignore missing right bracket.
 void setLeftBrace(String delimiter)
          Sets string used as left brace
 void setMap(Map 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)
          Should formatter throw exception if object for key was not found? If given map does not contain object for key specified, it could throw an exception.
 boolean willThrowExceptionIfKeyWasNotFound()
          Should formatter throw exception if object for key was not found? If given map does not contain object for key specified, it could throw an exception.
 
Methods inherited from class java.text.Format
clone, format, parseObject
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MapFormat

public MapFormat(Map arguments)
Constructor. For common work use format(pattern, arguments) .
Parameters:
pattern - String to be parsed.
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 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 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 return string.
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.
Overrides:
format in class Format
Parameters:
table - Map with key-value pairs to replace.
result - Buffer to be used for result.
Returns:
Formatted string

parseObject

public Object parseObject(String text,
                          ParsePosition status)
Parses the string. Does not yet handle recursion (where the substituted strings contain %n references.)
Overrides:
parseObject in class Format

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()
Should formatter 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)
Should formatter 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()
Do you require both brackets in expression? If not, use setExactMatch(false) and formatter will ignore missing right bracket. Advanced feature.

setExactMatch

public void setExactMatch(boolean flag)
Do you require both brackets in 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:
delimiter - Right brace.


Built on December 12 2001.  |  Portions Copyright 1997-2001 Sun Microsystems, Inc. All rights reserved.