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

com.developerdan.blocklist.tools.HostsParser Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
package com.developerdan.blocklist.tools;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;

public final class HostsParser extends BlocklistParser {

    private static final Logger LOGGER = LoggerFactory.getLogger(Domain.class);
    private static final Pattern STRIP_HOSTS_COMMENTS = Pattern.compile("^[\\d\\.:]+\\s+([^#\\s]+)", Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);

    @Override
    public final Optional parseLine(String line) {
        var matcher = HostsParser.STRIP_HOSTS_COMMENTS.matcher(line);
        if (matcher.find()) {
            var lineParts = matcher.group(0).split("\\s+", -1);
            if (lineParts.length == 2) {
                return Domain.fromString(lineParts[1]);
            }
        }
        LOGGER.debug("un-parsable hosts line: <{}>", line);
        return Optional.empty();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy