<commit_before>/*
 * Copyright (c) 2011-2015 Amedia Utvikling AS.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package no.api.meteo.service.sunrise;

import lombok.extern.slf4j.Slf4j;
import no.api.meteo.MeteoException;
import no.api.meteo.entity.core.Location;
import no.api.meteo.entity.core.Meta;
import no.api.meteo.entity.core.service.sunrise.Noon;
import no.api.meteo.entity.core.service.sunrise.PhaseType;
import no.api.meteo.entity.core.service.sunrise.Sunrise;
import no.api.meteo.service.MeteoDataParser;
import no.api.meteo.service.MeteoDataParserException;
import no.api.meteo.service.sunrise.builder.MoonBuilder;
import no.api.meteo.service.sunrise.builder.SunBuilder;
import no.api.meteo.service.sunrise.builder.SunriseBuilder;
import no.api.meteo.service.sunrise.builder.SunriseDateBuilder;
import no.api.meteo.util.MeteoConstants;
import no.api.meteo.util.MeteoNetUtils;
import no.api.meteo.util.MeteoXppUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.util.Stack;

import static no.api.meteo.util.MeteoConstants.*;
import static no.api.meteo.util.MeteoXppUtils.getBoolean;
import static no.api.meteo.util.MeteoXppUtils.getDouble;
import static no.api.meteo.util.MeteoXppUtils.getLocalDate;
import static no.api.meteo.util.MeteoXppUtils.getString;
import static no.api.meteo.util.MeteoXppUtils.getZoneDateTime;

@Slf4j
public final class SunriseParser implements MeteoDataParser<Sunrise> {

    private static final String COULD_NOT_CONVERT_DATE_FROM_XML = "Could not convert date from xml";

    @Override
    public Sunrise parse(String data) throws MeteoException {
        return doParse(MeteoXppUtils.createPullParser(data));
    }

    @Override
    public Sunrise parse(InputStream inputStream) throws MeteoException {
        return doParse(MeteoXppUtils.createPullParser(inputStream));
    }

    public Sunrise doParse(XmlPullParser xpp) throws MeteoException {
        try {
            SunriseBuilder sunriseBuilder = new SunriseBuilder();
            int eventType = xpp.getEventType();
            Stack<SunriseDateBuilder> stack = new Stack<>();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_TAG) {
                    handleStartTags(sunriseBuilder, xpp, stack);
                } else if (eventType == XmlPullParser.END_TAG) {
                    handleEndTags(sunriseBuilder, xpp, stack);
                } else {
                    log.trace("Skipping event type : " + eventType);
                }
                eventType = xpp.next();
            }
            return sunriseBuilder.build();
        } catch (XmlPullParserException e) {
            throw new MeteoDataParserException("A parsing problem occurred", e);
        } catch (IOException e) {
            throw new MeteoDataParserException("An IO problem occurred", e);
        }
    }

    private void handleStartTags(SunriseBuilder sunriseBuilder, XmlPullParser xpp, Stack<SunriseDateBuilder> stack) {
        switch (xpp.getName()) {
            case TAG_META:
                handleMetaTag(sunriseBuilder, xpp);
                break;
            case TAG_TIME: {
                handleTimeTag(xpp, stack);
                break;
            }
            case TAG_LOCATION:
                handleLocationTag(sunriseBuilder, xpp);
                break;
            case TAG_SUN: {
                handleSunTag(xpp, stack);
                break;
            }
            case TAG_NOON: {
                handleNoonTag(xpp, stack);
                break;
            }
            case TAG_ERROR:
                // FIXME Not implemented yet
                log.trace("Got error tag. This is not implemented yet");
                break;
            case TAG_TWILIGHT:
                // FIXME Not implemented yet
                log.trace("Got twilight tag. This is not implemented yet");
                break;
            case TAG_MOON: {
                handleMoonTag(xpp, stack);
                break;
            }
            default:
                log.trace("Unhandled start tag: " + xpp.getName());
                break;
        }
    }

    private void handleMetaTag(SunriseBuilder sunriseBuilder, XmlPullParser xpp) {
        try {
            Meta meta = new Meta(MeteoNetUtils.createUri(getString(xpp, ATTR_LICENSEURL)), null);
            sunriseBuilder.setMeta(meta);
            // FIXME sunriseBuilder.setCreated(getSimpleDate(xpp, ATTR_CREATED));<commit_msg>Reintroduced comment date function<commit_after>/*
 * Copyright (c) 2011-2015 <NAME>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package no.api.meteo.service.sunrise;

import lombok.extern.slf4j.Slf4j;
import no.api.meteo.MeteoException;
import no.api.meteo.entity.core.Location;
import no.api.meteo.entity.core.Meta;
import no.api.meteo.entity.core.service.sunrise.Noon;
import no.api.meteo.entity.core.service.sunrise.PhaseType;
import no.api.meteo.entity.core.service.sunrise.Sunrise;
import no.api.meteo.service.MeteoDataParser;
import no.api.meteo.service.MeteoDataParserException;
import no.api.meteo.service.sunrise.builder.MoonBuilder;
import no.api.meteo.service.sunrise.builder.SunBuilder;
import no.api.meteo.service.sunrise.builder.SunriseBuilder;
import no.api.meteo.service.sunrise.builder.SunriseDateBuilder;
import no.api.meteo.util.MeteoConstants;
import no.api.meteo.util.MeteoNetUtils;
import no.api.meteo.util.MeteoXppUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.InputStream;
import java.util.Stack;

import static no.api.meteo.util.MeteoConstants.*;
import static no.api.meteo.util.MeteoXppUtils.getBoolean;
import static no.api.meteo.util.MeteoXppUtils.getDouble;
import static no.api.meteo.util.MeteoXppUtils.getLocalDate;
import static no.api.meteo.util.MeteoXppUtils.getString;
import static no.api.meteo.util.MeteoXppUtils.getZoneDateTime;

@Slf4j
public final class SunriseParser implements MeteoDataParser<Sunrise> {

    private static final String COULD_NOT_CONVERT_DATE_FROM_XML = "Could not convert date from xml";

    @Override
    public Sunrise parse(String data) throws MeteoException {
        return doParse(MeteoXppUtils.createPullParser(data));
    }

    @Override
    public Sunrise parse(InputStream inputStream) throws MeteoException {
        return doParse(MeteoXppUtils.createPullParser(inputStream));
    }

    public Sunrise doParse(XmlPullParser xpp) throws MeteoException {
        try {
            SunriseBuilder sunriseBuilder = new SunriseBuilder();
            int eventType = xpp.getEventType();
            Stack<SunriseDateBuilder> stack = new Stack<>();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_TAG) {
                    handleStartTags(sunriseBuilder, xpp, stack);
                } else if (eventType == XmlPullParser.END_TAG) {
                    handleEndTags(sunriseBuilder, xpp, stack);
                } else {
                    log.trace("Skipping event type : " + eventType);
                }
                eventType = xpp.next();
            }
            return sunriseBuilder.build();
        } catch (XmlPullParserException e) {
            throw new MeteoDataParserException("A parsing problem occurred", e);
        } catch (IOException e) {
            throw new MeteoDataParserException("An IO problem occurred", e);
        }
    }

    private void handleStartTags(SunriseBuilder sunriseBuilder, XmlPullParser xpp, Stack<SunriseDateBuilder> stack) {
        switch (xpp.getName()) {
            case TAG_META:
                handleMetaTag(sunriseBuilder, xpp);
                break;
            case TAG_TIME: {
                handleTimeTag(xpp, stack);
                break;
            }
            case TAG_LOCATION:
                handleLocationTag(sunriseBuilder, xpp);
                break;
            case TAG_SUN: {
                handleSunTag(xpp, stack);
                break;
            }
            case TAG_NOON: {
                handleNoonTag(xpp, stack);
                break;
            }
            case TAG_ERROR:
                // FIXME Not implemented yet
                log.trace("Got error tag. This is not implemented yet");
                break;
            case TAG_TWILIGHT:
                // FIXME Not implemented yet
                log.trace("Got twilight tag. This is not implemented yet");
                break;
            case TAG_MOON: {
                handleMoonTag(xpp, stack);
                break;
            }
            default:
                log.trace("Unhandled start tag: " + xpp.getName());
                break;
        }
    }

    private void handleMetaTag(SunriseBuilder sunriseBuilder, XmlPullParser xpp) {
        try {
            Meta meta = new Meta(MeteoNetUtils.createUri(getString(xpp, ATTR_LICENSEURL)), null);
            sunriseBuilder.setMeta(meta);
            sunriseBuilder.setCreated(getZoneDateTime(xpp, ATTR_CREATED));
{'fraction_non_alphanumeric': 0.0885565285840795, 'fraction_numerical': 0.003345468857620781, 'mean_word_length': 2.466575716234652, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, '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': '1920219', 'n_tokens_mistral': 3068, 'n_tokens_neox': 2925, 'n_words': 677}