站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > NetBeans API Javadoc 5.0.0

MapFormat (NetBeans Utilities API) - NetBeans API Javadoc 5.0.0

 

org.openide.util
Class MapFormat

java.lang.Object
  extended byjava.text.Format
      extended byorg.openide.util.MapFormat
All Implemented Interfaces:
Cloneable, Serializable

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

Nested Class Summary
 
Nested classes inherited from class java.text.Format
Format.Field
 
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()
          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 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.
 
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
 

Constructor Detail

MapFormat

public MapFormat(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 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.

Parameters:
pat - a string pattern
result - Buffer to be used for result.
fpos - position
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.)


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

 

Built on May 3 2007.  |  Portions Copyright 1997-2005 Sun Microsystems, Inc. All rights reserved.