io.github.mngsk.devicedetector.util.AbstractParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of device-detector Show documentation
Show all versions of device-detector Show documentation
The Universal Device Detection library that parses User Agents and detects devices (desktop, tablet, mobile, tv, cars, console, etc.), clients (browsers, feed readers, media players, PIMs, ...), operating systems, brands and models.
package io.github.mngsk.devicedetector.util;
import java.util.Optional;
import java.util.regex.Matcher;
import org.apache.commons.lang3.StringUtils;
public abstract class AbstractParser {
public abstract Optional parse(String userAgent);
protected String buildByMatch(String item, Matcher matcher) {
if (item == null) {
return null;
}
if (!item.contains("$")) {
return item.trim();
}
for (int i = 1; i <= 3; i++) {
if (item.indexOf("$" + i) == -1) {
continue;
}
String group = matcher.group(i);
String replacement = Optional.ofNullable(group).orElse("");
item = item.replace("$" + i, replacement);
}
return item.trim();
}
protected String buildVersion(String version, Matcher matcher) {
version = buildByMatch(version, matcher);
if (version == null) {
return null;
}
version = version.replace("_", ".");
return StringUtils.strip(version, " .");
}
}