-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathRdfDocument.cs
More file actions
935 lines (792 loc) · 26.6 KB
/
RdfDocument.cs
File metadata and controls
935 lines (792 loc) · 26.6 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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
using System;
using System.Collections;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using System.Xml;
using Waher.Content.Getters;
using Waher.Content.Semantic.Model;
using Waher.Content.Semantic.Model.Literals;
using Waher.Content.Semantic.Ontologies;
using Waher.Content.Xml;
using Waher.Runtime.Collections;
using Waher.Runtime.Inventory;
namespace Waher.Content.Semantic
{
/// <summary>
/// Contains semantic information stored in an RDF document.
///
/// Ref: https://www.w3.org/TR/rdf-syntax-grammar/
/// </summary>
public class RdfDocument : InMemorySemanticCube, IWebServerMetaContent
{
/// <summary>
/// rdf:type predicate
/// </summary>
public static UriNode RdfType => new UriNode(Rdf.type, "rdf:type");
/// <summary>
/// Predefined reference to first element in a collection.
/// </summary>
public static UriNode RdfFirst => new UriNode(Rdf.first, "rdf:first");
/// <summary>
/// Predefined reference to next element in a collection.
/// </summary>
public static UriNode RdfRest => new UriNode(Rdf.rest, "rdf:rest");
/// <summary>
/// Predefined reference to end of collection.
/// </summary>
public static UriNode RdfNil => new UriNode(Rdf.nil, "rdf:nil");
/// <summary>
/// Subject reference, during reification.
/// </summary>
public static UriNode RdfSubject => new UriNode(Rdf.subject, "rdf:subject");
/// <summary>
/// Predicate reference, during reification.
/// </summary>
public static UriNode RdfPredicate => new UriNode(Rdf.predicate, "rdf:predicate");
/// <summary>
/// object reference, during reification.
/// </summary>
public static UriNode RdfObject => new UriNode(Rdf.@object, "rdf:object");
/// <summary>
/// Statement reference
/// </summary>
public static UriNode RdfStatement => new UriNode(Rdf.Statement, "rdf:Statement");
/// <summary>
/// Bag reference
/// </summary>
public static UriNode RdfBag => new UriNode(Rdf.Bag, "rdf:Bag");
/// <summary>
/// List item reference.
/// </summary>
public static UriNode RdfLi => new UriNode(Rdf.li, "rdf:li");
private readonly Dictionary<string, ISemanticLiteral> dataTypes = new Dictionary<string, ISemanticLiteral>();
private readonly XmlDocument xml;
private readonly string blankNodeIdPrefix;
private readonly BlankNodeIdMode blankNodeIdMode;
private readonly string text;
private Dictionary<Uri, XmlElement> aboutEach = null;
private Dictionary<Uri, XmlElement> aboutEachPrefix = null;
private DateTimeOffset? date = null;
private int blankNodeIndex = 0;
/// <summary>
/// Contains semantic information stored in a turtle document.
/// </summary>
/// <param name="Xml">XML content of RDF document.</param>
public RdfDocument(XmlDocument Xml)
: this(Xml, Xml.OuterXml, null)
{
}
/// <summary>
/// Contains semantic information stored in a turtle document.
/// </summary>
/// <param name="Xml">XML content of RDF document.</param>
/// <param name="BaseUri">Base URI</param>
public RdfDocument(XmlDocument Xml, Uri BaseUri)
: this(Xml, Xml.OuterXml, BaseUri, "n")
{
}
/// <summary>
/// Contains semantic information stored in a turtle document.
/// </summary>
/// <param name="Xml">XML content of RDF document.</param>
/// <param name="BaseUri">Base URI</param>
/// <param name="BlankNodeIdPrefix">Prefix to use when creating blank nodes.</param>
public RdfDocument(XmlDocument Xml, Uri BaseUri, string BlankNodeIdPrefix)
: this(Xml, Xml.OuterXml, BaseUri, BlankNodeIdPrefix, BlankNodeIdMode.Sequential)
{
}
/// <summary>
/// Contains semantic information stored in a turtle document.
/// </summary>
/// <param name="Xml">XML content of RDF document.</param>
/// <param name="BaseUri">Base URI</param>
/// <param name="BlankNodeIdPrefix">Prefix to use when creating blank nodes.</param>
/// <param name="BlankNodeIdMode">How Blank Node IDs are generated</param>
public RdfDocument(XmlDocument Xml, Uri BaseUri, string BlankNodeIdPrefix, BlankNodeIdMode BlankNodeIdMode)
: this(Xml, Xml.OuterXml, BaseUri, BlankNodeIdPrefix, BlankNodeIdMode)
{
}
/// <summary>
/// Contains semantic information stored in a turtle document.
/// </summary>
/// <param name="Text">Text representation of RDF document.</param>
public RdfDocument(string Text)
: this(ToXml(Text), Text, null)
{
}
/// <summary>
/// Contains semantic information stored in a turtle document.
/// </summary>
/// <param name="Text">Text representation of RDF document.</param>
/// <param name="BaseUri">Base URI</param>
public RdfDocument(string Text, Uri BaseUri)
: this(ToXml(Text), Text, BaseUri, "n")
{
}
/// <summary>
/// Contains semantic information stored in a turtle document.
/// </summary>
/// <param name="Text">Text representation of RDF document.</param>
/// <param name="BaseUri">Base URI</param>
/// <param name="BlankNodeIdPrefix">Prefix to use when creating blank nodes.</param>
public RdfDocument(string Text, Uri BaseUri, string BlankNodeIdPrefix)
: this(ToXml(Text), Text, BaseUri, BlankNodeIdPrefix, BlankNodeIdMode.Sequential)
{
}
/// <summary>
/// Contains semantic information stored in a turtle document.
/// </summary>
/// <param name="Text">Text representation of RDF document.</param>
/// <param name="BaseUri">Base URI</param>
/// <param name="BlankNodeIdPrefix">Prefix to use when creating blank nodes.</param>
/// <param name="BlankNodeIdMode">How Blank Node IDs are generated</param>
public RdfDocument(string Text, Uri BaseUri, string BlankNodeIdPrefix, BlankNodeIdMode BlankNodeIdMode)
: this(ToXml(Text), Text, BaseUri, BlankNodeIdPrefix, BlankNodeIdMode)
{
}
internal static XmlDocument ToXml(string Text)
{
XmlDocument Xml = XML.ParseXml(Text, true);
return Xml;
}
/// <summary>
/// Contains semantic information stored in a turtle document.
/// </summary>
/// <param name="Xml">XML content of RDF document.</param>
/// <param name="Text">Text representation of RDF document.</param>
public RdfDocument(XmlDocument Xml, string Text)
: this(Xml, Text, null)
{
}
/// <summary>
/// Contains semantic information stored in a turtle document.
/// </summary>
/// <param name="Xml">XML content of RDF document.</param>
/// <param name="Text">Text representation of RDF document.</param>
/// <param name="BaseUri">Base URI</param>
public RdfDocument(XmlDocument Xml, string Text, Uri BaseUri)
: this(Xml, Text, BaseUri, "n")
{
}
/// <summary>
/// Contains semantic information stored in a turtle document.
/// </summary>
/// <param name="Xml">XML content of RDF document.</param>
/// <param name="Text">Text representation of RDF document.</param>
/// <param name="BaseUri">Base URI</param>
/// <param name="BlankNodeIdPrefix">Prefix to use when creating blank nodes.</param>
public RdfDocument(XmlDocument Xml, string Text, Uri BaseUri, string BlankNodeIdPrefix)
: this(Xml, Text, BaseUri, BlankNodeIdPrefix, BlankNodeIdMode.Sequential)
{
}
/// <summary>
/// Contains semantic information stored in a turtle document.
/// </summary>
/// <param name="Xml">XML content of RDF document.</param>
/// <param name="Text">Text representation of RDF document.</param>
/// <param name="BaseUri">Base URI</param>
/// <param name="BlankNodeIdPrefix">Prefix to use when creating blank nodes.</param>
/// <param name="BlankNodeIdMode">How Blank Node IDs are generated</param>
public RdfDocument(XmlDocument Xml, string Text, Uri BaseUri, string BlankNodeIdPrefix, BlankNodeIdMode BlankNodeIdMode)
{
this.xml = Xml;
this.blankNodeIdPrefix = BlankNodeIdPrefix;
this.blankNodeIdMode = BlankNodeIdMode;
this.text = Text;
if (Xml is null || Xml.DocumentElement is null)
throw new ArgumentNullException(nameof(Xml));
string Language = null;
string BagId = null;
foreach (XmlAttribute Attr in Xml.DocumentElement.Attributes)
{
if (Attr.Prefix == "xml")
{
switch (Attr.LocalName)
{
case "lang":
Language = Attr.Value;
break;
case "base":
BaseUri = this.CreateUri(Attr.Value, BaseUri);
break;
}
}
else if (Attr.NamespaceURI == Rdf.Namespace)
{
switch (Attr.LocalName)
{
case "bagID":
BagId = Attr.Value;
break;
}
}
}
ISemanticElement Bag;
if (BagId is null)
Bag = null;
else
{
Bag = this.CreateUriNode("#" + BagId, BaseUri);
this.Add(new SemanticTriple(Bag, RdfType, RdfBag));
}
if (Xml.DocumentElement.LocalName == "RDF" && Xml.DocumentElement.NamespaceURI == Rdf.Namespace)
this.ParseDescriptions(Xml.DocumentElement, Language, BaseUri);
else
{
BlankNode RootSubject = this.CreateBlankNode();
UriNode RootType = this.CreateUriNode(Xml.DocumentElement, BaseUri);
this.Add(new SemanticTriple(RootSubject, RdfType, RootType));
int ItemCounter = 0;
this.ParseDescription(Xml.DocumentElement, RootSubject, Language, BaseUri, Bag, null, ref ItemCounter);
}
}
/// <summary>
/// Original XML of document.
/// </summary>
public XmlDocument Xml => this.xml;
/// <summary>
/// Text representation.
/// </summary>
public string Text => this.text;
/// <summary>
/// Server timestamp of document.
/// </summary>
public DateTimeOffset? Date => this.date;
private void ParseDescriptions(XmlElement E, string Language, Uri BaseUri)
{
foreach (XmlNode N in E.ChildNodes)
{
if (!(N is XmlElement E2))
continue;
int ItemCounter = 0;
this.ParseDescription(E2, Language, BaseUri, ref ItemCounter);
}
}
private ISemanticElement ParseDescription(XmlElement E, string Language, Uri BaseUri,
ref int ItemCounter)
{
XmlAttribute About = null;
XmlAttribute Type = null;
XmlAttribute Id = null;
string NodeId = null;
string BagId = null;
string AboutEach = null;
string AboutEachPrefix = null;
ChunkedList<XmlAttribute> Properties = null;
XmlElement ForEach = null;
foreach (XmlAttribute Attr in E.Attributes)
{
if (Attr.NamespaceURI == Rdf.Namespace)
{
switch (Attr.LocalName)
{
case "resource":
case "datatype":
case "parseType":
continue;
case "nodeID":
NodeId = Attr.Value;
continue;
case "about":
About = Attr;
continue;
case "ID":
Id = Attr;
continue;
case "bagID":
BagId = Attr.Value;
continue;
case "type":
Type = Attr;
continue;
case "aboutEach":
AboutEach = Attr.Value;
continue;
case "aboutEachPrefix":
AboutEachPrefix = Attr.Value;
continue;
}
}
else if (Attr.Prefix == "xml")
{
switch (Attr.LocalName)
{
case "lang":
Language = Attr.Value;
break;
case "base":
BaseUri = this.CreateUri(Attr.Value, BaseUri);
break;
}
continue;
}
else if (Attr.Prefix == "xmlns" || Attr.Name == "xmlns")
continue;
if (Properties is null)
Properties = new ChunkedList<XmlAttribute>();
Properties.Add(Attr);
}
if (!(AboutEach is null))
{
if (this.aboutEach is null)
this.aboutEach = new Dictionary<Uri, XmlElement>();
this.aboutEach[this.CreateUri(AboutEach, BaseUri)] = E;
return null;
}
if (!(AboutEachPrefix is null))
{
if (this.aboutEachPrefix is null)
this.aboutEachPrefix = new Dictionary<Uri, XmlElement>();
this.aboutEachPrefix[this.CreateUri(AboutEachPrefix, BaseUri)] = E;
return null;
}
bool HasLanguage = !string.IsNullOrEmpty(Language);
ISemanticElement Subject;
ISemanticElement Bag;
if (BagId is null)
Bag = null;
else
{
Bag = this.CreateUriNode("#" + BagId, BaseUri);
this.Add(new SemanticTriple(Bag, RdfType, RdfBag));
}
if (!(About is null) || !(Id is null))
{
UriNode AboutUriNode = About is null ?
this.CreateUriNode("#" + Id.Value, BaseUri) :
this.CreateUriNode(About.Value, BaseUri);
Subject = AboutUriNode;
if (ForEach is null)
{
if (!(Id is null))
{
if (!(this.aboutEach?.TryGetValue(AboutUriNode.Uri, out ForEach) ?? false) &&
!(this.aboutEachPrefix is null))
{
foreach (KeyValuePair<Uri, XmlElement> P in this.aboutEachPrefix)
{
if (AboutUriNode.Uri.ToString().StartsWith(P.Key.ToString()))
{
ForEach = P.Value;
break;
}
}
}
}
}
}
else if (!(NodeId is null))
Subject = new BlankNode(NodeId);
else
Subject = this.CreateBlankNode();
ISemanticElement DescriptionNode = null;
if (E.NamespaceURI != Rdf.Namespace)
{
DescriptionNode = this.CreateUriNode(E, BaseUri);
this.Add(new SemanticTriple(Subject, RdfType, DescriptionNode));
}
else if (E.LocalName != "Description")
{
if (E.LocalName == "li")
{
string Item = "_" + (++ItemCounter).ToString();
this.Add(new SemanticTriple(Subject, RdfType,
new UriNode(new Uri(Rdf.Namespace + Item), "rdf:" + Item)));
}
else
{
DescriptionNode = this.CreateUriNode(E, BaseUri);
this.Add(new SemanticTriple(Subject, RdfType, DescriptionNode));
}
}
if (!(Bag is null) && !(DescriptionNode is null))
{
ISemanticElement BagDescription = this.CreateBlankNode();
string Item = "_" + (++ItemCounter).ToString();
this.Add(new SemanticTriple(Bag,
new UriNode(new Uri(Rdf.Namespace + Item), "rdf:" + Item),
BagDescription));
this.Add(new SemanticTriple(BagDescription, RdfType, RdfStatement));
this.Add(new SemanticTriple(BagDescription, RdfSubject, Subject));
this.Add(new SemanticTriple(BagDescription, RdfPredicate, RdfType));
this.Add(new SemanticTriple(BagDescription, RdfObject, DescriptionNode));
}
if (!(Type is null))
{
ISemanticElement TypeObject = this.CreateUriNode(Type.Value, BaseUri);
this.Add(new SemanticTriple(Subject, RdfType, TypeObject));
if (!(Bag is null))
{
ISemanticElement ReificationNode = this.CreateBlankNode();
string Item = "_" + (++ItemCounter).ToString();
this.Add(new SemanticTriple(Bag,
new UriNode(new Uri(Rdf.Namespace + Item), "rdf:" + Item),
ReificationNode));
this.Add(new SemanticTriple(ReificationNode, RdfType, RdfStatement));
this.Add(new SemanticTriple(ReificationNode, RdfSubject, Subject));
this.Add(new SemanticTriple(ReificationNode, RdfPredicate, RdfType));
this.Add(new SemanticTriple(ReificationNode, RdfObject, TypeObject));
}
}
if (!(Properties is null))
{
foreach (XmlAttribute Attr in Properties)
{
UriNode Predicate = this.CreateUriNode(Attr, BaseUri);
StringLiteral Object;
if (HasLanguage)
Object = new StringLiteral(Attr.Value, Language);
else
Object = new StringLiteral(Attr.Value);
this.Add(new SemanticTriple(Subject, Predicate, Object));
if (!(Bag is null))
{
ISemanticElement ReificationNode = this.CreateBlankNode();
string Item = "_" + (++ItemCounter).ToString();
this.Add(new SemanticTriple(Bag,
new UriNode(new Uri(Rdf.Namespace + Item), "rdf:" + Item),
ReificationNode));
this.Add(new SemanticTriple(ReificationNode, RdfType, RdfStatement));
this.Add(new SemanticTriple(ReificationNode, RdfSubject, Subject));
this.Add(new SemanticTriple(ReificationNode, RdfPredicate, Predicate));
this.Add(new SemanticTriple(ReificationNode, RdfObject, Object));
}
}
}
int ChildItemCounter = Bag is null ? 0 : ItemCounter;
ISemanticElement Result = this.ParseDescription(E, Subject, Language, BaseUri, Bag, ForEach, ref ChildItemCounter);
return Result;
// TODO: Fix item counters & bags.
//
//if (!(Bag is null))
// ItemCounter = ChildItemCounter;
//
//return Result;
//
//return this.ParseDescription(E, Subject, Language, BaseUri, Bag, ForEach, ref ItemCounter);
}
private ISemanticElement ParseDescription(XmlElement E, ISemanticElement Subject,
string Language, Uri BaseUri, ISemanticElement Bag, XmlElement ForEach,
ref int ItemCounter)
{
foreach (XmlNode N in E.ChildNodes)
{
if (!(N is XmlElement E2))
continue;
UriNode Predicate = this.CreateUriNode(E2, BaseUri);
if (Predicate.UriString.EndsWith("#li") && Predicate.Uri == Rdf.li)
{
string Item = "_" + (++ItemCounter).ToString();
Predicate = new UriNode(new Uri(Rdf.Namespace + Item), "rdf:" + Item);
}
IEnumerable Attributes = ForEach is null ? (IEnumerable)E2.Attributes : new JoinAttributes(E2.Attributes, ForEach.Attributes);
IEnumerable ChildNodes = ForEach is null ? (IEnumerable)E2.ChildNodes : new JoinNodes(E2.ChildNodes, ForEach.ChildNodes);
ChunkedList<XmlAttribute> Properties = null;
ISemanticElement Object = null;
ISemanticElement Bag2 = null;
Uri BaseUri2 = BaseUri;
XmlAttribute Resource = null;
string NodeId = null;
string Id = null;
string DataType = null;
string Language2 = Language;
string ParseType = null;
string BagId = null;
foreach (XmlAttribute Attr in Attributes)
{
if (Attr.NamespaceURI == Rdf.Namespace)
{
switch (Attr.LocalName)
{
case "resource":
Resource = Attr;
continue;
case "nodeID":
NodeId = Attr.Value;
continue;
case "ID":
Id = Attr.Value;
continue;
case "datatype":
DataType = Attr.Value;
continue;
case "parseType":
ParseType = Attr.Value;
continue;
case "bagID":
BagId = Attr.Value;
continue;
case "aboutEach":
case "aboutEachPrefix":
continue;
}
}
else if (Attr.Prefix == "xml")
{
switch (Attr.LocalName)
{
case "lang":
Language2 = Attr.Value;
break;
case "base":
BaseUri2 = this.CreateUri(Attr.Value, BaseUri2);
break;
}
continue;
}
else if (Attr.Prefix == "xmlns" || Attr.Name == "xmlns")
continue;
if (Properties is null)
Properties = new ChunkedList<XmlAttribute>();
Properties.Add(Attr);
}
if (!(BagId is null))
{
Bag2 = this.CreateUriNode("#" + BagId, BaseUri2);
this.Add(new SemanticTriple(Bag2, RdfType, RdfBag));
}
if (!(Resource is null))
{
Object = this.CreateUriNode(Resource.Value, BaseUri2);
this.Add(new SemanticTriple(Subject, Predicate, Object));
}
else if (!(NodeId is null))
{
Object = new BlankNode(NodeId);
this.Add(new SemanticTriple(Subject, Predicate, Object));
}
if (!(Properties is null))
{
if (Object is null)
{
Object = this.CreateBlankNode();
this.Add(new SemanticTriple(Subject, Predicate, Object));
}
bool HasLanguage2 = !string.IsNullOrEmpty(Language2);
foreach (XmlAttribute Attr in Properties)
{
ISemanticElement Predicate2 = this.CreateUriNode(Attr, BaseUri2);
ISemanticElement Literal;
if (HasLanguage2)
Literal = new StringLiteral(Attr.Value, Language2);
else
Literal = new StringLiteral(Attr.Value);
this.Add(new SemanticTriple(Object, Predicate2, Literal));
if (!(Bag2 is null))
{
ISemanticElement ReificationNode = this.CreateBlankNode();
string Item = "_" + (++ItemCounter).ToString();
this.Add(new SemanticTriple(Bag2,
new UriNode(new Uri(Rdf.Namespace + Item), "rdf:" + Item),
ReificationNode));
this.Add(new SemanticTriple(ReificationNode, RdfType, RdfStatement));
this.Add(new SemanticTriple(ReificationNode, RdfSubject, Object));
this.Add(new SemanticTriple(ReificationNode, RdfPredicate, Predicate2));
this.Add(new SemanticTriple(ReificationNode, RdfObject, Literal));
}
}
}
switch (ParseType)
{
case null:
foreach (XmlNode N2 in ChildNodes)
{
if (N2 is XmlText Text)
{
if (DataType is null)
{
if (string.IsNullOrEmpty(Language2))
Object = new StringLiteral(Text.InnerText);
else
Object = new StringLiteral(Text.InnerText, Language2);
}
else
{
Uri DataTypeUri = this.CreateUri(DataType, BaseUri2);
DataType = DataTypeUri.ToString();
if (!this.dataTypes.TryGetValue(DataType, out ISemanticLiteral LiteralType))
{
LiteralType = Types.FindBest<ISemanticLiteral, string>(DataType)
?? new CustomLiteral(string.Empty, DataType, Language2);
this.dataTypes[DataType] = LiteralType;
}
Object = LiteralType.Parse(Text.InnerText, DataType, Language2);
}
this.Add(new SemanticTriple(Subject, Predicate, Object));
}
else if (N2 is XmlElement E3)
{
ISemanticElement Def = this.ParseDescription(E3, Language2, BaseUri2, ref ItemCounter);
if (!(Def is null))
{
if (Object is null || Object.ToString() != Def.ToString())
this.Add(new SemanticTriple(Subject, Predicate, Def));
if (Object is null)
Object = Def;
}
}
}
if (Object is null)
{
if (!(Bag2 is null))
Object = this.CreateBlankNode();
else
Object = new StringLiteral(string.Empty);
this.Add(new SemanticTriple(Subject, Predicate, Object));
}
break;
case "Literal":
default:
Object = new XmlLiteral(ChildNodes, E2.NamespaceURI, Language2);
this.Add(new SemanticTriple(Subject, Predicate, Object));
break;
case "Resource":
Object = this.CreateBlankNode();
this.Add(new SemanticTriple(Subject, Predicate, Object));
int ChildItemCounter = 0;
this.ParseDescription(E2, Object, Language2, BaseUri2, Bag2, null, ref ChildItemCounter);
break;
case "Collection":
ChunkedList<ISemanticElement> Elements = null;
foreach (XmlNode N2 in ChildNodes)
{
ISemanticElement Element;
if (N2 is XmlText Text)
{
if (DataType is null)
{
if (string.IsNullOrEmpty(Language2))
Element = new StringLiteral(Text.InnerText);
else
Element = new StringLiteral(Text.InnerText, Language2);
}
else
{
Uri DataTypeUri = this.CreateUri(DataType, BaseUri2);
DataType = DataTypeUri.ToString();
if (!this.dataTypes.TryGetValue(DataType, out ISemanticLiteral LiteralType))
{
LiteralType = Types.FindBest<ISemanticLiteral, string>(DataType)
?? new CustomLiteral(string.Empty, DataType);
this.dataTypes[DataType] = LiteralType;
}
Element = LiteralType.Parse(Text.InnerText, DataType, Language2);
}
}
else if (N2 is XmlElement E3)
Element = this.ParseDescription(E3, Language2, BaseUri2, ref ItemCounter);
else
continue;
if (Elements is null)
Elements = new ChunkedList<ISemanticElement>();
if (!(Element is null))
Elements.Add(Element);
}
if (Elements is null)
this.Add(new SemanticTriple(Subject, Predicate, RdfNil));
else
{
ChunkNode<ISemanticElement> Loop = Elements.FirstChunk;
BlankNode Current = this.CreateBlankNode();
int i, c;
this.Add(new SemanticTriple(Subject, Predicate, Current));
while (!(Loop is null))
{
for (i = Loop.Start, c = Loop.Pos; i < c; i++)
{
this.Add(new SemanticTriple(Current, RdfFirst, Loop[i]));
if (i < c - 1 || !(Loop.Next is null))
{
BlankNode Next = this.CreateBlankNode();
this.Add(new SemanticTriple(Current, RdfRest, Next));
Current = Next;
}
}
Loop = Loop.Next;
}
this.Add(new SemanticTriple(Current, RdfRest, RdfNil));
}
break;
}
if (!(Id is null) || !(Bag is null))
{
ISemanticElement ReificationNode;
if (!(Id is null))
ReificationNode = this.CreateUriNode("#" + Id, BaseUri2);
else
ReificationNode = this.CreateBlankNode();
if (!(Bag is null))
{
string Item = "_" + (++ItemCounter).ToString();
this.Add(new SemanticTriple(Bag,
new UriNode(new Uri(Rdf.Namespace + Item), "rdf:" + Item),
ReificationNode));
}
this.Add(new SemanticTriple(ReificationNode, RdfType, RdfStatement));
this.Add(new SemanticTriple(ReificationNode, RdfSubject, Subject));
this.Add(new SemanticTriple(ReificationNode, RdfPredicate, Predicate));
this.Add(new SemanticTriple(ReificationNode, RdfObject, Object));
}
}
return Subject;
}
private UriNode CreateUriNode(string Reference, Uri BaseUri)
{
return new UriNode(this.CreateUri(Reference, BaseUri), Reference);
}
private UriNode CreateUriNode(XmlElement Reference, Uri BaseUri)
{
if (string.IsNullOrEmpty(Reference.Prefix))
return new UriNode(this.CreateUri(Reference.NamespaceURI + Reference.LocalName, BaseUri), Reference.LocalName);
else
return new UriNode(this.CreateUri(Reference.NamespaceURI + Reference.LocalName, BaseUri), Reference.Prefix + ":" + Reference.LocalName);
}
private UriNode CreateUriNode(XmlAttribute Reference, Uri BaseUri)
{
if (string.IsNullOrEmpty(Reference.Prefix))
return new UriNode(this.CreateUri(Reference.NamespaceURI + Reference.LocalName, BaseUri), Reference.LocalName);
else
return new UriNode(this.CreateUri(Reference.NamespaceURI + Reference.LocalName, BaseUri), Reference.Prefix + ":" + Reference.LocalName);
}
private Uri CreateUri(string Reference, Uri BaseUri)
{
if (BaseUri is null)
{
if (Uri.TryCreate(Reference, UriKind.Absolute, out Uri URI))
return URI;
else
throw this.ParsingException("Invalid URI: " + Reference);
}
else
{
if (string.IsNullOrEmpty(Reference))
return BaseUri;
else if (Uri.TryCreate(BaseUri, Reference, out Uri URI))
return URI;
else
throw this.ParsingException("Invalid URI: " + Reference + " (base: " + BaseUri.ToString() + ")");
}
}
private BlankNode CreateBlankNode()
{
if (this.blankNodeIdMode == BlankNodeIdMode.Guid)
return new BlankNode(this.blankNodeIdPrefix + Guid.NewGuid().ToString());
else
return new BlankNode(this.blankNodeIdPrefix + (++this.blankNodeIndex).ToString());
}
private Exception ParsingException(string Message)
{
return new Exception(Message);
}
/// <summary>
/// Decodes meta-information available in the HTTP Response.
/// </summary>
/// <param name="HttpResponse">HTTP Response.</param>
public Task DecodeMetaInformation(HttpResponseMessage HttpResponse)
{
this.date = HttpResponse.Headers.Date;
return Task.CompletedTask;
}
}
}