5using System.Threading.Tasks;
74 private readonly Dictionary<string, ISemanticLiteral> dataTypes =
new Dictionary<string, ISemanticLiteral>();
75 private readonly XmlDocument xml;
76 private readonly
string blankNodeIdPrefix;
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;
89 : this(
Xml,
Xml.OuterXml, null)
99 : this(
Xml,
Xml.OuterXml, BaseUri,
"n")
141 : this(ToXml(
Text),
Text, BaseUri,
"n")
168 internal static XmlDocument ToXml(
string Text)
192 : this(
Xml,
Text, BaseUri,
"n")
219 this.blankNodeIdPrefix = BlankNodeIdPrefix;
223 if (
Xml is
null ||
Xml.DocumentElement is
null)
224 throw new ArgumentNullException(nameof(
Xml));
226 string Language =
null;
229 foreach (XmlAttribute Attr
in Xml.DocumentElement.Attributes)
231 if (Attr.Prefix ==
"xml")
233 switch (Attr.LocalName)
236 Language = Attr.Value;
240 BaseUri = this.CreateUri(Attr.Value, BaseUri);
246 switch (Attr.LocalName)
261 Bag = this.CreateUriNode(
"#" + BagId, BaseUri);
265 if (
Xml.DocumentElement.LocalName ==
"RDF" &&
Xml.DocumentElement.NamespaceURI ==
Rdf.
Namespace)
266 this.ParseDescriptions(
Xml.DocumentElement, Language, BaseUri);
269 BlankNode RootSubject = this.CreateBlankNode();
270 UriNode RootType = this.CreateUriNode(
Xml.DocumentElement, BaseUri);
275 this.ParseDescription(
Xml.DocumentElement, RootSubject, Language, BaseUri, Bag,
null, ref ItemCounter);
282 public XmlDocument
Xml => this.xml;
287 public string Text => this.text;
292 public DateTimeOffset?
Date => this.date;
294 private void ParseDescriptions(XmlElement E,
string Language, Uri BaseUri)
296 foreach (XmlNode N
in E.ChildNodes)
298 if (!(N is XmlElement E2))
302 this.ParseDescription(E2, Language, BaseUri, ref ItemCounter);
306 private ISemanticElement ParseDescription(XmlElement E,
string Language, Uri BaseUri,
309 XmlAttribute About =
null;
310 XmlAttribute Type =
null;
311 XmlAttribute Id =
null;
312 string NodeId =
null;
314 string AboutEach =
null;
315 string AboutEachPrefix =
null;
317 XmlElement ForEach =
null;
319 foreach (XmlAttribute Attr
in E.Attributes)
323 switch (Attr.LocalName)
351 AboutEach = Attr.Value;
354 case "aboutEachPrefix":
355 AboutEachPrefix = Attr.Value;
359 else if (Attr.Prefix ==
"xml")
361 switch (Attr.LocalName)
364 Language = Attr.Value;
368 BaseUri = this.CreateUri(Attr.Value, BaseUri);
374 else if (Attr.Prefix ==
"xmlns" || Attr.Name ==
"xmlns")
377 if (Properties is
null)
380 Properties.
Add(Attr);
383 if (!(AboutEach is
null))
385 if (this.aboutEach is
null)
386 this.aboutEach =
new Dictionary<Uri, XmlElement>();
388 this.aboutEach[this.CreateUri(AboutEach, BaseUri)] = E;
392 if (!(AboutEachPrefix is
null))
394 if (this.aboutEachPrefix is
null)
395 this.aboutEachPrefix =
new Dictionary<Uri, XmlElement>();
397 this.aboutEachPrefix[this.CreateUri(AboutEachPrefix, BaseUri)] = E;
401 bool HasLanguage = !
string.IsNullOrEmpty(Language);
409 Bag = this.CreateUriNode(
"#" + BagId, BaseUri);
413 if (!(About is
null) || !(Id is
null))
415 UriNode AboutUriNode = About is
null ?
416 this.CreateUriNode(
"#" + Id.Value, BaseUri) :
417 this.CreateUriNode(About.Value, BaseUri);
418 Subject = AboutUriNode;
424 if (!(this.aboutEach?.TryGetValue(AboutUriNode.
Uri, out ForEach) ??
false) &&
425 !(
this.aboutEachPrefix is
null))
427 foreach (KeyValuePair<Uri, XmlElement> P
in this.aboutEachPrefix)
429 if (AboutUriNode.
Uri.ToString().StartsWith(P.Key.ToString()))
439 else if (!(NodeId is
null))
442 Subject = this.CreateBlankNode();
448 DescriptionNode = this.CreateUriNode(E, BaseUri);
451 else if (E.LocalName !=
"Description")
453 if (E.LocalName ==
"li")
455 string Item =
"_" + (++ItemCounter).
ToString();
461 DescriptionNode = this.CreateUriNode(E, BaseUri);
466 if (!(Bag is
null) && !(DescriptionNode is
null))
470 string Item =
"_" + (++ItemCounter).
ToString();
491 string Item =
"_" + (++ItemCounter).
ToString();
503 if (!(Properties is
null))
505 foreach (XmlAttribute Attr
in Properties)
507 UriNode Predicate = this.CreateUriNode(Attr, BaseUri);
521 string Item =
"_" + (++ItemCounter).
ToString();
534 int ChildItemCounter = Bag is
null ? 0 : ItemCounter;
536 ISemanticElement Result = this.ParseDescription(E, Subject, Language, BaseUri, Bag, ForEach, ref ChildItemCounter);
554 foreach (XmlNode N
in E.ChildNodes)
556 if (!(N is XmlElement E2))
559 UriNode Predicate = this.CreateUriNode(E2, BaseUri);
562 string Item =
"_" + (++ItemCounter).
ToString();
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);
571 Uri BaseUri2 = BaseUri;
572 XmlAttribute Resource =
null;
573 string NodeId =
null;
575 string DataType =
null;
576 string Language2 = Language;
577 string ParseType =
null;
580 foreach (XmlAttribute Attr
in Attributes)
584 switch (Attr.LocalName)
599 DataType = Attr.Value;
603 ParseType = Attr.Value;
611 case "aboutEachPrefix":
615 else if (Attr.Prefix ==
"xml")
617 switch (Attr.LocalName)
620 Language2 = Attr.Value;
624 BaseUri2 = this.CreateUri(Attr.Value, BaseUri2);
630 else if (Attr.Prefix ==
"xmlns" || Attr.Name ==
"xmlns")
633 if (Properties is
null)
636 Properties.
Add(Attr);
639 if (!(BagId is
null))
641 Bag2 = this.CreateUriNode(
"#" + BagId, BaseUri2);
645 if (!(Resource is
null))
647 Object = this.CreateUriNode(Resource.Value, BaseUri2);
650 else if (!(NodeId is
null))
656 if (!(Properties is
null))
660 Object = this.CreateBlankNode();
664 bool HasLanguage2 = !
string.IsNullOrEmpty(Language2);
666 foreach (XmlAttribute Attr
in Properties)
682 string Item =
"_" + (++ItemCounter).
ToString();
698 foreach (XmlNode N2
in ChildNodes)
700 if (N2 is XmlText
Text)
702 if (DataType is
null)
704 if (
string.IsNullOrEmpty(Language2))
711 Uri DataTypeUri = this.CreateUri(DataType, BaseUri2);
712 DataType = DataTypeUri.ToString();
714 if (!this.dataTypes.TryGetValue(DataType, out
ISemanticLiteral LiteralType))
719 this.dataTypes[DataType] = LiteralType;
722 Object = LiteralType.Parse(
Text.InnerText, DataType, Language2);
727 else if (N2 is XmlElement E3)
729 ISemanticElement Def = this.ParseDescription(E3, Language2, BaseUri2, ref ItemCounter);
733 if (Object is
null || Object.ToString() != Def.ToString())
745 Object = this.CreateBlankNode();
755 Object =
new XmlLiteral(ChildNodes, E2.NamespaceURI, Language2);
760 Object = this.CreateBlankNode();
763 int ChildItemCounter = 0;
764 this.ParseDescription(E2, Object, Language2, BaseUri2, Bag2,
null, ref ChildItemCounter);
770 foreach (XmlNode N2
in ChildNodes)
774 if (N2 is XmlText
Text)
776 if (DataType is
null)
778 if (
string.IsNullOrEmpty(Language2))
785 Uri DataTypeUri = this.CreateUri(DataType, BaseUri2);
786 DataType = DataTypeUri.ToString();
788 if (!this.dataTypes.TryGetValue(DataType, out
ISemanticLiteral LiteralType))
793 this.dataTypes[DataType] = LiteralType;
796 Element = LiteralType.Parse(
Text.InnerText, DataType, Language2);
799 else if (N2 is XmlElement E3)
800 Element = this.ParseDescription(E3, Language2, BaseUri2, ref ItemCounter);
804 if (Elements is
null)
807 if (!(Element is
null))
808 Elements.
Add(Element);
811 if (Elements is
null)
816 BlankNode Current = this.CreateBlankNode();
821 while (!(Loop is
null))
823 for (i = Loop.
Start, c = Loop.
Pos; i < c; i++)
827 if (i < c - 1 || !(Loop.
Next is
null))
844 if (!(Id is
null) || !(Bag is
null))
849 ReificationNode = this.CreateUriNode(
"#" + Id, BaseUri2);
851 ReificationNode = this.CreateBlankNode();
855 string Item =
"_" + (++ItemCounter).
ToString();
871 private UriNode CreateUriNode(
string Reference, Uri BaseUri)
873 return new UriNode(this.CreateUri(Reference, BaseUri), Reference);
876 private UriNode CreateUriNode(XmlElement Reference, Uri BaseUri)
878 if (
string.IsNullOrEmpty(Reference.Prefix))
879 return new UriNode(this.CreateUri(Reference.NamespaceURI + Reference.LocalName, BaseUri), Reference.LocalName);
881 return new UriNode(this.CreateUri(Reference.NamespaceURI + Reference.LocalName, BaseUri), Reference.Prefix +
":" + Reference.LocalName);
884 private UriNode CreateUriNode(XmlAttribute Reference, Uri BaseUri)
886 if (
string.IsNullOrEmpty(Reference.Prefix))
887 return new UriNode(this.CreateUri(Reference.NamespaceURI + Reference.LocalName, BaseUri), Reference.LocalName);
889 return new UriNode(this.CreateUri(Reference.NamespaceURI + Reference.LocalName, BaseUri), Reference.Prefix +
":" + Reference.LocalName);
892 private Uri CreateUri(
string Reference, Uri BaseUri)
896 if (Uri.TryCreate(Reference, UriKind.Absolute, out Uri URI))
903 if (
string.IsNullOrEmpty(Reference))
905 else if (Uri.TryCreate(BaseUri, Reference, out Uri URI))
908 throw this.
ParsingException(
"Invalid URI: " + Reference +
" (base: " + BaseUri.ToString() +
")");
915 return new BlankNode(this.blankNodeIdPrefix + Guid.NewGuid().ToString());
917 return new BlankNode(this.blankNodeIdPrefix + (++this.blankNodeIndex).
ToString());
922 return new Exception(Message);
931 this.date = HttpResponse.Headers.Date;
932 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 nil
URI representing rdf:nil
static readonly Uri Bag
URI representing rdf:Bag, an unordered container.
static readonly Uri type
URI representing rdf:type
static readonly Uri first
URI representing rdf:first
static readonly Uri rest
URI representing rdf:rest
static readonly Uri subject
URI representing rdf:subject
const string Namespace
http://www.w3.org/1999/02/22-rdf-syntax-ns#
static readonly Uri li
URI representing rdf:li
static readonly Uri predicate
URI representing rdf:predicate
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.
Helps with common XML-related tasks.
static XmlDocument ParseXml(string Xml)
Parses an XML Document from its string representation.
Node referencing a chunk in a ChunkedList<T>
ChunkNode< T > Next
Next chunk
int Pos
Index after the last element in chunk.
int Start
Index of first element in chunk.
A chunked list is a linked list of chunks of objects of type T .
ChunkNode< T > FirstChunk
First chunk
void Add(T Item)
Adds an item to the collection.
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.