SimpleExcerptProvider { static String highlight(StringBuilder text, Set<String> searchToken) { Set<String> tokens = tokenize(searchToken); String escaped = encodeIllegalXMLCharacters(text.toString()); BitSet highlight = new BitSet(); for (String token : tokens) { highlight(escaped, highlight, token); } StringBuilder excerpt = new StringBuilder(EXCERPT_BEGIN); boolean strong = false; for (int i = 0; i < escaped.length(); i++) { if (highlight.get(i) && !strong) { strong = true; excerpt.append("<strong>"); } else if (!highlight.get(i) && strong) { strong = false; excerpt.append("</strong>"); } excerpt.append(escaped.charAt(i)); } if (strong) { excerpt.append("</strong>"); } excerpt.append(EXCERPT_END); return excerpt.toString(); } private  SimpleExcerptProvider(); static int indexOfIgnoreCase(String str, String searchStr, int startPos);  }
@Test public void caseInsensitiveTest() throws Exception { assertEquals("<div><span><strong>fox</strong> is jumping and dancing foxtrot</span></div>", highlight(sb("fox is jumping and dancing foxtrot"), of("Fox"))); assertEquals("<div><span>fox is <strong>jumping</strong></span></div>", highlight(sb("fox is jumping"), of("jUmP*"))); }
@Test public void simpleTest() throws Exception { assertEquals("<div><span><strong>fox</strong> is jumping</span></div>", highlight(sb("fox is jumping"), of("fox"))); assertEquals("<div><span>fox is <strong>jumping</strong></span></div>", highlight(sb("fox is jumping"), of("jump*"))); }
@Test public void highlightWithWildCard() throws Exception { assertEquals("<div><span><strong>fox</strong> is jumping</span></div>", highlight(sb("fox is jumping"), of("fox *"))); }
@Test public void highlightIgnoreStar() throws Exception { assertEquals("<div><span>10 * 10</span></div>", highlight(sb("10 * 10"), of("fox *"))); }
@Test public void randomized() throws Exception { Random r = new Random(1); String set = "abc*\'\"<> "; for (int i = 0; i < 10000; i++) { highlight(sb(randomString(r, set)), of(randomString(r, set))); } }
@Test public void hightlightCompleteWordOnly() { String[] delimiters = new String[] {" ", "\t", "\n", ":", "\u1680", "\u00A0"}; Map<String, String> simpleCheck = Maps.newHashMap(); simpleCheck.put("official conflict of interest", "<div><span>official conflict <strong>of</strong> interest</span></div>"); simpleCheck.put("of to new city", "<div><span><strong>of</strong> to new city</span></div>"); simpleCheck.put("out of the roof", "<div><span>out <strong>of</strong> the roof</span></div>"); simpleCheck.put("well this is of", "<div><span>well this is <strong>of</strong></span></div>"); for (Map.Entry<String, String> simple : simpleCheck.entrySet()) { for (String delimiter : delimiters) { String text = simple.getKey().replaceAll(" ", delimiter); String expect = simple.getValue().replaceAll(" ", delimiter); assertEquals("highlighting '" + text + "' for 'of' (delimiter - '" + delimiter + "')", expect, highlight(sb(text), of("of"))); } } Map<String, String> wildcardCheck = Maps.newHashMap(); wildcardCheck.put("office room", "<div><span><strong>office</strong> room</span></div>"); wildcardCheck.put("office room off", "<div><span><strong>office</strong> room <strong>off</strong></span></div>"); wildcardCheck.put("big office room", "<div><span>big <strong>office</strong> room</span></div>"); for (Map.Entry<String, String> wildcard : wildcardCheck.entrySet()) { for (String delimiter : delimiters) { String text = wildcard.getKey().replaceAll(" ", delimiter); String expect = wildcard.getValue().replaceAll(" ", delimiter); assertEquals("highlighting '" + text + "' for 'of*' (delimiter - '" + delimiter + "')", expect, highlight(sb(text), of("of*"))); } } }
@Test public void multipleSearchTokens() { String text = "To be, or not to be. That is the question!"; String expected = "<div><span>To <strong>be</strong>, " + "or not to <strong>be</strong>. " + "That is the <strong>question</strong>!</span></div>"; assertEquals(expected, highlight(sb(text), of("question", "be"))); assertEquals(expected, highlight(sb(text), of("quest*", "be"))); assertEquals(expected, highlight(sb(text), of("quest*", "b*"))); }