All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.mngsk.devicedetector.util.AbstractParser Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 1.0.10
Show newest version
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, " .");
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy