class StringUtilities {

    public static String expandProperties(String input, Properties properties)
    {
        if (input == null)
        {
            return null;
        }
        final String beginToken = "${";
        final String endToken = "}";
        final StringBuilder sb = new StringBuilder(input);
        boolean moreTokens = true;
        int position = sb.length();
        while (moreTokens)
        {
            final int beginIndex = sb.lastIndexOf(beginToken, position);
            if (beginIndex == -1)
            {
                moreTokens = false;
            }
            else
            {
                final int endIndex = sb.indexOf(endToken, beginIndex);
                if (endIndex == -1)
                {
                    position = beginIndex - 1;
                }
                else
                {
                    final String key = sb.substring(beginIndex + 2, endIndex);
                    final String value = properties.getProperty(key);
                    if (value != null && !value.equals(sb.substring(beginIndex, endIndex + 1)))
                    {
                        sb.replace(beginIndex, endIndex + 1, value);
                        position = beginIndex + value.length();
                    }
                    else
                    {
                        position = beginIndex - 1;
                    }
                }
            }
        }

        return sb.toString();
    }

    private  StringUtilities();

}