4using System.Threading.Tasks;
32 private static readonly
string[] SparqlResultSetContentTypes =
new string[]
34 "application/sparql-results+xml"
42 private static readonly
string[] SparqlResultSetFileExtensions =
new string[]
55 if (Array.IndexOf(SparqlResultSetContentTypes, ContentType) >= 0)
77 public Task<ContentResponse>
DecodeAsync(
string ContentType,
byte[] Data, Encoding Encoding,
78 KeyValuePair<string, string>[] Fields, Uri BaseUri,
ICodecProgress Progress)
106 else if (Object is
bool &&
127 public Task<ContentResponse>
EncodeAsync(
object Object, Encoding Encoding,
ICodecProgress Progress, params
string[] AcceptedContentTypes)
129 if (Encoding is
null)
130 Encoding = Encoding.UTF8;
132 StringBuilder sb =
new StringBuilder();
133 sb.Append(
"<?xml version=\"1.0\" encoding=\"");
134 sb.Append(Encoding.WebName);
135 sb.AppendLine(
"\"?>");
137 XmlWriterSettings Settings =
new XmlWriterSettings()
139 ConformanceLevel = ConformanceLevel.Document,
142 NamespaceHandling = NamespaceHandling.OmitDuplicates,
143 NewLineHandling = NewLineHandling.None,
144 NewLineOnAttributes =
false,
145 OmitXmlDeclaration =
true,
146 WriteEndDocumentOnClose =
true
151 Settings.Indent =
true;
152 Settings.IndentChars =
"\t";
155 using (XmlWriter w = XmlWriter.Create(sb, Settings))
161 else if (Object is
bool b)
164 return Task.FromResult(
new ContentResponse(
new ArgumentException(
"Unable to encode object.", nameof(Object))));
168 string Text = sb.ToString();
170 byte[] Bin = Encoding.GetBytes(Text);
171 string ContentType = SparqlResultSetContentTypes[0] +
"; charset=" + Encoding.WebName;
181 w.WriteStartElement(
"head");
185 foreach (
string Name
in Result.
Variables)
187 w.WriteStartElement(
"variable");
188 w.WriteAttributeString(
"name", Name);
193 if (!(Result.
Links is
null))
195 foreach (Uri Link
in Result.
Links)
197 w.WriteStartElement(
"link");
198 w.WriteAttributeString(
"href", Link.ToString());
209 w.WriteStartElement(
"results");
215 w.WriteStartElement(
"result");
219 w.WriteStartElement(
"binding");
220 w.WriteAttributeString(
"name", Item.
Name);
222 OutputValue(w, Item.
Value);
237 private static void Encode(
ObjectMatrix Result, XmlWriter w)
241 w.WriteStartElement(
"head");
247 w.WriteStartElement(
"variable");
248 w.WriteAttributeString(
"name", Name);
249 w.WriteEndAttribute();
254 w.WriteStartElement(
"results");
257 int NrRows = Result.
Rows;
258 int NrColumns = Result.
Columns;
260 for (y = 0; y < NrRows; y++)
262 w.WriteStartElement(
"result");
264 for (x = 0; x < NrColumns; x++)
266 w.WriteStartElement(
"binding");
267 w.WriteAttributeString(
"name", Result.
ColumnNames[x]);
269 OutputValue(w, Result.
GetElement(x, y)?.AssociatedObjectValue);
281 private static void Encode(
bool Result, XmlWriter w)
284 w.WriteElementString(
"head",
string.Empty);
289 private static void OutputValue(XmlWriter w,
object Value)
295 w.WriteStartElement(
"literal");
297 if (!
string.IsNullOrEmpty(Literal.StringType))
298 w.WriteAttributeString(
"datatype", Literal.StringType);
311 w.WriteValue(Literal.Value);
315 w.WriteElementString(
"uri", N.Uri.ToString());
317 w.WriteElementString(
"bnode", N2.NodeId);
320 w.WriteStartElement(
"triple");
322 w.WriteStartElement(
"subject");
323 OutputValue(w, T.Subject);
326 w.WriteStartElement(
"predicate");
327 OutputValue(w, T.Predicate);
330 w.WriteStartElement(
"object");
331 OutputValue(w, T.Object);
337 w.WriteElementString(
"literal", Value?.
ToString() ??
string.Empty);
340 w.WriteElementString(
"literal", Value?.
ToString() ??
string.Empty);
351 if (
string.Compare(FileExtension, SparqlResultSetFileExtensions[0],
true) == 0)
353 ContentType = SparqlResultSetContentTypes[0];
371 if (Array.IndexOf(SparqlResultSetContentTypes, ContentType) >= 0)
373 FileExtension = SparqlResultSetFileExtensions[0];
378 FileExtension =
null;
Helps with parsing of commong data types.
static string Encode(bool x)
Encodes a Boolean for use in XML and other formats.
Contains information about a response to a content request.
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.
Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri, ICodecProgress Progress)
Decodes an object
Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an object
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.
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.
Static class managing binary representations of strings.
static string GetString(byte[] Data, int Offset, int Count, Encoding DefaultEncoding)
Gets a string from its binary representation, taking any Byte Order Mark (BOM) into account.
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.
Interface for reporting progress about an encoding or decoding.
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.