diff options
Diffstat (limited to 'OpenSim/Framework/Configuration/XML/XmlConfiguration.cs')
-rw-r--r-- | OpenSim/Framework/Configuration/XML/XmlConfiguration.cs | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/OpenSim/Framework/Configuration/XML/XmlConfiguration.cs b/OpenSim/Framework/Configuration/XML/XmlConfiguration.cs new file mode 100644 index 0000000..e56c657 --- /dev/null +++ b/OpenSim/Framework/Configuration/XML/XmlConfiguration.cs | |||
@@ -0,0 +1,124 @@ | |||
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.IO; | ||
30 | using System.Xml; | ||
31 | |||
32 | using OpenSim.Framework.Interfaces; | ||
33 | using OpenSim.Framework.Configuration.Interfaces; | ||
34 | |||
35 | namespace OpenSim.Framework.Configuration | ||
36 | { | ||
37 | public class XmlConfiguration : IGenericConfig | ||
38 | { | ||
39 | private XmlDocument doc; | ||
40 | private XmlNode rootNode; | ||
41 | private XmlNode configNode; | ||
42 | private string fileName; | ||
43 | private bool createdFile = false; | ||
44 | |||
45 | public void SetFileName(string file) | ||
46 | { | ||
47 | fileName = file; | ||
48 | } | ||
49 | |||
50 | public void LoadData() | ||
51 | { | ||
52 | doc = new XmlDocument(); | ||
53 | |||
54 | if (File.Exists(fileName)) | ||
55 | { | ||
56 | XmlTextReader reader = new XmlTextReader(fileName); | ||
57 | reader.WhitespaceHandling = WhitespaceHandling.None; | ||
58 | doc.Load(reader); | ||
59 | reader.Close(); | ||
60 | } | ||
61 | else | ||
62 | { | ||
63 | createdFile = true; | ||
64 | rootNode = doc.CreateNode(XmlNodeType.Element, "Root", ""); | ||
65 | doc.AppendChild(rootNode); | ||
66 | configNode = doc.CreateNode(XmlNodeType.Element, "Config", ""); | ||
67 | rootNode.AppendChild(configNode); | ||
68 | } | ||
69 | |||
70 | |||
71 | rootNode = doc.FirstChild; | ||
72 | if (rootNode.Name != "Root") | ||
73 | throw new Exception("Error: Invalid .xml File. Missing <Root>"); | ||
74 | |||
75 | configNode = rootNode.FirstChild; | ||
76 | if (configNode.Name != "Config") | ||
77 | throw new Exception("Error: Invalid .xml File. <Root> first child should be <Config>"); | ||
78 | |||
79 | if (createdFile) | ||
80 | { | ||
81 | this.Commit(); | ||
82 | } | ||
83 | } | ||
84 | |||
85 | public string GetAttribute(string attributeName) | ||
86 | { | ||
87 | string result = null; | ||
88 | if (configNode.Attributes[attributeName] != null) | ||
89 | { | ||
90 | result = ((XmlAttribute)configNode.Attributes.GetNamedItem(attributeName)).Value; | ||
91 | } | ||
92 | return result; | ||
93 | } | ||
94 | |||
95 | public bool SetAttribute(string attributeName, string attributeValue) | ||
96 | { | ||
97 | if (configNode.Attributes[attributeName] != null) | ||
98 | { | ||
99 | ((XmlAttribute)configNode.Attributes.GetNamedItem(attributeName)).Value = attributeValue; | ||
100 | } | ||
101 | else | ||
102 | { | ||
103 | XmlAttribute attri; | ||
104 | attri = doc.CreateAttribute(attributeName); | ||
105 | attri.Value = attributeValue; | ||
106 | configNode.Attributes.Append(attri); | ||
107 | } | ||
108 | return true; | ||
109 | } | ||
110 | |||
111 | public void Commit() | ||
112 | { | ||
113 | doc.Save(fileName); | ||
114 | } | ||
115 | |||
116 | public void Close() | ||
117 | { | ||
118 | configNode = null; | ||
119 | rootNode = null; | ||
120 | doc = null; | ||
121 | } | ||
122 | |||
123 | } | ||
124 | } | ||