public class TextUtil {
private static final String END_SPAN = "</span>";
private static final String COMPARE_BACKGROUND = "<span style=\"background-color:yellow\">";
public static Map<String, String> compareText(String text1, String text2) {
char[] value1 = text1.toCharArray();
char[] value2 = text2.toCharArray();
int v1_i = 0;
int v2_i = 0;
String v1_new_value = "";
String v2_new_value = "";
int eq_min = 3;
int eq_index = 5;
int v1_index = 0;
int v1_eq_length = 0;
int v1_eq_max = 0;
int v1_start = 0;
int v2_index = 0;
int v2_eq_length = 0;
int v2_eq_max = 0;
int v2_start = 0;
while (v1_i < value1.length && v2_i < value2.length) {
// 去除已确定的字符外,字符串1和字符串2的下一个字符相同,则认为为改变
if (value1[v1_i] == value2[v2_i]) {
v1_new_value += value1[v1_i];
v2_new_value += value2[v2_i];
v1_i += 1;
v2_i += 1;
// 如果字符串1已结束,那么字符串2剩下的为新增内容
if (v1_i >= value1.length) {
String a = String.valueOf(value2).substring(v2_i, value2.length);
v2_new_value += (COMPARE_BACKGROUND + a + END_SPAN);
break;
}
// 如果字符串2已结束,那么字符串1剩下的为新增内容
if (v2_i >= value2.length) {
String a = String.valueOf(value1).substring(v1_i, value1.length);
v1_new_value += (COMPARE_BACKGROUND + a + END_SPAN);
break;
}
} else {
v1_index = v1_i + 1;
v1_eq_length = 0;
v1_eq_max = 0;
v1_start = v1_i + 1;
while (v1_index < value1.length) {
if ((v2_i + v1_eq_length) < value2.length && value1[v1_index] == value2[v2_i + v1_eq_length]) {
v1_eq_length += 1;
} else if (v1_eq_length > 0) {
if (v1_eq_max < v1_eq_length) {
v1_eq_max = v1_eq_length;
v1_start = v1_index - v1_eq_length;
}
v1_eq_length = 0;
break;// 只寻找最近的
}
v1_index += 1;
}
if (v1_eq_max < v1_eq_length) {
v1_eq_max = v1_eq_length;
v1_start = v1_index - v1_eq_length;
}
v2_index = v2_i + 1;
v2_eq_length = 0;
v2_eq_max = 0;
v2_start = v2_i + 1;
while (v2_index < value2.length) {
if (((v1_i + v2_eq_length) < value1.length) && value2[v2_index] == value1[v1_i + v2_eq_length]) {
v2_eq_length += 1;
} else if (v2_eq_length > 0) {
if (v2_eq_max < v2_eq_length) {
v2_eq_max = v2_eq_length;
v2_start = v2_index - v2_eq_length;
}
v1_eq_length = 0;
break;// 只寻找最近的
}
v2_index += 1;
}
if (v2_eq_max < v2_eq_length) {
v2_eq_max = v2_eq_length;
v2_start = v2_index - v2_eq_length;
}
if (v1_eq_max < eq_min && v1_start - v1_i > eq_index) {
v1_eq_max = 0;
}
if (v2_eq_max < eq_min && v2_start - v2_i > eq_index) {
v2_eq_max = 0;
}
if (v1_eq_max == 0 && v2_eq_max == 0) {
v1_new_value += (COMPARE_BACKGROUND + value1[v1_i] + END_SPAN);
v2_new_value += (COMPARE_BACKGROUND + value2[v2_i] + END_SPAN);
v1_i += 1;
v2_i += 1;
if (v1_i >= value1.length) {
v2_new_value += COMPARE_BACKGROUND
+ String.valueOf(value2).substring(v2_i, value2.length) + END_SPAN;
break;
}
if (v2_i >= value2.length) {
v1_new_value += (COMPARE_BACKGROUND
+ String.valueOf(value1).substring(v1_i, value1.length) + END_SPAN);
break;
}
} else if (v1_eq_max > v2_eq_max) {
v1_new_value += (COMPARE_BACKGROUND
+ String.valueOf(value1).substring(v1_i, v1_start) + END_SPAN);
v1_i = v1_start;
} else {
v2_new_value += (COMPARE_BACKGROUND
+ String.valueOf(value2).substring(v2_i, v2_start) + END_SPAN);
v2_i = v2_start;
}
}
}
Map<String, String> result = new HashMap<String, String>();
result.put("new_value1", v1_new_value);
result.put("new_value2", v2_new_value);
return result;
}
public static String toStackTrace(Exception e)
{
StringWriter sw = new StringWriter();
try{
e.printStackTrace(new PrintWriter(sw));
return sw.getBuffer().toString();
}
catch(Exception e1) {
return "";
}
}
}
/**
* 将有发生变化的每个字符前后都加上<span color="red"> </span>的标签
*/
public static void dealString(String str, StringBuffer resultBuffer) {
String beginKey = COMPARE_BACKGROUND;
String endKey = END_SPAN;
int beginKeyLen = beginKey.length();
int endKeyLen = endKey.length();
int beginIndex = str.indexOf(beginKey);
int endIndex = str.indexOf(endKey);
String strTmp = "";
if (beginIndex == -1) {
resultBuffer.append(str);
return;
} else {
strTmp = str.substring(beginIndex + beginKeyLen, endIndex);
resultBuffer.append(str.substring(0, beginIndex));
if (strTmp.length() > 1) {
char[] charTmp = strTmp.toCharArray();
;
for (int i = 0; i < charTmp.length; i++) {
resultBuffer.append(beginKey + charTmp[i] + endKey);
}
} else {
resultBuffer.append(beginKey + strTmp + endKey);
}
str = str.substring(endIndex + endKeyLen);
}
dealString(str, resultBuffer);
}