2using System.Collections.Generic;
9 internal class JsonLdContext
11 private readonly Dictionary<string, object> context;
16 public JsonLdContext()
26 public JsonLdContext(Dictionary<string, object> Context, Uri BaseUri)
28 this.context =
new Dictionary<string, object>();
30 if (!(Context is
null))
32 foreach (KeyValuePair<string, object> P
in Context)
34 string Name = this.GetFullyQualifiedName(P.Key);
35 this.context[Name] = P.Value;
36 this.SetProperty(Name, P.Value, BaseUri);
41 private void SetProperty(
string Name,
object Value, Uri BaseUri)
46 this.Id = this.ToUri(Value, BaseUri);
50 this.Type = this.ToUri(Value, BaseUri);
54 this.Base = this.ToUri(Value, BaseUri);
58 if (Value is
double d)
63 this.Vocabulary = this.ToUri(Value, BaseUri);
68 private string GetFullyQualifiedName(
string Name)
70 int i = Name.IndexOf(
':');
73 string Prefix = Name.Substring(0, i);
75 if (this.TryGetStringValue(Prefix, out
string PrefixUrl))
76 return PrefixUrl + Name.Substring(i + 1);
82 private Uri ToUri(
object Value, Uri BaseUri)
84 if (Value is
string s)
86 s = this.GetFullyQualifiedName(s);
87 BaseUri = this.Base ?? BaseUri;
91 if (Uri.TryCreate(s, UriKind.Absolute, out Uri ParsedUri))
96 if (Uri.TryCreate(BaseUri, s, out Uri ParsedUri))
107 public Uri Id {
get;
private set; }
112 public Uri Type {
get;
private set; }
117 public Uri Base {
get;
private set; }
122 public double Version {
get;
private set; }
127 public Uri Vocabulary {
get;
private set; }
134 public object this[
string Name]
138 if (this.context.TryGetValue(Name, out
object Value))
151 public bool TryGetStringValue(
string Name, out
string StringValue)
153 if (this.context.TryGetValue(Name, out
object Value) && Value is
string s)
171 public bool TryGetObjectValue(
string Name, out
object Value)
173 return this.context.TryGetValue(Name, out Value);
181 public void Append(JsonLdContext Context, Uri BaseUri)
183 foreach (KeyValuePair<string, object> P
in Context.context)
185 string Name = this.GetFullyQualifiedName(P.Key);
186 object Value = P.Value;
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))
193 Value = NewUri.AbsoluteUri;
196 this.context[Name] = Value;
197 this.SetProperty(Name, Value, BaseUri);
205 public void Append(JsonLdContext Context)
207 foreach (KeyValuePair<string, object> P
in Context.context)
208 this.context[P.Key] = P.Value;
Prefix
SI prefixes. http://physics.nist.gov/cuu/Units/prefixes.html