ExternalRepresentationUtils.cs 7.9 KB

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