StringUtil { public static String substituteWithProperties(String string0, String startStr, String endStr, Properties properties) throws SubstitutionException { if (string0 == null) { return null; } StringBuilder result = new StringBuilder(string0.length()); int end = -1; int pos = string0.indexOf(startStr); while (pos >= 0) { if (pos > end+1) { result.append(string0.substring(end+endStr.length(), pos)); } end = string0.indexOf(endStr, pos+startStr.length()); if (end < 0) { throw new SubstitutionException("End of substitution pattern '" + endStr + "' not found [@position=" + pos + "]"); } String key = string0.substring(pos+startStr.length(), end); if (key == null || key.equals("")) { throw new SubstitutionException("Property key was empty in string with an occurrence of '" + startStr + endStr + "' [@position=" + pos + "]"); } String value = properties.getProperty(key); if (value == null) { throw new SubstitutionException("A property value for '" + startStr + key + endStr + "' was not found (property missing?)"); } result.append(value); pos = string0.indexOf(startStr, end+endStr.length()); } if (end+endStr.length() < string0.length()) { result.append(string0.substring(end+endStr.length())); } return result.toString(); }  static String substituteWithEnvironment(String string0); static String substituteWithProperties(String string0, String startStr, String endStr, Properties properties); static boolean isSafeString(String string0); static boolean isSafeChar(char ch); static String capitalize(String string0); static String uncapitalize(String string0); static boolean contains(String[] strings, String targetString); static int indexOf(String[] strings, String targetString); static String stripQuotes(String string0); static boolean containsOnlyDigits(String string0); static String[] split(String str, char delim); final static String formatForPrint(String input); static String[] toStringArray(Object[] objArray); static byte[] getAsciiBytes(String input); static String getAsciiString(byte[] input); static String trimTrailing(String str); static String truncate(String value, int length); static String slice(String value,
            int beginOffset, int endOffset,
            boolean trim); @Deprecated static String toHexString(byte[] bytes); @Deprecated static String toHexString(byte[] bytes, int offset, int length); @Deprecated static byte[] toHexByte(String hexString, int offset, int length); @Deprecated static byte[] fromHexString(String hexString); @Deprecated static byte[] fromHexString(String hexString, int offset, int length); static boolean isPrintableChar(char ch); static String hexDump(String prefix, byte[] data); static String SQLToUpperCase(String s); static boolean SQLEqualsIgnoreCase(String s1, String s2); static String normalizeSQLIdentifier(String id); static String compressQuotes(String source, String quotes); static String quoteStringLiteral(String string); static String stringify(int[] raw); static boolean isEmpty(String string0); static boolean isEqual(String string0, String string1); static String toStringWithNullAsEmpty(Object obj); static String toStringWithNullAsReplaced(Object obj); static String toStringWithNullAsNull(Object obj); static boolean isEqual(String string0, String string1, boolean caseSensitive); static String readToString(InputStream in); static String escapeXml(String value); static String removeAllCharsExceptDigits(String str0); static char[] HEX_TABLE; }
@Test public void substituteWithProperties() throws Exception { Properties props = new Properties(); props.put("TEST1", "Hello World"); props.put(" TEST1", "whitespace matters"); props.put("TEST2", "Hello"); props.put("TEST3", "World"); String result = StringUtil.substituteWithProperties("$ENV{TEST1}", "$ENV{", "}", props); Assert.assertEquals("Hello World", result); result = StringUtil.substituteWithProperties("{{{TEST1}}}", "{{{", "}}}", props); Assert.assertEquals("Hello World", result); result = StringUtil.substituteWithProperties("no key here", "$ENV{", "}", props); Assert.assertEquals("no key here", result); result = StringUtil.substituteWithProperties("Yo $ENV{TEST1} this is cool", "$ENV{", "}", props); Assert.assertEquals("Yo Hello World this is cool", result); result = StringUtil.substituteWithProperties("Y$ENV{TEST1}", "$ENV{", "}", props); Assert.assertEquals("YHello World", result); result = StringUtil.substituteWithProperties("$ENV{TEST1}Y", "$ENV{", "}", props); Assert.assertEquals("Hello WorldY", result); result = StringUtil.substituteWithProperties("$ENV{ TEST1}", "$ENV{", "}", props); Assert.assertEquals("whitespace matters", result); result = StringUtil.substituteWithProperties("", "$ENV{", "}", props); Assert.assertEquals("", result); result = StringUtil.substituteWithProperties(null, "$ENV{", "}", props); Assert.assertEquals(null, result); result = StringUtil.substituteWithProperties("$ENV{TEST2}$ENV{TEST3}", "$ENV{", "}", props); Assert.assertEquals("HelloWorld", result); result = StringUtil.substituteWithProperties("$ENV{TEST2} $ENV{TEST3}", "$ENV{", "}", props); Assert.assertEquals("Hello World", result); result = StringUtil.substituteWithProperties("Yo $ENV{TEST2} $ENV{TEST3} dude", "$ENV{", "}", props); Assert.assertEquals("Yo Hello World dude", result); }
@Test(expected=SubstitutionException.class) public void substituteWithPropertiesMissingKeyThrowsException() throws Exception { Properties props = new Properties(); props.put("TEST1", "Hello World"); String string0 = StringUtil.substituteWithProperties("$ENV{TEST2}", "$ENV{", "}", props); }
@Test(expected=SubstitutionException.class) public void substituteWithPropertiesEmptyKeyThrowsException() throws Exception { Properties props = new Properties(); props.put("TEST1", "Hello World"); String string0 = StringUtil.substituteWithProperties("$ENV{}", "$ENV{", "}", props); }
@Test(expected=SubstitutionException.class) public void substituteWithPropertiesMissingEndTokenThrowsException() throws Exception { Properties props = new Properties(); props.put("TEST1", "Hello World"); String string0 = StringUtil.substituteWithProperties("$ENV{TEST1", "$ENV{", "}", props); }