
struct read_xml
(
	str_xmlStruct,
	report 					= false,
	
	xmldoc,
	docEle,
	rootName 				= undefined,
	
	rootChildrenIDX 		= 0,
	rootChildrenIDX_2 	= 0,
	
	rootIDX 					= undefined,
	depth 					= 0,
	idx 						= 0,
	depthArr 				= #(),
	numRecurse 			= 0,
	
	_xml_template 		= #(),
	_xml 						= #(),	
	
	attribName,
	attribValue,
	
	attribN,
	attribV,
	
	--format indents for tree level
	fn format_tabs depth =
	(
		for i=1 to depth do format "\t" 
	),
	
	fn get_Nested_def \
								&theNode: =
	(
		local attribN 		= undefined
		local attribV 		= undefined		
		
		for n=0 to theNode.attributes.count-1 do
		(
			attribN 				= theNode.attributes.itemof[n].name
			attribV 				= theNode.attributes.itemof[n].value
			
			local rootP 				= ::g_dirpath
			
			local refPath 				= pathConfig.resolvePathSymbols (pathConfig.appendpath rootP attribV) --construct the final path
			
			if ::rollout_Report.chk_XML_Report.state then
			(			
				format "attribN: %\n" attribN
				format "attribV: %\n" attribV
			)
			
			--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//
			if attribN == "definition" AND (doesfileexist refPath) then
			(
				if (doesfileexist refPath) then
				(
					local arrDef 						= #() --reset array
			
					str_read_xml 				= read_xml()
					str_read_xml.report 		= true
			
					--read definition
					arrDef = str_read_xml.main \
															xmlPath: 	&refPath \
															xml_arr: 	&str_read_xml._xml
					
					--format "attribV:% \n" refPath
					--Append VARIANT_MESH_REFERENCE_array arrDef
				)
			)
			--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//			
		)		
	),
	
	--get the attributes
	fn get_Attribs \
						&theNode:  \
						&xml_arr: =
	(
		local attribName 			= undefined
		local attribValue 			= undefined
		
		for n=0 to theNode.attributes.count-1 do
		(
			attribName 				= theNode.attributes.itemof[n].name
			attribValue 				= theNode.attributes.itemof[n].value			
			
			Append 		str_xmlStruct.attribs[1] 		attribName
			Append 		str_xmlStruct.attribs[2] 		attribValue
			
			if ::rollout_Report.chk_XML_Report.state then
			(
				----------------depth format
				format_tabs depth
				format " --m_attribName: %\n" attribName
				----------------depth format
				format_tabs depth
				format " --m_attribValue: %\n" attribValue
			)
		)
	),
	
	--recurse function
	fn recurse \
					&children: \
					&xml_arr: =
	(
		numRecurse += 1		
		
		----------------depth add
		depth += 1										
		
		local children 				= children.childnodes	
		
		local childNum2 			= undefined --the count of the child
		local childName 			= undefined
		local parentNode 		= undefined
		local parentName 		= undefined
		
		for x=0 to children.count-1 do
		(			
			------------------------------------------------------------------------------------
			if x == 0 then
			(
				rootChildrenIDX_2 += 1
			)
			------------------------------------------------------------------------------------
			
			local child 					= children.itemof[x]
			local childNum2 			= children.itemof[x].childnodes.count --the count of the child
			local childName 			= child.name
			local parentNode 		= child.parentNode
			local parentName 		= child.parentNode.name
			
			----array append----
			str_xmlStruct 							= xmlStruct() --create instance
				str_xmlStruct.rootName 		= rootName
				str_xmlStruct.numChildren 	= childNum2
				str_xmlStruct.nodeName 		= childName
				str_xmlStruct.parentName 	= parentName
				str_xmlStruct.depth 				= depth
			
			/*
			--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//
			if childName == "VARIANT_MESH_REFERENCE" then
			(
				--get attribs
				get_Nested_def \												
										theNode: &child			
			)
			--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//--//
			--*/

			
				Append str_xmlStruct.parentIDX rootIDX --always zero, this is the root index
					Append str_xmlStruct.parentIDX idx --children directly under root
						Append str_xmlStruct.parentIDX rootChildrenIDX
							if depth > 2 then
							(
								Append str_xmlStruct.parentIDX (rootChildrenIDX_2) --idx
								Append str_xmlStruct.parentIDX (x+1) --children count
							)			
			
			if ::rollout_Report.chk_XML_Report.state then
			(
				----------------depth format
				format_tabs depth
				format "\n"
				
				--print tree index level
				format_tabs depth
				
				for d=1 to depth do
				(				
					format "%." str_xmlStruct.parentIDX[d]					
				)				

				--format the name of the node
				format "%. Name: %\t(parent: %)\n" (x+1) childName parentName
			)
	
			--get attribs
			get_Attribs \
							theNode: 		&child \ 
							xml_arr: 		&xml_arr		
			
			Append xml_arr str_xmlStruct
			
			--the recurse 2
			recurse \
						children: 	&child \
						xml_arr: 	&xml_arr			

		)
		----------------depth substract
		depth -= 1
	),
	
	fn csharp_assembly \
								&xmlPath: =
	(
		if NOT Binary_XML_check xmlPath then
		(		
			--------------------------------Create the dotNet xmlDoc----------------------------
			--Load the xml assemply
			dotNet.loadAssembly "system.xml"
			
			--Create an xml document object.
			local xmldoc = dotnetobject "System.XML.XMLDocument"

			--This is the Root of the XML
			if template != "" then
			(
				xmldoc.Load xmlPath
			)
			return xmldoc.documentElement
		)
		else
		(
			format "Not a valid XML File - this is an xmlB (binary XML) file! - use the binary reader instead \n"
		)
	),
	
	fn main \
				&xmlPath: \
				&xml_arr: =
	(
		if NOT Binary_XML_check xmlPath then
		(
			local docEle = (csharp_assembly xmlPath: &xmlPath)
			
			--read the root and get inside the xml to read the rest via the recurse function
			if docEle !=undefined then
			(
				xml_arr = #()			
				
				local rootIDX = 0
				
				if ::rollout_Report.chk_XML_Report.state then
				(
					rootName = docEle.name
					format "%. Root: %\n" rootIDX rootName --->>>
				)
				
					---- array append ( this is the root - only one ofcourse) ----
					str_xmlStruct 						= xmlStruct()
						str_xmlStruct.rootName 	= rootName
						str_xmlStruct.isRoot 		= true
						Append 		str_xmlStruct.parentIDX 		rootIDX
					Append 		xml_arr 		str_xmlStruct
				
				--first pass the root to get its children (if there arent any children to the root, we cannot go in here)
				if docEle.childnodes.count > 0 then
				(
					----------------depthadd
					depth += 1
					
					--recurse through each of the root children -- it will go as deep as it needs to
					for n=0 to docEle.childnodes.count-1 do
					(
						child 				= docEle.childnodes.itemof[n]
						childName 		= child.name
						
						if ::rollout_Report.chk_XML_Report.state then
						(
							--REPORT
							----------------depth format
							format_tabs depth
							format "\n"
							----------------depth format
							format_tabs depth
							format "%.%. Name: %\n" rootIDX (n+1) childName --->>>
						)
						
						idx += 1
						
						----array append----
						str_xmlStruct 							= xmlStruct()
							str_xmlStruct.nodeName 		= childName
							str_xmlStruct.numChildren 	= docEle.childnodes.count						
							Append 		str_xmlStruct.children 		childName						
							str_xmlStruct.parentName 	= child.parentNode.name
							str_xmlStruct.depth 			= depth
						
							Append str_xmlStruct.parentIDX rootIDX
							Append str_xmlStruct.parentIDX idx
						
							if rootChildrenIDX > 0 then
							(
								Append str_xmlStruct.parentIDX rootChildrenIDX
							)

							--get attribs
							get_Attribs \
											theNode: 		&child \ 
											xml_arr: 		&xml_arr					

						Append xml_arr str_xmlStruct
						
						rootChildrenIDX += 1 --the index of the children directly under the Root
						
						recurse \
									children: 	&child \
									xml_arr: 	&xml_arr
					)
					depth -= 1
				)
			)
			
			return xml_arr;
		)
		else
		(
			format "Not a valid XML File - this is an xmlB (binary XML) file! - use the binary reader instead \n"
			return xml_arr = #();
		)
	)
)

struct xml_PROCESSOR
(
	--format indents for tree level
	fn format_tabs depth: =
	(
		for i=1 to depth do format "\t" 
	),	
	
	fn printNodes \
						&xml_arr: =
	(
		for i=1 to xml_arr.count do
		(
			format_tabs depth:xml_arr[i].depth
				
			if (stricmp (xml_arr[i].parentName as string) "Material") == 0 then
			(
				print xml_arr[i]
			)
		)		

	)
)





