<commit_before>package net.vexelon.currencybg.srv.remote;

import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import net.vexelon.currencybg.srv.reports.ConsoleReporter;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.http.HttpResponse;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Charsets;
import com.google.common.collect.Lists;

import net.vexelon.currencybg.srv.Defs;
import net.vexelon.currencybg.srv.db.models.CurrencyData;
import net.vexelon.currencybg.srv.db.models.Sources;
import net.vexelon.currencybg.srv.reports.Reporter;
import net.vexelon.currencybg.srv.utils.DateTimeUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

public class AllianzSource extends AbstractSource {

    private static final Logger log = LoggerFactory.getLogger(AllianzSource.class);
    private static final String TAG_NAME = AllianzSource.class.getSimpleName();

    private static final String URL_SOURCE = "https://remote.bank.allianz.bg/EBank/Common";
    private static final String HTTP_CONTENT_TYPE = "text/xml";
    private static final String HTTP_REQUEST_BODY = "<COMMON lang=\"BG\"><EXCHANGERATES/></COMMON>";

    private static final String DATE_FORMAT = "dd/MM/yyyy";

    private static final String XML_TAG_ROOT = "COMMON";
    private static final String XML_TAG_SUBROOT = "EXCHANGERATES";
    private static final String XML_TAG_ROW = "EXCHANGERATE";
    private static final String XML_TAG_ATTR_CODE = "currid";
    private static final String XML_TAG_ATTR_DATETIME = "dtstamp";
    private static final String XML_TAG_ATTR_TYPE = "type";
    private static final String XML_TAG_ATTR_RATIO = "units";
    private static final String XML_TAG_ATTR_BUY = "buy";
    private static final String XML_TAG_ATTR_SELL = "sell";

    public AllianzSource(Reporter reporter) {
        super(reporter);
    }

    public List<CurrencyData> getAllianzRates(InputStream input) throws IOException, XmlPullParserException {
        List<CurrencyData> result = new ArrayList<>();

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser parser = factory.newPullParser();
        parser.setInput(input, Charsets.UTF_8.name());

        DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
        int eventType = parser.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {
            String tagName = parser.getName();

            switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (XML_TAG_ROW.equalsIgnoreCase(tagName)) {
                        if ("S".equalsIgnoreCase(parser.getAttributeValue("", XML_TAG_ATTR_TYPE))) {
                            CurrencyData currencyData = new CurrencyData();
                            currencyData.setSource(Sources.ALLIANZ.getID());
                            try {
                                currencyData.setCode(parser.getAttributeValue("", XML_TAG_ATTR_CODE));
                                currencyData.setBuy(parser.getAttributeValue("", XML_TAG_ATTR_BUY));
                                currencyData.setSell(parser.getAttributeValue("", XML_TAG_ATTR_SELL));
                                currencyData.setRatio(Integer.parseInt(parser.getAttributeValue("", XML_TAG_ATTR_RATIO)));
                                currencyData.setDate(dateFormat.parse(parser.getAttributeValue("", XML_TAG_ATTR_DATETIME)));
                                result.add(currencyData);
                                System.out.println("TEST: " + currencyData.toString());
                            } catch (Throwable t) {
                                log.warn("Could not parse Allianz currency!", t);
                                getReporter().write(TAG_NAME, "Could not parse Allianz currency - '{}'! ({})",
                                        currencyData.toString(), t.getMessage());
                            }
                        }
                    }
                    break;
            }

            eventType = parser.next();
        }

        return normalizeCurrencyData(result);
    }

    @Override
    public void getRates(final Callback callback) throws SourceException {
        try {
            doPost(URL_SOURCE, HTTP_REQUEST_BODY, HTTP_CONTENT_TYPE, new HTTPCallback() {

                @Override
                public void onRequestFailed(Exception e) {
                    getReporter().write(TAG_NAME, "Connection failure= {}", ExceptionUtils.getStackTrace(e));

                    AllianzSource.this.close();
                    callback.onFailed(e);
                }

                @Override
                public void onRequestCompleted(HttpResponse response, boolean isCanceled) {<commit_msg>Removed debug line<commit_after>package net.vexelon.currencybg.srv.remote;

import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import net.vexelon.currencybg.srv.reports.ConsoleReporter;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.http.HttpResponse;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Charsets;
import com.google.common.collect.Lists;

import net.vexelon.currencybg.srv.Defs;
import net.vexelon.currencybg.srv.db.models.CurrencyData;
import net.vexelon.currencybg.srv.db.models.Sources;
import net.vexelon.currencybg.srv.reports.Reporter;
import net.vexelon.currencybg.srv.utils.DateTimeUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

public class AllianzSource extends AbstractSource {

    private static final Logger log = LoggerFactory.getLogger(AllianzSource.class);
    private static final String TAG_NAME = AllianzSource.class.getSimpleName();

    private static final String URL_SOURCE = "https://remote.bank.allianz.bg/EBank/Common";
    private static final String HTTP_CONTENT_TYPE = "text/xml";
    private static final String HTTP_REQUEST_BODY = "<COMMON lang=\"BG\"><EXCHANGERATES/></COMMON>";

    private static final String DATE_FORMAT = "dd/MM/yyyy";

    private static final String XML_TAG_ROOT = "COMMON";
    private static final String XML_TAG_SUBROOT = "EXCHANGERATES";
    private static final String XML_TAG_ROW = "EXCHANGERATE";
    private static final String XML_TAG_ATTR_CODE = "currid";
    private static final String XML_TAG_ATTR_DATETIME = "dtstamp";
    private static final String XML_TAG_ATTR_TYPE = "type";
    private static final String XML_TAG_ATTR_RATIO = "units";
    private static final String XML_TAG_ATTR_BUY = "buy";
    private static final String XML_TAG_ATTR_SELL = "sell";

    public AllianzSource(Reporter reporter) {
        super(reporter);
    }

    public List<CurrencyData> getAllianzRates(InputStream input) throws IOException, XmlPullParserException {
        List<CurrencyData> result = new ArrayList<>();

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser parser = factory.newPullParser();
        parser.setInput(input, Charsets.UTF_8.name());

        DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
        int eventType = parser.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {
            String tagName = parser.getName();

            switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (XML_TAG_ROW.equalsIgnoreCase(tagName)) {
                        if ("S".equalsIgnoreCase(parser.getAttributeValue("", XML_TAG_ATTR_TYPE))) {
                            CurrencyData currencyData = new CurrencyData();
                            currencyData.setSource(Sources.ALLIANZ.getID());
                            try {
                                currencyData.setCode(parser.getAttributeValue("", XML_TAG_ATTR_CODE));
                                currencyData.setBuy(parser.getAttributeValue("", XML_TAG_ATTR_BUY));
                                currencyData.setSell(parser.getAttributeValue("", XML_TAG_ATTR_SELL));
                                currencyData.setRatio(Integer.parseInt(parser.getAttributeValue("", XML_TAG_ATTR_RATIO)));
                                currencyData.setDate(dateFormat.parse(parser.getAttributeValue("", XML_TAG_ATTR_DATETIME)));
                                result.add(currencyData);
                            } catch (Throwable t) {
                                log.warn("Could not parse Allianz currency!", t);
                                getReporter().write(TAG_NAME, "Could not parse Allianz currency - '{}'! ({})",
                                        currencyData.toString(), t.getMessage());
                            }
                        }
                    }
                    break;
            }

            eventType = parser.next();
        }

        return normalizeCurrencyData(result);
    }

    @Override
    public void getRates(final Callback callback) throws SourceException {
        try {
            doPost(URL_SOURCE, HTTP_REQUEST_BODY, HTTP_CONTENT_TYPE, new HTTPCallback() {

                @Override
                public void onRequestFailed(Exception e) {
                    getReporter().write(TAG_NAME, "Connection failure= {}", ExceptionUtils.getStackTrace(e));

                    AllianzSource.this.close();
                    callback.onFailed(e);
                }

                @Override
                public void onRequestCompleted(HttpResponse response, boolean isCanceled) {
{'fraction_non_alphanumeric': 0.093452661328309, 'fraction_numerical': 0.0013188883655204898, 'mean_word_length': 2.608429639700884, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3109580', 'n_tokens_mistral': 2967, 'n_tokens_neox': 2843, 'n_words': 562}