2using System.Collections;
3using System.Collections.Generic;
5using System.Threading.Tasks;
72 private readonly Dictionary<string, ISemanticLiteral> dataTypes =
new Dictionary<string, ISemanticLiteral>();
73 private readonly XmlDocument xml;
74 private readonly
string blankNodeIdPrefix;
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;
87 : this(
Xml,
Xml.OuterXml, null)
97 : this(
Xml,
Xml.OuterXml, BaseUri,
"n")
139 : this(ToXml(
Text),
Text, BaseUri,
"n")
166 internal static XmlDocument ToXml(
string Text)
168 XmlDocument
Xml =
new XmlDocument()
170 PreserveWhitespace =
true
194 : this(
Xml,
Text, BaseUri,
"n")
221 this.blankNodeIdPrefix = BlankNodeIdPrefix;
225 if (
Xml is
null ||
Xml.DocumentElement is
null)
226 throw new ArgumentNullException(nameof(
Xml));
228 string Language =
null;
231 foreach (XmlAttribute Attr
in Xml.DocumentElement.Attributes)
233 if (Attr.Prefix ==
"xml")
235 switch (Attr.LocalName)
238 Language = Attr.Value;
242 BaseUri = this.CreateUri(Attr.Value, BaseUri);
248 switch (Attr.LocalName)
263 Bag = this.CreateUriNode(
"#" + BagId, BaseUri);
267 if (
Xml.DocumentElement.LocalName ==
"RDF" &&
Xml.DocumentElement.NamespaceURI ==
Rdf.
Namespace)
268 this.ParseDescriptions(
Xml.DocumentElement, Language, BaseUri);
271 BlankNode RootSubject = this.CreateBlankNode();
272 UriNode RootType = this.CreateUriNode(
Xml.DocumentElement, BaseUri);
277 this.ParseDescription(
Xml.DocumentElement, RootSubject, Language, BaseUri, Bag,
null, ref ItemCounter);
284 public XmlDocument
Xml => this.xml;
289 public string Text => this.text;
294 public DateTimeOffset?
Date => this.date;
296 private void ParseDescriptions(XmlElement E,
string Language, Uri BaseUri)
298 foreach (XmlNode N
in E.ChildNodes)
300 if (!(N is XmlElement E2))
304 this.ParseDescription(E2, Language, BaseUri, ref ItemCounter);
308 private ISemanticElement ParseDescription(XmlElement E,
string Language, Uri BaseUri,
311 XmlAttribute About =
null;
312 XmlAttribute Type =
null;
313 XmlAttribute Id =
null;
314 string NodeId =
null;
316 string AboutEach =
null;
317 string AboutEachPrefix =
null;
318 LinkedList<XmlAttribute> Properties =
null;
319 XmlElement ForEach =
null;
321 foreach (XmlAttribute Attr
in E.Attributes)
325 switch (Attr.LocalName)
353 AboutEach = Attr.Value;
356 case "aboutEachPrefix":
357 AboutEachPrefix = Attr.Value;
361 else if (Attr.Prefix ==
"xml")
363 switch (Attr.LocalName)
366 Language = Attr.Value;
370 BaseUri = this.CreateUri(Attr.Value, BaseUri);
376 else if (Attr.Prefix ==
"xmlns" || Attr.Name ==
"xmlns")
379 if (Properties is
null)
380 Properties =
new LinkedList<XmlAttribute>();
382 Properties.AddLast(Attr);
385 if (!(AboutEach is
null))
387 if (this.aboutEach is
null)
388 this.aboutEach =
new Dictionary<Uri, XmlElement>();
390 this.aboutEach[this.CreateUri(AboutEach, BaseUri)] = E;
394 if (!(AboutEachPrefix is
null))
396 if (this.aboutEachPrefix is
null)
397 this.aboutEachPrefix =
new Dictionary<Uri, XmlElement>();
399 this.aboutEachPrefix[this.CreateUri(AboutEachPrefix, BaseUri)] = E;
403 bool HasLanguage = !
string.IsNullOrEmpty(Language);
411 Bag = this.CreateUriNode(
"#" + BagId, BaseUri);
415 if (!(About is
null) || !(Id is
null))
417 UriNode AboutUriNode = About is
null ?
418 this.CreateUriNode(
"#" + Id.Value, BaseUri) :
419 this.CreateUriNode(About.Value, BaseUri);
420 Subject = AboutUriNode;
426 if (!(this.aboutEach?.TryGetValue(AboutUriNode.
Uri, out ForEach) ??
false) &&
427 !(
this.aboutEachPrefix is
null))
429 foreach (KeyValuePair<Uri, XmlElement> P
in this.aboutEachPrefix)
431 if (AboutUriNode.
Uri.ToString().StartsWith(P.Key.ToString()))
441 else if (!(NodeId is
null))
444 Subject = this.CreateBlankNode();
450 DescriptionNode = this.CreateUriNode(E, BaseUri);
453 else if (E.LocalName !=
"Description")
455 if (E.LocalName ==
"li")
457 string Item =
"_" + (++ItemCounter).
ToString();
463 DescriptionNode = this.CreateUriNode(E, BaseUri);
468 if (!(Bag is
null) && !(DescriptionNode is
null))
472 string Item =
"_" + (++ItemCounter).
ToString();
493 string Item =
"_" + (++ItemCounter).
ToString();
505 if (!(Properties is
null))
507 foreach (XmlAttribute Attr
in Properties)
509 UriNode Predicate = this.CreateUriNode(Attr, BaseUri);
523 string Item =
"_" + (++ItemCounter).
ToString();
536 int ChildItemCounter = Bag is
null ? 0 : ItemCounter;
538 ISemanticElement Result = this.ParseDescription(E, Subject, Language, BaseUri, Bag, ForEach, ref ChildItemCounter);
556 foreach (XmlNode N
in E.ChildNodes)
558 if (!(N is XmlElement E2))
561 UriNode Predicate = this.CreateUriNode(E2, BaseUri);
564 string Item =
"_" + (++ItemCounter).
ToString();
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;
573 Uri BaseUri2 = BaseUri;
574 XmlAttribute Resource =
null;
575 string NodeId =
null;
577 string DataType =
null;
578 string Language2 = Language;
579 string ParseType =
null;
582 foreach (XmlAttribute Attr
in Attributes)
586 switch (Attr.LocalName)
601 DataType = Attr.Value;
605 ParseType = Attr.Value;
613 case "aboutEachPrefix":
617 else if (Attr.Prefix ==
"xml")
619 switch (Attr.LocalName)
622 Language2 = Attr.Value;
626 BaseUri2 = this.CreateUri(Attr.Value, BaseUri2);
632 else if (Attr.Prefix ==
"xmlns" || Attr.Name ==
"xmlns")
635 if (Properties is
null)
636 Properties =
new LinkedList<XmlAttribute>();
638 Properties.AddLast(Attr);
641 if (!(BagId is
null))
643 Bag2 = this.CreateUriNode(
"#" + BagId, BaseUri2);
647 if (!(Resource is
null))
649 Object = this.CreateUriNode(Resource.Value, BaseUri2);
652 else if (!(NodeId is
null))
658 if (!(Properties is
null))
662 Object = this.CreateBlankNode();
666 bool HasLanguage2 = !
string.IsNullOrEmpty(Language2);
668 foreach (XmlAttribute Attr
in Properties)
684 string Item =
"_" + (++ItemCounter).
ToString();
700 foreach (XmlNode N2
in ChildNodes)
702 if (N2 is XmlText
Text)
704 if (DataType is
null)
706 if (
string.IsNullOrEmpty(Language2))
713 Uri DataTypeUri = this.CreateUri(DataType, BaseUri2);
714 DataType = DataTypeUri.ToString();
716 if (!this.dataTypes.TryGetValue(DataType, out
ISemanticLiteral LiteralType))
721 this.dataTypes[DataType] = LiteralType;
724 Object = LiteralType.Parse(
Text.InnerText, DataType, Language2);
729 else if (N2 is XmlElement E3)
731 ISemanticElement Def = this.ParseDescription(E3, Language2, BaseUri2, ref ItemCounter);
735 if (Object is
null || Object.ToString() != Def.ToString())
747 Object = this.CreateBlankNode();
757 Object =
new XmlLiteral(ChildNodes, E2.NamespaceURI, Language2);
762 Object = this.CreateBlankNode();
765 int ChildItemCounter = 0;
766 this.ParseDescription(E2, Object, Language2, BaseUri2, Bag2,
null, ref ChildItemCounter);
770 LinkedList<ISemanticElement> Elements =
null;
772 foreach (XmlNode N2
in ChildNodes)
776 if (N2 is XmlText
Text)
778 if (DataType is
null)
780 if (
string.IsNullOrEmpty(Language2))
787 Uri DataTypeUri = this.CreateUri(DataType, BaseUri2);
788 DataType = DataTypeUri.ToString();
790 if (!this.dataTypes.TryGetValue(DataType, out
ISemanticLiteral LiteralType))
795 this.dataTypes[DataType] = LiteralType;
798 Element = LiteralType.Parse(
Text.InnerText, DataType, Language2);
801 else if (N2 is XmlElement E3)
802 Element = this.ParseDescription(E3, Language2, BaseUri2, ref ItemCounter);
806 if (Elements is
null)
807 Elements =
new LinkedList<ISemanticElement>();
809 if (!(Element is
null))
810 Elements.AddLast(Element);
813 if (Elements is
null)
817 LinkedListNode<ISemanticElement> Loop = Elements.First;
818 BlankNode Current = this.CreateBlankNode();
822 while (!(Loop is
null))
842 if (!(Id is
null) || !(Bag is
null))
847 ReificationNode = this.CreateUriNode(
"#" + Id, BaseUri2);
849 ReificationNode = this.CreateBlankNode();
853 string Item =
"_" + (++ItemCounter).
ToString();
869 private UriNode CreateUriNode(
string Reference, Uri BaseUri)
871 return new UriNode(this.CreateUri(Reference, BaseUri), Reference);
874 private UriNode CreateUriNode(XmlElement Reference, Uri BaseUri)
876 if (
string.IsNullOrEmpty(Reference.Prefix))
877 return new UriNode(this.CreateUri(Reference.NamespaceURI + Reference.LocalName, BaseUri), Reference.LocalName);
879 return new UriNode(this.CreateUri(Reference.NamespaceURI + Reference.LocalName, BaseUri), Reference.Prefix +
":" + Reference.LocalName);
882 private UriNode CreateUriNode(XmlAttribute Reference, Uri BaseUri)
884 if (
string.IsNullOrEmpty(Reference.Prefix))
885 return new UriNode(this.CreateUri(Reference.NamespaceURI + Reference.LocalName, BaseUri), Reference.LocalName);
887 return new UriNode(this.CreateUri(Reference.NamespaceURI + Reference.LocalName, BaseUri), Reference.Prefix +
":" + Reference.LocalName);
890 private Uri CreateUri(
string Reference, Uri BaseUri)
894 if (Uri.TryCreate(Reference, UriKind.Absolute, out Uri URI))
901 if (
string.IsNullOrEmpty(Reference))
903 else if (Uri.TryCreate(BaseUri, Reference, out Uri URI))
906 throw this.
ParsingException(
"Invalid URI: " + Reference +
" (base: " + BaseUri.ToString() +
")");
913 return new BlankNode(this.blankNodeIdPrefix + Guid.NewGuid().ToString());
915 return new BlankNode(this.blankNodeIdPrefix + (++this.blankNodeIndex).
ToString());
920 return new Exception(Message);
929 this.date = HttpResponse.Headers.Date;
930 return Task.CompletedTask;
override void Add(ISemanticTriple Triple)
Adds a triple to the cube.
Joins attributes from two XML elements.
Joins two sets of XML nodes.
Represents a custom literal.
Represents a string literal.
Represents an XML literal.
Implements a semantic triple.
string UriString
URI string
static readonly Uri Object
URI representing rdf:object
static readonly Uri Bag
URI representing rdf:Bag
static readonly Uri Subject
URI representing rdf:subject
static readonly Uri Predicate
URI representing rdf:predicate
const string Namespace
http://www.w3.org/1999/02/22-rdf-syntax-ns#
static readonly Uri Rest
URI representing rdf:rest
static readonly Uri First
URI representing rdf:first
static readonly Uri Nil
URI representing rdf:nil
static readonly Uri Type
URI representing rdf:type
static readonly Uri Li
URI representing rdf:li
static readonly Uri Statement
URI representing rdf:Statement
Contains semantic information stored in an RDF document.
static UriNode RdfLi
List item reference.
DateTimeOffset? Date
Server timestamp of document.
RdfDocument(XmlDocument Xml)
Contains semantic information stored in a turtle document.
static UriNode RdfPredicate
Predicate reference, during reification.
static UriNode RdfStatement
Statement reference
static UriNode RdfBag
Bag reference
Task DecodeMetaInformation(HttpResponseMessage HttpResponse)
Decodes meta-information available in the HTTP Response.
RdfDocument(string Text, Uri BaseUri, string BlankNodeIdPrefix, BlankNodeIdMode BlankNodeIdMode)
Contains semantic information stored in a turtle document.
RdfDocument(string Text)
Contains semantic information stored in a turtle document.
RdfDocument(XmlDocument Xml, Uri BaseUri)
Contains semantic information stored in a turtle document.
RdfDocument(XmlDocument Xml, string Text)
Contains semantic information stored in a turtle document.
static UriNode RdfObject
object reference, during reification.
static UriNode RdfType
rdf:type predicate
static UriNode RdfNil
Predefined reference to end of collection.
static UriNode RdfRest
Predefined reference to next element in a collection.
static UriNode RdfFirst
Predefined reference to first element in a collection.
string Text
Text representation.
RdfDocument(string Text, Uri BaseUri)
Contains semantic information stored in a turtle document.
RdfDocument(string Text, Uri BaseUri, string BlankNodeIdPrefix)
Contains semantic information stored in a turtle document.
RdfDocument(XmlDocument Xml, string Text, Uri BaseUri, string BlankNodeIdPrefix, BlankNodeIdMode BlankNodeIdMode)
Contains semantic information stored in a turtle document.
RdfDocument(XmlDocument Xml, Uri BaseUri, string BlankNodeIdPrefix, BlankNodeIdMode BlankNodeIdMode)
Contains semantic information stored in a turtle document.
XmlDocument Xml
Original XML of document.
RdfDocument(XmlDocument Xml, string Text, Uri BaseUri, string BlankNodeIdPrefix)
Contains semantic information stored in a turtle document.
RdfDocument(XmlDocument Xml, Uri BaseUri, string BlankNodeIdPrefix)
Contains semantic information stored in a turtle document.
RdfDocument(XmlDocument Xml, string Text, Uri BaseUri)
Contains semantic information stored in a turtle document.
static UriNode RdfSubject
Subject reference, during reification.
Static class that dynamically manages types and interfaces available in the runtime environment.
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.