2using System.Collections.Generic;
4using System.Threading.Tasks;
31 private static readonly
string[] SparqlResultSetContentTypes =
new string[]
33 "application/sparql-results+xml"
41 private static readonly
string[] SparqlResultSetFileExtensions =
new string[]
54 if (Array.IndexOf(SparqlResultSetContentTypes, ContentType) >= 0)
75 public Task<object>
DecodeAsync(
string ContentType,
byte[] Data, Encoding Encoding, KeyValuePair<string, string>[] Fields, Uri BaseUri)
79 return Task.FromResult<
object>(Parsed);
103 else if (Object is
bool &&
123 public Task<KeyValuePair<byte[], string>>
EncodeAsync(
object Object, Encoding Encoding, params
string[] AcceptedContentTypes)
125 if (Encoding is
null)
126 Encoding = Encoding.UTF8;
128 StringBuilder sb =
new StringBuilder();
129 sb.Append(
"<?xml version=\"1.0\" encoding=\"");
130 sb.Append(Encoding.WebName);
131 sb.AppendLine(
"\"?>");
133 XmlWriterSettings Settings =
new XmlWriterSettings()
135 ConformanceLevel = ConformanceLevel.Document,
138 NamespaceHandling = NamespaceHandling.OmitDuplicates,
139 NewLineHandling = NewLineHandling.None,
140 NewLineOnAttributes =
false,
141 OmitXmlDeclaration =
true,
142 WriteEndDocumentOnClose =
true
147 Settings.Indent =
true;
148 Settings.IndentChars =
"\t";
151 using (XmlWriter w = XmlWriter.Create(sb, Settings))
157 else if (Object is
bool b)
160 throw new ArgumentException(
"Unable to encode object.", nameof(Object));
164 string Text = sb.ToString();
166 byte[] Bin = Encoding.GetBytes(Text);
167 string ContentType = SparqlResultSetContentTypes[0] +
"; charset=" + Encoding.WebName;
169 return Task.FromResult(
new KeyValuePair<
byte[],
string>(Bin, ContentType));
177 w.WriteStartElement(
"head");
181 foreach (
string Name
in Result.
Variables)
183 w.WriteStartElement(
"variable");
184 w.WriteAttributeString(
"name", Name);
189 if (!(Result.
Links is
null))
191 foreach (Uri Link
in Result.
Links)
193 w.WriteStartElement(
"link");
194 w.WriteAttributeString(
"href", Link.ToString());
205 w.WriteStartElement(
"results");
211 w.WriteStartElement(
"result");
215 w.WriteStartElement(
"binding");
216 w.WriteAttributeString(
"name", Item.
Name);
218 OutputValue(w, Item.
Value);
237 w.WriteStartElement(
"head");
243 w.WriteStartElement(
"variable");
244 w.WriteAttributeString(
"name", Name);
245 w.WriteEndAttribute();
250 w.WriteStartElement(
"results");
253 int NrRows = Result.
Rows;
254 int NrColumns = Result.
Columns;
256 for (y = 0; y < NrRows; y++)
258 w.WriteStartElement(
"result");
260 for (x = 0; x < NrColumns; x++)
262 w.WriteStartElement(
"binding");
263 w.WriteAttributeString(
"name", Result.
ColumnNames[x]);
265 OutputValue(w, Result.
GetElement(x, y)?.AssociatedObjectValue);
280 w.WriteElementString(
"head",
string.Empty);
285 private static void OutputValue(XmlWriter w,
object Value)
291 w.WriteStartElement(
"literal");
293 if (!
string.IsNullOrEmpty(Literal.StringType))
294 w.WriteAttributeString(
"datatype", Literal.StringType);
307 w.WriteValue(Literal.Value);
311 w.WriteElementString(
"uri", N.Uri.ToString());
313 w.WriteElementString(
"bnode", N2.NodeId);
316 w.WriteStartElement(
"triple");
318 w.WriteStartElement(
"subject");
319 OutputValue(w, T.Subject);
322 w.WriteStartElement(
"predicate");
323 OutputValue(w, T.Predicate);
326 w.WriteStartElement(
"object");
327 OutputValue(w, T.Object);
333 w.WriteElementString(
"literal", Value?.
ToString() ??
string.Empty);
336 w.WriteElementString(
"literal", Value?.
ToString() ??
string.Empty);
347 if (
string.Compare(FileExtension, SparqlResultSetFileExtensions[0],
true) == 0)
349 ContentType = SparqlResultSetContentTypes[0];
367 if (Array.IndexOf(SparqlResultSetContentTypes, ContentType) >= 0)
369 FileExtension = SparqlResultSetFileExtensions[0];
374 FileExtension =
null;
Helps with parsing of commong data types.
static string Encode(bool x)
Encodes a Boolean for use in XML and other formats.
static string GetString(byte[] Data, Encoding DefaultEncoding)
Gets a string from its binary representation, taking any Byte Order Mark (BOM) into account.
Static class managing encoding and decoding of internet content.
static bool IsAccepted(string ContentType, params string[] AcceptedContentTypes)
Checks if a given content type is acceptable.
Represents a custom literal.
string Language
Language of string.
Represents a string literal.
string Language
Language of string.
Contains the results of a SPARQL query. https://www.w3.org/TR/2023/WD-sparql12-results-xml-20230516/ ...
bool? BooleanResult
Any Boolean result returned.
const string LocalName
sparql
const string Namespace
http://www.w3.org/2005/sparql-results#
bool Pretty
If pretty output is desired.
string[] Variables
Names of variables in result set.
ISparqlResultRecord[] Records
Records in result set.
Uri[] Links
Links to additional metadata about result set.
Encoder and Decoder of semantic information from SPARQL queries using XML. https://www....
string[] FileExtensions
Supported file extensions.
string[] ContentTypes
Supported Internet Content Types.
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of content of a given content type.
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes content of a given Internet Content Type.
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a specific object.
Task< object > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object
SparqlResultSetXmlCodec()
Encoder and Decoder of semantic information from SPARQL queries using XML.
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of content of a given file extension.
Task< KeyValuePair< byte[], string > > EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
Encodes an object
IElement GetElement(int Index)
Gets an element of the vector.
bool HasColumnNames
If the matrix has column names defined.
int Columns
Number of columns.
string[] ColumnNames
Contains optional column names.
Basic interface for Internet Content decoders. A class implementing this interface and having a defau...
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...
Interface for semantic nodes.
Interface for semantic literals.
Interface for semantic triples.
Interface for items in a record from the results of a SPARQL query.
string Name
Name of item in record.
ISemanticElement Value
Value of item in record.
Interface for result records of a SPARQL query.
delegate string ToString(IElement Element)
Delegate for callback methods that convert an element value to a string.