@ -28,6 +28,33 @@ public class ShowValueUtils {
public static final String ANY_THING_REGEX = "." ;
public static final String NUMBER_REGEX = "[0-9]" ;
public static final String ENGLISH_REGEX = "[a-zA-Z]" ;
public static final char LEFT_HTML_CHAR = '<' ;
public static final char RIGHT_HTML_CHAR = '>' ;
public static final String HEAD = "<html><nobr>" ;
public static final String TAIL = "</nobr></html>" ;
public static final String FONT_HEAD = "<font color = 'rgb(61,153,249)'>" ;
public static final String FONT_TAIL = "</font>" ;
public static final String HTML = "<html>" ;
public static final String NOBR = "<nobr>" ;
public static final String HTML_TAIL = "</html>" ;
public static final String NOBR_TAIL = "</nobr>" ;
public static final int HEAD_LEN = HEAD . length ( ) ;
public static final int TAIL_LEN = TAIL . length ( ) ;
public static final int FONT_HEAD_LEN = FONT_HEAD . length ( ) ;
public static final int FONT_TAIL_LEN = FONT_TAIL . length ( ) ;
public static final int HTML_LEN = HTML . length ( ) ;
public static final int HTML_TAIL_LEN = HTML_TAIL . length ( ) ;
public static final int NOBR_LEN = NOBR . length ( ) ;
public static final int NOBR_TAIL_LEN = NOBR_TAIL . length ( ) ;
/ * *
* < 转变为 & lt ; 的长度差
* /
public static final int LEFT_DIFFERENT = 3 ;
/ * *
* > 转变为 & gt的长度差
* /
public static final int RIGHT_DIFFERENT = 2 ;
public static Pattern containPattern ;
private static List < String > specialCharList = new ArrayList < > ( ) ;
@ -135,33 +162,93 @@ public class ShowValueUtils {
String result = str ;
Pattern pattern = containPattern ;
Matcher matcher = pattern . matcher ( str ) ;
String head = "<font color = 'rgb(61,153,249)'>" ;
String tail = "</font>" ;
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 "<html><body><div><nobr>" +
builder . toString ( ) +
"</nobr></div></body></html>" ;
return changeOriginHtmlStr ( HEAD + builder . toString ( ) + TAIL ) ;
} else {
return "<html><body><div><nobr>" +
replaceAll ( str , searchStr , "<font color = 'rgb(61,153,249)'>" + changeHtmlStr ( searchStr ) + "</font>" ) +
"</nobr></div></body></html>" ;
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