import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashSet;
import java.util.Set;

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

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

import com.sun.xml.internal.ws.util.xml.NodeListIterator;


public class Dom1 {
	static Document getDocument(String fileName) 
	throws ParserConfigurationException, SAXException, IOException{
		DocumentBuilder parser =
			DocumentBuilderFactory
				.newInstance()
				.newDocumentBuilder();
			Document doc =
			parser.parse("/home/sep/fh/java/skript.xml");
			return doc;
	}
	
	static int count(Node node){
		int result = 1;
		final NodeList nos = node.getChildNodes();
		for (int i=0;i<nos.getLength();i++){
			result=result+count(nos.item(i));	
		}
		return result;
	}
	static void getTagNames(Node node,Set<String> result){
		if (node.getNodeType()==Node.ELEMENT_NODE){
			result.add(node.getNodeName());
		}
		final NodeList nos = node.getChildNodes();
		for (int i=0;i<nos.getLength();i++){
			getTagNames(nos.item(i),result);	
		}
	}
	static Set<String> getTagNames(Node node){
		Set<String> result=new HashSet<String>();
		getTagNames(node,result);
		return result;
	}

	
	static void writeText(Node node,Writer out) 
	                            throws  IOException{
		if (node.getNodeType()==Node.TEXT_NODE){
			out.write(node.getNodeValue());
		}
		final NodeList nos = node.getChildNodes();
		for (int i=0;i<nos.getLength();i++){
			writeText(nos.item(i),out);	
		}
	}

	static void writeText(Node node,String fileName) throws IOException{
		writeText(node, new FileWriter(fileName));
	}

	static boolean containsTagName(Node node,String tagName){
		if (node.getNodeType()==Node.ELEMENT_NODE 
				&& node.getNodeName().equals(tagName)) 
			return true;
		final NodeList nos = node.getChildNodes();
		for (int i=0;i<nos.getLength();i++){
			if (containsTagName(nos.item(i),tagName))
				return true;
		}	
		return false;
	}
	final static String CODE="code";
	final static String CLASS="class";
	static void writeCodeForClass(String className,Node node,Writer out) 
	                                                  throws IOException{
		if (node.getNodeType()==Node.ELEMENT_NODE
				&& node.getNodeName().equals(CODE)
				&& node.getAttributes().getNamedItem(CLASS)!=null
				&& node.getAttributes().getNamedItem(CLASS).getNodeValue().equals(className)
				){
			writeText(node, out);
		}else{
			final NodeList nos = node.getChildNodes();
			for (int i=0;i<nos.getLength();i++){
				writeCodeForClass(className, nos.item(i), out);
			}
		}
	}
	static void writeCodeForClass(String className,Node node) throws IOException{
		writeCodeForClass(className, node,new FileWriter(className+".java"));
	}

	
	public static void main(String[] args) 
	    throws ParserConfigurationException, SAXException, IOException {
		Node node =getDocument("/home/sep/fh/java/skript.xml");
		System.out.println(count(node));
		System.out.println(getTagNames(node));
		writeText(node, "skriptout.txt");
		System.out.println(containsTagName(node, "table"));
		writeCodeForClass("Li", node);
	}

}
