/// <summary> /// Gets description (for logging purposes). /// </summary> /// <returns></returns> public string GetDescription() { // ordered list of topics; List <TopicInfo> orderedTopics = new List <TopicInfo>(); TopicInfo currentTopic = this; while (currentTopic != null) { orderedTopics.Add(currentTopic); currentTopic = currentTopic.ParentTopic; } // namespaces prefixes cache; Dictionary <string, string> namespacePrefixes = new Dictionary <string, string>(); foreach (TopicInfo topicInfo in orderedTopics) { if (!string.IsNullOrEmpty(topicInfo.Namespace) && !string.IsNullOrEmpty(topicInfo.NamespacePrefix)) { if (!namespacePrefixes.ContainsKey(topicInfo.Namespace)) { namespacePrefixes.Add(topicInfo.Namespace, topicInfo.NamespacePrefix); } } } string topicPath = null; topicPath = orderedTopics[orderedTopics.Count - 1].QName; for (int i = orderedTopics.Count - 2; i >= 0; i--) { TopicInfo nextTopic = orderedTopics[i]; topicPath = string.Format("{0}/{1}", topicPath, nextTopic.QName); } StringBuilder sb = new StringBuilder(); bool bFirst = true; foreach (string nameSpace in namespacePrefixes.Keys) { string entry = string.Format(" xmlns:{0}={1}", namespacePrefixes[nameSpace], nameSpace); if (bFirst) { sb.Append(entry); bFirst = false; } else { sb.Append(Environment.NewLine); sb.Append(entry); } } string namespacesDefinition = sb.ToString(); if (!string.IsNullOrEmpty(namespacesDefinition)) { return(string.Format("{0}, where {1}{2}", topicPath, Environment.NewLine, namespacesDefinition)); } else { return(topicPath); } }
/// <summary> /// Constructs TopicInfo from XmlElement (received in GetEventProperties) /// </summary> /// <param name="element"></param> /// <returns></returns> public static TopicInfo ConstructTopicInfo(XmlElement element) { List <XmlElement> orderedTopics = new List <XmlElement>(); orderedTopics.Add(element); XmlNode parent = element.ParentNode; while (parent != null && !(parent.LocalName == "TopicSet" && parent.NamespaceURI == BaseNotification.T1)) { orderedTopics.Add(parent as XmlElement); parent = parent.ParentNode; } TopicInfo current = null; Dictionary <string, string> prefixes = new Dictionary <string, string>(); string lastNamespace = string.Empty; for (int i = orderedTopics.Count - 1; i >= 0; i--) { XmlElement currentElement = orderedTopics[i]; string topicNs = /*string.IsNullOrEmpty(currentElement.NamespaceURI) * ? lastNamespace * : */currentElement.NamespaceURI; string namespacePrefix = currentElement.GetPrefixOfNamespace(currentElement.NamespaceURI); if (!string.IsNullOrEmpty(namespacePrefix)) { if (prefixes.ContainsKey(namespacePrefix) && topicNs != prefixes[namespacePrefix]) { string pattern = "tns{0}"; int j = 1; while (true) { string prefix = string.Format(pattern, j); if (!prefixes.ContainsKey(prefix)) { namespacePrefix = prefix; break; } j++; } } if (!prefixes.ContainsKey(namespacePrefix)) { prefixes.Add(namespacePrefix, topicNs); } } TopicInfo topicInfo = new TopicInfo() { Name = currentElement.LocalName, Namespace = topicNs, NamespacePrefix = namespacePrefix }; topicInfo.ParentTopic = current; lastNamespace = topicNs; current = topicInfo; } ; return(current); }