Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
KycApplicationItem.cs
2{
3 using System;
5 using System.Collections.ObjectModel;
6 using System.Globalization;
7 using System.Xml;
9
13 public sealed class KycApplicationItem
14 {
15 private const string XmlLanguageNamespace = "http://www.w3.org/XML/1998/namespace";
16 private readonly IReadOnlyDictionary<string, string> localizedNames;
17 private readonly string? primaryDisplayName;
18
19 private KycApplicationItem(
20 string nodeId,
21 string itemId,
22 string processXml,
23 string? serviceAddress,
24 string? publisher,
25 DateTime? published,
26 IReadOnlyDictionary<string, string> localizedNames,
27 string? primaryDisplayName)
28 {
29 this.NodeId = nodeId;
30 this.ItemId = itemId;
31 this.ProcessXml = processXml;
32 this.ServiceAddress = serviceAddress;
33 this.Publisher = publisher;
34 this.Published = published;
35 this.localizedNames = localizedNames;
36 this.primaryDisplayName = primaryDisplayName;
37 }
38
42 public string NodeId { get; }
43
47 public string ItemId { get; }
48
52 public string ProcessXml { get; }
53
57 public string? ServiceAddress { get; }
58
62 public string? Publisher { get; }
63
67 public DateTime? Published { get; }
68
72 public IReadOnlyDictionary<string, string> LocalizedNames => this.localizedNames;
73
77 public string DisplayName
78 {
79 get
80 {
81 if (!string.IsNullOrEmpty(this.primaryDisplayName))
82 {
83 return this.primaryDisplayName;
84 }
85 string Preferred = CultureInfo.CurrentUICulture.TwoLetterISOLanguageName;
86 string? Value = this.GetLocalizedName(Preferred);
87 return Value ?? string.Empty;
88 }
89 }
90
94 public string? PrimaryDisplayName => this.primaryDisplayName;
95
102 public static bool TryCreate(PubSubItem Item, out KycApplicationItem? Application)
103 {
104 Application = null;
105 if (Item is null)
106 return false;
107
108 string? ItemId = Item.ItemId;
109 if (string.IsNullOrWhiteSpace(ItemId))
110 return false;
111
112 XmlElement? Payload = Item.Item;
113 if (Payload is null)
114 {
115 string? PayloadXml = Item.Payload;
116 if (string.IsNullOrWhiteSpace(PayloadXml))
117 return false;
118
119 try
120 {
121 XmlDocument Document = new XmlDocument();
122 Document.LoadXml(PayloadXml);
123 Payload = Document.DocumentElement;
124 }
125 catch (XmlException)
126 {
127 return false;
128 }
129 }
130
131 if (Payload is null)
132 return false;
133
134 XmlElement? PayloadElement = NormalizePayload(Payload);
135 if (PayloadElement is null)
136 return false;
137
138 string ProcessXml = PayloadElement.OuterXml;
139 if (string.IsNullOrWhiteSpace(ProcessXml))
140 return false;
141
142 Dictionary<string, string> NamesDictionary = ExtractLocalizedNames(PayloadElement, out string? PrimaryName);
143 ReadOnlyDictionary<string, string> Names = new ReadOnlyDictionary<string, string>(NamesDictionary);
144
145 string NodeId = Item.Node ?? string.Empty;
146 string? ServiceAddress = Item.ServiceAddress;
147 string? Publisher = Item.Publisher;
148 DateTime? Published = null;
149
150 Application = new KycApplicationItem(NodeId, ItemId, ProcessXml, ServiceAddress, Publisher, Published, Names, PrimaryName);
151 return true;
152 }
153
159 public static IReadOnlyList<KycApplicationItem> CreateMany(IEnumerable<PubSubItem> Items)
160 {
161 List<KycApplicationItem> Results = new List<KycApplicationItem>();
162 foreach (PubSubItem Item in Items)
163 {
164 if (Item is null)
165 continue;
166
167 if (TryCreate(Item, out KycApplicationItem? Application) && Application is not null)
168 Results.Add(Application);
169 }
170
171 return Results;
172 }
173
179 public string? GetLocalizedName(string? Language)
180 {
181 if (Language is not null && this.localizedNames.TryGetValue(Language, out string? Value))
182 return Value;
183
184 if (this.localizedNames.TryGetValue("en", out string? English))
185 return English;
186
187 foreach (KeyValuePair<string, string> Pair in this.localizedNames)
188 return Pair.Value;
189
190 return null;
191 }
192
193 private static XmlElement? NormalizePayload(XmlElement Payload)
194 {
195 if (Payload.LocalName == "item" && Payload.NamespaceURI == PubSubClient.NamespacePubSub)
196 {
197 foreach (XmlNode Child in Payload.ChildNodes)
198 {
199 if (Child is XmlElement Element)
200 return Element;
201 }
202
203 return null;
204 }
205
206 return Payload;
207 }
208
209 private static Dictionary<string, string> ExtractLocalizedNames(XmlElement Payload, out string? primaryName)
210 {
211 Dictionary<string, string> Names = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
212 primaryName = null;
213 XmlElement? NameElement = SelectChild(Payload, "Name");
214 if (NameElement is null)
215 return Names;
216
217 foreach (XmlNode Child in NameElement.ChildNodes)
218 {
219 if (Child is not XmlElement TextElement)
220 continue;
221
222 if (!string.Equals(TextElement.LocalName, "Text", StringComparison.Ordinal))
223 continue;
224
225 string Language = TextElement.GetAttribute("lang", XmlLanguageNamespace);
226 if (string.IsNullOrWhiteSpace(Language))
227 Language = "und";
228
229 string Value = TextElement.InnerText;
230 if (string.IsNullOrWhiteSpace(Value))
231 continue;
232
233 Names[Language] = Value.Trim();
234 if (primaryName is null)
235 primaryName = Value.Trim();
236 }
237
238 return Names;
239 }
240
241 private static XmlElement? SelectChild(XmlElement Parent, string LocalName, string? NamespaceUri = null)
242 {
243 foreach (XmlNode Child in Parent.ChildNodes)
244 {
245 if (Child is not XmlElement Candidate)
246 continue;
247
248 if (!string.Equals(Candidate.LocalName, LocalName, StringComparison.Ordinal))
249 continue;
250
251 if (NamespaceUri is not null && !string.Equals(Candidate.NamespaceURI, NamespaceUri, StringComparison.Ordinal))
252 continue;
253
254 return Candidate;
255 }
256
257 return null;
258 }
259 }
260}
Represents a KYC application template retrieved from PubSub storage.
string? ServiceAddress
Gets the service address that hosted the node, if available.
IReadOnlyDictionary< string, string > LocalizedNames
Gets localized display names extracted from the payload.
string ItemId
Gets the PubSub item identifier.
string NodeId
Gets the PubSub node identifier that produced the item.
static bool TryCreate(PubSubItem Item, out KycApplicationItem? Application)
Attempts to create an instance from a PubSub item.
string? GetLocalizedName(string? Language)
Retrieves a localized name for the provided language code.
string DisplayName
Gets a localized display name using the current UI culture as the preference.
string? Publisher
Gets the publisher that submitted the item, if available.
static IReadOnlyList< KycApplicationItem > CreateMany(IEnumerable< PubSubItem > Items)
Converts a collection of PubSub items to KYC application descriptors.
string ProcessXml
Gets the raw KYC process XML payload.
string? PrimaryDisplayName
Gets the first display name found in the payload, if available.
DateTime? Published
Gets the publish timestamp, if available.
Client managing communication with a Publish/Subscribe component. https://xmpp.org/extensions/xep-006...
Definition: PubSubClient.cs:20
const string NamespacePubSub
http://jabber.org/protocol/pubsub
Definition: PubSubClient.cs:24
Represents a published item.
Definition: PubSubItem.cs:12
string ServiceAddress
Address of service hosting the node.
Definition: PubSubItem.cs:59
string Publisher
Publisher of content.
Definition: PubSubItem.cs:68
XmlElement Item
Item XML Element.
Definition: PubSubItem.cs:86
class Names(Vector NamesVector)
Contains a collection of distinguished names.
Definition: Names.cs:9