XmlConfig.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. */
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Text;
  31. using System.Xml;
  32. using OpenSim.Framework.Interfaces;
  33. namespace OpenSim.GenericConfig
  34. {
  35. public class XmlConfig : IGenericConfig
  36. {
  37. private XmlDocument doc;
  38. private XmlNode rootNode;
  39. private XmlNode configNode;
  40. private string fileName;
  41. private bool createdFile = false;
  42. public XmlConfig(string filename)
  43. {
  44. fileName = filename;
  45. }
  46. public void LoadData()
  47. {
  48. doc = new XmlDocument();
  49. try
  50. {
  51. if (System.IO.File.Exists(fileName))
  52. {
  53. XmlTextReader reader = new XmlTextReader(fileName);
  54. reader.WhitespaceHandling = WhitespaceHandling.None;
  55. doc.Load(reader);
  56. reader.Close();
  57. }
  58. else
  59. {
  60. createdFile = true;
  61. rootNode = doc.CreateNode(XmlNodeType.Element, "Root", "");
  62. doc.AppendChild(rootNode);
  63. configNode = doc.CreateNode(XmlNodeType.Element, "Config", "");
  64. rootNode.AppendChild(configNode);
  65. }
  66. }
  67. catch (Exception e)
  68. {
  69. Console.WriteLine(e.Message);
  70. return;
  71. }
  72. try
  73. {
  74. rootNode = doc.FirstChild;
  75. if (rootNode.Name != "Root")
  76. throw new Exception("Error: Invalid .xml File. Missing <Root>");
  77. configNode = rootNode.FirstChild;
  78. if (configNode.Name != "Config")
  79. throw new Exception("Error: Invalid .xml File. <Root> first child should be <Config>");
  80. }
  81. catch (Exception e)
  82. {
  83. Console.WriteLine(e.Message);
  84. }
  85. if (createdFile)
  86. {
  87. this.Commit();
  88. }
  89. }
  90. public string GetAttribute(string attributeName)
  91. {
  92. string result = "";
  93. if (configNode.Attributes[attributeName] != null)
  94. {
  95. result = ((XmlAttribute)configNode.Attributes.GetNamedItem(attributeName)).Value;
  96. }
  97. return result;
  98. }
  99. public bool SetAttribute(string attributeName, string attributeValue)
  100. {
  101. if (configNode.Attributes[attributeName] != null)
  102. {
  103. ((XmlAttribute)configNode.Attributes.GetNamedItem(attributeName)).Value = attributeValue;
  104. }
  105. else
  106. {
  107. XmlAttribute attri;
  108. attri = doc.CreateAttribute(attributeName);
  109. attri.Value = attributeValue;
  110. configNode.Attributes.Append(attri);
  111. }
  112. return true;
  113. }
  114. public void Commit()
  115. {
  116. doc.Save(fileName);
  117. }
  118. public void Close()
  119. {
  120. configNode = null;
  121. rootNode = null;
  122. doc = null;
  123. }
  124. }
  125. }