Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
RdfDocument.cs
1using System;
4using System.Net.Http;
5using System.Threading.Tasks;
6using System.Xml;
14
16{
23 {
27 public static UriNode RdfType => new UriNode(Rdf.type, "rdf:type");
28
32 public static UriNode RdfFirst => new UriNode(Rdf.first, "rdf:first");
33
37 public static UriNode RdfRest => new UriNode(Rdf.rest, "rdf:rest");
38
42 public static UriNode RdfNil => new UriNode(Rdf.nil, "rdf:nil");
43
47 public static UriNode RdfSubject => new UriNode(Rdf.subject, "rdf:subject");
48
52 public static UriNode RdfPredicate => new UriNode(Rdf.predicate, "rdf:predicate");
53
57 public static UriNode RdfObject => new UriNode(Rdf.@object, "rdf:object");
58
62 public static UriNode RdfStatement => new UriNode(Rdf.Statement, "rdf:Statement");
63
67 public static UriNode RdfBag => new UriNode(Rdf.Bag, "rdf:Bag");
68
72 public static UriNode RdfLi => new UriNode(Rdf.li, "rdf:li");
73
74 private readonly Dictionary<string, ISemanticLiteral> dataTypes = new Dictionary<string, ISemanticLiteral>();
75 private readonly XmlDocument xml;
76 private readonly string blankNodeIdPrefix;
77 private readonly BlankNodeIdMode blankNodeIdMode;
78 private readonly string text;
79 private Dictionary<Uri, XmlElement> aboutEach = null;
80 private Dictionary<Uri, XmlElement> aboutEachPrefix = null;
81 private DateTimeOffset? date = null;
82 private int blankNodeIndex = 0;
83
88 public RdfDocument(XmlDocument Xml)
89 : this(Xml, Xml.OuterXml, null)
90 {
91 }
92
98 public RdfDocument(XmlDocument Xml, Uri BaseUri)
99 : this(Xml, Xml.OuterXml, BaseUri, "n")
100 {
101 }
102
109 public RdfDocument(XmlDocument Xml, Uri BaseUri, string BlankNodeIdPrefix)
110 : this(Xml, Xml.OuterXml, BaseUri, BlankNodeIdPrefix, BlankNodeIdMode.Sequential)
111 {
112 }
113
121 public RdfDocument(XmlDocument Xml, Uri BaseUri, string BlankNodeIdPrefix, BlankNodeIdMode BlankNodeIdMode)
122 : this(Xml, Xml.OuterXml, BaseUri, BlankNodeIdPrefix, BlankNodeIdMode)
123 {
124 }
125
130 public RdfDocument(string Text)
131 : this(ToXml(Text), Text, null)
132 {
133 }
134
140 public RdfDocument(string Text, Uri BaseUri)
141 : this(ToXml(Text), Text, BaseUri, "n")
142 {
143 }
144
151 public RdfDocument(string Text, Uri BaseUri, string BlankNodeIdPrefix)
152 : this(ToXml(Text), Text, BaseUri, BlankNodeIdPrefix, BlankNodeIdMode.Sequential)
153 {
154 }
155
163 public RdfDocument(string Text, Uri BaseUri, string BlankNodeIdPrefix, BlankNodeIdMode BlankNodeIdMode)
164 : this(ToXml(Text), Text, BaseUri, BlankNodeIdPrefix, BlankNodeIdMode)
165 {
166 }
167
168 internal static XmlDocument ToXml(string Text)
169 {
170 XmlDocument Xml = XML.ParseXml(Text, true);
171
172 return Xml;
173 }
174
180 public RdfDocument(XmlDocument Xml, string Text)
181 : this(Xml, Text, null)
182 {
183 }
184
191 public RdfDocument(XmlDocument Xml, string Text, Uri BaseUri)
192 : this(Xml, Text, BaseUri, "n")
193 {
194 }
195
203 public RdfDocument(XmlDocument Xml, string Text, Uri BaseUri, string BlankNodeIdPrefix)
204 : this(Xml, Text, BaseUri, BlankNodeIdPrefix, BlankNodeIdMode.Sequential)
205 {
206 }
207
216 public RdfDocument(XmlDocument Xml, string Text, Uri BaseUri, string BlankNodeIdPrefix, BlankNodeIdMode BlankNodeIdMode)
217 {
218 this.xml = Xml;
219 this.blankNodeIdPrefix = BlankNodeIdPrefix;
220 this.blankNodeIdMode = BlankNodeIdMode;
221 this.text = Text;
222
223 if (Xml is null || Xml.DocumentElement is null)
224 throw new ArgumentNullException(nameof(Xml));
225
226 string Language = null;
227 string BagId = null;
228
229 foreach (XmlAttribute Attr in Xml.DocumentElement.Attributes)
230 {
231 if (Attr.Prefix == "xml")
232 {
233 switch (Attr.LocalName)
234 {
235 case "lang":
236 Language = Attr.Value;
237 break;
238
239 case "base":
240 BaseUri = this.CreateUri(Attr.Value, BaseUri);
241 break;
242 }
243 }
244 else if (Attr.NamespaceURI == Rdf.Namespace)
245 {
246 switch (Attr.LocalName)
247 {
248 case "bagID":
249 BagId = Attr.Value;
250 break;
251 }
252 }
253 }
254
256
257 if (BagId is null)
258 Bag = null;
259 else
260 {
261 Bag = this.CreateUriNode("#" + BagId, BaseUri);
262 this.Add(new SemanticTriple(Bag, RdfType, RdfBag));
263 }
264
265 if (Xml.DocumentElement.LocalName == "RDF" && Xml.DocumentElement.NamespaceURI == Rdf.Namespace)
266 this.ParseDescriptions(Xml.DocumentElement, Language, BaseUri);
267 else
268 {
269 BlankNode RootSubject = this.CreateBlankNode();
270 UriNode RootType = this.CreateUriNode(Xml.DocumentElement, BaseUri);
271
272 this.Add(new SemanticTriple(RootSubject, RdfType, RootType));
273
274 int ItemCounter = 0;
275 this.ParseDescription(Xml.DocumentElement, RootSubject, Language, BaseUri, Bag, null, ref ItemCounter);
276 }
277 }
278
282 public XmlDocument Xml => this.xml;
283
287 public string Text => this.text;
288
292 public DateTimeOffset? Date => this.date;
293
294 private void ParseDescriptions(XmlElement E, string Language, Uri BaseUri)
295 {
296 foreach (XmlNode N in E.ChildNodes)
297 {
298 if (!(N is XmlElement E2))
299 continue;
300
301 int ItemCounter = 0;
302 this.ParseDescription(E2, Language, BaseUri, ref ItemCounter);
303 }
304 }
305
306 private ISemanticElement ParseDescription(XmlElement E, string Language, Uri BaseUri,
307 ref int ItemCounter)
308 {
309 XmlAttribute About = null;
310 XmlAttribute Type = null;
311 XmlAttribute Id = null;
312 string NodeId = null;
313 string BagId = null;
314 string AboutEach = null;
315 string AboutEachPrefix = null;
316 ChunkedList<XmlAttribute> Properties = null;
317 XmlElement ForEach = null;
318
319 foreach (XmlAttribute Attr in E.Attributes)
320 {
321 if (Attr.NamespaceURI == Rdf.Namespace)
322 {
323 switch (Attr.LocalName)
324 {
325 case "resource":
326 case "datatype":
327 case "parseType":
328 continue;
329
330 case "nodeID":
331 NodeId = Attr.Value;
332 continue;
333
334 case "about":
335 About = Attr;
336 continue;
337
338 case "ID":
339 Id = Attr;
340 continue;
341
342 case "bagID":
343 BagId = Attr.Value;
344 continue;
345
346 case "type":
347 Type = Attr;
348 continue;
349
350 case "aboutEach":
351 AboutEach = Attr.Value;
352 continue;
353
354 case "aboutEachPrefix":
355 AboutEachPrefix = Attr.Value;
356 continue;
357 }
358 }
359 else if (Attr.Prefix == "xml")
360 {
361 switch (Attr.LocalName)
362 {
363 case "lang":
364 Language = Attr.Value;
365 break;
366
367 case "base":
368 BaseUri = this.CreateUri(Attr.Value, BaseUri);
369 break;
370 }
371
372 continue;
373 }
374 else if (Attr.Prefix == "xmlns" || Attr.Name == "xmlns")
375 continue;
376
377 if (Properties is null)
378 Properties = new ChunkedList<XmlAttribute>();
379
380 Properties.Add(Attr);
381 }
382
383 if (!(AboutEach is null))
384 {
385 if (this.aboutEach is null)
386 this.aboutEach = new Dictionary<Uri, XmlElement>();
387
388 this.aboutEach[this.CreateUri(AboutEach, BaseUri)] = E;
389 return null;
390 }
391
392 if (!(AboutEachPrefix is null))
393 {
394 if (this.aboutEachPrefix is null)
395 this.aboutEachPrefix = new Dictionary<Uri, XmlElement>();
396
397 this.aboutEachPrefix[this.CreateUri(AboutEachPrefix, BaseUri)] = E;
398 return null;
399 }
400
401 bool HasLanguage = !string.IsNullOrEmpty(Language);
402 ISemanticElement Subject;
404
405 if (BagId is null)
406 Bag = null;
407 else
408 {
409 Bag = this.CreateUriNode("#" + BagId, BaseUri);
410 this.Add(new SemanticTriple(Bag, RdfType, RdfBag));
411 }
412
413 if (!(About is null) || !(Id is null))
414 {
415 UriNode AboutUriNode = About is null ?
416 this.CreateUriNode("#" + Id.Value, BaseUri) :
417 this.CreateUriNode(About.Value, BaseUri);
418 Subject = AboutUriNode;
419
420 if (ForEach is null)
421 {
422 if (!(Id is null))
423 {
424 if (!(this.aboutEach?.TryGetValue(AboutUriNode.Uri, out ForEach) ?? false) &&
425 !(this.aboutEachPrefix is null))
426 {
427 foreach (KeyValuePair<Uri, XmlElement> P in this.aboutEachPrefix)
428 {
429 if (AboutUriNode.Uri.ToString().StartsWith(P.Key.ToString()))
430 {
431 ForEach = P.Value;
432 break;
433 }
434 }
435 }
436 }
437 }
438 }
439 else if (!(NodeId is null))
440 Subject = new BlankNode(NodeId);
441 else
442 Subject = this.CreateBlankNode();
443
444 ISemanticElement DescriptionNode = null;
445
446 if (E.NamespaceURI != Rdf.Namespace)
447 {
448 DescriptionNode = this.CreateUriNode(E, BaseUri);
449 this.Add(new SemanticTriple(Subject, RdfType, DescriptionNode));
450 }
451 else if (E.LocalName != "Description")
452 {
453 if (E.LocalName == "li")
454 {
455 string Item = "_" + (++ItemCounter).ToString();
456 this.Add(new SemanticTriple(Subject, RdfType,
457 new UriNode(new Uri(Rdf.Namespace + Item), "rdf:" + Item)));
458 }
459 else
460 {
461 DescriptionNode = this.CreateUriNode(E, BaseUri);
462 this.Add(new SemanticTriple(Subject, RdfType, DescriptionNode));
463 }
464 }
465
466 if (!(Bag is null) && !(DescriptionNode is null))
467 {
468 ISemanticElement BagDescription = this.CreateBlankNode();
469
470 string Item = "_" + (++ItemCounter).ToString();
471 this.Add(new SemanticTriple(Bag,
472 new UriNode(new Uri(Rdf.Namespace + Item), "rdf:" + Item),
473 BagDescription));
474
475 this.Add(new SemanticTriple(BagDescription, RdfType, RdfStatement));
476 this.Add(new SemanticTriple(BagDescription, RdfSubject, Subject));
477 this.Add(new SemanticTriple(BagDescription, RdfPredicate, RdfType));
478 this.Add(new SemanticTriple(BagDescription, RdfObject, DescriptionNode));
479 }
480
481 if (!(Type is null))
482 {
483 ISemanticElement TypeObject = this.CreateUriNode(Type.Value, BaseUri);
484
485 this.Add(new SemanticTriple(Subject, RdfType, TypeObject));
486
487 if (!(Bag is null))
488 {
489 ISemanticElement ReificationNode = this.CreateBlankNode();
490
491 string Item = "_" + (++ItemCounter).ToString();
492 this.Add(new SemanticTriple(Bag,
493 new UriNode(new Uri(Rdf.Namespace + Item), "rdf:" + Item),
494 ReificationNode));
495
496 this.Add(new SemanticTriple(ReificationNode, RdfType, RdfStatement));
497 this.Add(new SemanticTriple(ReificationNode, RdfSubject, Subject));
498 this.Add(new SemanticTriple(ReificationNode, RdfPredicate, RdfType));
499 this.Add(new SemanticTriple(ReificationNode, RdfObject, TypeObject));
500 }
501 }
502
503 if (!(Properties is null))
504 {
505 foreach (XmlAttribute Attr in Properties)
506 {
507 UriNode Predicate = this.CreateUriNode(Attr, BaseUri);
508 StringLiteral Object;
509
510 if (HasLanguage)
511 Object = new StringLiteral(Attr.Value, Language);
512 else
513 Object = new StringLiteral(Attr.Value);
514
515 this.Add(new SemanticTriple(Subject, Predicate, Object));
516
517 if (!(Bag is null))
518 {
519 ISemanticElement ReificationNode = this.CreateBlankNode();
520
521 string Item = "_" + (++ItemCounter).ToString();
522 this.Add(new SemanticTriple(Bag,
523 new UriNode(new Uri(Rdf.Namespace + Item), "rdf:" + Item),
524 ReificationNode));
525
526 this.Add(new SemanticTriple(ReificationNode, RdfType, RdfStatement));
527 this.Add(new SemanticTriple(ReificationNode, RdfSubject, Subject));
528 this.Add(new SemanticTriple(ReificationNode, RdfPredicate, Predicate));
529 this.Add(new SemanticTriple(ReificationNode, RdfObject, Object));
530 }
531 }
532 }
533
534 int ChildItemCounter = Bag is null ? 0 : ItemCounter;
535
536 ISemanticElement Result = this.ParseDescription(E, Subject, Language, BaseUri, Bag, ForEach, ref ChildItemCounter);
537 return Result;
538
539 // TODO: Fix item counters & bags.
540
541 //
542 //if (!(Bag is null))
543 // ItemCounter = ChildItemCounter;
544 //
545 //return Result;
546 //
547 //return this.ParseDescription(E, Subject, Language, BaseUri, Bag, ForEach, ref ItemCounter);
548 }
549
550 private ISemanticElement ParseDescription(XmlElement E, ISemanticElement Subject,
551 string Language, Uri BaseUri, ISemanticElement Bag, XmlElement ForEach,
552 ref int ItemCounter)
553 {
554 foreach (XmlNode N in E.ChildNodes)
555 {
556 if (!(N is XmlElement E2))
557 continue;
558
559 UriNode Predicate = this.CreateUriNode(E2, BaseUri);
560 if (Predicate.UriString.EndsWith("#li") && Predicate.Uri == Rdf.li)
561 {
562 string Item = "_" + (++ItemCounter).ToString();
563 Predicate = new UriNode(new Uri(Rdf.Namespace + Item), "rdf:" + Item);
564 }
565
566 IEnumerable Attributes = ForEach is null ? (IEnumerable)E2.Attributes : new JoinAttributes(E2.Attributes, ForEach.Attributes);
567 IEnumerable ChildNodes = ForEach is null ? (IEnumerable)E2.ChildNodes : new JoinNodes(E2.ChildNodes, ForEach.ChildNodes);
568 ChunkedList<XmlAttribute> Properties = null;
569 ISemanticElement Object = null;
570 ISemanticElement Bag2 = null;
571 Uri BaseUri2 = BaseUri;
572 XmlAttribute Resource = null;
573 string NodeId = null;
574 string Id = null;
575 string DataType = null;
576 string Language2 = Language;
577 string ParseType = null;
578 string BagId = null;
579
580 foreach (XmlAttribute Attr in Attributes)
581 {
582 if (Attr.NamespaceURI == Rdf.Namespace)
583 {
584 switch (Attr.LocalName)
585 {
586 case "resource":
587 Resource = Attr;
588 continue;
589
590 case "nodeID":
591 NodeId = Attr.Value;
592 continue;
593
594 case "ID":
595 Id = Attr.Value;
596 continue;
597
598 case "datatype":
599 DataType = Attr.Value;
600 continue;
601
602 case "parseType":
603 ParseType = Attr.Value;
604 continue;
605
606 case "bagID":
607 BagId = Attr.Value;
608 continue;
609
610 case "aboutEach":
611 case "aboutEachPrefix":
612 continue;
613 }
614 }
615 else if (Attr.Prefix == "xml")
616 {
617 switch (Attr.LocalName)
618 {
619 case "lang":
620 Language2 = Attr.Value;
621 break;
622
623 case "base":
624 BaseUri2 = this.CreateUri(Attr.Value, BaseUri2);
625 break;
626 }
627
628 continue;
629 }
630 else if (Attr.Prefix == "xmlns" || Attr.Name == "xmlns")
631 continue;
632
633 if (Properties is null)
634 Properties = new ChunkedList<XmlAttribute>();
635
636 Properties.Add(Attr);
637 }
638
639 if (!(BagId is null))
640 {
641 Bag2 = this.CreateUriNode("#" + BagId, BaseUri2);
642 this.Add(new SemanticTriple(Bag2, RdfType, RdfBag));
643 }
644
645 if (!(Resource is null))
646 {
647 Object = this.CreateUriNode(Resource.Value, BaseUri2);
648 this.Add(new SemanticTriple(Subject, Predicate, Object));
649 }
650 else if (!(NodeId is null))
651 {
652 Object = new BlankNode(NodeId);
653 this.Add(new SemanticTriple(Subject, Predicate, Object));
654 }
655
656 if (!(Properties is null))
657 {
658 if (Object is null)
659 {
660 Object = this.CreateBlankNode();
661 this.Add(new SemanticTriple(Subject, Predicate, Object));
662 }
663
664 bool HasLanguage2 = !string.IsNullOrEmpty(Language2);
665
666 foreach (XmlAttribute Attr in Properties)
667 {
668 ISemanticElement Predicate2 = this.CreateUriNode(Attr, BaseUri2);
669 ISemanticElement Literal;
670
671 if (HasLanguage2)
672 Literal = new StringLiteral(Attr.Value, Language2);
673 else
674 Literal = new StringLiteral(Attr.Value);
675
676 this.Add(new SemanticTriple(Object, Predicate2, Literal));
677
678 if (!(Bag2 is null))
679 {
680 ISemanticElement ReificationNode = this.CreateBlankNode();
681
682 string Item = "_" + (++ItemCounter).ToString();
683 this.Add(new SemanticTriple(Bag2,
684 new UriNode(new Uri(Rdf.Namespace + Item), "rdf:" + Item),
685 ReificationNode));
686
687 this.Add(new SemanticTriple(ReificationNode, RdfType, RdfStatement));
688 this.Add(new SemanticTriple(ReificationNode, RdfSubject, Object));
689 this.Add(new SemanticTriple(ReificationNode, RdfPredicate, Predicate2));
690 this.Add(new SemanticTriple(ReificationNode, RdfObject, Literal));
691 }
692 }
693 }
694
695 switch (ParseType)
696 {
697 case null:
698 foreach (XmlNode N2 in ChildNodes)
699 {
700 if (N2 is XmlText Text)
701 {
702 if (DataType is null)
703 {
704 if (string.IsNullOrEmpty(Language2))
705 Object = new StringLiteral(Text.InnerText);
706 else
707 Object = new StringLiteral(Text.InnerText, Language2);
708 }
709 else
710 {
711 Uri DataTypeUri = this.CreateUri(DataType, BaseUri2);
712 DataType = DataTypeUri.ToString();
713
714 if (!this.dataTypes.TryGetValue(DataType, out ISemanticLiteral LiteralType))
715 {
716 LiteralType = Types.FindBest<ISemanticLiteral, string>(DataType)
717 ?? new CustomLiteral(string.Empty, DataType, Language2);
718
719 this.dataTypes[DataType] = LiteralType;
720 }
721
722 Object = LiteralType.Parse(Text.InnerText, DataType, Language2);
723 }
724
725 this.Add(new SemanticTriple(Subject, Predicate, Object));
726 }
727 else if (N2 is XmlElement E3)
728 {
729 ISemanticElement Def = this.ParseDescription(E3, Language2, BaseUri2, ref ItemCounter);
730
731 if (!(Def is null))
732 {
733 if (Object is null || Object.ToString() != Def.ToString())
734 this.Add(new SemanticTriple(Subject, Predicate, Def));
735
736 if (Object is null)
737 Object = Def;
738 }
739 }
740 }
741
742 if (Object is null)
743 {
744 if (!(Bag2 is null))
745 Object = this.CreateBlankNode();
746 else
747 Object = new StringLiteral(string.Empty);
748
749 this.Add(new SemanticTriple(Subject, Predicate, Object));
750 }
751 break;
752
753 case "Literal":
754 default:
755 Object = new XmlLiteral(ChildNodes, E2.NamespaceURI, Language2);
756 this.Add(new SemanticTriple(Subject, Predicate, Object));
757 break;
758
759 case "Resource":
760 Object = this.CreateBlankNode();
761 this.Add(new SemanticTriple(Subject, Predicate, Object));
762
763 int ChildItemCounter = 0;
764 this.ParseDescription(E2, Object, Language2, BaseUri2, Bag2, null, ref ChildItemCounter);
765 break;
766
767 case "Collection":
768 ChunkedList<ISemanticElement> Elements = null;
769
770 foreach (XmlNode N2 in ChildNodes)
771 {
772 ISemanticElement Element;
773
774 if (N2 is XmlText Text)
775 {
776 if (DataType is null)
777 {
778 if (string.IsNullOrEmpty(Language2))
779 Element = new StringLiteral(Text.InnerText);
780 else
781 Element = new StringLiteral(Text.InnerText, Language2);
782 }
783 else
784 {
785 Uri DataTypeUri = this.CreateUri(DataType, BaseUri2);
786 DataType = DataTypeUri.ToString();
787
788 if (!this.dataTypes.TryGetValue(DataType, out ISemanticLiteral LiteralType))
789 {
790 LiteralType = Types.FindBest<ISemanticLiteral, string>(DataType)
791 ?? new CustomLiteral(string.Empty, DataType);
792
793 this.dataTypes[DataType] = LiteralType;
794 }
795
796 Element = LiteralType.Parse(Text.InnerText, DataType, Language2);
797 }
798 }
799 else if (N2 is XmlElement E3)
800 Element = this.ParseDescription(E3, Language2, BaseUri2, ref ItemCounter);
801 else
802 continue;
803
804 if (Elements is null)
805 Elements = new ChunkedList<ISemanticElement>();
806
807 if (!(Element is null))
808 Elements.Add(Element);
809 }
810
811 if (Elements is null)
812 this.Add(new SemanticTriple(Subject, Predicate, RdfNil));
813 else
814 {
816 BlankNode Current = this.CreateBlankNode();
817 int i, c;
818
819 this.Add(new SemanticTriple(Subject, Predicate, Current));
820
821 while (!(Loop is null))
822 {
823 for (i = Loop.Start, c = Loop.Pos; i < c; i++)
824 {
825 this.Add(new SemanticTriple(Current, RdfFirst, Loop[i]));
826
827 if (i < c - 1 || !(Loop.Next is null))
828 {
829 BlankNode Next = this.CreateBlankNode();
830 this.Add(new SemanticTriple(Current, RdfRest, Next));
831 Current = Next;
832 }
833 }
834
835 Loop = Loop.Next;
836 }
837
838 this.Add(new SemanticTriple(Current, RdfRest, RdfNil));
839 }
840 break;
841 }
842
843
844 if (!(Id is null) || !(Bag is null))
845 {
846 ISemanticElement ReificationNode;
847
848 if (!(Id is null))
849 ReificationNode = this.CreateUriNode("#" + Id, BaseUri2);
850 else
851 ReificationNode = this.CreateBlankNode();
852
853 if (!(Bag is null))
854 {
855 string Item = "_" + (++ItemCounter).ToString();
856 this.Add(new SemanticTriple(Bag,
857 new UriNode(new Uri(Rdf.Namespace + Item), "rdf:" + Item),
858 ReificationNode));
859 }
860
861 this.Add(new SemanticTriple(ReificationNode, RdfType, RdfStatement));
862 this.Add(new SemanticTriple(ReificationNode, RdfSubject, Subject));
863 this.Add(new SemanticTriple(ReificationNode, RdfPredicate, Predicate));
864 this.Add(new SemanticTriple(ReificationNode, RdfObject, Object));
865 }
866 }
867
868 return Subject;
869 }
870
871 private UriNode CreateUriNode(string Reference, Uri BaseUri)
872 {
873 return new UriNode(this.CreateUri(Reference, BaseUri), Reference);
874 }
875
876 private UriNode CreateUriNode(XmlElement Reference, Uri BaseUri)
877 {
878 if (string.IsNullOrEmpty(Reference.Prefix))
879 return new UriNode(this.CreateUri(Reference.NamespaceURI + Reference.LocalName, BaseUri), Reference.LocalName);
880 else
881 return new UriNode(this.CreateUri(Reference.NamespaceURI + Reference.LocalName, BaseUri), Reference.Prefix + ":" + Reference.LocalName);
882 }
883
884 private UriNode CreateUriNode(XmlAttribute Reference, Uri BaseUri)
885 {
886 if (string.IsNullOrEmpty(Reference.Prefix))
887 return new UriNode(this.CreateUri(Reference.NamespaceURI + Reference.LocalName, BaseUri), Reference.LocalName);
888 else
889 return new UriNode(this.CreateUri(Reference.NamespaceURI + Reference.LocalName, BaseUri), Reference.Prefix + ":" + Reference.LocalName);
890 }
891
892 private Uri CreateUri(string Reference, Uri BaseUri)
893 {
894 if (BaseUri is null)
895 {
896 if (Uri.TryCreate(Reference, UriKind.Absolute, out Uri URI))
897 return URI;
898 else
899 throw this.ParsingException("Invalid URI: " + Reference);
900 }
901 else
902 {
903 if (string.IsNullOrEmpty(Reference))
904 return BaseUri;
905 else if (Uri.TryCreate(BaseUri, Reference, out Uri URI))
906 return URI;
907 else
908 throw this.ParsingException("Invalid URI: " + Reference + " (base: " + BaseUri.ToString() + ")");
909 }
910 }
911
912 private BlankNode CreateBlankNode()
913 {
914 if (this.blankNodeIdMode == BlankNodeIdMode.Guid)
915 return new BlankNode(this.blankNodeIdPrefix + Guid.NewGuid().ToString());
916 else
917 return new BlankNode(this.blankNodeIdPrefix + (++this.blankNodeIndex).ToString());
918 }
919
920 private Exception ParsingException(string Message)
921 {
922 return new Exception(Message);
923 }
924
929 public Task DecodeMetaInformation(HttpResponseMessage HttpResponse)
930 {
931 this.date = HttpResponse.Headers.Date;
932 return Task.CompletedTask;
933 }
934 }
935}
override void Add(ISemanticTriple Triple)
Adds a triple to the cube.
Represents a blank node
Definition: BlankNode.cs:7
Joins attributes from two XML elements.
Joins two sets of XML nodes.
Definition: JoinNodes.cs:10
static readonly Uri nil
URI representing rdf:nil
Definition: Rdf.cs:66
static readonly Uri Bag
URI representing rdf:Bag, an unordered container.
Definition: Rdf.cs:96
static readonly Uri type
URI representing rdf:type
Definition: Rdf.cs:51
static readonly Uri first
URI representing rdf:first
Definition: Rdf.cs:56
static readonly Uri rest
URI representing rdf:rest
Definition: Rdf.cs:61
static readonly Uri subject
URI representing rdf:subject
Definition: Rdf.cs:71
const string Namespace
http://www.w3.org/1999/02/22-rdf-syntax-ns#
Definition: Rdf.cs:46
static readonly Uri li
URI representing rdf:li
Definition: Rdf.cs:112
static readonly Uri predicate
URI representing rdf:predicate
Definition: Rdf.cs:76
static readonly Uri Statement
URI representing rdf:Statement
Definition: Rdf.cs:86
Contains semantic information stored in an RDF document.
Definition: RdfDocument.cs:23
static UriNode RdfLi
List item reference.
Definition: RdfDocument.cs:72
DateTimeOffset? Date
Server timestamp of document.
Definition: RdfDocument.cs:292
RdfDocument(XmlDocument Xml)
Contains semantic information stored in a turtle document.
Definition: RdfDocument.cs:88
static UriNode RdfPredicate
Predicate reference, during reification.
Definition: RdfDocument.cs:52
static UriNode RdfStatement
Statement reference
Definition: RdfDocument.cs:62
static UriNode RdfBag
Bag reference
Definition: RdfDocument.cs:67
Task DecodeMetaInformation(HttpResponseMessage HttpResponse)
Decodes meta-information available in the HTTP Response.
Definition: RdfDocument.cs:929
RdfDocument(string Text, Uri BaseUri, string BlankNodeIdPrefix, BlankNodeIdMode BlankNodeIdMode)
Contains semantic information stored in a turtle document.
Definition: RdfDocument.cs:163
RdfDocument(string Text)
Contains semantic information stored in a turtle document.
Definition: RdfDocument.cs:130
RdfDocument(XmlDocument Xml, Uri BaseUri)
Contains semantic information stored in a turtle document.
Definition: RdfDocument.cs:98
RdfDocument(XmlDocument Xml, string Text)
Contains semantic information stored in a turtle document.
Definition: RdfDocument.cs:180
static UriNode RdfObject
object reference, during reification.
Definition: RdfDocument.cs:57
static UriNode RdfType
rdf:type predicate
Definition: RdfDocument.cs:27
static UriNode RdfNil
Predefined reference to end of collection.
Definition: RdfDocument.cs:42
static UriNode RdfRest
Predefined reference to next element in a collection.
Definition: RdfDocument.cs:37
static UriNode RdfFirst
Predefined reference to first element in a collection.
Definition: RdfDocument.cs:32
string Text
Text representation.
Definition: RdfDocument.cs:287
RdfDocument(string Text, Uri BaseUri)
Contains semantic information stored in a turtle document.
Definition: RdfDocument.cs:140
RdfDocument(string Text, Uri BaseUri, string BlankNodeIdPrefix)
Contains semantic information stored in a turtle document.
Definition: RdfDocument.cs:151
RdfDocument(XmlDocument Xml, string Text, Uri BaseUri, string BlankNodeIdPrefix, BlankNodeIdMode BlankNodeIdMode)
Contains semantic information stored in a turtle document.
Definition: RdfDocument.cs:216
RdfDocument(XmlDocument Xml, Uri BaseUri, string BlankNodeIdPrefix, BlankNodeIdMode BlankNodeIdMode)
Contains semantic information stored in a turtle document.
Definition: RdfDocument.cs:121
XmlDocument Xml
Original XML of document.
Definition: RdfDocument.cs:282
RdfDocument(XmlDocument Xml, string Text, Uri BaseUri, string BlankNodeIdPrefix)
Contains semantic information stored in a turtle document.
Definition: RdfDocument.cs:203
RdfDocument(XmlDocument Xml, Uri BaseUri, string BlankNodeIdPrefix)
Contains semantic information stored in a turtle document.
Definition: RdfDocument.cs:109
RdfDocument(XmlDocument Xml, string Text, Uri BaseUri)
Contains semantic information stored in a turtle document.
Definition: RdfDocument.cs:191
static UriNode RdfSubject
Subject reference, during reification.
Definition: RdfDocument.cs:47
Helps with common XML-related tasks.
Definition: XML.cs:21
static XmlDocument ParseXml(string Xml)
Parses an XML Document from its string representation.
Definition: XML.cs:713
Node referencing a chunk in a ChunkedList<T>
Definition: ChunkNode.cs:11
ChunkNode< T > Next
Next chunk
Definition: ChunkNode.cs:26
int Pos
Index after the last element in chunk.
Definition: ChunkNode.cs:51
int Start
Index of first element in chunk.
Definition: ChunkNode.cs:46
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
ChunkNode< T > FirstChunk
First chunk
Definition: ChunkedList.cs:259
void Add(T Item)
Adds an item to the collection.
Definition: ChunkedList.cs:272
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:15
Interface for content classes, that process information available in HTTP headers in the response.
Interface for semantic nodes.
Interface for semantic literals.
BlankNodeIdMode
How blank node IDs are generated
delegate string ToString(IElement Element)
Delegate for callback methods that convert an element value to a string.