package me.tdjones.main.util;

import me.tdjones.main.model.Episode;
import me.tdjones.main.model.Feed;
import me.tdjones.main.model.RSS;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.IOException;
import java.net.URL;
import java.util.List;

public class XMLUtil {
    public static Feed parseRSS(URL feedURL){
        try {
            JAXBContext jc = JAXBContext.newInstance(RSS.class);
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            RSS rss = (RSS) unmarshaller.unmarshal(feedURL.openStream());
            validateRss(rss);
            return rss.getFeed();
        }catch (JAXBException e){
            e.printStackTrace();
            return null;
        }catch (IOException e){
            e.printStackTrace();
            return null;
        }
    }

    private static void validateRss(RSS rss){
        validateFeed(rss.getFeed());
        for (Episode episode : rss.getFeed().getEpisodeList()){
            episode.setFeed(rss.getFeed());
            validateEpisode(episode);
        }
    }

    private static void validateFeed(Feed feed){

    }

    private static void validateEpisode(Episode episode){
        if (episode.getThumbnail().getUrl() == null){
            episode.setThumbnail(episode.getFeed().getThumbnail());
        }
    }
}

@@ -135,115 +135,426 @@
 SS;%0A
-%0Aimport javax.xml.bind.JAXBContext;%0Aimport javax.xml.bind.JAXBException;%0Aimport javax.xml.bind.Unmarshaller
+import org.w3c.dom.Document;%0Aimport org.w3c.dom.Element;%0Aimport org.w3c.dom.Node;%0Aimport org.w3c.dom.NodeList;%0Aimport org.xml.sax.SAXException;%0A%0Aimport javax.xml.bind.JAXBContext;%0Aimport javax.xml.bind.JAXBException;%0Aimport javax.xml.bind.Unmarshaller;%0Aimport javax.xml.parsers.DocumentBuilder;%0Aimport javax.xml.parsers.DocumentBuilderFactory;%0Aimport javax.xml.parsers.ParserConfigurationException;%0Aimport java.io.File
 ;%0Aim
@@ -600,16 +600,44 @@
 et.URL;%0A
+import java.util.ArrayList;%0A
 import j
@@ -675,16 +675,979 @@
 LUtil %7B%0A
+    public static List%3CURL%3E parseOPML(File OPMLFile)%7B%0A        List%3CURL%3E feedURLList = new ArrayList%3C%3E();%0A        try%7B%0A            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();%0A            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();%0A            Document doc = dBuilder.parse(OPMLFile);%0A%0A            doc.getDocumentElement().normalize();%0A%0A            NodeList nList = doc.getElementsByTagName(%22outline%22);%0A%0A            for (int index = 0; index %3C nList.getLength(); index++)%7B%0A                Element outline = (Element) nList.item(index);%0A                URL xmlUrl = new URL(outline.getAttribute(%22xmlUrl%22));%0A%0A                feedURLList.add(xmlUrl);%0A            %7D%0A        %7Dcatch (SAXException e)%7B%0A            e.printStackTrace();%0A        %7Dcatch (IOException e)%7B%0A            e.printStackTrace();%0A        %7Dcatch (ParserConfigurationException e)%7B%0A            e.printStackTrace();%0A        %7D%0A%0A        return feedURLList;%0A    %7D%0A%0A
     publ
@@ -1923,18 +1923,18 @@
 alidateR
-ss
+SS
 (rss);%0A 
@@ -2198,18 +2198,18 @@
 alidateR
-ss
+SS
 (RSS rss
@@ -2522,24 +2522,42 @@
 e episode)%7B%0A
+        try %7B%0A    
         if (
@@ -2596,18 +2596,23 @@
 == null)
+ 
 %7B%0A
+    
         
@@ -2667,24 +2667,124 @@
 umbnail());%0A
+            %7D else %7B%0A%0A            %7D%0A        %7D catch (Exception e)%7B%0A            e.printStackTrace();%0A
         %7D%0A  

Add opml parse support