package dom;
import java.io.*;
import java.util.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

/**
 *
 * @author Tomáš Dlouhý
 */
public class TestDom {

    private static String INPUTFILE = "../data.xml";
    private static String OUTPUTFILE = "../data.out.xml";    

    public static void main() {
	run();
    }

    public static void run() {
        File file = new File(INPUTFILE);
        Document doc = null;
        
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(false);
            DocumentBuilder builder = factory.newDocumentBuilder();
            doc = builder.parse(file);
        } catch (Exception e) {
            e.printStackTrace();
        }	


                Scanner scan=new Scanner(System.in); 
                Node data = doc.getDocumentElement();
                
                NodeList nl = doc.getElementsByTagName("movie");
                int ic = nl.getLength()+1;
                Node data1 = nl.item(ic);
                
		Text CRLF = doc.createTextNode("\n  ");
                
                System.out.println("Vlozit do XML pomoci DOM");
                System.out.println("-------------------------");
                
                System.out.println("Zadejte jmeno filmu");
                Text tin = doc.createTextNode(scan.nextLine());
                System.out.println("Zadejte rok uvedeni filmu");
                Text tiy = doc.createTextNode(scan.nextLine());
                System.out.println("Zadejte zanr filmu");
                Text tit = doc.createTextNode(scan.nextLine());
                System.out.println("Zadejte disrtibutora filmu");
                Text dis = doc.createTextNode(scan.nextLine());
                System.out.println("Vlozil");
                Text isd = doc.createTextNode(scan.nextLine());
                
                System.out.println("Zadejte jmeno rezisera");
                Text tpd = doc.createTextNode(scan.nextLine());                                
                
		Element movie = doc.createElement("movie");
                Element info = doc.createElement("info");
                Element iname = doc.createElement("name");
                Element iyear = doc.createElement("year");
                Element itype = doc.createElement("type");
		Element dist = doc.createElement("distribution");
		Element insr = doc.createElement("inserted");

                Element per = doc.createElement("personal");
                Element director = doc.createElement("director");
                Element actors = doc.createElement("actors");
                Element actor = doc.createElement("actor");
                Element aname = doc.createElement("name");
                Element aplayed = doc.createElement("played");
                Element aphoto = doc.createElement("photo");

                Element desc = doc.createElement("desc");

                data.appendChild(movie);
                movie.appendChild(CRLF);
                movie.setAttribute("id", String.valueOf(ic));
                movie.appendChild(CRLF);
                
                movie.appendChild(info);
                
                    info.appendChild(iname);
                    iname.appendChild(tin);
                    iname.appendChild(CRLF);
                
                    info.appendChild(iyear);
                    iyear.appendChild(tiy);
                    iyear.appendChild(CRLF);

                    info.appendChild(itype);
                    itype.appendChild(tit);
                    itype.appendChild(CRLF);

		    info.appendChild(dist);
		    dist.appendChild(dis);
		    dist.appendChild(CRLF);

		    info.appendChild(insr);
		    insr.appendChild(isd);
		    insr.appendChild(CRLF);
                    
		    movie.appendChild(per);
                    per.appendChild(director);
                    director.appendChild(tpd);

                    per.appendChild(actors);
                    System.out.println("Pridat dalsiho herce? pro ukonceni napiste: ne");
                    String next = scan.nextLine();
                    while (!next.startsWith("ne")) {
                        
                        System.out.println("Zadejte jmeno herce");
                        Text tan = doc.createTextNode(scan.nextLine());
                        System.out.println("Zadejte jmeno postavy, kterou hral");
                        Text tap = doc.createTextNode(scan.nextLine());
                        System.out.println("Zadejte cestu k obrazku");
                        Text taph = doc.createTextNode(scan.nextLine());
                        
                        actors.appendChild(actor);
                            actor.appendChild(aname);
                            aname.appendChild(tan);
                            aname.appendChild(CRLF);

                            actor.appendChild(aplayed);
                            aplayed.appendChild(tap);
                            aplayed.appendChild(CRLF);

                            actor.appendChild(aphoto);
                            aphoto.appendChild(taph);
                            aphoto.appendChild(CRLF);
                            
                        System.out.println("Pridat dalsiho herce? pro ukonceni napiste: ne");
                        next = scan.nextLine();
                    }
                System.out.println("Zadejte popis filmu");
                Text td = doc.createTextNode(scan.nextLine());
                movie.appendChild(desc);
                desc.appendChild(td);
                data.appendChild(CRLF);


      try {
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new FileOutputStream(new File(OUTPUTFILE)));
	
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();

        transformer.transform(source, result);
      } catch (Exception e) {
        e.printStackTrace();
      } 

    }
   
}