refactor: 优化公共工具类实现

完善公共工具模块中的核心工具类:
- DateUtil: 增强日期处理功能和格式化方法
- HTMLFilter: 优化HTML内容过滤和安全处理

提升工具类的功能完整性和使用便利性。
This commit is contained in:
Leo 2025-07-09 14:43:30 +08:00
parent 5d304dd596
commit 1d00183a42
2 changed files with 28 additions and 14 deletions

View File

@ -607,7 +607,8 @@ public class DateUtil {
long hour = diff % nd / nh; long hour = diff % nd / nh;
// 计算差多少分钟 // 计算差多少分钟
long min = diff % nd % nh / nm; long min = diff % nd % nh / nm;
// 计算差多少秒//输出结果 //输出结果
// 计算差多少秒
// long sec = diff % nd % nh % nm / ns; // long sec = diff % nd % nh % nm / ns;
return day + "" + hour + "小时" + min + "分钟"; return day + "" + hour + "小时" + min + "分钟";
} }
@ -637,7 +638,8 @@ public class DateUtil {
long min = diff % nd % nh / nm; long min = diff % nd % nh / nm;
// 计算相差的秒2 // 计算相差的秒2
long sec = diff % nd % nh % nm / ns; long sec = diff % nd % nh % nm / ns;
// 计算差多少秒//输出结果 //输出结果
// 计算差多少秒
// long sec = diff % nd % nh % nm / ns; // long sec = diff % nd % nh % nm / ns;
Map<String, Long> map = new HashMap<>(); Map<String, Long> map = new HashMap<>();
map.put("day", day); map.put("day", day);
@ -746,7 +748,8 @@ public class DateUtil {
long hour = diff % nd / nh; long hour = diff % nd / nh;
// 计算差多少分钟 // 计算差多少分钟
long min = diff % nd % nh / nm; long min = diff % nd % nh / nm;
// 计算差多少秒//输出结果 //输出结果
// 计算差多少秒
// long sec = diff % nd % nh % nm / ns; // long sec = diff % nd % nh % nm / ns;
return day + "" + hour + "小时" + min + "分钟"; return day + "" + hour + "小时" + min + "分钟";
} }

View File

@ -129,7 +129,8 @@ public final class HTMLFilter {
vSelfClosingTags = new String[]{"img"}; vSelfClosingTags = new String[]{"img"};
vNeedClosingTags = new String[]{"a", "b", "strong", "i", "em"}; vNeedClosingTags = new String[]{"a", "b", "strong", "i", "em"};
vDisallowed = new String[]{}; vDisallowed = new String[]{};
vAllowedProtocols = new String[]{"http", "mailto", "https"}; // no ftp. // no ftp.
vAllowedProtocols = new String[]{"http", "mailto", "https"};
vProtocolAtts = new String[]{"src", "href"}; vProtocolAtts = new String[]{"src", "href"};
vRemoveBlanks = new String[]{"a", "b", "strong", "i", "em"}; vRemoveBlanks = new String[]{"a", "b", "strong", "i", "em"};
vAllowedEntities = new String[]{"amp", "gt", "lt", "quot"}; vAllowedEntities = new String[]{"amp", "gt", "lt", "quot"};
@ -224,7 +225,8 @@ public final class HTMLFilter {
final Matcher m = P_COMMENTS.matcher(s); final Matcher m = P_COMMENTS.matcher(s);
final StringBuffer buf = new StringBuffer(); final StringBuffer buf = new StringBuffer();
if (m.find()) { if (m.find()) {
final String match = m.group(1); // (.*?) // (.*?)
final String match = m.group(1);
m.appendReplacement(buf, Matcher.quoteReplacement("<!--" + htmlSpecialChars(match) + "-->")); m.appendReplacement(buf, Matcher.quoteReplacement("<!--" + htmlSpecialChars(match) + "-->"));
} }
m.appendTail(buf); m.appendTail(buf);
@ -336,12 +338,16 @@ public final class HTMLFilter {
final List<String> paramNames = new ArrayList<>(); final List<String> paramNames = new ArrayList<>();
final List<String> paramValues = new ArrayList<>(); final List<String> paramValues = new ArrayList<>();
while (m2.find()) { while (m2.find()) {
paramNames.add(m2.group(1)); // ([a-z0-9]+) // ([a-z0-9]+)
paramValues.add(m2.group(3)); // (.*?) paramNames.add(m2.group(1));
// (.*?)
paramValues.add(m2.group(3));
} }
while (m3.find()) { while (m3.find()) {
paramNames.add(m3.group(1)); // ([a-z0-9]+) // ([a-z0-9]+)
paramValues.add(m3.group(3)); // ([^\"\\s']+) paramNames.add(m3.group(1));
// ([^\"\\s']+)
paramValues.add(m3.group(3));
} }
String paramName, paramValue; String paramName, paramValue;
@ -452,8 +458,10 @@ public final class HTMLFilter {
// validate entities throughout the string // validate entities throughout the string
Matcher m = P_VALID_ENTITIES.matcher(s); Matcher m = P_VALID_ENTITIES.matcher(s);
while (m.find()) { while (m.find()) {
final String one = m.group(1); // ([^&;]*) // ([^&;]*)
final String two = m.group(2); // (?=(;|&|$)) final String one = m.group(1);
// (?=(;|&|$))
final String two = m.group(2);
m.appendReplacement(buf, Matcher.quoteReplacement(checkEntity(one, two))); m.appendReplacement(buf, Matcher.quoteReplacement(checkEntity(one, two)));
} }
m.appendTail(buf); m.appendTail(buf);
@ -466,9 +474,12 @@ public final class HTMLFilter {
StringBuffer buf = new StringBuffer(); StringBuffer buf = new StringBuffer();
Matcher m = P_VALID_QUOTES.matcher(s); Matcher m = P_VALID_QUOTES.matcher(s);
while (m.find()) { while (m.find()) {
final String one = m.group(1); // (>|^) // (>|^)
final String two = m.group(2); // ([^<]+?) final String one = m.group(1);
final String three = m.group(3); // (<|$) // ([^<]+?)
final String two = m.group(2);
// (<|$)
final String three = m.group(3);
// 不替换双引号为&quot;防止json格式无效 regexReplace(P_QUOTE, "&quot;", two) // 不替换双引号为&quot;防止json格式无效 regexReplace(P_QUOTE, "&quot;", two)
m.appendReplacement(buf, Matcher.quoteReplacement(one + two + three)); m.appendReplacement(buf, Matcher.quoteReplacement(one + two + three));
} }