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;
3using System.Text;
4using System.Threading.Tasks;
5using Waher.Content;
8
10{
16 {
21 {
22 }
23
27 public string[] ContentTypes => SparqlQueryContentTypes;
28
29 private static readonly string[] SparqlQueryContentTypes = new string[]
30 {
31 "application/sparql-query"
32 };
33
37 public string[] FileExtensions => SparqlQueryFileExtensions;
38
39 private static readonly string[] SparqlQueryFileExtensions = new string[]
40 {
41 "rq"
42 };
43
50 public bool Decodes(string ContentType, out Grade Grade)
51 {
52 if (Array.IndexOf(SparqlQueryContentTypes, ContentType) >= 0)
53 {
54 Grade = Grade.Excellent;
55 return true;
56 }
57 else
58 {
59 Grade = Grade.NotAtAll;
60 return false;
61 }
62 }
63
74 public Task<ContentResponse> DecodeAsync(string ContentType, byte[] Data, Encoding Encoding,
75 KeyValuePair<string, string>[] Fields, Uri BaseUri, ICodecProgress Progress)
76 {
77 string s = Strings.GetString(Data, Encoding ?? Encoding.UTF8);
78 Expression Exp = new Expression(s, BaseUri?.AbsolutePath);
79
80 if (!(Exp.Root is SparqlQuery Query))
81 return Task.FromResult(new ContentResponse(new Exception("Invalid SPARQL query.")));
82 else
83 return Task.FromResult(new ContentResponse(ContentType, Query, Data));
84 }
85
93 public bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
94 {
95 if (Object is SparqlQuery &&
96 InternetContent.IsAccepted(SparqlQueryContentTypes, AcceptedContentTypes))
97 {
98 Grade = Grade.Excellent;
99 return true;
100 }
101 else if (Object is Expression Exp &&
102 Exp.Root is SparqlQuery &&
103 InternetContent.IsAccepted(SparqlQueryContentTypes, AcceptedContentTypes))
104 {
105 Grade = Grade.Excellent;
106 return true;
107 }
108 else
109 {
110 Grade = Grade.NotAtAll;
111 return false;
112 }
113 }
114
123 public Task<ContentResponse> EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
124 {
125 if (Encoding is null)
126 Encoding = Encoding.UTF8;
127
128 if (!(Object is SparqlQuery Query))
129 {
130 if (Object is Expression Exp && Exp.Root is SparqlQuery Query2)
131 Query = Query2;
132 else
133 return Task.FromResult(new ContentResponse(new ArgumentException("Unable to encode object.", nameof(Object))));
134 }
135
136 string Text = JSON.Encode(Query.SubExpression, false);
137 byte[] Bin = Encoding.GetBytes(Text);
138 string ContentType = SparqlQueryContentTypes[0] + "; charset=" + Encoding.WebName;
139
140 return Task.FromResult(new ContentResponse(ContentType, Object, Bin));
141 }
142
149 public bool TryGetContentType(string FileExtension, out string ContentType)
150 {
151 if (string.Compare(FileExtension, SparqlQueryFileExtensions[0], true) == 0)
152 {
153 ContentType = SparqlQueryContentTypes[0];
154 return true;
155 }
156 else
157 {
158 ContentType = null;
159 return false;
160 }
161 }
162
169 public bool TryGetFileExtension(string ContentType, out string FileExtension)
170 {
171 if (Array.IndexOf(SparqlQueryContentTypes, ContentType) >= 0)
172 {
173 FileExtension = SparqlQueryFileExtensions[0];
174 return true;
175 }
176 else
177 {
178 FileExtension = null;
179 return false;
180 }
181 }
182 }
183}
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.
Helps with common JSON-related tasks.
Definition: JSON.cs:16
static string Encode(string s)
Encodes a string for inclusion in JSON.
Definition: JSON.cs:537
Static class managing binary representations of strings.
Definition: Strings.cs:10
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.
Definition: Strings.cs:148
Class managing a script expression.
Definition: Expression.cs:41
ScriptNode Root
Root script node.
Definition: Expression.cs:4496
Encoder and Decoder of SPARQL queries. https://www.w3.org/TR/sparql12-query/
bool Decodes(string ContentType, out Grade Grade)
If the decoder decodes content of a given Internet Content Type.
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
bool Encodes(object Object, out Grade Grade, params string[] AcceptedContentTypes)
If the encoder encodes a specific object.
Task< ContentResponse > EncodeAsync(object Object, Encoding Encoding, ICodecProgress Progress, params string[] AcceptedContentTypes)
Encodes an 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.
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...
Grade
Grade enumeration
Definition: Grade.cs:7