boldHighlight方法通过<b></b>
标记
colorHighlight方法采用文本字符串并通过<b style='background-color: #color'></b>
以12种交替颜色突出显示int q关键字
String text = "The use of hello as a telephone greeting has been credited to Thomas
Edison; according to one source, he expressed his surprise with a
misheard Hullo. Alexander Graham Bell initially used Ahoy (as used on
ships) as a telephone greeting"
String keywords = "HELLO Surprise"
boldHighlight(text, keywords); // will produce:
使用
<b>hello</b>
作为电话问候已归功于托马斯爱迪生;根据一个消息来源,他表达了他的<b>surprise</b>
听错了Hullo。 Alexander Graham Bell最初使用Ahoy(在船上使用)作为电话问候语
colorHighlight(text, keywords); // will produce:
使用
<b style='background-color:#ffff66'>hello</b>
作为电话问候已归功于托马斯爱迪生;&gt;根据一位消息来源,他表达了他的<b style='background-color:#a0ffff'>surprise</b>
听错了Hullo。 Alexander Graham Bell最初使用Ahoy(在船上使用)作为电话问候语
private static final String[] colors = new String[]{"ffff66", "a0ffff", "99ff99", "ff9999", "ff66ff", "880000", "00aa00", "886800", "004699", "990099", "ffff66", "a0ffff"};
public static String safeCharWithSpace(String input) {
input = input.trim();
return Normalizer.normalize(input.toLowerCase(), Normalizer.Form.NFD)
.replaceAll("\\p{InCombiningDiacriticalMarks}+", "")
.replaceAll("[^\\p{Alnum}]+", " ");
}
private static String prepQuery(String q) {
try {
log.debug("qr encoded: " + q);
q = URLDecoder.decode(q, "UTF-8");
} catch (UnsupportedEncodingException ignore) {
}
log.debug("qr decoded: " + q);
return removeIgnoreCase(q, stopWords);
}
public static String boldHighlight(String text, String q) {
return highlight(text, q, false);
}
public static String colorHighlight(String text, String q) {
return highlight(text, q, true);
}
private static String replaceWord(String text, String keyword, int colorNumber, boolean useColor) {
String color = "";
keyword = safeCharWithSpace(keyword);
if (StringUtils.isNotEmpty(keyword) && !StringUtils.isWhitespace(keyword)) {
if (useColor) color = " style='background-color: " + colors[colorNumber] + "'";
return text.replaceAll("(?i)(" + keyword + ")(?!([^<]+)?>>)", "<b" + color + ">$1</b>");
} else
return text;
}
public static String highlight(String text, String q, boolean useColor) {
String qr = prepQuery(q);
String rtn = null;
int i = 0;
if (qr.startsWith("\"")) {
String keywords = StringUtils.remove(qr, "\"");
rtn = replaceWord(text, keywords, 0, useColor);
} else {
String[] keywords = qr.split("\\s");
for (String keyword : keywords) {
rtn = replaceWord(text, keyword, i, useColor);
if (useColor) {
if (i < 11) i++;
else i = 0;
}
}
}
return rtn;
}
要删除removeIgnoreCase()
方法中的停用词prepQuery()
,请参阅我的其他帖子:Removing strings from another string in java
答案 0 :(得分:1)
即
${statics["java.lang.System"].currentTimeMillis()}
MVC要做的事情是在处理模板之前进行此处理,但我知道你只是维护代码。
看起来它只是做了几个替换所有的,所以改变Java方法应该有效。我不得不建议你看看escaping tools Freemarker。
Freemarker真的很棒documentation,并且内置ins涵盖了许多情况。