clockwerk-opensim-stable – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 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.IO;
31 using System.Reflection;
32 using System.Xml;
33  
34 namespace OpenSim.Framework.Capabilities
35 {
36 public class LLSDHelpers
37 {
38 // private static readonly log4net.ILog m_log
39 // = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
40  
41 public static string SerialiseLLSDReply(object obj)
42 {
43 StringWriter sw = new StringWriter();
44 XmlTextWriter writer = new XmlTextWriter(sw);
45 writer.Formatting = Formatting.None;
46 writer.WriteStartElement(String.Empty, "llsd", String.Empty);
47 SerializeOSDType(writer, obj);
48 writer.WriteEndElement();
49 writer.Close();
50  
51 //m_log.DebugFormat("[LLSD Helpers]: Generated serialized LLSD reply {0}", sw.ToString());
52  
53 return sw.ToString();
54 }
55  
56 private static void SerializeOSDType(XmlTextWriter writer, object obj)
57 {
58 Type myType = obj.GetType();
59 LLSDType[] llsdattributes = (LLSDType[]) myType.GetCustomAttributes(typeof (LLSDType), false);
60 if (llsdattributes.Length > 0)
61 {
62 switch (llsdattributes[0].ObjectType)
63 {
64 case "MAP":
65 writer.WriteStartElement(String.Empty, "map", String.Empty);
66 FieldInfo[] fields = myType.GetFields();
67 for (int i = 0; i < fields.Length; i++)
68 {
69 if (fields[i] != null && fields[i].GetValue(obj) != null)
70 {
71 object fieldValue = fields[i].GetValue(obj);
72 LLSDType[] fieldAttributes =
73 (LLSDType[]) fieldValue.GetType().GetCustomAttributes(typeof (LLSDType), false);
74 if (fieldAttributes.Length > 0)
75 {
76 writer.WriteStartElement(String.Empty, "key", String.Empty);
77 string fieldName = fields[i].Name;
78 fieldName = fieldName.Replace("___", "-");
79 writer.WriteString(fieldName);
80 writer.WriteEndElement();
81 SerializeOSDType(writer, fieldValue);
82 }
83 else
84 {
85 writer.WriteStartElement(String.Empty, "key", String.Empty);
86 string fieldName = fields[i].Name;
87 fieldName = fieldName.Replace("___", "-");
88 writer.WriteString(fieldName);
89 writer.WriteEndElement();
90 LLSD.LLSDWriteOne(writer, fieldValue);
91 // OpenMetaverse.StructuredData.LLSDParser.SerializeXmlElement(
92 // writer, OpenMetaverse.StructuredData.OSD.FromObject(fieldValue));
93 }
94 }
95 else
96 {
97 // TODO from ADAM: There is a nullref being caused by fields[i] being null
98 // on some computers. Unsure what is causing this, but would appreciate
99 // if sdague could take a look at this.
100 }
101 }
102 writer.WriteEndElement();
103 break;
104 case "ARRAY":
105 // OSDArray arrayObject = obj as OSDArray;
106 // ArrayList a = arrayObject.Array;
107 ArrayList a = (ArrayList) obj.GetType().GetField("Array").GetValue(obj);
108 if (a != null)
109 {
110 writer.WriteStartElement(String.Empty, "array", String.Empty);
111 foreach (object item in a)
112 {
113 SerializeOSDType(writer, item);
114 }
115 writer.WriteEndElement();
116 }
117 break;
118 }
119 }
120 else
121 {
122 LLSD.LLSDWriteOne(writer, obj);
123 //OpenMetaverse.StructuredData.LLSDParser.SerializeXmlElement(
124 // writer, OpenMetaverse.StructuredData.OSD.FromObject(obj));
125 }
126 }
127  
128 public static object DeserialiseOSDMap(Hashtable llsd, object obj)
129 {
130 Type myType = obj.GetType();
131 LLSDType[] llsdattributes = (LLSDType[]) myType.GetCustomAttributes(typeof (LLSDType), false);
132 if (llsdattributes.Length > 0)
133 {
134 switch (llsdattributes[0].ObjectType)
135 {
136 case "MAP":
137 IDictionaryEnumerator enumerator = llsd.GetEnumerator();
138 while (enumerator.MoveNext())
139 {
140 string keyName = (string)enumerator.Key;
141 keyName = keyName.Replace("-","_");
142 FieldInfo field = myType.GetField(keyName);
143 if (field != null)
144 {
145 // if (enumerator.Value is OpenMetaverse.StructuredData.OSDMap)
146 if (enumerator.Value is Hashtable)
147 {
148 object fieldValue = field.GetValue(obj);
149 DeserialiseOSDMap((Hashtable) enumerator.Value, fieldValue);
150 // DeserialiseOSDMap((OpenMetaverse.StructuredData.OSDMap) enumerator.Value, fieldValue);
151 }
152 else if (enumerator.Value is ArrayList)
153 {
154 object fieldValue = field.GetValue(obj);
155 fieldValue.GetType().GetField("Array").SetValue(fieldValue, enumerator.Value);
156 //TODO
157 // the LLSD map/array types in the array need to be deserialised
158 // but first we need to know the right class to deserialise them into.
159 }
160 else
161 {
162 field.SetValue(obj, enumerator.Value);
163 }
164 }
165 }
166 break;
167 }
168 }
169 return obj;
170 }
171 }
172 }