29 using System.Collections.Generic;
30 using System.Diagnostics;
32 using System.Reflection;
36 using OpenSim.Services.Interfaces;
38 namespace OpenSim.Framework.Serialization.External
45 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
54 public static bool ExecuteReadProcessors<NodeType>(
55 NodeType nodeToFill, Dictionary<string, Action<NodeType, XmlReader>> processors, XmlReader xtr)
57 return ExecuteReadProcessors(
62 m_log.Debug(string.Format(
"[ExternalRepresentationUtils]: Error while parsing element {0} ",
77 public static bool ExecuteReadProcessors<NodeType>(
79 Dictionary<string, Action<NodeType, XmlReader>> processors,
81 Action<NodeType, string, Exception> parseExceptionAction)
86 Stopwatch timer =
new Stopwatch();
89 string nodeName = string.Empty;
90 while (xtr.NodeType != XmlNodeType.EndElement)
96 Action<NodeType, XmlReader> p = null;
97 if (processors.TryGetValue(xtr.Name, out p))
108 parseExceptionAction(nodeToFill, nodeName, e);
112 m_log.Debug(
"[ExternalRepresentationUtils]: Aborting ExecuteReadProcessors due to unexpected end of XML");
116 if (++numErrors == 10)
118 m_log.Debug(
"[ExternalRepresentationUtils]: Aborting ExecuteReadProcessors due to too many parsing errors");
122 if (xtr.NodeType == XmlNodeType.EndElement)
132 if (timer.Elapsed.TotalSeconds >= 60)
134 m_log.Debug(
"[ExternalRepresentationUtils]: Aborting ExecuteReadProcessors due to timeout");
152 [Obsolete(
"This method is deprecated. Use RewriteSOP instead.")]
155 if (xml ==
string.Empty || homeURL ==
string.Empty || userService == null)
158 XmlDocument doc =
new XmlDocument();
160 XmlNodeList sops = doc.GetElementsByTagName(
"SceneObjectPart");
162 foreach (XmlNode sop
in sops)
165 bool hasCreatorData =
false;
166 XmlNodeList nodes = sop.ChildNodes;
167 foreach (XmlNode node
in nodes)
169 if (node.Name ==
"CreatorID")
171 UUID uuid = UUID.Zero;
172 UUID.TryParse(node.InnerText, out uuid);
173 creator = userService.GetUserAccount(scopeID, uuid);
176 if (node.Name ==
"CreatorData" && node.InnerText != null && node.InnerText !=
string.Empty)
177 hasCreatorData =
true;
186 if (!hasCreatorData && creator != null)
188 XmlElement creatorData = doc.CreateElement(
"CreatorData");
189 creatorData.InnerText = CalcCreatorData(homeURL, creator.FirstName +
" " + creator.LastName);
190 sop.AppendChild(creatorData);
194 using (StringWriter wr =
new StringWriter())
197 return wr.ToString();
214 if (xmlData ==
string.Empty || homeURL ==
string.Empty || userService == null)
218 xmlData = ExternalRepresentationUtils.SanitizeXml(xmlData);
220 using (StringWriter sw =
new StringWriter())
221 using (XmlTextWriter writer =
new XmlTextWriter(sw))
222 using (XmlTextReader wrappedReader =
new XmlTextReader(xmlData, XmlNodeType.Element, null))
223 using (XmlReader reader = XmlReader.Create(wrappedReader,
new XmlReaderSettings() { IgnoreWhitespace =
true, ConformanceLevel = ConformanceLevel.Fragment }))
225 TransformXml(reader, writer, sceneName, homeURL, userService, scopeID);
229 return sw.ToString();
239 bool hasCreatorData =
false;
241 while (reader.Read())
245 switch (reader.NodeType)
247 case XmlNodeType.Attribute:
249 writer.WriteAttributeString(reader.Name, reader.Value);
252 case XmlNodeType.CDATA:
253 writer.WriteCData(reader.Value);
256 case XmlNodeType.Comment:
257 writer.WriteComment(reader.Value);
260 case XmlNodeType.DocumentType:
261 writer.WriteDocType(reader.Name, reader.Value, null, null);
264 case XmlNodeType.Element:
267 writer.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
269 if (reader.HasAttributes)
271 while (reader.MoveToNextAttribute())
272 writer.WriteAttributeString(reader.Name, reader.Value);
274 reader.MoveToElement();
277 if (reader.LocalName ==
"SceneObjectPart")
281 sopDepth = reader.Depth;
287 if (sopDepth >= 0 && reader.Depth == sopDepth + 1)
289 if (reader.Name ==
"CreatorID")
292 if (reader.NodeType == XmlNodeType.Element && reader.Name ==
"Guid" || reader.Name ==
"UUID")
296 if (reader.NodeType == XmlNodeType.Text)
298 UUID uuid = UUID.Zero;
299 UUID.TryParse(reader.Value, out uuid);
300 creator = userAccountService.GetUserAccount(scopeID, uuid);
301 writer.WriteElementString(
"UUID", reader.Value);
308 TransformXml(reader, writer, sceneName, homeURI, userAccountService, scopeID);
315 TransformXml(reader, writer, sceneName, homeURI, userAccountService, scopeID);
318 else if (reader.Name ==
"CreatorData")
321 if (reader.NodeType == XmlNodeType.Text)
323 hasCreatorData =
true;
324 writer.WriteString(reader.Value);
330 TransformXml(reader, writer, sceneName, homeURI, userAccountService, scopeID);
336 if (reader.IsEmptyElement)
339 writer.WriteEndElement();
344 case XmlNodeType.EndElement:
346 if (sopDepth == reader.Depth)
348 if (!hasCreatorData && creator != null)
349 writer.WriteElementString(reader.Prefix,
"CreatorData", reader.NamespaceURI, string.Format(
"{0};{1} {2}", homeURI, creator.FirstName, creator.LastName));
354 hasCreatorData =
false;
356 writer.WriteEndElement();
359 case XmlNodeType.EntityReference:
360 writer.WriteEntityRef(reader.Name);
363 case XmlNodeType.ProcessingInstruction:
364 writer.WriteProcessingInstruction(reader.Name, reader.Value);
367 case XmlNodeType.Text:
368 writer.WriteString(reader.Value);
371 case XmlNodeType.XmlDeclaration:
378 "[HG ASSET MAPPER]: Unrecognized node {0} in asset XML transform in {1}",
379 reader.NodeType, sceneName);
387 return homeURL +
";" + name;
390 internal static string CalcCreatorData(
string homeURL, UUID uuid,
string name)
392 return homeURL +
"/" + uuid +
";" + name;
402 string fixedData = xmlData;
403 if (fixedData != null)
405 while (fixedData.Contains(
"xmlns:xmlns:"))
406 fixedData = fixedData.Replace(
"xmlns:xmlns:",
"xmlns:");
static string CalcCreatorData(string homeURL, string name)
static string RewriteSOP_Old(string xml, string homeURL, IUserAccountService userService, UUID scopeID)
Takes a XML representation of a SceneObjectPart and returns another XML representation with creator d...
static void TransformXml(XmlReader reader, XmlWriter writer, string sceneName, string homeURI, IUserAccountService userAccountService, UUID scopeID)
Utilities for manipulating external representations of data structures in OpenSim ...
Interactive OpenSim region server
static string SanitizeXml(string xmlData)
Sanitation for bug introduced in Oct. 20 (1eb3e6cc43e2a7b4053bc1185c7c88e22356c5e8) ...
static string RewriteSOP(string xmlData, string sceneName, string homeURL, IUserAccountService userService, UUID scopeID)
Takes a XML representation of a SceneObjectPart and returns another XML representation with creator d...