<commit_before>package org.arrah.framework.xml;

import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

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

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 org.arrah.framework.ndtable.ReportTableModel;

/**
 * Read an XML file and create a report table
 * 
 * @author jchamblee
 */
public class XmlReader {
	 
	
	 String colNames ="";
	/**
	 * Read an XML file and create a report table
	 * 
	 * @param file
	 *            XML file to be read
	 * @return report table to be displayed
	 */
    
        // getRulesName() method added by Dareppa B
    
        public String[] getRulesName( File file, String rule, String ruleName ) {
            if( file.exists() && file.length() > 0 ) {
                parseDocument(file);
                NodeList nList = document.getElementsByTagName(rule);
                nodeList = new String[nList.getLength()];
                for (int temp = 0; temp < nList.getLength(); temp++) {
                    Node nNode = nList.item(temp);
                    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element eElement = (Element) nNode;
                        nodeList[temp] = eElement.getElementsByTagName(ruleName).item(0).getTextContent().trim();
                    }
                }
            }
            return nodeList;
        }
        
        public String[] getDBNames() {
            File file = new File(FilePaths.getConnFilePath());
            if( file.exists() && file.length() > 0 ) {
                parseDocument(file);
                NodeList nList = document.getElementsByTagName("entry");
                nodeList = new String[nList.getLength()];
                for (int temp = 0; temp < nList.getLength(); temp++) {
                    Node nNode = nList.item(temp);
                    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element eElement = (Element) nNode;
                        nodeList[temp] = eElement.getElementsByTagName("database_ConnectionName").item(0).getTextContent().trim();
                    }
                }
            }
            return nodeList;
        }
        
        // getDatabaseDetails() method added by <NAME>
        public Hashtable<String, String> getDatabaseDetails( File file, String fileEntry, String connName ) {
            if( file.exists() && file.length() > 0 ) {
                parseDocument(file);
                NodeList nList = document.getElementsByTagName(fileEntry);
                nodeList = new String[nList.getLength()];
                for (int temp = 0; temp < nList.getLength(); temp++) {
                    Node nNode = nList.item(temp);
                    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element eElement = (Element) nNode;
                        if( eElement.getElementsByTagName("database_ConnectionName").item(0).getTextContent().trim().equals(connName) ) {
                            hashTable = new Hashtable<String, String>();
                            hashTable.put("Database_Type", eElement.getElementsByTagName("database_Type").item(0).getTextContent().trim());
                            hashTable.put("Database_DSN", eElement.getElementsByTagName("database_DSN").item(0).getTextContent().trim());
                            hashTable.put("Database_Protocol", eElement.getElementsByTagName("database_Protocol").item(0).getTextContent().trim());
                            hashTable.put("Database_Driver", eElement.getElementsByTagName("database_Driver").item(0).getTextContent().trim());
                            hashTable.put("Database_User", eElement.getElementsByTagName("database_User").item(0).getTextContent().trim());
                            hashTable.put("Database_Passwd", eElement.getElementsByTagName("database_Passwd").item(0).getTextContent().trim());
                            hashTable.put("Database_JDBC", eElement.getElementsByTagName("database_JDBC").item(0).getTextContent().trim());
                            hashTable.put("Database_ConnName", eElement.getElementsByTagName("database_ConnectionName").item(0).getTextContent().trim());
                        }
                    }
                }
            }
            return hashTable;
        }
        
        public Hashtable<String, String> getRuleDetails( File file, String ruleEntry, String ruleName ) {
            if( file.exists() && file.length() > 0 ) {
                parseDocument(file);
                NodeList nList = document.getElementsByTagName(ruleEntry);
                nodeList = new String[nList.getLength()];
                for (int temp = 0; temp < nList.getLength(); temp++) {
                    Node nNode = nList.item(temp);
                    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element eElement = (Element) nNode;
                        if( eElement.getElementsByTagName("rule_Name").item(0).getTextContent().trim().equals(ruleName) ) {
                            hashRule = new Hashtable<String, String>();
                            hashRule.put("rule_Name", eElement.getElementsByTagName("rule_Name").item(0).getTextContent().trim());
                            hashRule.put("database_ConnectionName", eElement.getElementsByTagName("database_ConnectionName").item(0).getTextContent().trim());
                            hashRule.put("rule_Type", eElement.getElementsByTagName("rule_Type").item(0).getTextContent().trim());
                            hashRule.put("table_Names", eElement.getElementsByTagName("table_Names").item(0).getTextContent().trim());
                            hashRule.put("column_Names", eElement.getElementsByTagName("column_Names").item(0).getTextContent().trim());
                            hashRule.put("condition_Names", eElement.getElementsByTagName("condition_Names").item(0).getTextContent().trim());
                            hashRule.put("join_Name", eElement.getElementsByTagName("join_Name").item(0).getTextContent().trim());
                            //hashRule.put("rule_Description", eElement.getElementsByTagName("rule_Description").item(0).getTextContent().trim());
                        }
                    }
                }
            }
            return hashRule;
        }
     
        public String getColumnNames( File file, String ruleName) {
        	 parseDocument(file);
        	
        	 NodeList nodes = document.getElementsByTagName("rule");
        	
        	 for (int i = 0; i < nodes.getLength(); i++) {
        	 Node node = nodes.item(i);

        	// if (node.getNodeType() == Node.ELEMENT_NODE) {
        	 Element element = (Element) node;
        	 if(ruleName.equals(getValue("rule_Name", element))){
        	 colNames=getValue("column_Names", element);
        	 }
        	 else{
        		 
        	 }
        	
        	 }
        	// }
        	 return colNames;
        	 }
        
        	 private static String getValue(String tag, Element element) {
        		 NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
        		 Node node = (Node) nodes.item(0);
        		 if (node == null) return "";
        		 return node.getNodeValue();
        		 }


        		
	public ReportTableModel read(final File file) {
		final DOMValidator domValidator = new DOMValidator();
		domValidator.setErrorFile(new File(DTD_ERRORS));
		domValidator.validate(file);
		if (domValidator.getErrorCount() > 0) {
			System.out.println("\n " + domValidator.getErrorCount()
					+ " DTD errors found, see " + DTD_ERRORS);
		}
		parseDocument(file);
		getHeadings();
		getRowData();
		return reportTable;
	}

	/**
	 * Parse the document
	 * 
	 * @param file
	 *            XML file to be read
	 */
	private void parseDocument(final File file) {
		try {
			final DocumentBuilderFactory dbf = DocumentBuilderFactory
					.newInstance();
			final DocumentBuilder docBuilder = dbf.newDocumentBuilder();
			document = docBuilder.parse(file);
			document.getDocumentElement().normalize();
		} catch (SAXException exc) {
			System.out.println("\n XmlReader error:" + exc.getMessage());
		} catch (ParserConfigurationException exc) {
			System.out.println("\n XmlReader error:" + exc.getMessage());
		} catch (IOException exc) {
			System.out.println("\n XmlReader error:" + exc.getMessage());
		}
	}

	/**
	 * Gets the column headings
	 */
	private void getHeadings() {
		if (document == null)
			return;
		final NodeList headerList = document.getElementsByTagName("header");
		final Node firstNode = headerList.item(0);
		if (firstNode.getNodeType() == Node.ELEMENT_NODE) {
			final Element element = (Element) firstNode;
			final NodeList nodeList = element
					.getElementsByTagName("columnName");
			headings = new String[nodeList.getLength()];
			for (int index = 0; index < nodeList.getLength(); index++) {<commit_msg>added check for null<commit_after>package org.arrah.framework.xml;

import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

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

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 org.arrah.framework.ndtable.ReportTableModel;

/**
 * Read an XML file and create a report table
 * 
 * @author jchamblee
 */
public class XmlReader {
	 
	
	 String colNames ="";
	/**
	 * Read an XML file and create a report table
	 * 
	 * @param file
	 *            XML file to be read
	 * @return report table to be displayed
	 */
    
        // getRulesName() method added by <NAME>
    
        public String[] getRulesName( File file, String rule, String ruleName ) {
            if( file.exists() && file.length() > 0 ) {
                parseDocument(file);
                NodeList nList = document.getElementsByTagName(rule);
                nodeList = new String[nList.getLength()];
                for (int temp = 0; temp < nList.getLength(); temp++) {
                    Node nNode = nList.item(temp);
                    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element eElement = (Element) nNode;
                        nodeList[temp] = eElement.getElementsByTagName(ruleName).item(0).getTextContent().trim();
                    }
                }
            }
            return nodeList;
        }
        
        public String[] getDBNames() {
            File file = new File(FilePaths.getConnFilePath());
            if( file.exists() && file.length() > 0 ) {
                parseDocument(file);
                NodeList nList = document.getElementsByTagName("entry");
                nodeList = new String[nList.getLength()];
                for (int temp = 0; temp < nList.getLength(); temp++) {
                    Node nNode = nList.item(temp);
                    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element eElement = (Element) nNode;
                        nodeList[temp] = eElement.getElementsByTagName("database_ConnectionName").item(0).getTextContent().trim();
                    }
                }
            }
            return nodeList;
        }
        
        // getDatabaseDetails() method added by <NAME>
        public Hashtable<String, String> getDatabaseDetails( File file, String fileEntry, String connName ) {
            if( file.exists() && file.length() > 0 ) {
                parseDocument(file);
                NodeList nList = document.getElementsByTagName(fileEntry);
                nodeList = new String[nList.getLength()];
                for (int temp = 0; temp < nList.getLength(); temp++) {
                    Node nNode = nList.item(temp);
                    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element eElement = (Element) nNode;
                        if( eElement.getElementsByTagName("database_ConnectionName").item(0).getTextContent().trim().equals(connName) ) {
                            hashTable = new Hashtable<String, String>();
                            hashTable.put("Database_Type", eElement.getElementsByTagName("database_Type").item(0).getTextContent().trim());
                            hashTable.put("Database_DSN", eElement.getElementsByTagName("database_DSN").item(0).getTextContent().trim());
                            hashTable.put("Database_Protocol", eElement.getElementsByTagName("database_Protocol").item(0).getTextContent().trim());
                            hashTable.put("Database_Driver", eElement.getElementsByTagName("database_Driver").item(0).getTextContent().trim());
                            hashTable.put("Database_User", eElement.getElementsByTagName("database_User").item(0).getTextContent().trim());
                            hashTable.put("Database_Passwd", eElement.getElementsByTagName("database_Passwd").item(0).getTextContent().trim());
                            hashTable.put("Database_JDBC", eElement.getElementsByTagName("database_JDBC").item(0).getTextContent().trim());
                            hashTable.put("Database_ConnName", eElement.getElementsByTagName("database_ConnectionName").item(0).getTextContent().trim());
                        }
                    }
                }
            }
            return hashTable;
        }
        
        public Hashtable<String, String> getRuleDetails( File file, String ruleEntry, String ruleName ) {
            if( file.exists() && file.length() > 0 ) {
                parseDocument(file);
                NodeList nList = document.getElementsByTagName(ruleEntry);
                nodeList = new String[nList.getLength()];
                for (int temp = 0; temp < nList.getLength(); temp++) {
                    Node nNode = nList.item(temp);
                    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element eElement = (Element) nNode;
                        if( eElement.getElementsByTagName("rule_Name").item(0).getTextContent().trim().equals(ruleName) ) {
                            hashRule = new Hashtable<String, String>();
                            hashRule.put("rule_Name", eElement.getElementsByTagName("rule_Name").item(0).getTextContent().trim());
                            hashRule.put("database_ConnectionName", eElement.getElementsByTagName("database_ConnectionName").item(0).getTextContent().trim());
                            hashRule.put("rule_Type", eElement.getElementsByTagName("rule_Type").item(0).getTextContent().trim());
                            hashRule.put("table_Names", eElement.getElementsByTagName("table_Names").item(0).getTextContent().trim());
                            hashRule.put("column_Names", eElement.getElementsByTagName("column_Names").item(0).getTextContent().trim());
                            hashRule.put("condition_Names", eElement.getElementsByTagName("condition_Names").item(0).getTextContent().trim());
                            hashRule.put("join_Name", eElement.getElementsByTagName("join_Name").item(0).getTextContent().trim());
                            //hashRule.put("rule_Description", eElement.getElementsByTagName("rule_Description").item(0).getTextContent().trim());
                        }
                    }
                }
            }
            return hashRule;
        }
     
        public String getColumnNames( File file, String ruleName) {
        	 parseDocument(file);
        	
        	 NodeList nodes = document.getElementsByTagName("rule");
        	
        	 for (int i = 0; i < nodes.getLength(); i++) {
        	 Node node = nodes.item(i);

        	// if (node.getNodeType() == Node.ELEMENT_NODE) {
        	 Element element = (Element) node;
        	 if(ruleName.equals(getValue("rule_Name", element))){
        	 colNames=getValue("column_Names", element);
        	 }
        	 else{
        		 
        	 }
        	
        	 }
        	// }
        	 return colNames;
        	 }
        
        	 private static String getValue(String tag, Element element) {
        		 NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
        		 Node node = (Node) nodes.item(0);
        		 if (node == null) return "";
        		 return node.getNodeValue();
        		 }


        		
	public ReportTableModel read(final File file) {
		final DOMValidator domValidator = new DOMValidator();
		domValidator.setErrorFile(new File(DTD_ERRORS));
		domValidator.validate(file);
		if (domValidator.getErrorCount() > 0) {
			System.out.println("\n " + domValidator.getErrorCount()
					+ " DTD errors found, see " + DTD_ERRORS);
		}
		parseDocument(file);
		getHeadings();
		getRowData();
		return reportTable;
	}

	/**
	 * Parse the document
	 * 
	 * @param file
	 *            XML file to be read
	 */
	private void parseDocument(final File file) {
		try {
			final DocumentBuilderFactory dbf = DocumentBuilderFactory
					.newInstance();
			final DocumentBuilder docBuilder = dbf.newDocumentBuilder();
			document = docBuilder.parse(file);
			document.getDocumentElement().normalize();
		} catch (SAXException exc) {
			System.out.println("\n XmlReader error:" + exc.getMessage());
		} catch (ParserConfigurationException exc) {
			System.out.println("\n XmlReader error:" + exc.getMessage());
		} catch (IOException exc) {
			System.out.println("\n XmlReader error:" + exc.getMessage());
		}
	}

	/**
	 * Gets the column headings
	 */
	private void getHeadings() {
		if (document == null)
			return;
		final NodeList headerList = document.getElementsByTagName("header");
		if (headerList == null) 
			return;
		final Node firstNode = headerList.item(0);
		if (firstNode == null) 
			return;
		if (firstNode.getNodeType() == Node.ELEMENT_NODE) {
			final Element element = (Element) firstNode;
			final NodeList nodeList = element
					.getElementsByTagName("columnName");
			headings = new String[nodeList.getLength()];
			for (int index = 0; index < nodeList.getLength(); index++) {
{'fraction_non_alphanumeric': 0.12254928731363661, 'fraction_numerical': 0.004150510622030474, 'mean_word_length': 2.155609167671894, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, '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': '6647610', 'n_tokens_mistral': 5126, 'n_tokens_neox': 4838, 'n_words': 916}