package org.etsi.mts.tdl.ui.wizard;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
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.NodeList;

import com.google.common.io.Files;

public class TemplateIDGenerator {

	public static void main(String[] args) throws Exception {
		//utility for adding ids to exported templates
		//TODO: later can potentially assemble from other files
		//TODO: extract to separate component?
		String prefix = "org.etsi.mts.tdl.tx.templates.";
		String source = "templates/templates.xml";
		Files.copy(new File(source), new File(source+"back"));
		DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		Document doc = builder.parse(new File(source));
		NodeList list = doc.getElementsByTagName("template");
		for (int i = 0; i<list.getLength(); i++) {
			Element item = (Element)list.item(i);
			String id = item.getAttribute("id");
			System.out.println("id:" + id);
			if (id.isBlank()) {
				item.setAttribute("id", prefix+item.getAttribute("name").replaceAll(" ", "."));
			}
			id = item.getAttribute("id");
			System.out.println("id:" + id);
			
		}
		DOMSource dom = new DOMSource(doc);
		Transformer transformer = TransformerFactory.newInstance().newTransformer();
	    StreamResult result = new StreamResult(new File(source));
	    transformer.transform(dom, result);
	}

}
