TextUtils { public static String replaceBoundedString(String initial, String prefix, String suffix, String replacement) { return replaceBoundedString(initial, prefix, suffix, new ConstantStringReplacementGenerator(replacement)); }  static String getStringBetween(char[] charArray, int start, int end); static T visitBoundedString(String initial, String prefix, String suffix, BoundedStringVisitor<T> visitor); static String replaceBoundedString(String initial, String prefix, String suffix, String replacement); static String replaceBoundedString(final String initial, final String prefix, final String suffix, ReplacementGenerator visitor); static String removeAccents(String term);  }
@Test public void testNestedBounds() { final String prefix = "<tag"; final String suffix = "/tag>"; final TextUtils.ReplacementGenerator generator = new TextUtils.ReplacementGenerator() { @Override public void appendReplacementForMatch(int matchStart, int matchEnd, char[] initialStringAsCharArray, StringBuilder builder, String prefix, String suffix) { int firstQuoteIndex = matchStart; while (initialStringAsCharArray[firstQuoteIndex++] != '\'') ; int secondQuoteIndex = firstQuoteIndex + 1; while (initialStringAsCharArray[secondQuoteIndex++] != '\'') ; builder.append("<new src='").append(initialStringAsCharArray, firstQuoteIndex, secondQuoteIndex - firstQuoteIndex - 1).append("'></new>"); } }; String initial = "aaa<tag src='1'>bbb<tag src='2'>ccc<tag src='3'>dddd<tag src='4'>eee</tag>ddd</tag>cccc</tag>bbb</tag>aaa<tag src='5'>foo</tag>"; String expected = "aaa<new src='1'></new>aaa<new src='5'></new>"; assertEquals(expected, TextUtils.replaceBoundedString(initial, prefix, suffix, generator)); initial = "aaa<tag src='1'>bbb<tag src='2'>ccc</tag> <tag src='3'>dddd<tag src='4'>eee</tag>ddd</tag> bbb</tag> aaa <tag src='5'>foo</tag> ffff"; expected = "aaa<new src='1'></new> aaa <new src='5'></new> ffff"; assertEquals(expected, TextUtils.replaceBoundedString(initial, prefix, suffix, generator)); initial = "foo <tag src='1'> aaaa\n\n <tag src='2'>\n bbbbbb </tag>\n\t cccccccc <tag src='3'>\n" + " dddd </tag>\n eee <tag src='4'>\n ffff </tag>\n g <tag src='5'>\n gggg </tag>\n hhhh </tag> bar"; expected = "foo <new src='1'></new> bar"; assertEquals(expected, TextUtils.replaceBoundedString(initial, prefix, suffix, generator)); }
@Test public void testReplaceAllInstancesOfBoundedString() { assertEquals("", TextUtils.replaceBoundedString("", PREFIX, SUFFIX, REPLACEMENT)); assertEquals("", TextUtils.replaceBoundedString("", PREFIX, "", REPLACEMENT)); assertEquals("", TextUtils.replaceBoundedString("", "", "", REPLACEMENT)); DEF_GEN.setExpectedMatches(""); assertEquals(REPLACEMENT, TextUtils.replaceBoundedString("PREFIXSUF", PREFIX, SUFFIX, DEF_GEN)); DEF_GEN.setExpectedMatches("bbbbb"); assertEquals("aaaaREPLACEMENTccccc", TextUtils.replaceBoundedString("aaaaPREFIXbbbbbSUFccccc", PREFIX, SUFFIX, DEF_GEN)); DEF_GEN.setExpectedMatches((String) null); assertEquals("aaaPREFIXbbbbSUFF", TextUtils.replaceBoundedString("aaaPREFIXbbbbSUFF", PREFIX, "SUFFI", DEF_GEN)); assertEquals("aRcccReeeR", TextUtils.replaceBoundedString("aPbbScccPdSeeePS", "P", "S", "R")); }
@Test public void testReplaceBoundedString() { final String empty = ""; buildTestWithDefaults(REPLACEMENT, PREFIX + SUFFIX, empty); buildTest(empty, PREFIX + SUFFIX, empty, PREFIX, SUFFIX, empty); buildTest("RaRcccReeeR", "PSaPScccPdSeeePS", "R", "P", "S", empty, empty, "d", empty); }