-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathSparqlResultSetXmlCodec.cs
More file actions
383 lines (331 loc) · 10.5 KB
/
SparqlResultSetXmlCodec.cs
File metadata and controls
383 lines (331 loc) · 10.5 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Waher.Content.Semantic.Model;
using Waher.Content.Semantic.Model.Literals;
using Waher.Runtime.Inventory;
using Waher.Runtime.IO;
using Waher.Script.Objects.Matrices;
namespace Waher.Content.Semantic
{
/// <summary>
/// Encoder and Decoder of semantic information from SPARQL queries using XML.
/// https://www.w3.org/TR/2023/WD-sparql12-results-xml-20230516/
/// </summary>
public class SparqlResultSetXmlCodec : IContentDecoder, IContentEncoder
{
/// <summary>
/// Encoder and Decoder of semantic information from SPARQL queries using XML.
/// </summary>
public SparqlResultSetXmlCodec()
{
}
/// <summary>
/// Supported Internet Content Types.
/// </summary>
public string[] ContentTypes => SparqlResultSetContentTypes;
private static readonly string[] SparqlResultSetContentTypes = new string[]
{
"application/sparql-results+xml"
};
/// <summary>
/// Supported file extensions.
/// </summary>
public string[] FileExtensions => SparqlResultSetFileExtensions;
private static readonly string[] SparqlResultSetFileExtensions = new string[]
{
"srx"
};
/// <summary>
/// If the decoder decodes content of a given Internet Content Type.
/// </summary>
/// <param name="ContentType">Content Type</param>
/// <param name="Grade">How well the decoder supports the given content type.</param>
/// <returns>If the decoder decodes the given content type.</returns>
public bool Decodes(string ContentType, out Grade Grade)
{
if (Array.IndexOf(SparqlResultSetContentTypes, ContentType) >= 0)
{
Grade = Grade.Excellent;
return true;
}
else
{
Grade = Grade.NotAtAll;
return false;
}
}
/// <summary>
/// Decodes an object
/// </summary>
/// <param name="ContentType">Content Type</param>
/// <param name="Data">Binary representation of object.</param>
/// <param name="Encoding">Encoding</param>
/// <param name="Fields">Additional fields</param>
/// <param name="BaseUri">Base URI</param>
/// <param name="Progress">Optional progress reporting of encoding/decoding. Can be null.</param>
/// <returns>Decoded object.</returns>
public Task<ContentResponse> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding,
KeyValuePair<string, string>[] Fields, Uri BaseUri, ICodecProgress Progress)
{
string s = Strings.GetString(Data, Encoding ?? Encoding.UTF8);
SparqlResultSet Parsed = new SparqlResultSet(s, BaseUri);
return Task.FromResult(new ContentResponse(ContentType, Parsed, Data));
}
/// <summary>
/// If the encoder encodes a specific object.
/// </summary>
/// <param name="Object">Object to encode.</param>
/// <param name="Grade">How well the encoder supports the given object.</param>
/// <param name="AcceptedContentTypes">Accepted content types.</param>
/// <returns>If the encoder encodes the given object.</returns>
public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
{
if (Object is SparqlResultSet &&
InternetContent.IsAccepted(SparqlResultSetContentTypes, AcceptedContentTypes))
{
Grade = Grade.Excellent;
return true;
}
else if (Object is ObjectMatrix M && M.HasColumnNames &&
InternetContent.IsAccepted(SparqlResultSetContentTypes, AcceptedContentTypes))
{
Grade = Grade.Ok;
return true;
}
else if (Object is bool &&
InternetContent.IsAccepted(SparqlResultSetContentTypes, AcceptedContentTypes))
{
Grade = Grade.Barely;
return true;
}
else
{
Grade = Grade.NotAtAll;
return false;
}
}
/// <summary>
/// Encodes an object
/// </summary>
/// <param name="Object">Object to encode</param>
/// <param name="Encoding">Encoding</param>
/// <param name="Progress">Optional progress reporting of encoding/decoding. Can be null.</param>
/// <param name="AcceptedContentTypes">Accepted content types.</param>
/// <returns>Encoded object.</returns>
public Task<ContentResponse> EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
{
if (Encoding is null)
Encoding = Encoding.UTF8;
StringBuilder sb = new StringBuilder();
sb.Append("<?xml version=\"1.0\" encoding=\"");
sb.Append(Encoding.WebName);
sb.AppendLine("\"?>");
XmlWriterSettings Settings = new XmlWriterSettings()
{
ConformanceLevel = ConformanceLevel.Document,
Encoding = Encoding,
Indent = false,
NamespaceHandling = NamespaceHandling.OmitDuplicates,
NewLineHandling = NewLineHandling.None,
NewLineOnAttributes = false,
OmitXmlDeclaration = true,
WriteEndDocumentOnClose = true
};
if (Object is SparqlResultSet Result && Result.Pretty)
{
Settings.Indent = true;
Settings.IndentChars = "\t";
}
using (XmlWriter w = XmlWriter.Create(sb, Settings))
{
if (Object is SparqlResultSet Result2)
Encode(Result2, w);
else if (Object is ObjectMatrix M)
Encode(M, w);
else if (Object is bool b)
Encode(b, w);
else
return Task.FromResult(new ContentResponse(new ArgumentException("Unable to encode object.", nameof(Object))));
w.Flush();
string Text = sb.ToString();
byte[] Bin = Encoding.GetBytes(Text);
string ContentType = SparqlResultSetContentTypes[0] + "; charset=" + Encoding.WebName;
return Task.FromResult(new ContentResponse(ContentType, Object, Bin));
}
}
private static void Encode(SparqlResultSet Result, XmlWriter w)
{
w.WriteStartElement(SparqlResultSet.LocalName, SparqlResultSet.Namespace);
w.WriteStartElement("head");
if (!(Result.Variables is null))
{
foreach (string Name in Result.Variables)
{
w.WriteStartElement("variable");
w.WriteAttributeString("name", Name);
w.WriteEndElement();
}
}
if (!(Result.Links is null))
{
foreach (Uri Link in Result.Links)
{
w.WriteStartElement("link");
w.WriteAttributeString("href", Link.ToString());
w.WriteEndElement();
}
}
w.WriteEndElement();
if (Result.BooleanResult.HasValue)
w.WriteElementString("boolean", CommonTypes.Encode(Result.BooleanResult.Value));
else
{
w.WriteStartElement("results");
if (!(Result.Records is null))
{
foreach (ISparqlResultRecord Record in Result.Records)
{
w.WriteStartElement("result");
foreach (ISparqlResultItem Item in Record)
{
w.WriteStartElement("binding");
w.WriteAttributeString("name", Item.Name);
OutputValue(w, Item.Value);
w.WriteEndElement();
}
w.WriteEndElement();
}
}
w.WriteEndElement();
}
w.WriteEndElement();
}
private static void Encode(ObjectMatrix Result, XmlWriter w)
{
w.WriteStartElement(SparqlResultSet.LocalName, SparqlResultSet.Namespace);
w.WriteStartElement("head");
if (!(Result.ColumnNames is null))
{
foreach (string Name in Result.ColumnNames)
{
w.WriteStartElement("variable");
w.WriteAttributeString("name", Name);
w.WriteEndAttribute();
}
}
w.WriteEndElement();
w.WriteStartElement("results");
int x, y;
int NrRows = Result.Rows;
int NrColumns = Result.Columns;
for (y = 0; y < NrRows; y++)
{
w.WriteStartElement("result");
for (x = 0; x < NrColumns; x++)
{
w.WriteStartElement("binding");
w.WriteAttributeString("name", Result.ColumnNames[x]);
OutputValue(w, Result.GetElement(x, y)?.AssociatedObjectValue);
w.WriteEndElement();
}
w.WriteEndElement();
}
w.WriteEndElement();
w.WriteEndElement();
}
private static void Encode(bool Result, XmlWriter w)
{
w.WriteStartElement(SparqlResultSet.LocalName, SparqlResultSet.Namespace);
w.WriteElementString("head", string.Empty);
w.WriteElementString("boolean", CommonTypes.Encode(Result));
w.WriteEndElement();
}
private static void OutputValue(XmlWriter w, object Value)
{
if (Value is ISemanticElement E)
{
if (E is ISemanticLiteral Literal)
{
w.WriteStartElement("literal");
if (!string.IsNullOrEmpty(Literal.StringType))
w.WriteAttributeString("datatype", Literal.StringType);
if (Literal is StringLiteral StringLiteral &&
!string.IsNullOrEmpty(StringLiteral.Language))
{
w.WriteAttributeString("xml", "lang", null, StringLiteral.Language);
}
else if (Literal is CustomLiteral CustomLiteral &&
!string.IsNullOrEmpty(CustomLiteral.Language))
{
w.WriteAttributeString("xml", "lang", null, CustomLiteral.Language);
}
w.WriteValue(Literal.Value);
w.WriteEndElement();
}
else if (E is UriNode N)
w.WriteElementString("uri", N.Uri.ToString());
else if (E is BlankNode N2)
w.WriteElementString("bnode", N2.NodeId);
else if (E is ISemanticTriple T)
{
w.WriteStartElement("triple");
w.WriteStartElement("subject");
OutputValue(w, T.Subject);
w.WriteEndElement();
w.WriteStartElement("predicate");
OutputValue(w, T.Predicate);
w.WriteEndElement();
w.WriteStartElement("object");
OutputValue(w, T.Object);
w.WriteEndElement();
w.WriteEndElement();
}
else
w.WriteElementString("literal", Value?.ToString() ?? string.Empty);
}
else
w.WriteElementString("literal", Value?.ToString() ?? string.Empty);
}
/// <summary>
/// Tries to get the content type of content of a given file extension.
/// </summary>
/// <param name="FileExtension">File Extension</param>
/// <param name="ContentType">Content Type, if recognized.</param>
/// <returns>If File Extension was recognized and Content Type found.</returns>
public bool TryGetContentType(string FileExtension, out string ContentType)
{
if (string.Compare(FileExtension, SparqlResultSetFileExtensions[0], true) == 0)
{
ContentType = SparqlResultSetContentTypes[0];
return true;
}
else
{
ContentType = null;
return false;
}
}
/// <summary>
/// Tries to get the file extension of content of a given content type.
/// </summary>
/// <param name="ContentType">Content Type</param>
/// <param name="FileExtension">File Extension, if recognized.</param>
/// <returns>If Content Type was recognized and File Extension found.</returns>
public bool TryGetFileExtension(string ContentType, out string FileExtension)
{
if (Array.IndexOf(SparqlResultSetContentTypes, ContentType) >= 0)
{
FileExtension = SparqlResultSetFileExtensions[0];
return true;
}
else
{
FileExtension = null;
return false;
}
}
}
}