package com.peetz.kontor.data;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class FileExport
{
	public void exportFile(java.io.PrintWriter out)
	{
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.newDocument();
			ExportComics exportComics = new ExportComics();
			ExportMedien exportMedien = new ExportMedien();
			ExportLibrary exportLibrary = new ExportLibrary();
			ExportTradingCards exportTradingCards = new ExportTradingCards();
			
			Element root = document.createElement("kontor");
			document.appendChild(root);
			root.appendChild(document.createTextNode("\n"));
			root.appendChild(exportComics.backupComics(document));
			root.appendChild(document.createTextNode("\n"));
			root.appendChild(exportMedien.backupMedien(document));
			root.appendChild(document.createTextNode("\n"));
			root.appendChild(exportLibrary.backupLibrary(document));
			root.appendChild(document.createTextNode("\n"));
			root.appendChild(exportTradingCards.backupTradingCards(document));
			root.appendChild(document.createTextNode("\n"));

			TransformerFactory tFactory = TransformerFactory.newInstance();
			Transformer transformer = tFactory.newTransformer();
			
			DOMSource source = new DOMSource(document);
			StreamResult result = new StreamResult(out);
			transformer.transform(source, result);
		} catch (DOMException e) {
			System.out.println(e.getMessage());
		} catch (TransformerConfigurationException e) {
			System.out.println(e.getMessage());
		} catch (FactoryConfigurationError e) {
			System.out.println(e.getMessage());
		} catch (ParserConfigurationException e) {
			System.out.println(e.getMessage());
		} catch (TransformerFactoryConfigurationError e) {
			System.out.println(e.getMessage());
		} catch (TransformerException e) {
			System.out.println(e.getMessage());
		}
	}
}
