From e56e10b75cccc61061490e56cd5125443d36a7c7 Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 27 Jun 2024 12:04:00 +0800 Subject: [PATCH] fix code --- .../format/parser/GlobalRegexDateParser.java | 84 ++++++++++++++++++ .../date/format/parser/RegexDateParser.java | 84 +++++++++++------- .../format/parser/RegexListDateParser.java | 88 ------------------- .../format/parser/RegexDateParserTest.java | 5 -- .../hutool/http/meta/HttpHeaderUtil.java | 2 +- 5 files changed, 139 insertions(+), 124 deletions(-) create mode 100644 hutool-core/src/main/java/org/dromara/hutool/core/date/format/parser/GlobalRegexDateParser.java delete mode 100644 hutool-core/src/main/java/org/dromara/hutool/core/date/format/parser/RegexListDateParser.java diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/date/format/parser/GlobalRegexDateParser.java b/hutool-core/src/main/java/org/dromara/hutool/core/date/format/parser/GlobalRegexDateParser.java new file mode 100644 index 000000000..de4dcced1 --- /dev/null +++ b/hutool-core/src/main/java/org/dromara/hutool/core/date/format/parser/GlobalRegexDateParser.java @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2024. looly(loolly@aliyun.com) + * Hutool is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * https://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +package org.dromara.hutool.core.date.format.parser; + +import java.util.Date; +import java.util.regex.Pattern; + +/** + * 全局正则日期解析器
+ * 通过使用预定义或自定义的正则规则,解析日期字符串 + * + * @author Looly + * @since 6.0.0 + */ +public class GlobalRegexDateParser { + + // hh:mm:ss.SSSSZ hh:mm:ss.SSSS hh:mm:ss hh:mm + private static final String timeRegex = "(" + + "\\s(?\\d{1,2})" + + ":(?\\d{1,2})" + + "(:(?\\d{1,2}))?" + + "(?:[.,](?\\d{1,9}))?(?z)?" + + "(\\s?(?am|pm))?" + + ")?"; + // +08:00 +0800 +08 + private static final String zoneOffsetRegex = "(\\s?(?[-+]\\d{1,2}:?(?:\\d{2})?))?"; + // CST UTC (CST) + private static final String zoneNameRegex = "(\\s[(]?(?[a-z ]+)[)]?)?"; + + private static final RegexDateParser PARSER; + + static { + final String dateRegexMonthFirst = "(?\\w+{3,9})\\W+(?\\d{1,2})(?:th)?\\W+(?\\d{2,4})"; + + PARSER = RegexDateParser.of( + // 年开头 + + //月开头,类似:May 8, 2009 5:57:51 + dateRegexMonthFirst + timeRegex + zoneOffsetRegex + + // 周开头 + + // 日开头 + ); + } + + /** + * 解析日期,此方法线程安全 + * + * @param source 日期字符串 + * @return 日期 + */ + public static Date parse(final CharSequence source) { + return PARSER.parse(source); + } + + /** + * 新增自定义日期正则 + * + * @param regex 日期正则 + */ + synchronized public static void registerRegex(final String regex) { + PARSER.addRegex(regex); + } + + /** + * 新增自定义日期正则 + * + * @param pattern 日期正则 + */ + synchronized public static void registerPattern(final Pattern pattern) { + PARSER.addPattern(pattern); + } +} diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/date/format/parser/RegexDateParser.java b/hutool-core/src/main/java/org/dromara/hutool/core/date/format/parser/RegexDateParser.java index d8673dfeb..9abee1378 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/date/format/parser/RegexDateParser.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/date/format/parser/RegexDateParser.java @@ -12,74 +12,98 @@ package org.dromara.hutool.core.date.format.parser; +import org.dromara.hutool.core.collection.ListUtil; import org.dromara.hutool.core.date.*; import org.dromara.hutool.core.lang.Opt; import org.dromara.hutool.core.regex.ReUtil; import org.dromara.hutool.core.text.StrUtil; +import java.io.Serializable; +import java.util.ArrayList; import java.util.Date; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; /** - * 正则日期解析器
- * 通过定义一个命名分组的正则匹配日期格式,使用正则分组获取日期各部分的值,命名分组使用{@code (?子表达式) }表示,如:
- *
{@code
- *  ^(?\d{4})(?\d{2})$ 匹配 201909
- * }
+ * 使用正则列表方式的日期解析器
+ * 通过定义若干的日期正则,遍历匹配到给定正则后,按照正则方式解析为日期 * * @author Looly - * @since 6.0.0 */ -public class RegexDateParser implements PredicateDateParser { +public class RegexDateParser implements DateParser, Serializable { + private static final long serialVersionUID = 1L; private static final int[] NSS = {100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10, 1}; /** - * 根据给定带名称的分组正则,创建RegexDateParser + * 根据给定的正则列表,创建RegexListDateParser * - * @param regex 正则表达式 - * @return RegexDateParser + * @param regexes 正则列表,默认忽略大小写 + * @return RegexListDateParser */ - public static RegexDateParser of(final String regex) { - // 日期正则忽略大小写 - return of(Pattern.compile(regex, Pattern.CASE_INSENSITIVE)); + public static RegexDateParser of(final String... regexes) { + final List patternList = new ArrayList<>(regexes.length); + for (final String regex : regexes) { + patternList.add(Pattern.compile(regex, Pattern.CASE_INSENSITIVE)); + } + return new RegexDateParser(patternList); } /** - * 根据给定带名称的分组正则,创建RegexDateParser + * 根据给定的正则列表,创建RegexListDateParser * - * @param pattern 正则表达式 - * @return RegexDateParser + * @param patterns 正则列表 + * @return RegexListDateParser */ - public static RegexDateParser of(final Pattern pattern) { - return new RegexDateParser(pattern); + public static RegexDateParser of(final Pattern... patterns) { + return new RegexDateParser(ListUtil.of(patterns)); } - private final Pattern pattern; + private final List patterns; /** * 构造 * - * @param pattern 正则表达式 + * @param patterns 正则列表 */ - public RegexDateParser(final Pattern pattern) { - this.pattern = pattern; + public RegexDateParser(final List patterns) { + this.patterns = patterns; } - @Override - public boolean test(final CharSequence source) { - return ReUtil.isMatch(this.pattern, source); + /** + * 新增自定义日期正则 + * + * @param regex 日期正则 + * @return this + */ + public RegexDateParser addRegex(final String regex) { + // 日期正则忽略大小写 + return addPattern(Pattern.compile(regex, Pattern.CASE_INSENSITIVE)); + } + + /** + * 新增自定义日期正则 + * + * @param pattern 日期正则 + * @return this + */ + public RegexDateParser addPattern(final Pattern pattern) { + this.patterns.add(pattern); + return this; } @Override public Date parse(final CharSequence source) throws DateException { - final Matcher matcher = this.pattern.matcher(source); - if (!matcher.matches()) { - throw new DateException("Invalid date string: [{}], not match the date regex: [{}].", source, this.pattern.pattern()); + Matcher matcher; + for (final Pattern pattern : this.patterns) { + matcher = pattern.matcher(source); + if (matcher.matches()) { + return parse(matcher); + } } - return parse(matcher); + throw new DateException("No valid pattern for date string: [{}]", source); } /** @@ -89,7 +113,7 @@ public class RegexDateParser implements PredicateDateParser { * @return 日期 * @throws DateException 日期解析异常 */ - public static Date parse(final Matcher matcher) throws DateException { + private static Date parse(final Matcher matcher) throws DateException { // 毫秒时间戳 final String millisecond = ReUtil.group(matcher, "millisecond"); if (StrUtil.isNotEmpty(millisecond)) { diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/date/format/parser/RegexListDateParser.java b/hutool-core/src/main/java/org/dromara/hutool/core/date/format/parser/RegexListDateParser.java deleted file mode 100644 index 950e0f2ed..000000000 --- a/hutool-core/src/main/java/org/dromara/hutool/core/date/format/parser/RegexListDateParser.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright (c) 2024. looly(loolly@aliyun.com) - * Hutool is licensed under Mulan PSL v2. - * You can use this software according to the terms and conditions of the Mulan PSL v2. - * You may obtain a copy of Mulan PSL v2 at: - * https://license.coscl.org.cn/MulanPSL2 - * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, - * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, - * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. - * See the Mulan PSL v2 for more details. - */ - -package org.dromara.hutool.core.date.format.parser; - -import org.dromara.hutool.core.collection.ListUtil; -import org.dromara.hutool.core.date.DateException; - -import java.io.Serializable; -import java.util.Date; -import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * 使用正则列表方式的日期解析器
- * 通过定义若干的日期正则,遍历匹配到给定正则后,按照正则方式解析为日期 - * - * @author Looly - */ -public class RegexListDateParser implements DateParser, Serializable { - private static final long serialVersionUID = 1L; - - /** - * 根据给定的正则列表,创建RegexListDateParser - * - * @param patterns 正则列表 - * @return RegexListDateParser - */ - public static RegexListDateParser of(final Pattern... patterns) { - return new RegexListDateParser(ListUtil.of(patterns)); - } - - private final List patterns; - - /** - * 构造 - * - * @param patterns 正则列表 - */ - public RegexListDateParser(final List patterns) { - this.patterns = patterns; - } - - /** - * 新增自定义日期正则 - * - * @param regex 日期正则 - * @return this - */ - public RegexListDateParser addRegex(final String regex) { - // 日期正则忽略大小写 - return addPattern(Pattern.compile(regex, Pattern.CASE_INSENSITIVE)); - } - - /** - * 新增自定义日期正则 - * - * @param pattern 日期正则 - * @return this - */ - public RegexListDateParser addPattern(final Pattern pattern) { - this.patterns.add(pattern); - return this; - } - - @Override - public Date parse(final CharSequence source) throws DateException { - Matcher matcher; - for (final Pattern pattern : this.patterns) { - matcher = pattern.matcher(source); - if (matcher.matches()) { - return RegexDateParser.parse(matcher); - } - } - - throw new DateException("No valid pattern for date string: [{}]", source); - } -} diff --git a/hutool-core/src/test/java/org/dromara/hutool/core/date/format/parser/RegexDateParserTest.java b/hutool-core/src/test/java/org/dromara/hutool/core/date/format/parser/RegexDateParserTest.java index 97ca539cc..580d61ef9 100644 --- a/hutool-core/src/test/java/org/dromara/hutool/core/date/format/parser/RegexDateParserTest.java +++ b/hutool-core/src/test/java/org/dromara/hutool/core/date/format/parser/RegexDateParserTest.java @@ -71,11 +71,6 @@ public class RegexDateParserTest { void parseMonthFirstTest() { final String dateRegex = "(?\\w+{3,9})\\W+(?\\d{1,2})(?:th)?\\W+(?\\d{2,4})"; - // +08:00 - // +0800 - // +08 - final String zoneOffsetRegex = "(\\s?(?[-+]\\d{1,2}:?(?:\\d{2})?))?"; - // May 8, 2009 5:57:51 final RegexDateParser parser = RegexDateParser.of(dateRegex + timeRegex + zoneOffsetRegex); diff --git a/hutool-http/src/main/java/org/dromara/hutool/http/meta/HttpHeaderUtil.java b/hutool-http/src/main/java/org/dromara/hutool/http/meta/HttpHeaderUtil.java index 760e51abd..6abea34b5 100644 --- a/hutool-http/src/main/java/org/dromara/hutool/http/meta/HttpHeaderUtil.java +++ b/hutool-http/src/main/java/org/dromara/hutool/http/meta/HttpHeaderUtil.java @@ -60,7 +60,7 @@ public class HttpHeaderUtil { */ public static String getFileNameFromDisposition(final Map> headers, String paramName) { paramName = ObjUtil.defaultIfNull(paramName, "filename"); - final List dispositions = headerList(headers, HeaderName.CONTENT_DISPOSITION.name()); + final List dispositions = headerList(headers, HeaderName.CONTENT_DISPOSITION.getValue()); String fileName = null; if (CollUtil.isNotEmpty(dispositions)) {