opensim-development – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | eva | 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.Generic; |
||
30 | using System.IO; |
||
31 | using System.Reflection; |
||
32 | using System.Xml; |
||
33 | using log4net; |
||
34 | using OpenMetaverse; |
||
35 | using OpenSim.Services.Interfaces; |
||
36 | |||
37 | namespace OpenSim.Framework.Serialization.External |
||
38 | { |
||
39 | /// <summary> |
||
40 | /// Utilities for manipulating external representations of data structures in OpenSim |
||
41 | /// </summary> |
||
42 | public class ExternalRepresentationUtils |
||
43 | { |
||
44 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); |
||
45 | |||
46 | /// <summary> |
||
47 | /// Populate a node with data read from xml using a dictinoary of processors |
||
48 | /// </summary> |
||
49 | /// <param name="nodeToFill"></param> |
||
50 | /// <param name="processors">/param> |
||
51 | /// <param name="xtr"></param> |
||
52 | /// <returns>true on successful, false if there were any processing failures</returns> |
||
53 | public static bool ExecuteReadProcessors<NodeType>( |
||
54 | NodeType nodeToFill, Dictionary<string, Action<NodeType, XmlTextReader>> processors, XmlTextReader xtr) |
||
55 | { |
||
56 | return ExecuteReadProcessors( |
||
57 | nodeToFill, |
||
58 | processors, |
||
59 | xtr, |
||
60 | (o, name, e) |
||
61 | => m_log.DebugFormat( |
||
62 | "[ExternalRepresentationUtils]: Exception while parsing element {0}, continuing. Exception {1}{2}", |
||
63 | name, e.Message, e.StackTrace)); |
||
64 | } |
||
65 | |||
66 | /// <summary> |
||
67 | /// Populate a node with data read from xml using a dictinoary of processors |
||
68 | /// </summary> |
||
69 | /// <param name="nodeToFill"></param> |
||
70 | /// <param name="processors"></param> |
||
71 | /// <param name="xtr"></param> |
||
72 | /// <param name="parseExceptionAction"> |
||
73 | /// Action to take if there is a parsing problem. This will usually just be to log the exception |
||
74 | /// </param> |
||
75 | /// <returns>true on successful, false if there were any processing failures</returns> |
||
76 | public static bool ExecuteReadProcessors<NodeType>( |
||
77 | NodeType nodeToFill, |
||
78 | Dictionary<string, Action<NodeType, XmlTextReader>> processors, |
||
79 | XmlTextReader xtr, |
||
80 | Action<NodeType, string, Exception> parseExceptionAction) |
||
81 | { |
||
82 | bool errors = false; |
||
83 | |||
84 | string nodeName = string.Empty; |
||
85 | while (xtr.NodeType != XmlNodeType.EndElement) |
||
86 | { |
||
87 | nodeName = xtr.Name; |
||
88 | |||
89 | // m_log.DebugFormat("[ExternalRepresentationUtils]: Processing: {0}", nodeName); |
||
90 | |||
91 | Action<NodeType, XmlTextReader> p = null; |
||
92 | if (processors.TryGetValue(xtr.Name, out p)) |
||
93 | { |
||
94 | // m_log.DebugFormat("[ExternalRepresentationUtils]: Found {0} processor, nodeName); |
||
95 | |||
96 | try |
||
97 | { |
||
98 | p(nodeToFill, xtr); |
||
99 | } |
||
100 | catch (Exception e) |
||
101 | { |
||
102 | errors = true; |
||
103 | parseExceptionAction(nodeToFill, nodeName, e); |
||
104 | |||
105 | if (xtr.NodeType == XmlNodeType.EndElement) |
||
106 | xtr.Read(); |
||
107 | } |
||
108 | } |
||
109 | else |
||
110 | { |
||
111 | // m_log.DebugFormat("[LandDataSerializer]: caught unknown element {0}", nodeName); |
||
112 | xtr.ReadOuterXml(); // ignore |
||
113 | } |
||
114 | } |
||
115 | |||
116 | return errors; |
||
117 | } |
||
118 | |||
119 | /// <summary> |
||
120 | /// Takes a XML representation of a SceneObjectPart and returns another XML representation |
||
121 | /// with creator data added to it. |
||
122 | /// </summary> |
||
123 | /// <param name="xml">The SceneObjectPart represented in XML2</param> |
||
124 | /// <param name="homeURL">The URL of the user agents service (home) for the creator</param> |
||
125 | /// <param name="userService">The service for retrieving user account information</param> |
||
126 | /// <param name="scopeID">The scope of the user account information (Grid ID)</param> |
||
127 | /// <returns>The SceneObjectPart represented in XML2</returns> |
||
128 | public static string RewriteSOP(string xml, string homeURL, IUserAccountService userService, UUID scopeID) |
||
129 | { |
||
130 | if (xml == string.Empty || homeURL == string.Empty || userService == null) |
||
131 | return xml; |
||
132 | |||
133 | XmlDocument doc = new XmlDocument(); |
||
134 | doc.LoadXml(xml); |
||
135 | XmlNodeList sops = doc.GetElementsByTagName("SceneObjectPart"); |
||
136 | |||
137 | foreach (XmlNode sop in sops) |
||
138 | { |
||
139 | UserAccount creator = null; |
||
140 | bool hasCreatorData = false; |
||
141 | XmlNodeList nodes = sop.ChildNodes; |
||
142 | foreach (XmlNode node in nodes) |
||
143 | { |
||
144 | if (node.Name == "CreatorID") |
||
145 | { |
||
146 | UUID uuid = UUID.Zero; |
||
147 | UUID.TryParse(node.InnerText, out uuid); |
||
148 | creator = userService.GetUserAccount(scopeID, uuid); |
||
149 | } |
||
150 | |||
151 | if (node.Name == "CreatorData" && node.InnerText != null && node.InnerText != string.Empty) |
||
152 | hasCreatorData = true; |
||
153 | |||
154 | //if (node.Name == "OwnerID") |
||
155 | //{ |
||
156 | // UserAccount owner = GetUser(node.InnerText); |
||
157 | // if (owner != null) |
||
158 | // node.InnerText = m_ProfileServiceURL + "/" + node.InnerText + "/" + owner.FirstName + " " + owner.LastName; |
||
159 | //} |
||
160 | } |
||
161 | if (!hasCreatorData && creator != null) |
||
162 | { |
||
163 | XmlElement creatorData = doc.CreateElement("CreatorData"); |
||
164 | creatorData.InnerText = homeURL + ";" + creator.FirstName + " " + creator.LastName; |
||
165 | sop.AppendChild(creatorData); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | using (StringWriter wr = new StringWriter()) |
||
170 | { |
||
171 | doc.Save(wr); |
||
172 | return wr.ToString(); |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | } |