-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathJsonLdContext.cs
More file actions
211 lines (184 loc) · 4.93 KB
/
JsonLdContext.cs
File metadata and controls
211 lines (184 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System;
using System.Collections.Generic;
namespace Waher.Content.Semantic
{
/// <summary>
/// Maintains information about a JSON-LD context.
/// </summary>
internal class JsonLdContext
{
private readonly Dictionary<string, object> context;
/// <summary>
/// Maintains information about a JSON-LD context.
/// </summary>
public JsonLdContext()
: this(null, null)
{
}
/// <summary>
/// Maintains information about a JSON-LD context.
/// </summary>
/// <param name="Context">Parsed JSON Context.</param>
/// <param name="BaseUri">Base URI</param>
public JsonLdContext(Dictionary<string, object> Context, Uri BaseUri)
{
this.context = new Dictionary<string, object>();
if (!(Context is null))
{
foreach (KeyValuePair<string, object> P in Context)
{
string Name = this.GetFullyQualifiedName(P.Key);
this.context[Name] = P.Value;
this.SetProperty(Name, P.Value, BaseUri);
}
}
}
private void SetProperty(string Name, object Value, Uri BaseUri)
{
switch (Name)
{
case "@id":
this.Id = this.ToUri(Value, BaseUri);
break;
case "@type":
this.Type = this.ToUri(Value, BaseUri);
break;
case "@base":
this.Base = this.ToUri(Value, BaseUri);
break;
case "@version":
if (Value is double d)
this.Version = d;
break;
case "@vocab":
this.Vocabulary = this.ToUri(Value, BaseUri);
break;
}
}
private string GetFullyQualifiedName(string Name)
{
int i = Name.IndexOf(':');
if (i >= 0)
{
string Prefix = Name.Substring(0, i);
if (this.TryGetStringValue(Prefix, out string PrefixUrl))
return PrefixUrl + Name.Substring(i + 1);
}
return Name;
}
private Uri ToUri(object Value, Uri BaseUri)
{
if (Value is string s)
{
s = this.GetFullyQualifiedName(s);
BaseUri = this.Base ?? BaseUri;
if (BaseUri is null)
{
if (Uri.TryCreate(s, UriKind.Absolute, out Uri ParsedUri))
return ParsedUri;
}
else
{
if (Uri.TryCreate(BaseUri, s, out Uri ParsedUri))
return ParsedUri;
}
}
return null;
}
/// <summary>
/// @id property of context.
/// </summary>
public Uri Id { get; private set; }
/// <summary>
/// @type property of context.
/// </summary>
public Uri Type { get; private set; }
/// <summary>
/// @base property of context.
/// </summary>
public Uri Base { get; private set; }
/// <summary>
/// @version property of context.
/// </summary>
public double Version { get; private set; }
/// <summary>
/// @vocab property of context.
/// </summary>
public Uri Vocabulary { get; private set; }
/// <summary>
/// Gives access to a property, given its name.
/// </summary>
/// <param name="Name">Name of property.</param>
/// <returns>Property value.</returns>
public object this[string Name]
{
get
{
if (this.context.TryGetValue(Name, out object Value))
return Value;
else
return null;
}
}
/// <summary>
/// Tries to get a string value from the context.
/// </summary>
/// <param name="Name">Name of property.</param>
/// <param name="StringValue">String Value, if found.</param>
/// <returns>If a string value with the given name was found.</returns>
public bool TryGetStringValue(string Name, out string StringValue)
{
if (this.context.TryGetValue(Name, out object Value) && Value is string s)
{
StringValue = s;
return true;
}
else
{
StringValue = null;
return false;
}
}
/// <summary>
/// Tries to get an untyped value from the context.
/// </summary>
/// <param name="Name">Name of property.</param>
/// <param name="Value">Untyped Value, if found.</param>
/// <returns>If a value with the given name was found.</returns>
public bool TryGetObjectValue(string Name, out object Value)
{
return this.context.TryGetValue(Name, out Value);
}
/// <summary>
/// Appends a context object with the another.
/// </summary>
/// <param name="Context">Context object to append to this context.</param>
/// <param name="BaseUri">Base URI</param>
public void Append(JsonLdContext Context, Uri BaseUri)
{
foreach (KeyValuePair<string, object> P in Context.context)
{
string Name = this.GetFullyQualifiedName(P.Key);
object Value = P.Value;
if (this.context.TryGetValue(Name, out object Prev) &&
Prev is string PrevString &&
Uri.TryCreate(BaseUri, PrevString, out Uri PrevUri) &&
Uri.TryCreate(PrevUri, P.Value?.ToString(), out Uri NewUri))
{
Value = NewUri.AbsoluteUri;
}
this.context[Name] = Value;
this.SetProperty(Name, Value, BaseUri);
}
}
/// <summary>
/// Appends a context object with the another.
/// </summary>
/// <param name="Context">Context object to append to this context.</param>
public void Append(JsonLdContext Context)
{
foreach (KeyValuePair<string, object> P in Context.context)
this.context[P.Key] = P.Value;
}
}
}