SearchHitDecoratorTag extends TagSupport { protected static String decorate(final String cssName, final String searchType, final String searchString, final String text) { if (StringUtils.isEmpty(cssName)) { throw new IllegalArgumentException("No CSS name specified!"); } final StringBuilder work = new StringBuilder(StringUtils.trimToEmpty(text)); if (StringUtils.trimToEmpty(searchType).equals(SearchEntriesController.SearchType.TEXT.name())) { final int offset = lastIndexOfIgnoreCase(work.toString(), searchString); if (offset >= 0) { work.insert(offset + searchString.length(), "</span>"); work.insert(offset, "<span class=\"" + cssName + "\">"); } } return work.toString(); }  @Override int doStartTag(); void setCssName(final String cssName); void setSearchType(final String searchType); void setSearchString(final String searchString); void setText(final String text);  }
@Test public void testDecorate() throws Exception { assertEquals("this is a <span class=\"hit\">test</span>!", SearchHitDecoratorTag.decorate("hit", SearchEntriesController.SearchType.TEXT.name(), "test", "this is a test!")); assertEquals("this is a double test <span class=\"hit\">test</span>!", SearchHitDecoratorTag.decorate("hit", SearchEntriesController.SearchType.TEXT.name(), "test", "this is a double test test!")); assertEquals("<span class=\"hit\">jesper</span>", SearchHitDecoratorTag.decorate("hit", SearchEntriesController.SearchType.TEXT.name(), "jesper", "jesper")); assertEquals("<span class=\"hit\">Jesper</span>", SearchHitDecoratorTag.decorate("hit", SearchEntriesController.SearchType.TEXT.name(), "jesper", "Jesper")); assertEquals("this is a test!", SearchHitDecoratorTag.decorate("hit", SearchEntriesController.SearchType.TEXT.name(), "not-found", "this is a test!")); assertEquals("this is a test!", SearchHitDecoratorTag.decorate("hit", SearchEntriesController.SearchType.CAMELCASE.name(), "test", "this is a test!")); }