2007-04-02 12:56:35 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Text;
|
|
|
|
using System.Xml;
|
|
|
|
using OpenSim.Framework.Interfaces;
|
|
|
|
|
|
|
|
namespace OpenSim.GenericConfig
|
|
|
|
{
|
|
|
|
public class XmlConfig : IGenericConfig
|
|
|
|
{
|
|
|
|
private XmlDocument doc;
|
|
|
|
private XmlNode rootNode;
|
|
|
|
private XmlNode configNode;
|
|
|
|
private string fileName;
|
2007-04-02 15:48:01 +00:00
|
|
|
private bool createdFile = false;
|
2007-04-02 12:56:35 +00:00
|
|
|
|
|
|
|
public XmlConfig(string filename)
|
|
|
|
{
|
|
|
|
fileName = filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void LoadData()
|
|
|
|
{
|
|
|
|
doc = new XmlDocument();
|
|
|
|
try
|
|
|
|
{
|
2007-04-02 15:48:01 +00:00
|
|
|
if (System.IO.File.Exists(fileName))
|
|
|
|
{
|
|
|
|
XmlTextReader reader = new XmlTextReader(fileName);
|
|
|
|
reader.WhitespaceHandling = WhitespaceHandling.None;
|
|
|
|
doc.Load(reader);
|
|
|
|
reader.Close();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
createdFile = true;
|
|
|
|
rootNode = doc.CreateNode(XmlNodeType.Element, "Root", "");
|
|
|
|
doc.AppendChild(rootNode);
|
|
|
|
configNode = doc.CreateNode(XmlNodeType.Element, "Config", "");
|
|
|
|
rootNode.AppendChild(configNode);
|
|
|
|
}
|
|
|
|
|
2007-04-02 12:56:35 +00:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Console.WriteLine(e.Message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
rootNode = doc.FirstChild;
|
|
|
|
if (rootNode.Name != "Root")
|
|
|
|
throw new Exception("Error: Invalid .xml File. Missing <Root>");
|
|
|
|
|
|
|
|
configNode = rootNode.FirstChild;
|
|
|
|
if (configNode.Name != "Config")
|
|
|
|
throw new Exception("Error: Invalid .xml File. <Root> first child should be <Config>");
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Console.WriteLine(e.Message);
|
|
|
|
}
|
2007-04-02 15:48:01 +00:00
|
|
|
if (createdFile)
|
|
|
|
{
|
|
|
|
this.Commit();
|
|
|
|
}
|
2007-04-02 12:56:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public string GetAttribute(string attributeName)
|
|
|
|
{
|
|
|
|
string result = "";
|
|
|
|
if (configNode.Attributes[attributeName] != null)
|
|
|
|
{
|
|
|
|
result = ((XmlAttribute)configNode.Attributes.GetNamedItem(attributeName)).Value;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool SetAttribute(string attributeName, string attributeValue)
|
|
|
|
{
|
|
|
|
if (configNode.Attributes[attributeName] != null)
|
|
|
|
{
|
|
|
|
configNode.Attributes.GetNamedItem(attributeName).Value = attributeValue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
XmlAttribute attri;
|
|
|
|
attri = doc.CreateAttribute(attributeName);
|
|
|
|
attri.Value = attributeValue;
|
|
|
|
configNode.Attributes.Append(attri);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Commit()
|
|
|
|
{
|
|
|
|
doc.Save(fileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Close()
|
|
|
|
{
|
|
|
|
configNode = null;
|
|
|
|
rootNode = null;
|
|
|
|
doc = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|