<commit_before>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 ClusterFileIO {
	private static ClusterFileIO instance = new ClusterFileIO();
	private static final String namespace = "http://rise.cs.drexel.edu/minos/clsx";
	
	private ClusterFileIO() {
		//use getInstance method instead
	}
	
	public static ClusterFileIO getInstance() {
		return instance;
	}
	
	public void saveClusterData(File path, TreeNode treeRoot) throws IOException {
		BufferedWriter out = new BufferedWriter(new FileWriter(path,false));
		out.write("<cluster xmlns=\"http://rise.cs.drexel.edu/minos/clsx\">");
		out.newLine();
		for(int i=0;i<treeRoot.getChildCount();i++) {
			writeGroup(out,(DefaultMutableTreeNode)treeRoot.getChildAt(0));
		}
		out.append("</cluster>");
		out.close();
	}
	
	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(i));
			}
			out.append("</group>");
			out.newLine();
		} else {
			out.append("<item name=\"" + attributeName + "\" />");
			out.newLine();
		}
	}
	
	public DefaultMutableTreeNode loadClusterData(File source) throws IOException, WrongXMLNamespaceException {
		DefaultMutableTreeNode treeRoot;
		
		DocumentBuilderFactory xmlFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder xmlBuilder = null;
		Document doc = null;
		try {
			xmlBuilder = xmlFactory.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// To be implemented...
		}
		try {
			doc = xmlBuilder.parse(source);
		} 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();
		}
		
		treeRoot = new DefaultMutableTreeNode(true);
		buildTree(doc.getDocumentElement(),treeRoot);
		
		return treeRoot;
	}<commit_msg>Default Tree root name modified...<commit_after>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 ClusterFileIO {
	private static ClusterFileIO instance = new ClusterFileIO();
	private static final String namespace = "http://rise.cs.drexel.edu/minos/clsx";
	
	private ClusterFileIO() {
		//use getInstance method instead
	}
	
	public static ClusterFileIO getInstance() {
		return instance;
	}
	
	public void saveClusterData(File path, TreeNode treeRoot) throws IOException {
		BufferedWriter out = new BufferedWriter(new FileWriter(path,false));
		out.write("<cluster xmlns=\"http://rise.cs.drexel.edu/minos/clsx\">");
		out.newLine();
		for(int i=0;i<treeRoot.getChildCount();i++) {
			writeGroup(out,(DefaultMutableTreeNode)treeRoot.getChildAt(0));
		}
		out.append("</cluster>");
		out.close();
	}
	
	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(i));
			}
			out.append("</group>");
			out.newLine();
		} else {
			out.append("<item name=\"" + attributeName + "\" />");
			out.newLine();
		}
	}
	
	public DefaultMutableTreeNode loadClusterData(File source) throws IOException, WrongXMLNamespaceException {
		DefaultMutableTreeNode treeRoot;
		
		DocumentBuilderFactory xmlFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder xmlBuilder = null;
		Document doc = null;
		try {
			xmlBuilder = xmlFactory.newDocumentBuilder();
		} catch (ParserConfigurationException e) {
			// To be implemented...
		}
		try {
			doc = xmlBuilder.parse(source);
		} 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();
		}
		
		treeRoot = new DefaultMutableTreeNode("ROOT",true);
		buildTree(doc.getDocumentElement(),treeRoot);
		
		return treeRoot;
	}
{'fraction_non_alphanumeric': 0.1393543114017164, 'fraction_numerical': 0.0032693093583980384, 'mean_word_length': 4.639400921658986, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 18}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7031463', 'n_tokens_mistral': 1706, 'n_tokens_neox': 1516, 'n_words': 302}