ExternalRepresentationUtils.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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.Diagnostics;
  30. using System.IO;
  31. using System.Reflection;
  32. using System.Xml;
  33. using log4net;
  34. using OpenMetaverse;
  35. using OpenSim.Services.Interfaces;
  36. namespace OpenSim.Framework.Serialization.External
  37. {
  38. /// <summary>
  39. /// Utilities for manipulating external representations of data structures in OpenSim
  40. /// </summary>
  41. public class ExternalRepresentationUtils
  42. {
  43. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  44. /// <summary>
  45. /// Populate a node with data read from xml using a dictinoary of processors
  46. /// </summary>
  47. /// <param name="nodeToFill"></param>
  48. /// <param name="processors"></param>
  49. /// <param name="xtr"></param>
  50. /// <returns>true on successful, false if there were any processing failures</returns>
  51. public static bool ExecuteReadProcessors<NodeType>(
  52. NodeType nodeToFill, Dictionary<string, Action<NodeType, XmlReader>> processors, XmlReader xtr)
  53. {
  54. return ExecuteReadProcessors(
  55. nodeToFill,
  56. processors,
  57. xtr,
  58. (o, nodeName, e) => {
  59. m_log.Debug(string.Format("[ExternalRepresentationUtils]: Error while parsing element {0} ",
  60. nodeName), e);
  61. });
  62. }
  63. /// <summary>
  64. /// Populate a node with data read from xml using a dictinoary of processors
  65. /// </summary>
  66. /// <param name="nodeToFill"></param>
  67. /// <param name="processors"></param>
  68. /// <param name="xtr"></param>
  69. /// <param name="parseExceptionAction">
  70. /// Action to take if there is a parsing problem. This will usually just be to log the exception
  71. /// </param>
  72. /// <returns>true on successful, false if there were any processing failures</returns>
  73. public static bool ExecuteReadProcessors<NodeType>(
  74. NodeType nodeToFill,
  75. Dictionary<string, Action<NodeType, XmlReader>> processors,
  76. XmlReader xtr,
  77. Action<NodeType, string, Exception> parseExceptionAction)
  78. {
  79. bool errors = false;
  80. int numErrors = 0;
  81. Stopwatch timer = new Stopwatch();
  82. timer.Start();
  83. string nodeName = string.Empty;
  84. while (xtr.NodeType != XmlNodeType.EndElement)
  85. {
  86. nodeName = xtr.Name;
  87. // m_log.DebugFormat("[ExternalRepresentationUtils]: Processing node: {0}", nodeName);
  88. Action<NodeType, XmlReader> p = null;
  89. if (processors.TryGetValue(xtr.Name, out p))
  90. {
  91. // m_log.DebugFormat("[ExternalRepresentationUtils]: Found processor for {0}", nodeName);
  92. try
  93. {
  94. p(nodeToFill, xtr);
  95. }
  96. catch (Exception e)
  97. {
  98. errors = true;
  99. parseExceptionAction(nodeToFill, nodeName, e);
  100. if (xtr.EOF)
  101. {
  102. m_log.Debug("[ExternalRepresentationUtils]: Aborting ExecuteReadProcessors due to unexpected end of XML");
  103. break;
  104. }
  105. if (++numErrors == 10)
  106. {
  107. m_log.Debug("[ExternalRepresentationUtils]: Aborting ExecuteReadProcessors due to too many parsing errors");
  108. break;
  109. }
  110. if (xtr.NodeType == XmlNodeType.EndElement)
  111. xtr.Read();
  112. }
  113. }
  114. else
  115. {
  116. // m_log.DebugFormat("[ExternalRepresentationUtils]: found unknown element \"{0}\"", nodeName);
  117. xtr.ReadOuterXml(); // ignore
  118. }
  119. if (timer.Elapsed.TotalSeconds >= 60)
  120. {
  121. m_log.Debug("[ExternalRepresentationUtils]: Aborting ExecuteReadProcessors due to timeout");
  122. errors = true;
  123. break;
  124. }
  125. }
  126. return errors;
  127. }
  128. /// <summary>
  129. /// Takes a XML representation of a SceneObjectPart and returns another XML representation
  130. /// with creator data added to it.
  131. /// </summary>
  132. /// <param name="xml">The SceneObjectPart represented in XML2</param>
  133. /// <param name="homeURL">The URL of the user agents service (home) for the creator</param>
  134. /// <param name="userService">The service for retrieving user account information</param>
  135. /// <param name="scopeID">The scope of the user account information (Grid ID)</param>
  136. /// <returns>The SceneObjectPart represented in XML2</returns>
  137. [Obsolete("This method is deprecated. Use RewriteSOP instead.")]
  138. public static string RewriteSOP_Old(string xml, string homeURL, IUserAccountService userService, UUID scopeID)
  139. {
  140. if (xml == string.Empty || homeURL == string.Empty || userService == null)
  141. return xml;
  142. XmlDocument doc = new XmlDocument();
  143. doc.LoadXml(xml);
  144. XmlNodeList sops = doc.GetElementsByTagName("SceneObjectPart");
  145. foreach (XmlNode sop in sops)
  146. {
  147. UserAccount creator = null;
  148. bool hasCreatorData = false;
  149. XmlNodeList nodes = sop.ChildNodes;
  150. foreach (XmlNode node in nodes)
  151. {
  152. if (node.Name == "CreatorID")
  153. {
  154. UUID uuid = UUID.Zero;
  155. UUID.TryParse(node.InnerText, out uuid);
  156. creator = userService.GetUserAccount(scopeID, uuid);
  157. }
  158. if (node.Name == "CreatorData" && node.InnerText != null && node.InnerText != string.Empty)
  159. hasCreatorData = true;
  160. //if (node.Name == "OwnerID")
  161. //{
  162. // UserAccount owner = GetUser(node.InnerText);
  163. // if (owner != null)
  164. // node.InnerText = m_ProfileServiceURL + "/" + node.InnerText + "/" + owner.FirstName + " " + owner.LastName;
  165. //}
  166. }
  167. if (!hasCreatorData && creator != null)
  168. {
  169. XmlElement creatorData = doc.CreateElement("CreatorData");
  170. creatorData.InnerText = CalcCreatorData(homeURL, creator.FirstName + " " + creator.LastName);
  171. sop.AppendChild(creatorData);
  172. }
  173. }
  174. using (StringWriter wr = new StringWriter())
  175. {
  176. doc.Save(wr);
  177. return wr.ToString();
  178. }
  179. }
  180. /// <summary>
  181. /// Takes a XML representation of a SceneObjectPart and returns another XML representation
  182. /// with creator data added to it.
  183. /// </summary>
  184. /// <param name="xml">The SceneObjectPart represented in XML2</param>
  185. /// <param name="sceneName">An identifier for the component that's calling this function</param>
  186. /// <param name="homeURL">The URL of the user agents service (home) for the creator</param>
  187. /// <param name="userService">The service for retrieving user account information</param>
  188. /// <param name="scopeID">The scope of the user account information (Grid ID)</param>
  189. /// <returns>The SceneObjectPart represented in XML2</returns>
  190. public static string RewriteSOP(string xmlData, string sceneName, string homeURL, IUserAccountService userService, UUID scopeID)
  191. {
  192. // Console.WriteLine("Input XML [{0}]", xmlData);
  193. if (xmlData == string.Empty || homeURL == string.Empty || userService == null)
  194. return xmlData;
  195. // Deal with bug
  196. xmlData = ExternalRepresentationUtils.SanitizeXml(xmlData);
  197. using (StringWriter sw = new StringWriter())
  198. using (XmlTextWriter writer = new XmlTextWriter(sw))
  199. using (XmlTextReader wrappedReader = new XmlTextReader(xmlData, XmlNodeType.Element, null))
  200. using (XmlReader reader = XmlReader.Create(wrappedReader, new XmlReaderSettings() { IgnoreWhitespace = true, ConformanceLevel = ConformanceLevel.Fragment}))
  201. {
  202. TransformXml(reader, writer, sceneName, homeURL, userService, scopeID);
  203. // Console.WriteLine("Output: [{0}]", sw.ToString());
  204. return sw.ToString();
  205. }
  206. }
  207. protected static void TransformXml(XmlReader reader, XmlWriter writer, string sceneName, string homeURI, IUserAccountService userAccountService, UUID scopeID)
  208. {
  209. // m_log.DebugFormat("[HG ASSET MAPPER]: Transforming XML");
  210. int sopDepth = -1;
  211. UserAccount creator = null;
  212. bool hasCreatorData = false;
  213. while (reader.Read())
  214. {
  215. // Console.WriteLine("Depth: {0}, name {1}", reader.Depth, reader.Name);
  216. switch (reader.NodeType)
  217. {
  218. case XmlNodeType.Attribute:
  219. // Console.WriteLine("FOUND ATTRIBUTE {0}", reader.Name);
  220. writer.WriteAttributeString(reader.Name, reader.Value);
  221. break;
  222. case XmlNodeType.CDATA:
  223. writer.WriteCData(reader.Value);
  224. break;
  225. case XmlNodeType.Comment:
  226. writer.WriteComment(reader.Value);
  227. break;
  228. case XmlNodeType.DocumentType:
  229. writer.WriteDocType(reader.Name, reader.Value, null, null);
  230. break;
  231. case XmlNodeType.Element:
  232. // m_log.DebugFormat("Depth {0} at element {1}", reader.Depth, reader.Name);
  233. writer.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
  234. if (reader.HasAttributes)
  235. {
  236. while (reader.MoveToNextAttribute())
  237. writer.WriteAttributeString(reader.Name, reader.Value);
  238. reader.MoveToElement();
  239. }
  240. if (reader.LocalName == "SceneObjectPart")
  241. {
  242. if (sopDepth < 0)
  243. {
  244. sopDepth = reader.Depth;
  245. // m_log.DebugFormat("[HG ASSET MAPPER]: Set sopDepth to {0}", sopDepth);
  246. }
  247. }
  248. else
  249. {
  250. if (sopDepth >= 0 && reader.Depth == sopDepth + 1)
  251. {
  252. if (reader.Name == "CreatorID")
  253. {
  254. reader.Read();
  255. if (reader.NodeType == XmlNodeType.Element && reader.Name == "Guid" || reader.Name == "UUID")
  256. {
  257. reader.Read();
  258. if (reader.NodeType == XmlNodeType.Text)
  259. {
  260. UUID uuid = UUID.Zero;
  261. UUID.TryParse(reader.Value, out uuid);
  262. creator = userAccountService.GetUserAccount(scopeID, uuid);
  263. writer.WriteElementString("UUID", reader.Value);
  264. reader.Read();
  265. }
  266. else
  267. {
  268. // If we unexpected run across mixed content in this node, still carry on
  269. // transforming the subtree (this replicates earlier behaviour).
  270. TransformXml(reader, writer, sceneName, homeURI, userAccountService, scopeID);
  271. }
  272. }
  273. else
  274. {
  275. // If we unexpected run across mixed content in this node, still carry on
  276. // transforming the subtree (this replicates earlier behaviour).
  277. TransformXml(reader, writer, sceneName, homeURI, userAccountService, scopeID);
  278. }
  279. }
  280. else if (reader.Name == "CreatorData")
  281. {
  282. reader.Read();
  283. if (reader.NodeType == XmlNodeType.Text)
  284. {
  285. hasCreatorData = true;
  286. writer.WriteString(reader.Value);
  287. }
  288. else
  289. {
  290. // If we unexpected run across mixed content in this node, still carry on
  291. // transforming the subtree (this replicates earlier behaviour).
  292. TransformXml(reader, writer, sceneName, homeURI, userAccountService, scopeID);
  293. }
  294. }
  295. }
  296. }
  297. if (reader.IsEmptyElement)
  298. {
  299. // m_log.DebugFormat("[HG ASSET MAPPER]: Writing end for empty element {0}", reader.Name);
  300. writer.WriteEndElement();
  301. }
  302. break;
  303. case XmlNodeType.EndElement:
  304. // m_log.DebugFormat("Depth {0} at EndElement", reader.Depth);
  305. if (sopDepth == reader.Depth)
  306. {
  307. if (!hasCreatorData && creator != null)
  308. writer.WriteElementString(reader.Prefix, "CreatorData", reader.NamespaceURI, string.Format("{0};{1} {2}", homeURI, creator.FirstName, creator.LastName));
  309. // m_log.DebugFormat("[HG ASSET MAPPER]: Reset sopDepth");
  310. sopDepth = -1;
  311. creator = null;
  312. hasCreatorData = false;
  313. }
  314. writer.WriteEndElement();
  315. break;
  316. case XmlNodeType.EntityReference:
  317. writer.WriteEntityRef(reader.Name);
  318. break;
  319. case XmlNodeType.ProcessingInstruction:
  320. writer.WriteProcessingInstruction(reader.Name, reader.Value);
  321. break;
  322. case XmlNodeType.Text:
  323. writer.WriteString(reader.Value);
  324. break;
  325. case XmlNodeType.XmlDeclaration:
  326. // For various reasons, not all serializations have xml declarations (or consistent ones)
  327. // and as it's embedded inside a byte stream we don't need it anyway, so ignore.
  328. break;
  329. default:
  330. m_log.WarnFormat(
  331. "[HG ASSET MAPPER]: Unrecognized node {0} in asset XML transform in {1}",
  332. reader.NodeType, sceneName);
  333. break;
  334. }
  335. }
  336. }
  337. public static string CalcCreatorData(string homeURL, string name)
  338. {
  339. return homeURL + ";" + name;
  340. }
  341. internal static string CalcCreatorData(string homeURL, UUID uuid, string name)
  342. {
  343. return homeURL + "/" + uuid + ";" + name;
  344. }
  345. /// <summary>
  346. /// Sanitation for bug introduced in Oct. 20 (1eb3e6cc43e2a7b4053bc1185c7c88e22356c5e8)
  347. /// </summary>
  348. /// <param name="xmlData"></param>
  349. /// <returns></returns>
  350. public static string SanitizeXml(string xmlData)
  351. {
  352. string fixedData = xmlData;
  353. if (fixedData != null)
  354. // Loop, because it may contain multiple
  355. while (fixedData.Contains("xmlns:xmlns:"))
  356. fixedData = fixedData.Replace("xmlns:xmlns:", "xmlns:");
  357. return fixedData;
  358. }
  359. }
  360. }