<commit_before>package com.github.sypexgeo;

import com.github.sypexgeo.model.SxCity;
import com.github.sypexgeo.model.SxCoordinates;
import com.github.sypexgeo.model.SxCountry;
import com.github.sypexgeo.model.SxGeoResult;
import com.github.sypexgeo.model.SxId;
import com.github.sypexgeo.model.SxName;
import com.github.sypexgeo.model.SxRegion;
import com.github.sypexgeo.model.SxValue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.regex.Pattern;


/**
 * Main class of the package: executes requests for Sypex server.
 */
public class SxRestClient {

    private static final Pattern IPV4_PATTERN = Pattern.compile("^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$");

    private static final Pattern IPV4_COMMA_SEPARATED_PATTERN =
            Pattern.compile("((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)(,\\n|,?$))");

    protected SxRestClient() {
    }

    @NotNull
    public static SxRestClient create() {
        return new SxRestClient();
    }

    /**
     * Executes REST request and return results for a given IP.
     * Normally there is only 1 result in the list per IP.
     *
     * @param ip IP to get geo info for. Multiple IPs can be provided with comma separator.
     * @return list of SxGeoResult results
     * @throws IllegalArgumentException if IP address is invalid.
     */
    @NotNull
    public List<SxGeoResult> getList(@NotNull String ip) {
        if (!IPV4_COMMA_SEPARATED_PATTERN.matcher(ip).matches()) {
            throw new IllegalArgumentException("Illegal IP address or list: " + ip);
        }
        try {
            NodeList ipNodes = query(ip);
            List<SxGeoResult> result = new ArrayList<>();
            for (int i = 0; i < ipNodes.getLength(); i++) {
                result.add(parseIp((Element) ipNodes.item(i)));
            }
            return result;
        } catch (ParserConfigurationException | SAXException | IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Nullable
    public SxGeoResult get(@NotNull String ip) {
        if (!IPV4_PATTERN.matcher(ip).matches()) {
            throw new IllegalArgumentException("Illegal IP address: " + ip);
        }
        try {
            NodeList ipNodes = query(ip);
            return ipNodes.getLength() == 0 ? null : parseIp((Element) ipNodes.item(0));
        } catch (ParserConfigurationException | SAXException | IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static NodeList query(@NotNull String ip) throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse("http://api.sypexgeo.net/xml/" + ip);
        return doc.getElementsByTagName("ip");
    }


    private static SxGeoResult parseIp(Element ipNode) {
        String ip = ipNode.getAttribute("num");

        NodeList cityNodes = ipNode.getElementsByTagName("city");
        SxCity city = cityNodes.getLength() == 0 ? null : parseCity((Element) cityNodes.item(0));

        NodeList regionNodes = ipNode.getElementsByTagName("region");
        SxRegion region = regionNodes.getLength() == 0 ? null : parseRegion((Element) regionNodes.item(0));

        NodeList countryNodes = ipNode.getElementsByTagName("country");
        SxCountry country = countryNodes.getLength() == 0 ? null : parseCountry((Element) countryNodes.item(0));

        return new SxGeoResult(ip, city, region, country);
    }

    private static SxCity parseCity(@NotNull Element city) {
        return new SxCity(getId(city), getCoordinates(city), getName(city), getTimeZone(city), getAttributes(city));<commit_msg>Fixing error on request<commit_after>package com.github.sypexgeo;

import com.github.sypexgeo.model.SxCity;
import com.github.sypexgeo.model.SxCoordinates;
import com.github.sypexgeo.model.SxCountry;
import com.github.sypexgeo.model.SxGeoResult;
import com.github.sypexgeo.model.SxId;
import com.github.sypexgeo.model.SxName;
import com.github.sypexgeo.model.SxRegion;
import com.github.sypexgeo.model.SxValue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.regex.Pattern;


/**
 * Main class of the package: executes requests for Sypex server.
 */
public class SxRestClient {

    private static final Pattern IPV4_PATTERN = Pattern.compile("^(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}$");

    private static final Pattern IPV4_COMMA_SEPARATED_PATTERN =
            Pattern.compile("((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)(,\\n|,?$))");

    protected SxRestClient() {
    }

    @NotNull
    public static SxRestClient create() {
        return new SxRestClient();
    }

    /**
     * Executes REST request and return results for a given IP.
     * Normally there is only 1 result in the list per IP.
     *
     * @param ip IP to get geo info for. Multiple IPs can be provided with comma separator.
     * @return list of SxGeoResult results
     * @throws IllegalArgumentException if IP address is invalid.
     */
    @NotNull
    public List<SxGeoResult> getList(@NotNull String ip) {
        if (!IPV4_COMMA_SEPARATED_PATTERN.matcher(ip).matches()) {
            throw new IllegalArgumentException("Illegal IP address or list: " + ip);
        }
        try {
            NodeList ipNodes = query(ip);
            List<SxGeoResult> result = new ArrayList<>();
            for (int i = 0; i < ipNodes.getLength(); i++) {
                result.add(parseIp((Element) ipNodes.item(i)));
            }
            return result;
        } catch (ParserConfigurationException | SAXException | IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Nullable
    public SxGeoResult get(@NotNull String ip) {
        if (!IPV4_PATTERN.matcher(ip).matches()) {
            throw new IllegalArgumentException("Illegal IP address: " + ip);
        }
        try {
            NodeList ipNodes = query(ip);
            return ipNodes.getLength() == 0 ? null : parseIp((Element) ipNodes.item(0));
        } catch (ParserConfigurationException | SAXException | IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static NodeList query(@NotNull String ip) throws ParserConfigurationException, SAXException, IOException {
        URL url = new URL("http://api.sypexgeo.net/xml/" + ip);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Accept", "application/xml");
        connection.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.99 Safari/537.36");
        try (InputStream is = connection.getInputStream()) {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(is);
            return doc.getElementsByTagName("ip");
        }
    }


    private static SxGeoResult parseIp(Element ipNode) {
        String ip = ipNode.getAttribute("num");

        NodeList cityNodes = ipNode.getElementsByTagName("city");
        SxCity city = cityNodes.getLength() == 0 ? null : parseCity((Element) cityNodes.item(0));

        NodeList regionNodes = ipNode.getElementsByTagName("region");
        SxRegion region = regionNodes.getLength() == 0 ? null : parseRegion((Element) regionNodes.item(0));

        NodeList countryNodes = ipNode.getElementsByTagName("country");
        SxCountry country = countryNodes.getLength() == 0 ? null : parseCountry((Element) countryNodes.item(0));

        return new SxGeoResult(ip, city, region, country);
    }

    private static SxCity parseCity(@NotNull Element city) {
        return new SxCity(getId(city), getCoordinates(city), getName(city), getTimeZone(city), getAttributes(city));
{'fraction_non_alphanumeric': 0.13377854373115036, 'fraction_numerical': 0.01863420939250323, 'mean_word_length': 3.2166212534059944, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 11}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3309071', 'n_tokens_mistral': 2997, 'n_tokens_neox': 2875, 'n_words': 614}