Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
JsonLdContext.cs
1using System;
2using System.Collections.Generic;
3
5{
9 internal class JsonLdContext
10 {
11 private readonly Dictionary<string, object> context;
12
16 public JsonLdContext()
17 : this(null, null)
18 {
19 }
20
26 public JsonLdContext(Dictionary<string, object> Context, Uri BaseUri)
27 {
28 this.context = new Dictionary<string, object>();
29
30 if (!(Context is null))
31 {
32 foreach (KeyValuePair<string, object> P in Context)
33 {
34 string Name = this.GetFullyQualifiedName(P.Key);
35 this.context[Name] = P.Value;
36 this.SetProperty(Name, P.Value, BaseUri);
37 }
38 }
39 }
40
41 private void SetProperty(string Name, object Value, Uri BaseUri)
42 {
43 switch (Name)
44 {
45 case "@id":
46 this.Id = this.ToUri(Value, BaseUri);
47 break;
48
49 case "@type":
50 this.Type = this.ToUri(Value, BaseUri);
51 break;
52
53 case "@base":
54 this.Base = this.ToUri(Value, BaseUri);
55 break;
56
57 case "@version":
58 if (Value is double d)
59 this.Version = d;
60 break;
61
62 case "@vocab":
63 this.Vocabulary = this.ToUri(Value, BaseUri);
64 break;
65 }
66 }
67
68 private string GetFullyQualifiedName(string Name)
69 {
70 int i = Name.IndexOf(':');
71 if (i >= 0)
72 {
73 string Prefix = Name.Substring(0, i);
74
75 if (this.TryGetStringValue(Prefix, out string PrefixUrl))
76 return PrefixUrl + Name.Substring(i + 1);
77 }
78
79 return Name;
80 }
81
82 private Uri ToUri(object Value, Uri BaseUri)
83 {
84 if (Value is string s)
85 {
86 s = this.GetFullyQualifiedName(s);
87 BaseUri = this.Base ?? BaseUri;
88
89 if (BaseUri is null)
90 {
91 if (Uri.TryCreate(s, UriKind.Absolute, out Uri ParsedUri))
92 return ParsedUri;
93 }
94 else
95 {
96 if (Uri.TryCreate(BaseUri, s, out Uri ParsedUri))
97 return ParsedUri;
98 }
99 }
100
101 return null;
102 }
103
107 public Uri Id { get; private set; }
108
112 public Uri Type { get; private set; }
113
117 public Uri Base { get; private set; }
118
122 public double Version { get; private set; }
123
127 public Uri Vocabulary { get; private set; }
128
134 public object this[string Name]
135 {
136 get
137 {
138 if (this.context.TryGetValue(Name, out object Value))
139 return Value;
140 else
141 return null;
142 }
143 }
144
151 public bool TryGetStringValue(string Name, out string StringValue)
152 {
153 if (this.context.TryGetValue(Name, out object Value) && Value is string s)
154 {
155 StringValue = s;
156 return true;
157 }
158 else
159 {
160 StringValue = null;
161 return false;
162 }
163 }
164
171 public bool TryGetObjectValue(string Name, out object Value)
172 {
173 return this.context.TryGetValue(Name, out Value);
174 }
175
181 public void Append(JsonLdContext Context, Uri BaseUri)
182 {
183 foreach (KeyValuePair<string, object> P in Context.context)
184 {
185 string Name = this.GetFullyQualifiedName(P.Key);
186 object Value = P.Value;
187
188 if (this.context.TryGetValue(Name, out object Prev) &&
189 Prev is string PrevString &&
190 Uri.TryCreate(BaseUri, PrevString, out Uri PrevUri) &&
191 Uri.TryCreate(PrevUri, P.Value?.ToString(), out Uri NewUri))
192 {
193 Value = NewUri.AbsoluteUri;
194 }
195
196 this.context[Name] = Value;
197 this.SetProperty(Name, Value, BaseUri);
198 }
199 }
200
205 public void Append(JsonLdContext Context)
206 {
207 foreach (KeyValuePair<string, object> P in Context.context)
208 this.context[P.Key] = P.Value;
209 }
210 }
211}
Prefix
SI prefixes. http://physics.nist.gov/cuu/Units/prefixes.html
Definition: Prefixes.cs:11