//package ext;

// cd /d D:\PTC\Windchill\ACL\Nasa_MFSC\DomainPolicyRule_Outputs
// cd /d C:\LJett\Windchill\ACL\DomainPolicyRule_Outputs_test\DomainPolicyRule_Outputs

// D:\Java\bin\javac.exe ACL_Windu_2_XML_TXT_10_read_09.java
// C:\LJett\Java\jdk1.8.0_65\bin\javac.exe ACL_Windu_2_XML_TXT_10_read_09.java

// java ACL_Windu_2_XML_TXT_10_read_09 D:\PTC\Windchill\ACL\Nasa_MFSC\DomainPolicyRule_Outputs
// java ACL_Windu_2_XML_TXT_10_read_09 D:\PTC\Windchill\ACL\Nasa_MFSC\DomainPolicyRule_Outputs>read_09.txt

// java ACL_Windu_2_XML_TXT_10_read_09 C:\LJett\Windchill\ACL\DomainPolicyRule_Outputs_test\DomainPolicyRule_Outputs>read_10.txt

import java.io.*;
import java.util.*;
import java.text.*;

public class ACL_Windu_2_XML_TXT_10_read_09 {
	public static void main(String[] args) {
		if(args.length == 0) {
			System.out.println("Usage: java ACL_Windu_2_XML_TXT_10_read_09 [Path to Windu DomainPolicyRule.txt]");
			return;
		}
		else {
			SimpleDateFormat df = new SimpleDateFormat ("MMddyy_HHmmss", Locale.getDefault());
			java.util.Date tm = new java.util.Date();
			dtStg = df.format(tm);

			inArgPath = args[0];

			inPath = new File(inArgPath+"\\DomainPolicyRule_10.1_minus.txt");
			outLFF = new File(inArgPath+"\\ACL_LoadFromFile_"+dtStg+".xml");
			outList = new File(inArgPath+"\\ACL_FileList_"+dtStg+".txt");
		}

		TotOut = "Root.Root";

		try {
			PrintWriter prwrLFF = new PrintWriter(new FileWriter(outLFF));
			PrintWriter prwrList = new PrintWriter(new FileWriter(outList));

			prwrLFF.println((new StringBuilder()).append(top).toString());

			BufferedReader bufferedreader = new BufferedReader(new FileReader(inPath));
			List inList = new ArrayList();

			String line = "";
			while ((line = bufferedreader.readLine()) != null) {
				inList.add(line);
			}

			inStgArr = new String[inList.size()];
			inList.toArray(inStgArr);

/*
------------------------------------------------------------------------------------------------------------------------------------------------------
Site /Root
   [/]/   (wt.admin.AdministrativeDomain:6)
------------------------------------------------------------------------------------------------------------------------------------------------------
   Rule for Type | com.ptc.core.components.chart.SavedChart| ALL| Participant | User - ALL | + | [Read, Create] |  |
   Rule for Type | com.ptc.windchill.enterprise.datamonitor.DataMonitor| ALL| Participant | User - ALL | + | [Read, Modify, Create, Delete] |  |
   Rule for Type | com.ptc.windchill.enterprise.datamonitor.SavedReportHolder| ALL| Participant | User - ALL | + | [Read, Modify, Create, Delete, Download] |  |
   Rule for Type | com.ptc.windchill.option.model.Choice| ALL| Participant | WTRole Principal - Option Manager | + | [Full Control (All)] |  | wt.org.WTRolePrincipal:199982297

------------------------------------------------------------------------------------------------------------------------------------------------------
Site, Organization - DJ_WC, Library - Library_DJ /Default
   [/wt.inf.container.OrgContainer=DJ_WC/wt.inf.library.WTLibrary=Library_DJ]/Default   (wt.admin.AdministrativeDomain:102430)
------------------------------------------------------------------------------------------------------------------------------------------------------
   Rule for Type | wt.fv.master.ReplicaVault                                    | ALL               | User - ALL                            | + Read                           | null
   Rule for Type | wt.org.WTRolePrincipal                                       | ALL               | User - ALL                            | + Read                           | null
   Rule for Type | wt.inf.library.WTLibrary                                     | ALL               | Group - LIBRARY CREATOR               | + Create                         | null

*/

			for( int y = 0; y < inStgArr.length; y++ ) {
				System.out.println("inStgArr["+y+"]: "+inStgArr[y]);

				if (inStgArr[y].startsWith("   [/")){
					System.out.println("@@@inStgArr[y]: "+inStgArr[y]);
					System.out.println("GOT THROUGH");

					if ( y < 5 ) { xmlOut = top+"\n"; }
					if ( tmpInt > 0 && tmpInt < y ) {
						xmlOut += fin+"\n";
						outpath_sh = new File(inArgPath+"\\"+TotOut+"_"+dtStg+".xml");
						System.out.println("=============\nTotOut(50): "+TotOut+"\n");
						System.out.println("outpath_sh(50): "+outpath_sh+"\n");

						prwrsh = new PrintWriter(new FileWriter(outpath_sh));
						prwrsh.println((new StringBuilder()).append(xmlOut).toString());
						prwrsh.flush();
						prwrsh.close();
						xmlOut = top+"\n";
					}

					tmpInt = y;
					condomStg = "";
					TotOut = "";
					AccOut = "";
					TdomOut = "";
					TconOut = "";

					condomStgArr = new String[0];
					condomStgArr = inStgArr[y].split("\\(wt.");
					condomStg = ltrim(condomStgArr[0]);
					condomStg = rtrim(condomStg);

					System.out.println("condomStg: "+condomStg);

					AccOut = condomStg+"\n";
					condomSp = condomStg.split("\\]");		// Split Container & Domain

					// Org - Container
					conStg = condomSp[0];					// Container
					conSp = conStg.split("\\/");			// Container Split
					conSpEq = new String[0];

					System.out.println("conStg: "+conStg);

					if (conSp.length < 2 ){ TconOut = "Root"; }
					else {
						for( int z = 1; z < conSp.length; z++ ) {
							conSpEq = conSp[z].split("\\=");
							conOut = conSpEq[1];

							System.out.println("conOut1: "+conOut);

							if (conOut.indexOf(" ")> 0){ conOut = conOut.replaceAll(" ", "_"); }
							if (conOut.indexOf("\\]")> 0){ conOut = conOut.replaceAll("\\]", ")"); }
							if (conOut.indexOf("\\[")> 0){ conOut = conOut.replaceAll("\\[", "("); }

							conOut = conOut.replaceAll("\\\\", "");

							if ( z > 1 ) { TconOut = TconOut +"."+ conOut; }
							else if (conOut.equals("/")) { TconOut = "Root"; }
							else { TconOut = conOut; }

							System.out.println("TconOut: "+TconOut);
						}
					}

					// Domain
					domStg = condomSp[1];

					System.out.println("domStg: "+domStg);

					if (domStg.equals("/") ) { TdomOut = "Root"; }
					else {
						domSp = domStg.split("\\/");		// Domain Split
						domSpEq = new String[0];
						domOut = "";

						for( int x = 0; x < domSp.length; x++ ) {
							domOut = domSp[x];

							System.out.println("domOut: "+domOut);

							if (domOut.indexOf(" ")> 0){ domOut = domOut.replaceAll(" ", "_"); }
							if (x > 1 ) { TdomOut = TdomOut +"."+ domOut; }
							else { TdomOut = domOut; }

							System.out.println("TdomOut: "+TdomOut);
						}
					}

					TotOut = TdomOut +"."+ TconOut;
					if (condomStg.equals("[/]/") ) { TotOut = "Root.Root"; }

					System.out.println("=============\nTdomOut(113): "+TdomOut+"\n");
					System.out.println("=============\nTconOut(113): "+TconOut+"\n");
					System.out.println("=============\nTotOut(113): "+TotOut+"\n");
					System.out.println("condomStg(113): "+condomStg+"\n\t"+TotOut+"\n");

					prwrList.println((new StringBuilder()).append(condomStg+"\n\t"+TotOut+"\n").toString());
				}
				else if (inStgArr[y].indexOf("Rule for Type")> 0){
					ruleStgArr = new String[0];
					ruleStgArr = inStgArr[y].split("\\|");

					String ruleStgArr6 = ruleStgArr[6];

					ruleStgArr6 = ruleStgArr6.replaceAll("\\[", "");
					ruleStgArr6 = ruleStgArr6.replaceAll("\\]", "");

					String typStg = ltrim(ruleStgArr[1]);
					typStg = rtrim(typStg);

					String st8Stg = ltrim(ruleStgArr[2]);
					st8Stg = rtrim(st8Stg);

					prinStg = ruleStgArr[4];	// principle field
					prinStgArr = prinStg.split(" \\(");
					prinStg = rtrim(ltrim(prinStgArr[0]));

					/*
					if (prinStg.indexOf(" (Product")> 0){
						prinStgArr.split(" (Product");
						prinStg = rtrim(ltrim(prinStgArr[0]));
					}

					if (prinStg.indexOf(" (Project")> 0){
						prinStgArr.split(" (Project");
						prinStg = rtrim(ltrim(prinStgArr[0]));
					}

					if (prinStg.indexOf(" (Library")> 0){
						prinStgArr.split(" (Library");
						prinStg = rtrim(ltrim(prinStgArr[0]));
					}
					*/

					prinStg.replace("Group - teamMembers", "Group - Team Members");
					prinStg.replace("Group - GUEST", "Group - Guest");

					System.out.println("prinStg:"+prinStg+"\n");
					System.out.println("ruleStgArr[6]:"+ruleStgArr[6]+"\n");

					permlStgArr = ruleStgArr6.split(",");

					System.out.println("permlStgArr[0]BEF:"+permlStgArr[0]+"\n");

					char permC = ruleStgArr[5].charAt(1);

					//String tmpStg = permlStgArr[0].substring( 3, permlStgArr[0].length() );
					//permlStgArr[0] = permlStgArr[0].substring( 3, permlStgArr[0].length() );

					System.out.println("permlStgArr[0]AFT:"+permlStgArr[0]+"\n");

					permlList = new ArrayList();

					for( int a = 0; a < permlStgArr.length; a++ ) {
						String permlTstStg = "";
						permlTstStg = ltrim(permlStgArr[a]);
						permlTstStg = rtrim(permlTstStg);

						for( int b = 0; b < permlListArr.length; b++ ) {
							tmpPermlArr = permlListArr[b].split("=");

							if (permlTstStg.equals(tmpPermlArr[1])){
								permlList.add(tmpPermlArr[0]);
							}
						}
					}

					permTmpArr = new String[0];
					permTmpArr = new String[permlList.size()];
					permlList.toArray(permTmpArr);

					permlStg = "";
					int intTmp = permTmpArr.length;

					for( int d = 0; d < permTmpArr.length; d++ ) {
						if ( d == (intTmp-1) ) {
							permlStg += permTmpArr[d];
						}
						else {
							permlStg += permTmpArr[d]+"/";
						}
						//System.out.println("permlStg @ ["+d+"]: "+permlStg+"\n");
					}

					String ruleStg = typStg+"|"+st8Stg+"|"+prinStg+"|"+permC+"|"+ruleStgArr6;
					System.out.println("ruleStg(262):"+ruleStg+"\n");

					AccOut += ruleStg+"\n";

					//System.out.println(beg+"\n\t"+usr);
					xmlOut += beg+"\n\t"+usr+"\n";
					prwrLFF.println((new StringBuilder()).append(beg+"\n\t"+usr).toString());

					//System.out.println("\t"+domB+condomStg+domE);
					xmlOut += "\t"+domB+condomStg+domE+"\n";
					prwrLFF.println((new StringBuilder()).append("\t"+domB+condomStg+domE).toString());

					//System.out.println("\t"+typidB+typStg+typidE);
					xmlOut += "\t"+typidB+typStg+typidE+"\n";
					prwrLFF.println((new StringBuilder()).append("\t"+typidB+typStg+typidE).toString());

					//System.out.println("\t"+permB+permC+permE);
					xmlOut += "\t"+permB+permC+permE+"\n";
					prwrLFF.println((new StringBuilder()).append("\t"+permB+permC+permE).toString());

					//System.out.println("\t"+prinB+prinStg+prinE);
					xmlOut += "\t"+prinB+prinStg+prinE+"\n";
					prwrLFF.println((new StringBuilder()).append("\t"+prinB+prinStg+prinE).toString());

					//System.out.println("\t"+permlB+permlStg+permlE);
					xmlOut += "\t"+permlB+permlStg+permlE+"\n";
					prwrLFF.println((new StringBuilder()).append("\t"+permlB+permlStg+permlE).toString());

					//System.out.println("\t"+st8B+st8Stg+st8E);
					xmlOut += "\t"+st8B+st8Stg+st8E+"\n";
					prwrLFF.println((new StringBuilder()).append("\t"+st8B+st8Stg+st8E).toString());

					//System.out.println(end+"\n");
					xmlOut += end+"\n\n";
					prwrLFF.println((new StringBuilder()).append(end+"\n").toString());

					if ( y == inStgArr.length-1 ) {
						xmlOut += fin+"\n";
						outpath_sh = new File(inArgPath+"\\"+TotOut+"_"+dtStg+".xml");
						System.out.println("=============\nTotOut(211): "+TotOut+"\n");
						System.out.println("outpath_sh(211): "+outpath_sh+"\n");

						prwrsh = new PrintWriter(new FileWriter(outpath_sh));
						prwrsh.println((new StringBuilder()).append(xmlOut).toString());
						prwrsh.flush();
						prwrsh.close();

						prwrLFF.println((new StringBuilder()).append(xmlOut).toString());
						prwrLFF.flush();
						prwrLFF.close();
					}
				}

				outpath_sh = new File(inArgPath+"\\"+TotOut+"_"+dtStg+".txt");
				System.out.println("=============\nTotOut(222): "+TotOut+"\n");
				System.out.println("outpath_sh(222): "+outpath_sh+"\n");

				prwrsh = new PrintWriter(new FileWriter(outpath_sh));
				prwrsh.println((new StringBuilder()).append(AccOut).toString());
				prwrsh.flush();
				prwrsh.close();
			}

			//System.out.println("\n"+fin);
			prwrLFF.println((new StringBuilder()).append("\n"+fin).toString());

			prwrLFF.flush();
			prwrLFF.close();
			prwrList.flush();
			prwrList.close();
		}
		catch(Exception exception) {
			exception.printStackTrace();
		}
		System.exit(0);
	}

	/* remove leading whitespace */
	public static String ltrim(String source) {
		return source.replaceAll("^\\s+", "");
	}

	/* remove trailing whitespace */
	public static String rtrim(String source) {
		return source.replaceAll("\\s+$", "");
	}

	public static String lrtrim(String source){
		return ltrim(rtrim(source));
	}

	static String top		= "<?xml version=\"1.0\"?>\n<!DOCTYPE NmLoader SYSTEM \"standardX10.dtd\">\n<NmLoader>\n";
	static String beg		= "<csvAccessRule handler=\"wt.load.LoadUser.createAccessRule\">";

	static String usr		= "<csvuser></csvuser>";
	static String domB		= "<csvdomain>";
	static String domE		= "</csvdomain>";
	static String typidB	= "<csvtypeId>";
	static String typidE	= "</csvtypeId>";
	static String permB		= "<csvpermission>";
	static String permE		= "</csvpermission>";
	static String prinB		= "<csvprincipal>";
	static String prinE		= "</csvprincipal>";
	static String permlB	= "<csvpermissionList>";
	static String permlE	= "</csvpermissionList>";
	static String st8B		= "<csvstate>";
	static String st8E		= "</csvstate>";

	static String end		= "</csvAccessRule>";
	static String fin		= "</NmLoader>";

	static String permlListArr[] 	= {"-1=Full Control (All)","0=Read","1=Modify","2=Create","5=Delete","6=Administrative","7=Revise","8=New View Version","9=Change Permissions","10=Download","11=Modify Content","12=Change Domain","13=Create By Move","14=Change Context","15=Set State","16=Modify Identity"};

	static String[] inStgArr,condomStgArr,ruleStgArr,prinStgArr,conSp,conSpEq,condomSp;
	static String[] permlStgArr,tmpPermlArr,permTmpArr,domSp,domSpEq;
	static String condomStg,permlStg,inArgPath,TotOut,TdomOut,TconOut,AccOut,xmlOut,prinStg;

	static String conStg,conOut,domStg,domOut,dtStg;
	static int tmpInt;
	static PrintWriter prwrsh;
	static File inPath,outLFF,outpath_sh,outList;
	static List permlList;

	/*
	-1.value=Full Control (All)
	0.value=Read
	1.value=Modify
	2.value=Create
	5.value=Delete
	6.value=Administrative
	7.value=Revise
	8.value=New View Version
	9.value=Change Permissions
	10.value=Download
	11.value=Modify Content
	12.value=Change Domain
	13.value=Create By Move
	14.value=Change Context
	15.value=Set State
	16.value=Modify Identity

	<?xml version="1.0"?>
	<!DOCTYPE NmLoader SYSTEM "standardX05.dtd">
	<NmLoader>
	<csvAccessRule handler="wt.load.LoadUser.createAccessRule">
		<csvuser></csvuser>
		<csvdomain>[/wt.inf.container.OrgContainer=DJ_WC]/System</csvdomain>
		<csvtypeId>wt.inf.team.ContainerTeam</csvtypeId>
		<csvpermission>+</csvpermission>
		<csvprincipal>Shared Team Creator</csvprincipal>
		<csvpermissionList>0/1/2/5</csvpermissionList>
		<csvstate>ALL</csvstate>
	</csvAccessRule>
	</NmLoader>
	*/
}
