diff options
author | MW | 2007-05-26 13:40:19 +0000 |
---|---|---|
committer | MW | 2007-05-26 13:40:19 +0000 |
commit | 3436961bb5c01d659d09be134368f4f69460cef9 (patch) | |
tree | 3753ba4d7818df2a6bce0bbe863ff033cdfd568a /Common/OpenSim.GenericConfig/Xml/XmlConfig.cs | |
download | opensim-SC-3436961bb5c01d659d09be134368f4f69460cef9.zip opensim-SC-3436961bb5c01d659d09be134368f4f69460cef9.tar.gz opensim-SC-3436961bb5c01d659d09be134368f4f69460cef9.tar.bz2 opensim-SC-3436961bb5c01d659d09be134368f4f69460cef9.tar.xz |
Start of rewrite 5279!
Diffstat (limited to 'Common/OpenSim.GenericConfig/Xml/XmlConfig.cs')
-rw-r--r-- | Common/OpenSim.GenericConfig/Xml/XmlConfig.cs | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/Common/OpenSim.GenericConfig/Xml/XmlConfig.cs b/Common/OpenSim.GenericConfig/Xml/XmlConfig.cs new file mode 100644 index 0000000..62e3cbf --- /dev/null +++ b/Common/OpenSim.GenericConfig/Xml/XmlConfig.cs | |||
@@ -0,0 +1,109 @@ | |||
1 | using System; | ||
2 | using System.Collections.Generic; | ||
3 | using System.Text; | ||
4 | using System.Xml; | ||
5 | using OpenSim.Framework.Interfaces; | ||
6 | |||
7 | namespace OpenSim.GenericConfig | ||
8 | { | ||
9 | public class XmlConfig : IGenericConfig | ||
10 | { | ||
11 | private XmlDocument doc; | ||
12 | private XmlNode rootNode; | ||
13 | private XmlNode configNode; | ||
14 | private string fileName; | ||
15 | private bool createdFile = false; | ||
16 | |||
17 | public XmlConfig(string filename) | ||
18 | { | ||
19 | fileName = filename; | ||
20 | } | ||
21 | |||
22 | public void LoadData() | ||
23 | { | ||
24 | doc = new XmlDocument(); | ||
25 | try | ||
26 | { | ||
27 | if (System.IO.File.Exists(fileName)) | ||
28 | { | ||
29 | XmlTextReader reader = new XmlTextReader(fileName); | ||
30 | reader.WhitespaceHandling = WhitespaceHandling.None; | ||
31 | doc.Load(reader); | ||
32 | reader.Close(); | ||
33 | } | ||
34 | else | ||
35 | { | ||
36 | createdFile = true; | ||
37 | rootNode = doc.CreateNode(XmlNodeType.Element, "Root", ""); | ||
38 | doc.AppendChild(rootNode); | ||
39 | configNode = doc.CreateNode(XmlNodeType.Element, "Config", ""); | ||
40 | rootNode.AppendChild(configNode); | ||
41 | } | ||
42 | |||
43 | } | ||
44 | catch (Exception e) | ||
45 | { | ||
46 | Console.WriteLine(e.Message); | ||
47 | return; | ||
48 | } | ||
49 | try | ||
50 | { | ||
51 | rootNode = doc.FirstChild; | ||
52 | if (rootNode.Name != "Root") | ||
53 | throw new Exception("Error: Invalid .xml File. Missing <Root>"); | ||
54 | |||
55 | configNode = rootNode.FirstChild; | ||
56 | if (configNode.Name != "Config") | ||
57 | throw new Exception("Error: Invalid .xml File. <Root> first child should be <Config>"); | ||
58 | |||
59 | } | ||
60 | catch (Exception e) | ||
61 | { | ||
62 | Console.WriteLine(e.Message); | ||
63 | } | ||
64 | if (createdFile) | ||
65 | { | ||
66 | this.Commit(); | ||
67 | } | ||
68 | } | ||
69 | |||
70 | public string GetAttribute(string attributeName) | ||
71 | { | ||
72 | string result = ""; | ||
73 | if (configNode.Attributes[attributeName] != null) | ||
74 | { | ||
75 | result = ((XmlAttribute)configNode.Attributes.GetNamedItem(attributeName)).Value; | ||
76 | } | ||
77 | return result; | ||
78 | } | ||
79 | |||
80 | public bool SetAttribute(string attributeName, string attributeValue) | ||
81 | { | ||
82 | if (configNode.Attributes[attributeName] != null) | ||
83 | { | ||
84 | ((XmlAttribute)configNode.Attributes.GetNamedItem(attributeName)).Value = attributeValue; | ||
85 | } | ||
86 | else | ||
87 | { | ||
88 | XmlAttribute attri; | ||
89 | attri = doc.CreateAttribute(attributeName); | ||
90 | attri.Value = attributeValue; | ||
91 | configNode.Attributes.Append(attri); | ||
92 | } | ||
93 | return true; | ||
94 | } | ||
95 | |||
96 | public void Commit() | ||
97 | { | ||
98 | doc.Save(fileName); | ||
99 | } | ||
100 | |||
101 | public void Close() | ||
102 | { | ||
103 | configNode = null; | ||
104 | rootNode = null; | ||
105 | doc = null; | ||
106 | } | ||
107 | |||
108 | } | ||
109 | } | ||