Exemple #1
0
        /*
         * Substitutes all entries like ${some.key}, found in specified string,
         * for specified values.
         * If some key is unknown, throws ExpansionFailedException.
         * @param str the string to be expanded
         * @param properties available key-value mappings
         * @return expanded string
         * @throws ExpansionFailedException
         */
        public static String expand(String str, java.util.Properties properties)
        {//throws ExpansionFailedException {
            java.lang.StringBuilder result = new java.lang.StringBuilder(str);
            int start = result.indexOf(START_MARK);

            while (start >= 0)
            {
                int end = result.indexOf(END_MARK, start);
                if (end >= 0)
                {
                    String key   = result.substring(start + START_OFFSET, end);
                    String value = properties.getProperty(key);
                    if (value != null)
                    {
                        result.replace(start, end + END_OFFSET, value);
                        start += value.length();
                    }
                    else
                    {
                        throw new ExpansionFailedException("Unknown key: " + key); //$NON-NLS-1$
                    }
                }
                start = result.indexOf(START_MARK, start);
            }
            return(result.toString());
        }