package model;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.xml.parsers.*;

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

import javax.swing.tree.*;

public class ClusterData {
	private File sourceFile;
	private DefaultMutableTreeNode treeRoot;
	private final String namespace = "http://rise.cs.drexel.edu/minos/clsx";
	
	public ClusterData() {
		System.out.println("I need a directory of the .clsx file!");
	}
	
//initializing with File instance	
	public ClusterData(File file) throws IOException, WrongXMLNamespaceException {
		this.sourceFile = file;
		initData();
	}
	
//load new .clsx file
	public void loadClusterData(File file) throws IOException, WrongXMLNamespaceException {
		this.sourceFile = file;
		initData();
	}

//Parse XML data from .clsx file into tree
	private void initData() throws  IOException, WrongXMLNamespaceException {
		DocumentBuilderFactory xmlFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder xmlBuilder=null;
		Document doc=null;
		try {
			xmlBuilder = xmlFactory.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// To be implemented...
		}
		try {
			doc = xmlBuilder.parse(this.sourceFile);
		} catch (SAXException e) {
			// To be implemented...
		}
		doc.getDocumentElement().normalize();
		Element tempclsx = (Element) doc.getChildNodes().item(0);
		if(!tempclsx.getAttribute("xmlns").equals(namespace)) {
			throw new WrongXMLNamespaceException();
		}
		
		this.treeRoot = new DefaultMutableTreeNode(true);
		buildTree(doc.getDocumentElement(),treeRoot);
	}
	
//Build tree recursively
	private void buildTree(Element DOMnode, DefaultMutableTreeNode treeNode) {
		NodeList nodeList = DOMnode.getChildNodes();
		for(int i=0; i<nodeList.getLength();i++) {
			Element elem=null;
			DefaultMutableTreeNode newNode=null;
			Node node = nodeList.item(i);
			if(node.getNodeType()==Node.ELEMENT_NODE) {
				elem = (Element) node;
			}
			newNode = new DefaultMutableTreeNode(elem.getAttribute("name"),elem.hasChildNodes());
			if(newNode.getAllowsChildren())
				buildTree(elem,newNode);
			treeNode.add(newNode);
		}
	}

//gives root of the Node(<cluster> level)	
	public DefaultMutableTreeNode getTree() {
		return this.treeRoot;
	}

//I suppose this method should be changed...	
	public void setTree(DefaultMutableTreeNode newTree) {
		this.treeRoot = newTree;
	}

//Call this method to save tree in XML format	
	public void saveClusterData() throws IOException {
		BufferedWriter out = new BufferedWriter(new FileWriter(this.sourceFile,false));
		out.write("<cluster xmlns=\"http://rise.cs.drexel.edu/minos/clsx\">");
		out.newLine();
		for(int i=0;i<this.treeRoot.getChildCount();i++) {
			writeGroup(out,(DefaultMutableTreeNode)this.treeRoot.getChildAt(0));
		}
		out.append("</cluster>");
		out.close();
	}
//saving with new Dir. of the file	
	public void saveClusterData(String newDir) throws IOException {
		this.sourceFile = new File(newDir);
		BufferedWriter out = new BufferedWriter(new FileWriter(this.sourceFile,false));
		out.write("<cluster xmlns=\"http://rise.cs.drexel.edu/minos/clsx\">");
		out.newLine();
		for(int i=0;i<this.treeRoot.getChildCount();i++) {
			writeGroup(out,(DefaultMutableTreeNode)this.treeRoot.getChildAt(0));
		}
		out.append("</cluster>");
		out.close();
	}

//recursively write group/item into file	
	private void writeGroup(BufferedWriter out, DefaultMutableTreeNode node) throws IOException {
		String attributeName = node.getUserObject().toString();
		if(node.getAllowsChildren()) {
			out.append("<group name=\"" + attributeName + "\">");
			out.newLine();
			for(int i=0;i<node.getChildCount();i++) {
				writeGroup(out,(DefaultMutableTreeNode)node.getChildAt(0));
			}
			out.append("</group>");
			out.newLine();
		} else {
			out.append("<item name=" + attributeName + "\" />");
			out.newLine();
		}
		
	}
}

@@ -2068,20 +2068,16 @@
  node;%0A%09
-%09%09%7D%0A
 %09%09%09newNo
@@ -2156,24 +2156,25 @@
 odes());%0A%09%09%09
+%09
 if(newNode.g
@@ -2193,16 +2193,17 @@
 dren())%0A
+%09
 %09%09%09%09buil
@@ -2226,16 +2226,17 @@
 de);%0A%09%09%09
+%09
 treeNode
@@ -2250,16 +2250,21 @@
 wNode);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%0A
@@ -3845,25 +3845,25 @@
 .getChildAt(
-0
+i
 ));%0A%09%09%09%7D%0A%09%09%09
@@ -3941,16 +3941,18 @@
 em name=
+%5C%22
 %22 + attr

Debug on cluster Data