diff options
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Framework/DAMap.cs | 273 |
1 files changed, 273 insertions, 0 deletions
diff --git a/OpenSim/Framework/DAMap.cs b/OpenSim/Framework/DAMap.cs new file mode 100644 index 0000000..64cea77 --- /dev/null +++ b/OpenSim/Framework/DAMap.cs | |||
@@ -0,0 +1,273 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.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 OpenSimulator 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; | ||
30 | using System.Collections.Generic; | ||
31 | using System.IO; | ||
32 | using System.Text; | ||
33 | using System.Xml; | ||
34 | using System.Xml.Schema; | ||
35 | using System.Xml.Serialization; | ||
36 | using OpenMetaverse; | ||
37 | using OpenMetaverse.StructuredData; | ||
38 | |||
39 | namespace OpenSim.Framework | ||
40 | { | ||
41 | /// <summary> | ||
42 | /// This class stores and retrieves dynamic attributes. | ||
43 | /// </summary> | ||
44 | /// <remarks> | ||
45 | /// Modules that want to use dynamic attributes need to do so in a private data store | ||
46 | /// which is accessed using a unique name. DAMap provides access to the data stores, | ||
47 | /// each of which is an OSDMap. Modules are free to store any type of data they want | ||
48 | /// within their data store. However, avoid storing large amounts of data because that | ||
49 | /// would slow down database access. | ||
50 | /// </remarks> | ||
51 | public class DAMap : IDictionary<string, OSDMap>, IXmlSerializable | ||
52 | { | ||
53 | private static readonly int MIN_STORE_NAME_LENGTH = 4; | ||
54 | |||
55 | protected OSDMap m_map; | ||
56 | |||
57 | public DAMap() { m_map = new OSDMap(); } | ||
58 | |||
59 | public XmlSchema GetSchema() { return null; } | ||
60 | |||
61 | public static DAMap FromXml(string rawXml) | ||
62 | { | ||
63 | DAMap map = new DAMap(); | ||
64 | map.ReadXml(rawXml); | ||
65 | return map; | ||
66 | } | ||
67 | |||
68 | public void ReadXml(string rawXml) | ||
69 | { | ||
70 | // System.Console.WriteLine("Trying to deserialize [{0}]", rawXml); | ||
71 | |||
72 | lock (this) | ||
73 | m_map = (OSDMap)OSDParser.DeserializeLLSDXml(rawXml); | ||
74 | } | ||
75 | |||
76 | // WARNING: this is temporary for experimentation only, it will be removed!!!! | ||
77 | public OSDMap TopLevelMap | ||
78 | { | ||
79 | get { return m_map; } | ||
80 | set { m_map = value; } | ||
81 | } | ||
82 | |||
83 | |||
84 | public void ReadXml(XmlReader reader) | ||
85 | { | ||
86 | ReadXml(reader.ReadInnerXml()); | ||
87 | } | ||
88 | |||
89 | public string ToXml() | ||
90 | { | ||
91 | lock (this) | ||
92 | return OSDParser.SerializeLLSDXmlString(m_map); | ||
93 | } | ||
94 | |||
95 | public void WriteXml(XmlWriter writer) | ||
96 | { | ||
97 | writer.WriteRaw(ToXml()); | ||
98 | } | ||
99 | |||
100 | public void CopyFrom(DAMap other) | ||
101 | { | ||
102 | // Deep copy | ||
103 | |||
104 | string data = null; | ||
105 | lock (other) | ||
106 | { | ||
107 | if (other.Count > 0) | ||
108 | { | ||
109 | data = OSDParser.SerializeLLSDXmlString(other.m_map); | ||
110 | } | ||
111 | } | ||
112 | |||
113 | lock (this) | ||
114 | { | ||
115 | if (data == null) | ||
116 | Clear(); | ||
117 | else | ||
118 | m_map = (OSDMap)OSDParser.DeserializeLLSDXml(data); | ||
119 | } | ||
120 | } | ||
121 | |||
122 | /// <summary> | ||
123 | /// Returns the number of data stores. | ||
124 | /// </summary> | ||
125 | public int Count { get { lock (this) { return m_map.Count; } } } | ||
126 | |||
127 | public bool IsReadOnly { get { return false; } } | ||
128 | |||
129 | /// <summary> | ||
130 | /// Returns the names of the data stores. | ||
131 | /// </summary> | ||
132 | public ICollection<string> Keys { get { lock (this) { return m_map.Keys; } } } | ||
133 | |||
134 | /// <summary> | ||
135 | /// Returns all the data stores. | ||
136 | /// </summary> | ||
137 | public ICollection<OSDMap> Values | ||
138 | { | ||
139 | get | ||
140 | { | ||
141 | lock (this) | ||
142 | { | ||
143 | List<OSDMap> stores = new List<OSDMap>(m_map.Count); | ||
144 | foreach (OSD llsd in m_map.Values) | ||
145 | stores.Add((OSDMap)llsd); | ||
146 | return stores; | ||
147 | } | ||
148 | } | ||
149 | } | ||
150 | |||
151 | /// <summary> | ||
152 | /// Gets or sets one data store. | ||
153 | /// </summary> | ||
154 | /// <param name="key">Store name</param> | ||
155 | /// <returns></returns> | ||
156 | public OSDMap this[string key] | ||
157 | { | ||
158 | get | ||
159 | { | ||
160 | OSD llsd; | ||
161 | |||
162 | lock (this) | ||
163 | { | ||
164 | if (m_map.TryGetValue(key, out llsd)) | ||
165 | return (OSDMap)llsd; | ||
166 | else | ||
167 | return null; | ||
168 | } | ||
169 | } | ||
170 | |||
171 | set | ||
172 | { | ||
173 | ValidateKey(key); | ||
174 | lock (this) | ||
175 | m_map[key] = value; | ||
176 | } | ||
177 | } | ||
178 | |||
179 | /// <summary> | ||
180 | /// Validate the key used for storing separate data stores. | ||
181 | /// </summary> | ||
182 | /// <param name='key'></param> | ||
183 | private static void ValidateKey(string key) | ||
184 | { | ||
185 | if (key.Length < MIN_STORE_NAME_LENGTH) | ||
186 | throw new Exception("Minimum store name length is " + MIN_STORE_NAME_LENGTH); | ||
187 | } | ||
188 | |||
189 | public bool ContainsKey(string key) | ||
190 | { | ||
191 | lock (this) | ||
192 | return m_map.ContainsKey(key); | ||
193 | } | ||
194 | |||
195 | public void Add(string key, OSDMap store) | ||
196 | { | ||
197 | ValidateKey(key); | ||
198 | lock (this) | ||
199 | m_map.Add(key, store); | ||
200 | } | ||
201 | |||
202 | public void Add(KeyValuePair<string, OSDMap> kvp) | ||
203 | { | ||
204 | ValidateKey(kvp.Key); | ||
205 | lock (this) | ||
206 | m_map.Add(kvp.Key, kvp.Value); | ||
207 | } | ||
208 | |||
209 | public bool Remove(string key) | ||
210 | { | ||
211 | lock (this) | ||
212 | return m_map.Remove(key); | ||
213 | } | ||
214 | |||
215 | public bool TryGetValue(string key, out OSDMap store) | ||
216 | { | ||
217 | lock (this) | ||
218 | { | ||
219 | OSD llsd; | ||
220 | if (m_map.TryGetValue(key, out llsd)) | ||
221 | { | ||
222 | store = (OSDMap)llsd; | ||
223 | return true; | ||
224 | } | ||
225 | else | ||
226 | { | ||
227 | store = null; | ||
228 | return false; | ||
229 | } | ||
230 | } | ||
231 | } | ||
232 | |||
233 | public void Clear() | ||
234 | { | ||
235 | lock (this) | ||
236 | m_map.Clear(); | ||
237 | } | ||
238 | |||
239 | public bool Contains(KeyValuePair<string, OSDMap> kvp) | ||
240 | { | ||
241 | lock (this) | ||
242 | return m_map.ContainsKey(kvp.Key); | ||
243 | } | ||
244 | |||
245 | public void CopyTo(KeyValuePair<string, OSDMap>[] array, int index) | ||
246 | { | ||
247 | throw new NotImplementedException(); | ||
248 | } | ||
249 | |||
250 | public bool Remove(KeyValuePair<string, OSDMap> kvp) | ||
251 | { | ||
252 | lock (this) | ||
253 | return m_map.Remove(kvp.Key); | ||
254 | } | ||
255 | |||
256 | public System.Collections.IDictionaryEnumerator GetEnumerator() | ||
257 | { | ||
258 | lock (this) | ||
259 | return m_map.GetEnumerator(); | ||
260 | } | ||
261 | |||
262 | IEnumerator<KeyValuePair<string, OSDMap>> IEnumerable<KeyValuePair<string, OSDMap>>.GetEnumerator() | ||
263 | { | ||
264 | return null; | ||
265 | } | ||
266 | |||
267 | IEnumerator IEnumerable.GetEnumerator() | ||
268 | { | ||
269 | lock (this) | ||
270 | return m_map.GetEnumerator(); | ||
271 | } | ||
272 | } | ||
273 | } \ No newline at end of file | ||