specialCharList = new ArrayList<>();
@@ -135,33 +162,93 @@ public class ShowValueUtils {
String result = str;
Pattern pattern = containPattern;
Matcher matcher = pattern.matcher(str);
- String head = "";
- String tail = "";
- int size = head.length() + tail.length();
+ int size = FONT_HEAD.length() + FONT_TAIL.length();
int index = 0;
StringBuilder builder = new StringBuilder(str);
while (matcher.find()) {
- builder.replace(matcher.start() + index, matcher.end() + index, head + changeHtmlStr(matcher.group()) + tail);
+ builder.replace(matcher.start() + index, matcher.end() + index, FONT_HEAD + changeHtmlStr(matcher.group()) + FONT_TAIL);
index += size;
+ index += getIncreaseCount(matcher.group());
}
- return "" +
- builder.toString() +
- "
";
+
+ return changeOriginHtmlStr(HEAD + builder.toString() + TAIL);
} else {
- return "" +
- replaceAll(str, searchStr, "" + changeHtmlStr(searchStr) + "") +
- "
";
+ String ans = HEAD + replaceAll(str, searchStr, FONT_HEAD + changeHtmlStr(searchStr) + FONT_TAIL) + TAIL;
+ return changeOriginHtmlStr(ans);
}
+ }
+ /**
+ * 用于处理原字符串中的尖括号
+ */
+ private static String changeOriginHtmlStr(String str) {
+ int count = 0;
+ StringBuilder builder = new StringBuilder(str);
+ for (int i = 0; i < str.length(); i++) {
+ char c = str.charAt(i);
+ if (c == LEFT_HTML_CHAR) {
+ if (!checkValid(str, i)) {
+ builder.replace(i + count, i + count + 1, "<");
+ count += LEFT_DIFFERENT;
+ }
+ }
+ }
+ return builder.toString();
+ }
+
+ private static boolean checkValid(String str, int i) {
+ int leftLen = str.length() - (i + 1);
+ return checkStrValid(str, i, leftLen, HTML_LEN, HTML) ||
+ checkStrValid(str, i, leftLen, HTML_TAIL_LEN, HTML_TAIL) ||
+ checkStrValid(str, i, leftLen, FONT_HEAD_LEN, FONT_HEAD) ||
+ checkStrValid(str, i, leftLen, FONT_TAIL_LEN, FONT_TAIL) ||
+ checkStrValid(str, i, leftLen, NOBR_LEN, NOBR) ||
+ checkStrValid(str, i, leftLen, NOBR_TAIL_LEN, NOBR_TAIL);
+ }
+
+ /**
+ * 是否是所指定的字符串
+ *
+ * @param str
+ * @param i
+ * @param leftLen
+ * @return
+ */
+ private static boolean checkStrValid(String str, int i, int leftLen, int len, String checkStr) {
+ if (leftLen < len - 1) {
+ return false;
+ } else {
+ return StringUtils.equals(str.substring(i, i + len), checkStr);
+ }
}
- private static String changeHtmlStr(String searchStr){
+
+ /**
+ * 用于处理搜索的字符中的尖括号
+ *
+ * @param searchStr
+ * @return
+ */
+ private static String changeHtmlStr(String searchStr) {
String showStr = searchStr;
- showStr = showStr.replace("<","<");
+ showStr = showStr.replace("<", "<");
showStr = showStr.replace(">", ">");
return showStr;
}
+ private static int getIncreaseCount(String str) {
+ int count = 0;
+ for (int i = 0; i < str.length(); i++) {
+ if (str.charAt(i) == LEFT_HTML_CHAR) {
+ count += LEFT_DIFFERENT;
+ }
+ if (str.charAt(i) == RIGHT_HTML_CHAR) {
+ count += RIGHT_DIFFERENT;
+ }
+ }
+ return count;
+ }
+
/**
* 集合是否不为空
*
@@ -193,6 +280,7 @@ public class ShowValueUtils {
/**
* 支持通配符的contains
+ *
* @param originStr
* @param matchStr
* @return