Class MapFormat

  • All Implemented Interfaces:
    Serializable, Cloneable

    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");
     
    Author:
    Slavek Psenicka
    See Also:
    MessageFormat, Serialized Form
    • 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.
        Specified by:
        format in class Format
        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.)
        Specified by:
        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()
        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