/*********************************************************************************************
 * Copyright 2004 - Volian Enterprises, Inc. All rights reserved.
 * Volian Enterprises - Proprietary Information - DO NOT COPY OR DISTRIBUTE
 * ------------------------------------------------------------------------------
 * $Workfile: PrivateProfile.cs $     $Revision: 1 $
 * $Author: Kathy $   $Date: 7/27/04 8:34a $
 *
 * $History: PrivateProfile.cs $
 * 
 * *****************  Version 1  *****************
 * User: Kathy        Date: 7/27/04    Time: 8:34a
 * Created in $/LibSource/Utils
 *********************************************************************************************/
using System;
using System.IO;
using System.Xml;
namespace Utils
{
	/// 
	/// PrivateProfile opens a private profile string and stores it's contents in an xml document.
	/// 
	public class PrivateProfile
	{
		private string ppName;
		private XmlDocument  ppXml;
		private XmlNode AddNode(XmlNode xParent, string sName, string sValue )
		{
			XmlNode nd;
			// Add a node 
			nd=xParent.OwnerDocument.CreateNode(System.Xml.XmlNodeType.Element,sName,"");
			xParent.AppendChild(nd);
			nd.Value=sValue;
			return nd;
		}
		private XmlNode AddNode(XmlNode xParent, string sName)
		{
			XmlNode nd;
			// Add a node 
			nd=xParent.OwnerDocument.CreateNode(System.Xml.XmlNodeType.Element,sName,"");
			xParent.AppendChild(nd);
			return nd;
		}
		private void AddAttribute(XmlNode xParent, string sName, string sValue )
		{
			XmlNode  xa=xParent.Attributes.GetNamedItem(sName);
			// bug fix. 09/15/03
			// If there was a space after an equal sign, that space character
			// was becomming part of the value string (reading the user.CFG file).
			// This was giving us a "Must have semi-colon" error message.
			// We now strip spaces before and after any Attribute that is written.
			sValue = sValue.Trim(' ');
			// Add an attribute 
			if(sValue=="")
			{
				if(xa != null)
				{
					xParent.Attributes.RemoveNamedItem(sName);
				}
			}
			else
			{
				if(xa == null)
				{
					xa = xParent.OwnerDocument.CreateNode(System.Xml.XmlNodeType.Attribute ,sName,"");
					xParent.Attributes.SetNamedItem(xa);
				}
				xa.Value=sValue;
			}
			
		}
		private XmlNode AddSection(XmlNode xParent, string sSection )
		{
			// Add a section [name]
			XmlNode nd =AddNode(xParent,"section");
			AddAttribute(nd,"name",sSection.Substring(1,sSection.IndexOf("]")-1));
			return nd;
		}
		private XmlNode AddSection_UC(XmlNode xParent, string sSection )
		{
			// Add a section [name]
			string name_uc = sSection.Substring(1,sSection.IndexOf("]")-1).ToUpper() + "__UC";
			XmlNode nd =AddNode(xParent,"sectionUC");
			AddAttribute(nd,"name",name_uc);
			return nd;
		}
		private void  AddComment(XmlNode xParent, string sComment)
		{
			
			if(xParent.ChildNodes.Count > 0)
			{
				XmlNode ndlast=xParent.ChildNodes.Item(xParent.ChildNodes.Count-1);
				if(ndlast.Name=="comment")
				{
					XmlNode xa = ndlast.Attributes.GetNamedItem("text");
					xa.Value=xa.Value + "\r\n" + sComment;
					return;
				}
			}
			// Add a comment text
			XmlNode nd =AddNode(xParent,"comment");			
			AddAttribute(nd,"text",sComment);
		}
		private void  AddLine(XmlNode xParent, string sLine)
		{
			// Add a comment text
			XmlNode nd =AddNode(xParent,"line");			
			AddAttribute(nd,"text",sLine);
		}
		private void AddParam(XmlNode xParent, string sParam)
		{
			int i = sParam.IndexOf("=");
			// add a param name=value
			string sName=sParam.Substring(0,i);
			string sValue=sParam.Substring(i+1);
			XmlNode nd =AddNode(xParent,"param");
			sName = sName.Trim(' ');
			AddAttribute(nd,"name",sName);
			AddAttribute(nd,"value",sValue);
//			AddAttribute(nd,"srchname",sName.ToUpper()+"__UC");
//			AddAttribute(nd,"value",sValue);
		}
		private void AddParam_UC(XmlNode xParent, string sParam)
		{
			int i = sParam.IndexOf("=");
			// add a param name=value
			string sName=sParam.Substring(0,i);
			string sValue=sParam.Substring(i+1);
			XmlNode nd =AddNode(xParent,"paramUC");
			sName = sName.Trim(' ');
			AddAttribute(nd,"name",sName.ToUpper()+"__UC");
			AddAttribute(nd,"value",sValue);
		}
		private void LoadXML()
		{
			string sLine;
			ppXml.LoadXml("");// initialize ppXml
			XmlNode  xmlTop=ppXml.DocumentElement;
			XmlNode  xmlNd=ppXml.DocumentElement;
			XmlNode  xmlNd_UC=ppXml.DocumentElement;
			StreamReader myReader = new StreamReader(ppName);// Open file
			while( (sLine = myReader.ReadLine())!= null)// read line-by-line
			{
				// add structure
				if(sLine.Length > 0)
				{
					switch (sLine.Substring(0,1))
					{
						case "[":
							xmlNd=AddSection(xmlTop, sLine);
							xmlNd_UC=AddSection_UC(xmlTop, sLine);
							break;
						case ";":
							AddComment(xmlNd, sLine);
							break;
						default:
							if(sLine.IndexOf("=") >= 0)
							{
								AddParam(xmlNd, sLine);
								AddParam_UC(xmlNd_UC, sLine);
							}
							else
							{
								AddLine(xmlNd, sLine);
							}
							break;
					}
				}
			}
			myReader.Close();
			// close file
		}
		public PrivateProfile(string sFileName)
		{
			ppName=sFileName; // Store file name
			ppXml= new XmlDocument();// Allocate XML
			LoadXML();// Load XML
		}
		~PrivateProfile()
		{
			// Clean-up
			// 
		}
		public string PrettyNode(XmlNode nd,int level)
		{
			string retval="";
			string prefix=new string(' ',level*2);
			if(nd.ChildNodes.Count > 0)
			{
				retval = prefix + "<" + nd.Name;
				for(int i=0;i";
				for(int i=0;i";
			}
			else
			{
				retval = prefix + "<" + nd.Name;
				for(int i=0;i";
			}
			return retval;
		}
		public string PrettyXML()
		{
			return PrettyNode(ppXml.DocumentElement,0);
		}
		public XmlDocument XML()
		{
			// return XML Document
			return ppXml;
		}
		public override string ToString()
		{
			// return string
			return "";
		}
		public void Save()
		{
			SaveAs(ppName);
		}
		public void SaveAs(string sName)
		{
		}
		public string Attr(string sPath)
		{
			string retval="";
			XmlNode xn = ppXml.SelectSingleNode(sPath);
			if(xn != null)
			{
				string quots = xn.Value;
				if (quots.Substring(0,1)=="\"" && quots.Substring(quots.Length-1,1)=="\"")
					retval = quots.Substring(1,quots.Length-2);
				else
					retval=xn.Value;
			}
			return retval;
		}
		public string Attr(string sSection, string sParameter)
		{
			string findstr = "/ini/sectionUC[@name='" + sSection.ToUpper() + "__UC']/paramUC[@name='" + sParameter.ToUpper() + "__UC']/@value";
			return Attr(findstr);
		}
	}
}