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