package de.metalmatze.krautreporter;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.GregorianCalendar;

import de.metalmatze.krautreporter.entities.Article;

public class KrautreporterRssParser {

    private ArrayList<Article> articles;
    private Article article;
    private String text;
    private static final String ns = null;
    protected XmlPullParser parser;

    public KrautreporterRssParser() throws XmlPullParserException {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        this.parser = factory.newPullParser();
        this.articles = new ArrayList<Article>();
        this.article = new Article();
    }

    public ArrayList<Article> parse(BufferedInputStream bufferedInputStream) throws XmlPullParserException, IOException {
        this.parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        this.parser.setInput(bufferedInputStream, null);

        int eventType = this.parser.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {

            String tagName = this.parser.getName();

            switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (tagName.equalsIgnoreCase("item")) {
                        this.article = new Article();
                    }
                    break;
                case XmlPullParser.TEXT:
                    this.text = this.parser.getText();
                    break;
                case XmlPullParser.END_TAG:
                    if (tagName.equalsIgnoreCase("item")) {
                        this.articles.add(this.article);
                    } else if (tagName.equalsIgnoreCase("guid")) {
                        this.article.setUuid(this.text);
                    } else if (tagName.equalsIgnoreCase("title")) {
                        if (this.text != null)
                            this.article.setTitle(this.text.trim());
                    }
                    else if (tagName.equalsIgnoreCase("pubDate")) {
                        try {
                            GregorianCalendar calendar = new GregorianCalendar();
                            calendar.setTime(DateFormat.getDateTimeInstance().parse(this.text));
                            this.article.setDate(calendar);
                        } catch (ParseException e) {
                            this.article.setDate(new GregorianCalendar());
                        }
                    }
                    else if (tagName.equalsIgnoreCase("link")) {
                        URL link = new URL(this.text);
                        this.article.setLink(link);
                    }
                    else if (tagName.equalsIgnoreCase("description")) {
                        this.article.setContent(this.text);
                    }
                    break;
                default:
                    break;
            }

            eventType = this.parser.next();
        }

        return this.articles;
    }

}

@@ -261,26 +261,30 @@
 va.text.
-DateFormat
+ParseException
 ;%0Aimport
@@ -290,38 +290,40 @@
 t java.text.
-ParseException
+SimpleDateFormat
 ;%0Aimport jav
@@ -375,16 +375,41 @@
 alendar;
+%0Aimport java.util.Locale;
 %0A%0Aimport
@@ -2375,405 +2375,55 @@
    t
-ry %7B%0A                            GregorianCalendar calendar = new GregorianCalendar();%0A                            calendar.setTime(DateFormat.getDateTimeInstance().parse(this.text));%0A                            this.article.
+his.article.setDate(this.par
 se
-t
 Date(
-calendar);%0A                        %7D catch (ParseException e) %7B%0A                            this.article.setDate(new GregorianCalendar());%0A                        %7D
+this.text));
 %0A   
@@ -2975,10 +2975,413 @@
 %0A    %7D%0A%0A
+    private GregorianCalendar parseDate(String text) %7B%0A        GregorianCalendar calendar = new GregorianCalendar();%0A        SimpleDateFormat dateFormat = new SimpleDateFormat(%22EEE, dd MMM yyyy HH:mm:ss Z%22, Locale.US);%0A%0A        try %7B%0A%0A            calendar.setTime(dateFormat.parse(text));%0A%0A        %7D catch (ParseException e) %7B%0A            e.printStackTrace();%0A        %7D%0A%0A        return calendar;%0A    %7D%0A%0A
 %7D%0A

Make pubDate parsing work