package com.peetz.kontor.data;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class FileImport
{
	public void importFile(File file)
	{
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(file);

			Node xmlNode = document.getFirstChild();
            if (!xmlNode.getNodeName().equals("kontor")) return;
            NodeList nodeList = xmlNode.getChildNodes();
            for (int i=0; i<nodeList.getLength(); i++)
            {
                Node node = nodeList.item(i);
                parseNode(node);
            }
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (FactoryConfigurationError e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void parseNode(Node xmlNode)
	{
        if (xmlNode.getNodeType() == Node.TEXT_NODE) return;
        
        if (xmlNode.getNodeName().equals("comics"))
        {
        	System.out.println("comics");
        	new ImportComics().parseNode(xmlNode);
        }
        if (xmlNode.getNodeName().equals("medien"))
        {
        	System.out.println("medien");
        	new ImportMedien().parseNode(xmlNode);
        }
        if (xmlNode.getNodeName().equals("library"))
        {
        	System.out.println("library");
        	new ImportLibrary().parseNode(xmlNode);
        }
        if (xmlNode.getNodeName().equals("tradingcards"))
        {
        	System.out.println("tradingcards");
        	new ImportTradingCards().parseNode(xmlNode);
        }
	}
}
