Files
hutool/hutool-http/src/main/java/cn/hutool/http/useragent/BrowserEngine.java

77 lines
2.0 KiB
Java
Raw Normal View History

2023-03-27 03:28:19 +08:00
/*
* Copyright (c) 2023 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:
* http://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.
*/
2021-01-20 17:10:45 +08:00
package cn.hutool.http.useragent;
2022-05-07 12:18:29 +08:00
import cn.hutool.core.collection.ListUtil;
2022-04-28 03:35:07 +08:00
import cn.hutool.core.regex.ReUtil;
2021-07-05 10:47:23 +08:00
import java.util.List;
import java.util.regex.Pattern;
2021-01-20 17:10:45 +08:00
/**
2022-10-26 00:11:19 +08:00
* 浏览器引擎对象
2021-07-05 10:47:23 +08:00
*
2021-01-20 17:10:45 +08:00
* @author looly
* @since 4.2.1
*/
2022-10-26 00:11:19 +08:00
public class BrowserEngine extends UserAgentInfo {
2021-07-09 20:11:25 +08:00
private static final long serialVersionUID = 1L;
2021-07-05 10:47:23 +08:00
2022-10-26 00:11:19 +08:00
/**
* 未知
*/
public static final BrowserEngine Unknown = new BrowserEngine(NameUnknown, null);
2021-01-20 17:10:45 +08:00
/**
* 支持的引擎类型
*/
2022-10-26 00:11:19 +08:00
public static final List<BrowserEngine> engines = ListUtil.view(
new BrowserEngine("Trident", "trident"),
new BrowserEngine("Webkit", "webkit"),
new BrowserEngine("Chrome", "chrome"),
new BrowserEngine("Opera", "opera"),
new BrowserEngine("Presto", "presto"),
new BrowserEngine("Gecko", "gecko"),
new BrowserEngine("KHTML", "khtml"),
new BrowserEngine("Konqueror", "konqueror"),
new BrowserEngine("MIDP", "MIDP")
2021-01-20 17:10:45 +08:00
);
2021-07-09 20:11:25 +08:00
private final Pattern versionPattern;
2021-01-20 17:10:45 +08:00
/**
* 构造
2021-07-05 10:47:23 +08:00
*
2022-10-26 00:11:19 +08:00
* @param name 引擎名称
2021-01-20 17:10:45 +08:00
* @param regex 关键字或表达式
*/
2022-10-26 00:11:19 +08:00
public BrowserEngine(final String name, final String regex) {
2021-01-20 17:10:45 +08:00
super(name, regex);
2022-09-22 03:33:30 +08:00
this.versionPattern = Pattern.compile(name + "[/\\- ]([\\w.\\-]+)", Pattern.CASE_INSENSITIVE);
2021-01-20 17:10:45 +08:00
}
2021-07-05 10:47:23 +08:00
/**
* 获取引擎版本
*
* @param userAgentString User-Agent字符串
* @return 版本
* @since 5.7.4
*/
2022-04-30 20:47:32 +08:00
public String getVersion(final String userAgentString) {
2022-10-26 00:11:19 +08:00
if (isUnknown()) {
2021-07-09 20:11:25 +08:00
return null;
}
return ReUtil.getGroup1(this.versionPattern, userAgentString);
2021-07-05 10:47:23 +08:00
}
2021-01-20 17:10:45 +08:00
}