<commit_before>package de.upb.soundgates.atomicbuilder;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
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.xml.sax.SAXException;

public class Application {

	public static void main(String[] args) {
		String templateFile = args[0];
		String pdFile = args[1];
		
		try {
			Document templateDoc = readTemplateFile(templateFile);
			PureDataInfo pureDataInfo = readPureDataCode(pdFile);
			insert(templateDoc, pureDataInfo);
			output(templateDoc);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	private static void output(Document templateDoc) throws TransformerException {
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		DOMSource source = new DOMSource(templateDoc);
		//StreamResult result = new StreamResult(new File("C:\\file.xml"));
		StreamResult result = new StreamResult(System.out);
		transformer.transform(source, result);
	}

	private static void insert(Document templateDoc, PureDataInfo pureDataInfo) {
		NodeList simulationNodes = templateDoc.getElementsByTagName("Simulation");
		for (int i = 0; i < simulationNodes.getLength(); i++){
			if (simulationNodes.item(i).getParentNode().getNodeName().equals("Code")){
				createPortMappings(templateDoc, simulationNodes.item(i), pureDataInfo.getInputDefinitions());
				createPortMappings(templateDoc, simulationNodes.item(i), pureDataInfo.getOutputDefinitions());
				createPropMappings(templateDoc, simulationNodes.item(i), pureDataInfo.getProperties());
				createCode(templateDoc, simulationNodes.item(i), pureDataInfo.getCode());
			}
		}
	}
	
	private static void createCode(Document doc, Node parent,
			String code){
		Element codeNode = doc.createElement("PD");
		parent.appendChild(codeNode);
		codeNode.appendChild(doc.createTextNode(code));
	}
	
	private static void createPropMappings(Document doc, Node parent,
			List<String> properties) {
		for (String property : properties){
			Element propMapping = doc.createElement("PropMapping");
			parent.appendChild(propMapping);
			propMapping.setAttribute("PropName", property);
			propMapping.setAttribute("Tag", "@" + property);
		}
	}

	private static void createPortMappings(Document doc, Node parent, List<String> ports){
		for (String port : ports){
			Element portMapping = doc.createElement("PortMapping");
			parent.appendChild(portMapping);
			portMapping.setAttribute("PortName", port);
			portMapping.setAttribute("PortNumber", "" + ports.indexOf(port));
		}
	}

	private static Document readTemplateFile(String path) throws ParserConfigurationException, SAXException, IOException {
		File file = new File(path);

		DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		return dBuilder.parse(file);
	}

	private static PureDataInfo readPureDataCode(String path) throws IOException {
		FileReader fileReader = new FileReader(new File(path));
		BufferedReader bufferedReader = new BufferedReader(fileReader);
		StringBuilder code = new StringBuilder();
		String read = "";
		boolean manipulateNextLine = false;
		int manipulationIndex = 0;
		String manipulationValue = "";
		PureDataInfo result = new PureDataInfo();
		while((read = bufferedReader.readLine()) != null){
			read = read.split(";")[0];
			if (manipulateNextLine){
				String [] splitLine = read.split(" ");
				splitLine[manipulationIndex] = "@" + manipulationValue;
				read = "";
				for (String single : splitLine){
					read += single + " "; 
				}
				manipulateNextLine = false;
			}
			if (read.contains("#X text")){<commit_msg>added the possibility to build a complete folder of atomic components<commit_after>package de.upb.soundgates.atomicbuilder;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
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.xml.sax.SAXException;

public class Application {

	static boolean folder = false;
	public static void main(String[] args) {
		if (args.length > 0 && args[0].equals("--folder")){
			folder = true;
		}
		if (args.length == 0){
			System.out.println("Usage:");
			System.out.println("\t -r <sourceFolder> <destinationFolder>");
			System.out.println("or");
			System.out.println("\t <pdFile> <templateFile> <destinationFile>");
		}
		
			if (folder){
				File sourceFolder = new File(args[1]);
				File destinationFolder = new File(args[2]);
				for (final File sub : sourceFolder.listFiles()){
					if (sub.isDirectory()){
						File pdFile = getFileFromFolder(sub, "pd");
						File xmlFile = getFileFromFolder(sub, "xml");
							try {
								generate(new File(destinationFolder, xmlFile.getName().replaceAll("(\\..*)$", "") + ".xml"), pdFile, xmlFile);
							} catch (ParserConfigurationException
									| SAXException | IOException
									| TransformerException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						
					}
				}
			} else {
				File pdFile = new File(args[0]);
				File templateFile = new File(args[0]);
				File destinationFile = new File(args[1]);
				try {
					generate(destinationFile, pdFile, templateFile);
				} catch (ParserConfigurationException | SAXException
						| IOException | TransformerException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			

		
	}
	
	private static void generate(File destinationFile, File pdFile, File templateFile) throws ParserConfigurationException, SAXException, IOException, TransformerException{
		Document templateDoc = readTemplateFile(templateFile);
		PureDataInfo pureDataInfo = readPureDataCode(pdFile);
		insert(templateDoc, pureDataInfo);
		output(templateDoc, destinationFile);
	}
	
	private static File getFileFromFolder(File folder, final String suffix) {
		for (final File sub : folder.listFiles()) {
			if (sub.getName().endsWith(suffix)) return sub;
		}
		return null;
	}

	private static void output(Document templateDoc, File destination) throws TransformerException {
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		DOMSource source = new DOMSource(templateDoc);
		StreamResult result = new StreamResult(destination);
		//StreamResult result = new StreamResult(System.out);
		transformer.transform(source, result);
	}

	private static void insert(Document templateDoc, PureDataInfo pureDataInfo) {
		NodeList simulationNodes = templateDoc.getElementsByTagName("Simulation");
		for (int i = 0; i < simulationNodes.getLength(); i++){
			if (simulationNodes.item(i).getParentNode().getNodeName().equals("Code")){
				createPortMappings(templateDoc, simulationNodes.item(i), pureDataInfo.getInputDefinitions());
				createPortMappings(templateDoc, simulationNodes.item(i), pureDataInfo.getOutputDefinitions());
				createPropMappings(templateDoc, simulationNodes.item(i), pureDataInfo.getProperties());
				createCode(templateDoc, simulationNodes.item(i), pureDataInfo.getCode());
			}
		}
	}
	
	private static void createCode(Document doc, Node parent,
			String code){
		Element codeNode = doc.createElement("PD");
		parent.appendChild(codeNode);
		codeNode.appendChild(doc.createTextNode(code));
	}
	
	private static void createPropMappings(Document doc, Node parent,
			List<String> properties) {
		for (String property : properties){
			Element propMapping = doc.createElement("PropMapping");
			parent.appendChild(propMapping);
			propMapping.setAttribute("PropName", property);
			propMapping.setAttribute("Tag", "@" + property);
		}
	}

	private static void createPortMappings(Document doc, Node parent, List<String> ports){
		for (String port : ports){
			Element portMapping = doc.createElement("PortMapping");
			parent.appendChild(portMapping);
			portMapping.setAttribute("PortName", port);
			portMapping.setAttribute("PortNumber", "" + ports.indexOf(port));
		}
	}

	private static Document readTemplateFile(File templateFile) throws ParserConfigurationException, SAXException, IOException {

		DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		return dBuilder.parse(templateFile);
	}

	private static PureDataInfo readPureDataCode(File pdFile) throws IOException {
		FileReader fileReader = new FileReader(pdFile);
		BufferedReader bufferedReader = new BufferedReader(fileReader);
		StringBuilder code = new StringBuilder();
		String read = "";
		boolean manipulateNextLine = false;
		int manipulationIndex = 0;
		String manipulationValue = "";
		PureDataInfo result = new PureDataInfo();
		while((read = bufferedReader.readLine()) != null){
			read = read.split(";")[0];
			if (manipulateNextLine){
				String [] splitLine = read.split(" ");
				splitLine[manipulationIndex] = "@" + manipulationValue;
				read = "";
				for (String single : splitLine){
					read += single + " "; 
				}
				manipulateNextLine = false;
			}
			if (read.contains("#X text")){
{'fraction_non_alphanumeric': 0.1152936725621867, 'fraction_numerical': 0.0022699328478199185, 'mean_word_length': 4.950478334271244, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 26}, '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': '4943900', 'n_tokens_mistral': 3373, 'n_tokens_neox': 2923, 'n_words': 678}