io.github.apkcloud.devicedetector.parser.client.AbstractClientParser Maven / Gradle / Ivy
Show all versions of DeviceDetector Show documentation
package io.github.apkcloud.devicedetector.parser.client;
import io.github.apkcloud.devicedetector.ClientHints;
import io.github.apkcloud.devicedetector.entity.Client;
import io.github.apkcloud.devicedetector.parser.AbstractParser;
import java.util.*;
public abstract class AbstractClientParser extends AbstractParser {
public AbstractClientParser() {
super();
}
public AbstractClientParser(String ua) {
super(ua);
}
public AbstractClientParser(String ua, ClientHints clientHints) {
super(ua, clientHints);
}
/**
* 解析当前 UA 并且检查是否包含任何的客户端信息。
*
* fixtureFile 的文件包含检测到的客户端列表。
*
* 步骤1:构建一个包含所有正则表达式的长正则表达式,并将 UA 与之匹配
* -> 如果没有匹配项:返回
* -> 否则:
* 步骤2:遍历 feed_readers.yml 中的正则表达式列表,并尝试匹配每一个
* -> 返回匹配到的 Feed阅读器
*
* 注意:在逐个匹配每个正则表达式之前先进行整体匹配可以加快检测速度。
*
* @return {@code Map | null}
*/
public Map parse(String fixtureFile, String parserName) throws Exception {
Map result = null;
if (!isNullOrEmpty(preMatchOverall(fixtureFile))) {
List regexes = getRegexes(fixtureFile, Client.class);
for (Client regex : regexes) {
List matches = matchUserAgent(regex.getRegex());
if (!isNullOrEmpty(matches)) {
result = new HashMap<>();
result.put("type", parserName);
result.put("name", buildByMatch(regex.getName(), matches));
result.put("version", buildVersion(regex.getVersion(), matches));
break;
}
}
}
return result;
}
/**
* 解析当前设置的 UserAgent 并返回可能的结果
*/
public abstract Map parse() throws Exception;
/**
* 返回正则表达式中定义的所有名称。
*
* 注意:这个方法可能不会返回检测到的客户端的所有名称。
*
* @return {@code List}
*/
public List getAvailableClients(String fixtureFile) {
List regexes = getRegexes(fixtureFile, Client.class);
List names = new ArrayList<>();
for (Client regex : regexes) {
if ("$1".equals(regex.getName())) {
continue;
}
names.add(regex.getName());
}
names.sort(String.CASE_INSENSITIVE_ORDER);
return new ArrayList<>(new HashSet<>(names));
}
}