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


public class TestCastProjectBuilder {
	private String files = "";
	private String sut = "";
	
	private String template1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + 
	"<solution>\n" + 
	" <scripts>\n" + 
	"  <items>\n"; 
	
	private String template2 = 
		"  </items>\n" + 
		" </scripts>\n" + 
		" <verstool />\n"; 

	private String template3 = 
	" <codec_rules>CodecRules_Project3.txt</codec_rules>\n" + 
	" <ExternalAttributeFiles>\n" + 
	"  <items />\n" + 
	" </ExternalAttributeFiles>\n" + 
	" <ModuleParFiles>\n" + 
	"  <items />\n" + 
	" </ModuleParFiles>\n" + 
	" <TestcaseSelections>\n" + 
	"  <items />\n" + 
	" </TestcaseSelections>\n" + 
	" <TestCampaigns>\n" + 
	"  <items />\n" + 
	" </TestCampaigns>\n" + 
	" <ttcn3_settings>\n" + 
	"  <tc_block />\n" + 
	"  <ttcn3_version>Ttcn3_2010</ttcn3_version>\n" + 
	"  <asn>BER</asn>\n" + 
	"  <dll_timeout>10</dll_timeout>\n" + 
	"  <port>7777</port>\n" + 
	"  <ctime>60</ctime>\n" + 
	"  <sa_count>1</sa_count>\n" + 
	"  <rtime>20</rtime>\n" + 
	"  <codec_type>internal</codec_type>\n" + 
	"  <enc>\n" + 
	"   <plain>\n" + 
	"    <byte_order>le</byte_order>\n" + 
	"    <signed />\n" + 
	"    <int>dword</int>\n" + 
	"    <float>double</float>\n" + 
	"    <custom_int>8</custom_int>\n" + 
	"    <string>null</string>\n" + 
	"    <custom_str>32</custom_str>\n" + 
	"    <char>U16</char>\n" + 
	"    <bool>dword</bool>\n" + 
	"    <bin>dword</bin>\n" + 
	"    <custom_bin>16</custom_bin>\n" + 
	"    <align>1</align>\n" + 
	"    <coll>word</coll>\n" + 
	"    <str_unit>bytes</str_unit>\n" + 
	"    <pad />\n" + 
	"    <str_end>\\r\\n</str_end>\n" + 
	"    <uni_end>\\r\\n</uni_end>\n" + 
	"    <any>none</any>\n" + 
	"   </plain>\n" + 
	"  </enc>\n" + 
	" </ttcn3_settings>\n" + 
	"</solution>";

	public void addFile(String filename) {
		File f = new File(filename);
		files += "   <script>\n";
		files += "    <name>" + f.getName().substring(0, f.getName().indexOf(".ttcn")) + "</name>\n";
		files += "    <file>" + filename + "</file>\n"; 
		files += "    <type>ttcn3</type>\n"; 
		files += "   </script>\n"; 
	}

	public void setSUT(String sutFile) {
		sut = " <sut>\n";
		sut += "  <exe>" + sutFile + "</exe>\n";
		sut += " </sut>\n";
	}
	
	public String getProjectFileString() {
		return template1 + files + template2 + sut + template3;
	}
	
	public void saveProjectFile(File f) {
		 PrintWriter out;
		try {
			out = new PrintWriter(new FileWriter(f));
			 out.print(getProjectFileString());
			 out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
