Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SparqlQueryCodec.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
5using Waher.Content;
7
9{
15 {
20 {
21 }
22
26 public string[] ContentTypes => SparqlQueryContentTypes;
27
28 private static readonly string[] SparqlQueryContentTypes = new string[]
29 {
30 "application/sparql-query"
31 };
32
36 public string[] FileExtensions => SparqlQueryFileExtensions;
37
38 private static readonly string[] SparqlQueryFileExtensions = new string[]
39 {
40 "rq"
41 };
42
49 public bool Decodes(string ContentType, out Grade Grade)
50 {
51 if (Array.IndexOf(SparqlQueryContentTypes, ContentType) >= 0)
52 {
53 Grade = Grade.Excellent;
54 return true;
55 }
56 else
57 {
58 Grade = Grade.NotAtAll;
59 return false;
60 }
61 }
62
72 public Task<object> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair<string, string>[] Fields, Uri BaseUri)
73 {
74 string s = CommonTypes.GetString(Data, Encoding ?? Encoding.UTF8);
75 Expression Exp = new Expression(s, BaseUri?.AbsolutePath);
76
77 if (!(Exp.Root is SparqlQuery Query))
78 throw new Exception("Invalid SPARQL query.");
79
80 return Task.FromResult<object>(Query);
81 }
82
90 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
91 {
92 if (Object is SparqlQuery &&
93 InternetContent.IsAccepted(SparqlQueryContentTypes, AcceptedContentTypes))
94 {
95 Grade = Grade.Excellent;
96 return true;
97 }
98 else if (Object is Expression Exp &&
99 Exp.Root is SparqlQuery &&
100 InternetContent.IsAccepted(SparqlQueryContentTypes, AcceptedContentTypes))
101 {
102 Grade = Grade.Excellent;
103 return true;
104 }
105 else
106 {
107 Grade = Grade.NotAtAll;
108 return false;
109 }
110 }
111
119 public Task<KeyValuePair<byte[], string>> EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
120 {
121 if (Encoding is null)
122 Encoding = Encoding.UTF8;
123
124 if (!(Object is SparqlQuery Query))
125 {
126 if (Object is Expression Exp && Exp.Root is SparqlQuery Query2)
127 Query = Query2;
128 else
129 throw new ArgumentException("Unable to encode object.", nameof(Object));
130 }
131
132 string Text = JSON.Encode(Query.SubExpression, false);
133 byte[] Bin = Encoding.GetBytes(Text);
134 string ContentType = SparqlQueryContentTypes[0] + "; charset=" + Encoding.WebName;
135
136 return Task.FromResult(new KeyValuePair<byte[], string>(Bin, ContentType));
137 }
138
145 public bool TryGetContentType(string FileExtension, out string ContentType)
146 {
147 if (string.Compare(FileExtension, SparqlQueryFileExtensions[0], true) == 0)
148 {
149 ContentType = SparqlQueryContentTypes[0];
150 return true;
151 }
152 else
153 {
154 ContentType = null;
155 return false;
156 }
157 }
158
165 public bool TryGetFileExtension(string ContentType, out string FileExtension)
166 {
167 if (Array.IndexOf(SparqlQueryContentTypes, ContentType) >= 0)
168 {
169 FileExtension = SparqlQueryFileExtensions[0];
170 return true;
171 }
172 else
173 {
174 FileExtension = null;
175 return false;
176 }
177 }
178 }
179}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
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.
Helps with common JSON-related tasks.
Definition: JSON.cs:14
static string Encode(string s)
Encodes a string for inclusion in JSON.
Definition: JSON.cs:507
Class managing a script expression.
Definition: Expression.cs:39
ScriptNode Root
Root script node.
Definition: Expression.cs:4299
Encoder and Decoder of SPARQL queries. https://www.w3.org/TR/sparql12-query/
Task< object > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes content of a given Internet Content Type.
Task< KeyValuePair< byte[], string > > EncodeAsync(object Object, Encoding Encoding, params string[] AcceptedContentTypes)
Encodes an object
string[] FileExtensions
Supported file extensions.
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a specific object.
SparqlQueryCodec()
Encoder and Decoder of SPARQL queries.
string[] ContentTypes
Supported Internet Content Types.
bool TryGetContentType(string FileExtension, out string ContentType)
Tries to get the content type of content of a given file extension.
bool TryGetFileExtension(string ContentType, out string FileExtension)
Tries to get the file extension of content of a given content type.
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...
Grade
Grade enumeration
Definition: Grade.cs:7